Sha256: b123c9526d45b0888e892b73ce30eda8c23214f673fe207713df2d8ce1868ce3

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

module Pacto
  class Response
    def initialize(definition)
      @definition = definition
    end

    def instantiate
      OpenStruct.new({
        'status' => @definition['status'],
        'headers' => @definition['headers'],
        'body' => JSON::Generator.generate(@definition['body'])
      })
    end

    def validate(response)
      @errors = []
      if @definition['status'] != response.status
        @errors << "Invalid status: expected #{@definition['status']} but got #{response.status}"
      end
      unless @definition['headers'].normalize_keys.subset_of?(response.headers.normalize_keys)
        @errors << "Invalid headers: expected #{@definition['headers'].inspect} to be a subset of #{response.headers.inspect}"
      end
      @errors << JSON::Validator.fully_validate(@definition['body'], response.body)
      @errors.flatten
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pacto-0.1.0 lib/pacto/response.rb
pacto-0.0.1 lib/pacto/response.rb