Sha256: eba9cf996678f43b04a39d121866fd3ce23778c630463baeed03c5521b134642

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 KB

Contents

require 'fitting/response'
require 'fitting/storage/documentation'
require 'fitting/configuration'

module Fitting
  module Matchers
    class Response
      def matches?(response)
        @response = Fitting::Response.new(
          response,
          Fitting::Storage::Documentation.tomogram
        )
        if @response.within_prefix?(Fitting.configuration.prefix)
          @response.fully_validates.valid?
        else
          true
        end
      end

      def ===(other)
        matches?(other)
      end

      def failure_message
        unless @response.documented?
          return "response not documented\n"\
                 "got: #{@response.real_request_with_status}"
        end

        unless @response.fully_validates.valid?
          "response does not conform to json-schema\n"\
          "schemas: \n#{@response.expected}\n\n"\
          "got: #{@response.got}\n\n"\
          "errors: \n#{@response.fully_validates}\n"
        end
      end
    end

    class StrictResponse
      def matches?(response)
        @response = Fitting::Response.new(
          response,
          Fitting::Storage::Documentation.tomogram
        )
        @response.strict_fully_validates.valid?
      end

      def ===(other)
        matches?(other)
      end

      def failure_message
        unless @response.documented?
          return "response not documented\n"\
                 "got: #{@response.real_request_with_status}"
        end

        unless @response.strict_fully_validates.valid?
          "response does not conform to json-schema\n"\
          "schemas: \n#{@response.expected}\n\n"\
          "got: #{@response.got}\n\n"\
          "errors: \n#{@response.strict_fully_validates}\n"
        end
      end
    end

    def match_schema
      Response.new
    end

    def strictly_match_schema
      StrictResponse.new
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
fitting-2.0.3 lib/fitting/matchers/response_matcher.rb