Sha256: c40ac33b0959d45b78c52cbe276b2fbcf92d51f516f4de4bacefe4df0d410c91

Contents?: true

Size: 1.54 KB

Versions: 3

Compression:

Stored size: 1.54 KB

Contents

# -*- encoding : utf-8 -*-
module Pacto
  module Cops
    class ResponseHeaderCop
      def self.investigate(_request, response, contract)
        expected_headers = contract.response.headers
        actual_headers = response.headers
        actual_headers = Pacto::Extensions.normalize_header_keys actual_headers
        headers_to_validate = Pacto::Extensions.normalize_header_keys expected_headers

        headers_to_validate.map do |expected_header, expected_value|
          if actual_headers.key? expected_header
            actual_value = actual_headers[expected_header]
            HeaderValidatorMap[expected_header].call(expected_value, actual_value)
          else
            "Missing expected response header: #{expected_header}"
          end
        end.compact
      end

      private

      HeaderValidatorMap = Hash.new do |_map, key|
        proc do |expected_value, actual_value|
          unless expected_value.eql? actual_value
            "Invalid response header #{key}: expected #{expected_value.inspect} but received #{actual_value.inspect}"
          end
        end
      end

      HeaderValidatorMap['Location'] = proc do |expected_value, actual_value|
        location_template = Addressable::Template.new(expected_value)
        if location_template.match(Addressable::URI.parse(actual_value))
          nil
        else
          "Invalid response header Location: expected URI #{actual_value} to match URI Template #{location_template.pattern}"
        end
      end
    end
  end
end

Pacto::Cops.register_cop Pacto::Cops::ResponseHeaderCop

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pacto-0.4.0.rc3 lib/pacto/cops/response_header_cop.rb
pacto-0.4.0.rc2 lib/pacto/cops/response_header_cop.rb
pacto-0.4.0.rc1 lib/pacto/cops/response_header_cop.rb