Class: Rack::Spec::ResponseValidation::Validator

Inherits:
BaseRequestHandler show all
Defined in:
lib/rack/spec/response_validation.rb

Instance Method Summary collapse

Methods inherited from BaseRequestHandler

call

Constructor Details

#initialize(env: nil, response: nil, schema: nil) ⇒ Validator

Returns a new instance of Validator.

Parameters:

  • env (Hash) (defaults to: nil)

    Rack env

  • response (Array) (defaults to: nil)

    Rack response

  • schema (JsonSchema::Schema) (defaults to: nil)

    Schema object



25
26
27
28
29
# File 'lib/rack/spec/response_validation.rb', line 25

def initialize(env: nil, response: nil, schema: nil)
  @env = env
  @response = response
  @schema = schema
end

Instance Method Details

#bodyString

Returns Response body.

Returns:

  • (String)

    Response body



80
81
82
83
84
# File 'lib/rack/spec/response_validation.rb', line 80

def body
  result = ""
  @response[2].each {|str| result << str }
  result
end

#callObject

Raises an error if any error detected, skipping validation for non-defined link

Raises:

  • (Rack::Spec::ResponseValidation::InvalidResponse)


33
34
35
36
37
38
39
40
41
42
# File 'lib/rack/spec/response_validation.rb', line 33

def call
  if has_link_for_current_action?
    case
    when !has_json_content_type?
      raise InvalidResponseContentType
    when !valid?
      raise InvalidResponseType, validator.errors
    end
  end
end

#dataArray, Hash

Returns Response body data, decoded from JSON.

Returns:

  • (Array, Hash)

    Response body data, decoded from JSON



64
65
66
# File 'lib/rack/spec/response_validation.rb', line 64

def data
  MultiJson.decode(body)
end

#example_itemHash

Returns Choose an item from response data, to be validated.

Returns:

  • (Hash)

    Choose an item from response data, to be validated



55
56
57
58
59
60
61
# File 'lib/rack/spec/response_validation.rb', line 55

def example_item
  if has_list_data?
    data.first
  else
    data
  end
end

#has_json_content_type?true, false

Returns True if response Content-Type is for JSON.

Returns:

  • (true, false)

    True if response Content-Type is for JSON



45
46
47
# File 'lib/rack/spec/response_validation.rb', line 45

def has_json_content_type?
  %r<\Aapplication/.*json> === headers["Content-Type"]
end

#headersHash

Returns Response headers.

Returns:

  • (Hash)

    Response headers



75
76
77
# File 'lib/rack/spec/response_validation.rb', line 75

def headers
  @response[1]
end

#valid?true, false

Returns True if given data is valid to the JSON schema.

Returns:

  • (true, false)

    True if given data is valid to the JSON schema



50
51
52
# File 'lib/rack/spec/response_validation.rb', line 50

def valid?
  validator.validate(example_item)
end

#validatorJsonSchema::Validator

Note:

The result is memoized for returning errors in invalid case

Returns:

  • (JsonSchema::Validator)


70
71
72
# File 'lib/rack/spec/response_validation.rb', line 70

def validator
  @validator ||= JsonSchema::Validator.new(schema_for_current_link)
end