Class: ApiTester::MethodCaseTest

Inherits:
Object
  • Object
show all
Defined in:
lib/api-tester/method_case_test.rb

Overview

Class for testing methods

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response:, payload:, expected_response:, url:, verb:, module_name:) ⇒ MethodCaseTest

Returns a new instance of MethodCaseTest.



10
11
12
13
14
15
16
17
# File 'lib/api-tester/method_case_test.rb', line 10

def initialize(response:, payload:, expected_response:, url:, verb:, module_name:)
  self.payload = payload
  self.response = response
  self.expected_response = expected_response
  self.reports = []
  self.url = "#{verb} #{url}"
  self.module_name = module_name
end

Instance Attribute Details

#expected_responseObject

Returns the value of attribute expected_response.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def expected_response
  @expected_response
end

#module_nameObject

Returns the value of attribute module_name.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def module_name
  @module_name
end

#payloadObject

Returns the value of attribute payload.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def payload
  @payload
end

#reportsObject

Returns the value of attribute reports.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def reports
  @reports
end

#responseObject

Returns the value of attribute response.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def response
  @response
end

#urlObject

Returns the value of attribute url.



8
9
10
# File 'lib/api-tester/method_case_test.rb', line 8

def url
  @url
end

Instance Method Details

#checkObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/api-tester/method_case_test.rb', line 52

def check
  if check_response_code
    print '.'
    evaluator = ApiTester::ResponseEvaluator.new actual_body: json_parse(response.body),
                                                 expected_fields: expected_response
    evaluator.missing_fields.map { |field| missing_field_report(field) }
    evaluator.extra_fields.map { |field| extra_field_report(field) }
    increment_fields evaluator.seen_fields
  end
  reports
end

#check_response_codeObject



64
65
66
67
68
69
70
71
# File 'lib/api-tester/method_case_test.rb', line 64

def check_response_code
  if response && (response.code != expected_response.code)
    print 'F'
    response_code_report
    return false
  end
  true
end

#extra_field_report(field) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'lib/api-tester/method_case_test.rb', line 41

def extra_field_report(field)
  print 'F'
  report = Report.new description: "#{module_name} - Found extra field #{field}",
                      url: url,
                      request: payload,
                      expected_response: expected_response,
                      actual_response: response
  reports << report
  nil
end

#increment_fields(seen_fields) ⇒ Object



73
74
75
# File 'lib/api-tester/method_case_test.rb', line 73

def increment_fields(seen_fields)
  seen_fields.each(&:seen)
end

#json_parse(body) ⇒ Object



77
78
79
80
81
# File 'lib/api-tester/method_case_test.rb', line 77

def json_parse(body)
  JSON.parse!(body)
rescue JSON::ParserError
  body
end

#missing_field_report(field) ⇒ Object



30
31
32
33
34
35
36
37
38
39
# File 'lib/api-tester/method_case_test.rb', line 30

def missing_field_report(field)
  print 'F'
  report = Report.new description: "#{module_name} - Missing field #{field}",
                      url: url,
                      request: payload,
                      expected_response: expected_response,
                      actual_response: response
  reports << report
  nil
end

#response_code_reportObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/api-tester/method_case_test.rb', line 19

def response_code_report
  print 'F'
  report = StatusCodeReport.new description: "#{module_name} - Incorrect response code",
                                url: url,
                                request: payload,
                                expected_status_code: expected_response.code,
                                actual_status_code: "#{response.code} : #{response.body}"
  reports << report
  nil
end