Class: OpenapiRspec::RequestValidator

Inherits:
Object
  • Object
show all
Includes:
Rack::Test::Methods
Defined in:
lib/openapi_rspec/request_validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#codeObject (readonly)

Returns the value of attribute code.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def code
  @code
end

#headersObject (readonly)

Returns the value of attribute headers.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def headers
  @headers
end

#media_typeObject (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

#methodObject (readonly)

Returns the value of attribute method.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def path
  @path
end

#queryObject (readonly)

Returns the value of attribute query.



22
23
24
# File 'lib/openapi_rspec/request_validator.rb', line 22

def query
  @query
end

#responseObject (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

#appObject



8
9
10
# File 'lib/openapi_rspec/request_validator.rb', line 8

def app
  OpenapiRspec.app
end

#descriptionObject



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_messageObject



62
63
64
65
66
67
68
# File 'lib/openapi_rspec/request_validator.rb', line 62

def failure_message
  if @response
    (%W(Response: #{@response.body}) + @result.errors).join("\n")
  else
    @result.errors.join("\n")
  end
end

#matches?(doc) ⇒ Boolean

Returns:

  • (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_paramsObject



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