Class: ApiTester::MethodCaseTest

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

Direct Known Subclasses

FormatTest, GoodCaseTest, TypoClass, VerbClass

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.



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

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.



5
6
7
# File 'lib/api-tester/method_case_test.rb', line 5

def expected_response
  @expected_response
end

#module_nameObject

Returns the value of attribute module_name.



10
11
12
# File 'lib/api-tester/method_case_test.rb', line 10

def module_name
  @module_name
end

#payloadObject

Returns the value of attribute payload.



6
7
8
# File 'lib/api-tester/method_case_test.rb', line 6

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.



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

def response
  @response
end

#urlObject

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#checkObject



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

def check
    if check_response_code
        evaluator = ApiTester::ResponseEvaluator.new json_parse(self.response.body), self.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
    return self.reports
end

#check_response_codeObject



49
50
51
52
53
54
55
# File 'lib/api-tester/method_case_test.rb', line 49

def check_response_code
    if response.code != expected_response.code
        response_code_report
        return false
    end
    return true
end

#extra_field_report(field) ⇒ Object



33
34
35
36
37
# File 'lib/api-tester/method_case_test.rb', line 33

def extra_field_report field
    report = Report.new "#{module_name} - Found extra field #{field}", self.url, self.payload, self.expected_response, self.response
    self.reports << report
    nil
end

#increment_fields(seen_fields) ⇒ Object



57
58
59
60
61
# File 'lib/api-tester/method_case_test.rb', line 57

def increment_fields seen_fields
    seen_fields.each do |field|
        field.seen
    end
end

#json_parse(body) ⇒ Object



63
64
65
66
67
# File 'lib/api-tester/method_case_test.rb', line 63

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

#missing_field_report(field) ⇒ Object



27
28
29
30
31
# File 'lib/api-tester/method_case_test.rb', line 27

def missing_field_report field
    report = Report.new "#{module_name} - Missing field #{field}", self.url, self.payload, self.expected_response, self.response
    self.reports << report
    nil
end

#response_code_reportObject



21
22
23
24
25
# File 'lib/api-tester/method_case_test.rb', line 21

def response_code_report
    report = StatusCodeReport.new "#{module_name} - Incorrect response code", self.url, self.payload, self.expected_response.code, self.response.code
    self.reports << report
    nil
end