Class: OpenapiRspec::RequestValidator
- Inherits:
-
Object
- Object
- OpenapiRspec::RequestValidator
- Includes:
- Rack::Test::Methods
- Defined in:
- lib/openapi_rspec/request_validator.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#media_type ⇒ Object
readonly
Returns the value of attribute media_type.
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
Instance Method Summary collapse
- #app ⇒ Object
- #description ⇒ Object
- #failure_message ⇒ Object
-
#initialize(path:, method:, code:, media_type: "application/json", params: {}, query: {}, headers: {}) ⇒ RequestValidator
constructor
A new instance of RequestValidator.
- #matches?(doc) ⇒ Boolean
- #request_params ⇒ Object
- #request_uri(doc) ⇒ Object
Constructor Details
#initialize(path:, method:, code:, media_type: "application/json", params: {}, query: {}, headers: {}) ⇒ RequestValidator
Returns a new instance of RequestValidator.
12 13 14 15 16 17 18 19 20 |
# File 'lib/openapi_rspec/request_validator.rb', line 12 def initialize(path:, method:, code:, media_type: "application/json", params: {}, query: {}, headers: {}) @path = path @method = method @code = code @media_type = media_type @query = query @headers = headers @params = params end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def code @code end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def headers @headers end |
#media_type ⇒ Object (readonly)
Returns the value of attribute media_type.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def media_type @media_type end |
#method ⇒ Object (readonly)
Returns the value of attribute method.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def method @method end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def path @path end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def query @query end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
22 23 24 |
# File 'lib/openapi_rspec/request_validator.rb', line 22 def response @response end |
Instance Method Details
#app ⇒ Object
8 9 10 |
# File 'lib/openapi_rspec/request_validator.rb', line 8 def app OpenapiRspec.app end |
#description ⇒ Object
58 59 60 |
# File 'lib/openapi_rspec/request_validator.rb', line 58 def description "return valid response with code #{code} on `#{method.to_s.upcase} #{path}`" end |
#failure_message ⇒ Object
62 63 64 65 66 67 68 |
# File 'lib/openapi_rspec/request_validator.rb', line 62 def if @response (%W(Response: #{@response.body}) + @result.errors).join("\n") else @result.errors.join("\n") end end |
#matches?(doc) ⇒ Boolean
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/openapi_rspec/request_validator.rb', line 24 def matches?(doc) @result = doc.validate_request(path: path, method: method, code: code, media_type: media_type) return false unless @result.valid? headers.each do |key, value| header key, value end request(request_uri(doc), method: method, **request_params) @response = last_response @result.validate_response(body: @response.body, code: @response.status) @result.valid? end |
#request_params ⇒ Object
51 52 53 54 55 56 |
# File 'lib/openapi_rspec/request_validator.rb', line 51 def request_params { headers: headers, params: params, } end |
#request_uri(doc) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/openapi_rspec/request_validator.rb', line 38 def request_uri(doc) path.scan(/\{([^\}]*)\}/).each do |param| key = param.first.to_sym if params && params[key] @path = path.gsub "{#{key}}", params.delete(key).to_s else raise URI::InvalidURIError, "No substitution data found for {#{key}}"\ " to test the path #{path}." end end "#{doc.api_base_path}#{path}?#{URI.encode_www_form(query)}" end |