Class: Rack::Spec::ResponseValidation::Validator
- Inherits:
-
BaseRequestHandler
- Object
- BaseRequestHandler
- Rack::Spec::ResponseValidation::Validator
- Defined in:
- lib/rack/spec/response_validation.rb
Instance Method Summary collapse
-
#body ⇒ String
Response body.
-
#call ⇒ Object
Raises an error if any error detected, skipping validation for non-defined link.
-
#data ⇒ Array, Hash
Response body data, decoded from JSON.
-
#example_item ⇒ Hash
Choose an item from response data, to be validated.
-
#has_json_content_type? ⇒ true, false
True if response Content-Type is for JSON.
-
#headers ⇒ Hash
Response headers.
-
#initialize(env: nil, response: nil, schema: nil) ⇒ Validator
constructor
A new instance of Validator.
-
#valid? ⇒ true, false
True if given data is valid to the JSON schema.
- #validator ⇒ JsonSchema::Validator
Methods inherited from BaseRequestHandler
Constructor Details
#initialize(env: nil, response: nil, schema: nil) ⇒ Validator
Returns a new instance of Validator.
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
#body ⇒ String
Returns 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 |
#call ⇒ Object
Raises an error if any error detected, skipping validation for non-defined link
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 |
#data ⇒ Array, Hash
Returns 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_item ⇒ Hash
Returns 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.
45 46 47 |
# File 'lib/rack/spec/response_validation.rb', line 45 def has_json_content_type? %r<\Aapplication/.*json> === headers["Content-Type"] end |
#headers ⇒ Hash
Returns 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.
50 51 52 |
# File 'lib/rack/spec/response_validation.rb', line 50 def valid? validator.validate(example_item) end |
#validator ⇒ JsonSchema::Validator
Note:
The result is memoized for returning errors in invalid case
70 71 72 |
# File 'lib/rack/spec/response_validation.rb', line 70 def validator @validator ||= JsonSchema::Validator.new(schema_for_current_link) end |