<commit_before>require 'active_support/core_ext/hash/slice'
require 'json-schema'
require 'json'
require 'rswag/specs/extended_schema'

module Rswag
  module Specs
    class ResponseValidator

      def initialize(config = ::Rswag::Specs.config)
        @config = config
      end

      def validate!(metadata, response)
        swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])

        validate_code!(metadata, response.code)
        validate_headers!(metadata, response.headers)
        validate_body!(metadata, swagger_doc, response.body)
      end

      private

      def validate_code!(metadata, code)
        expected = metadata[:response][:code].to_s
        if code != expected
          raise UnexpectedResponse, "Expected response code '#{code}' to match '#{expected}'"
        end
      end

      def validate_headers!(metadata, headers)
        expected = (metadata[:response][:headers] || {}).keys
        expected.each do |name|
          raise UnexpectedResponse, "Expected response header #{name} to be present" if headers[name.to_s].nil?
        end
      end

      def validate_body!(metadata, swagger_doc, body)
        response_schema = metadata[:response][:schema]
        return if response_schema.nil?

        validation_schema = response_schema
          .merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
          .merge(swagger_doc.slice(:definitions))
        errors = JSON::Validator.fully_validate(validation_schema, body)
        raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
      end
    end

    class UnexpectedResponse < StandardError; end
  end
end<commit_msg>Show response body when the response code is unexpected. Makes it much easier to debug test failures<commit_after>require 'active_support/core_ext/hash/slice'
require 'json-schema'
require 'json'
require 'rswag/specs/extended_schema'

module Rswag
  module Specs
    class ResponseValidator

      def initialize(config = ::Rswag::Specs.config)
        @config = config
      end

      def validate!(metadata, response)
        swagger_doc = @config.get_swagger_doc(metadata[:swagger_doc])

        validate_code!(metadata, response)
        validate_headers!(metadata, response.headers)
        validate_body!(metadata, swagger_doc, response.body)
      end

      private

      def validate_code!(metadata, response)
        expected = metadata[:response][:code].to_s
        if response.code != expected
          raise UnexpectedResponse,
                "Expected response code '#{response.code}' to match '#{expected}'\n" \
                "Response body: #{response.body}"
        end
      end

      def validate_headers!(metadata, headers)
        expected = (metadata[:response][:headers] || {}).keys
        expected.each do |name|
          raise UnexpectedResponse, "Expected response header #{name} to be present" if headers[name.to_s].nil?
        end
      end

      def validate_body!(metadata, swagger_doc, body)
        response_schema = metadata[:response][:schema]
        return if response_schema.nil?

        validation_schema = response_schema
          .merge('$schema' => 'http://tempuri.org/rswag/specs/extended_schema')
          .merge(swagger_doc.slice(:definitions))
        errors = JSON::Validator.fully_validate(validation_schema, body)
        raise UnexpectedResponse, "Expected response body to match schema: #{errors[0]}" if errors.any?
      end
    end

    class UnexpectedResponse < StandardError; end
  end
end
{'fraction_non_alphanumeric': 0.10073363431151242, 'fraction_numerical': 0.000564334085778781, 'mean_word_length': 3.0375854214123006, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7242879', 'n_tokens_mistral': 975, 'n_tokens_neox': 949, 'n_words': 240}