lib/rspec-api/dsl/request/headers.rb in rspec-api-0.2.0 vs lib/rspec-api/dsl/request/headers.rb in rspec-api-0.4.0

- old
+ new

@@ -1,24 +1,40 @@ +require 'active_support' +require 'rack/utils' require 'rspec-api/matchers' module DSL module Request extend ActiveSupport::Concern module ClassMethods - def should_match_headers_expectations(status_code) - should_have_json_content_type if has_entity_body? status_code - should_be_paginated(rspec_api[:page][:name]) if rspec_api[:array] && rspec_api[:page] + # 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 + private - def should_have_json_content_type - it { expect(response_headers).to have_json_content_type } - end + def should_include_content_type(type) + expect(response).to include_content_type_if response_has_body?, type + end - def should_be_paginated(page_parameter) - it { expect(response_headers).to have_pagination_links(request_params[page_parameter.to_s]) } - 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 \ No newline at end of file