Sha256: 15e88b56485555aae3dee51edede0df35110f0edfa3f6ec78b66f63c67cce512

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'active_support'
require 'rack/utils'
require 'rspec-api/matchers'

module DSL
  module Request
    extend ActiveSupport::Concern

    module ClassMethods
      # Creates an example group for expectations on the response headers of
      # last API request and runs it to verify that it matches best practices:
      # * if request has entity body, the Content-Type header should be JSON
      # * if request has pages, the Link header should have a 'rel=prev' link
      def should_respond_with_expected_headers(options = {})
        context 'responds with headers that' do
          it { should_include_content_type :json }
          it { should_have_prev_page_link options[:page] }
        end
      end
    end

  private

    def should_include_content_type(type)
      expect(response).to include_content_type_if response_has_body?, type
    end

    def should_have_prev_page_link(page)
      expect(response).to have_prev_page_link_if request_is_paginated?(page)
    end

    def response_has_body?
      !Rack::Utils::STATUS_WITH_NO_ENTITY_BODY.include?(response.status)
    end

    def request_is_paginated?(page_parameter)
      request_params.key? page_parameter.to_s
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rspec-api-0.4.0 lib/rspec-api/dsl/request/headers.rb