Class: Apivore::Validator

Inherits:
Object
  • Object
show all
Includes:
ActionDispatch::Integration::Runner
Defined in:
lib/apivore/validator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, path, expected_response_code, params = {}) ⇒ Validator

Returns a new instance of Validator.


10
11
12
13
14
15
# File 'lib/apivore/validator.rb', line 10

def initialize(method, path, expected_response_code, params = {})
  @method = method.to_s
  @path = path.to_s
  @params = params
  @expected_response_code = expected_response_code.to_i
end

Instance Attribute Details

#expected_response_codeObject (readonly)

Returns the value of attribute expected_response_code


8
9
10
# File 'lib/apivore/validator.rb', line 8

def expected_response_code
  @expected_response_code
end

#methodObject (readonly)

Returns the value of attribute method


8
9
10
# File 'lib/apivore/validator.rb', line 8

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params


8
9
10
# File 'lib/apivore/validator.rb', line 8

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path


8
9
10
# File 'lib/apivore/validator.rb', line 8

def path
  @path
end

Instance Method Details

#apivore_build_path(path, data) ⇒ Object


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/apivore/validator.rb', line 46

def apivore_build_path(path, data)
  path.scan(/\{([^\}]*)\}/).each do |param|
    key = param.first
    if data && data[key]
      path = path.gsub "{#{key}}", data[key].to_s
    else
      raise URI::InvalidURIError, "No substitution data found for {#{key}}"\
        " to test the path #{path}.", caller
    end
  end
  path + (data['_query_string'] ? "?#{data['_query_string']}" : '')
end

#appObject

Required by ActionDispatch::Integration::Runner


130
131
132
# File 'lib/apivore/validator.rb', line 130

def app
  ::Rails.application
end

#check_request_path(swagger_checker) ⇒ Object


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/apivore/validator.rb', line 68

def check_request_path(swagger_checker)
  if !swagger_checker.has_path?(path)
    errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\
      " a documented path for #{path}"
  elsif !swagger_checker.has_method_at_path?(path, method)
    errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\
      " a documented path for #{method} #{path}"
  elsif !swagger_checker.has_response_code_for_path?(path, method, expected_response_code)
    errors << "Swagger doc: #{swagger_checker.swagger_path} does not have"\
      " a documented response code of #{expected_response_code} at path"\
      " #{method} #{path}. "\
      "\n             Available response codes: #{swagger_checker.response_codes_for_path(path, method)}"
  elsif method == "get" && swagger_checker.fragment(path, method, expected_response_code).nil?
    errors << "Swagger doc: #{swagger_checker.swagger_path} missing"\
      " response model for get request with #{path} for code"\
      " #{expected_response_code}"
  end
end

#check_response_is_valid(swagger_checker) ⇒ Object


94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/apivore/validator.rb', line 94

def check_response_is_valid(swagger_checker)
  swagger_errors = swagger_checker.has_matching_document_for(
    path, method, response.status, response_body
  )
  unless swagger_errors.empty?
    errors.concat(
      swagger_errors.map do |e|
        e.sub("'#", "'#{full_path(swagger_checker)}#").gsub(
          /^The property|in schema.*$/,''
        )
      end
    )
  end
end

#check_status_codeObject


87
88
89
90
91
92
# File 'lib/apivore/validator.rb', line 87

def check_status_code
  if response.status != expected_response_code
    errors << "Path #{path} did not respond with expected status code."\
      " Expected #{expected_response_code} got #{response.status}"\
  end
end

#descriptionObject


125
126
127
# File 'lib/apivore/validator.rb', line 125

def description
  "validate that #{method} #{path} returns #{expected_response_code}"
end

#errorsObject


121
122
123
# File 'lib/apivore/validator.rb', line 121

def errors
  @errors ||= []
end

#failure_messageObject


117
118
119
# File 'lib/apivore/validator.rb', line 117

def failure_message
  errors.join(" ")
end

#full_path(swagger_checker) ⇒ Object


42
43
44
# File 'lib/apivore/validator.rb', line 42

def full_path(swagger_checker)
  apivore_build_path(swagger_checker.base_path + path, params)
end

#has_errors?Boolean

Returns:

  • (Boolean)

113
114
115
# File 'lib/apivore/validator.rb', line 113

def has_errors?
  !errors.empty?
end

#matches?(swagger_checker) ⇒ Boolean

Returns:

  • (Boolean)

17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/apivore/validator.rb', line 17

def matches?(swagger_checker)
  pre_checks(swagger_checker)

  unless has_errors?
    send(
      method,
      full_path(swagger_checker),
      params['_data'] || {},
      params['_headers'] || {}
    )
    swagger_checker.response = response
    post_checks(swagger_checker)

    if has_errors? && response.body.length > 0
      errors << "\nResponse body:\n #{JSON.pretty_generate(JSON.parse(response.body))}"
    end

    swagger_checker.remove_tested_end_point_response(
      path, method, expected_response_code
    )
  end

  !has_errors?
end

#post_checks(swagger_checker) ⇒ Object


63
64
65
66
# File 'lib/apivore/validator.rb', line 63

def post_checks(swagger_checker)
  check_status_code
  check_response_is_valid(swagger_checker) unless has_errors?
end

#pre_checks(swagger_checker) ⇒ Object


59
60
61
# File 'lib/apivore/validator.rb', line 59

def pre_checks(swagger_checker)
  check_request_path(swagger_checker)
end

#reset_template_assertionObject

Required by rails


135
136
# File 'lib/apivore/validator.rb', line 135

def reset_template_assertion
end

#response_bodyObject


109
110
111
# File 'lib/apivore/validator.rb', line 109

def response_body
  JSON.parse(response.body) unless response.body.blank?
end