Class: Webspicy::Resource::Service::Invocation
- Inherits:
-
Object
- Object
- Webspicy::Resource::Service::Invocation
- Defined in:
- lib/webspicy/resource/service/invocation.rb
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#response ⇒ Object
readonly
Returns the value of attribute response.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#test_case ⇒ Object
readonly
Returns the value of attribute test_case.
Instance Method Summary collapse
-
#assertions_unmet ⇒ Object
Check of assertions.
-
#done? ⇒ Boolean
Query methods.
-
#expected_content_type_unmet ⇒ Object
Check of the expected output type.
-
#expected_error_unmet ⇒ Object
Check of expected error message.
-
#expected_headers_unmet ⇒ Object
Check of expected headers.
-
#expected_schema_unmet ⇒ Object
Check of output schema.
-
#expected_status_unmet ⇒ Object
Check of HTTP status.
-
#initialize(service, test_case, response, client) ⇒ Invocation
constructor
A new instance of Invocation.
- #is_empty_response? ⇒ Boolean
- #is_expected_success? ⇒ Boolean
- #is_redirect? ⇒ Boolean
- #is_success? ⇒ Boolean
- #meets_expected_content_type? ⇒ Boolean
- #meets_expected_schema? ⇒ Boolean
- #meets_expected_status? ⇒ Boolean
-
#postconditions_unmet ⇒ Object
Check of postconditions.
-
#response_code ⇒ Object
Getters on response.
- #value_equal(exp, got) ⇒ Object
Constructor Details
#initialize(service, test_case, response, client) ⇒ Invocation
Returns a new instance of Invocation.
6 7 8 9 10 11 |
# File 'lib/webspicy/resource/service/invocation.rb', line 6 def initialize(service, test_case, response, client) @service = service @test_case = test_case @response = response @client = client end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
13 14 15 |
# File 'lib/webspicy/resource/service/invocation.rb', line 13 def client @client end |
#response ⇒ Object (readonly)
Returns the value of attribute response.
13 14 15 |
# File 'lib/webspicy/resource/service/invocation.rb', line 13 def response @response end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
13 14 15 |
# File 'lib/webspicy/resource/service/invocation.rb', line 13 def service @service end |
#test_case ⇒ Object (readonly)
Returns the value of attribute test_case.
13 14 15 |
# File 'lib/webspicy/resource/service/invocation.rb', line 13 def test_case @test_case end |
Instance Method Details
#assertions_unmet ⇒ Object
Check of assertions
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/webspicy/resource/service/invocation.rb', line 96 def assertions_unmet unmet = [] asserter = Tester::Asserter.new(dressed_body) test_case.assert.each do |assert| begin asserter.instance_eval(assert) rescue => ex unmet << ex. end end unmet.empty? ? nil : unmet.join("\n") end |
#done? ⇒ Boolean
Query methods
25 26 27 |
# File 'lib/webspicy/resource/service/invocation.rb', line 25 def done? !response.nil? end |
#expected_content_type_unmet ⇒ Object
Check of the expected output type
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/webspicy/resource/service/invocation.rb', line 59 def expected_content_type_unmet ect = test_case.expected_content_type got = response.content_type got = got.mime_type if got.respond_to?(:mime_type) if ect.nil? got.nil? ? nil : "#{ect} != #{got}" else ect.to_s == got.to_s ? nil : "#{ect} != #{got}" end end |
#expected_error_unmet ⇒ Object
Check of expected error message
122 123 124 125 126 127 128 129 130 131 |
# File 'lib/webspicy/resource/service/invocation.rb', line 122 def expected_error_unmet expected = test_case.expected_error case test_case.expected_content_type when %r{json} got = meets_expected_schema? ? dressed_body[:description] : response.body expected == got ? nil : "`#{expected}` vs. `#{got}`" else dressed_body.include?(expected) ? nil : "#{expected} not found" unless expected.nil? end end |
#expected_headers_unmet ⇒ Object
Check of expected headers
135 136 137 138 139 140 141 142 143 |
# File 'lib/webspicy/resource/service/invocation.rb', line 135 def expected_headers_unmet unmet = [] expected = test_case.expected_headers expected.each_pair do |k,v| got = response.headers[k] unmet << "#{v} expected for #{k}, got #{got}" unless (got == v) end unmet.empty? ? nil : unmet.join("\n") end |
#expected_schema_unmet ⇒ Object
Check of output schema
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/webspicy/resource/service/invocation.rb', line 76 def expected_schema_unmet if is_empty_response? body = response.body.to_s.strip body.empty? ? nil : "Expected empty body, got #{body}" elsif is_redirect? else case dressed_body when Finitio::TypeError then dressed_body.root_cause. when StandardError then dressed_body. else nil end end end |
#expected_status_unmet ⇒ Object
Check of HTTP status
47 48 49 50 51 |
# File 'lib/webspicy/resource/service/invocation.rb', line 47 def expected_status_unmet expected = test_case.expected_status got = response.status expected == got ? nil : "#{expected} != #{got}" end |
#is_empty_response? ⇒ Boolean
37 38 39 |
# File 'lib/webspicy/resource/service/invocation.rb', line 37 def is_empty_response? response_code == 204 end |
#is_expected_success? ⇒ Boolean
29 30 31 |
# File 'lib/webspicy/resource/service/invocation.rb', line 29 def is_expected_success? test_case.expected_status >= 200 && test_case.expected_status < 300 end |
#is_redirect? ⇒ Boolean
41 42 43 |
# File 'lib/webspicy/resource/service/invocation.rb', line 41 def is_redirect? response_code >= 300 && response_code < 400 end |
#is_success? ⇒ Boolean
33 34 35 |
# File 'lib/webspicy/resource/service/invocation.rb', line 33 def is_success? response_code >= 200 && response_code < 300 end |
#meets_expected_content_type? ⇒ Boolean
70 71 72 |
# File 'lib/webspicy/resource/service/invocation.rb', line 70 def meets_expected_content_type? expected_content_type_unmet.nil? end |
#meets_expected_schema? ⇒ Boolean
90 91 92 |
# File 'lib/webspicy/resource/service/invocation.rb', line 90 def meets_expected_schema? expected_schema_unmet.nil? end |
#meets_expected_status? ⇒ Boolean
53 54 55 |
# File 'lib/webspicy/resource/service/invocation.rb', line 53 def meets_expected_status? expected_status_unmet.nil? end |
#postconditions_unmet ⇒ Object
Check of postconditions
147 148 149 150 151 152 |
# File 'lib/webspicy/resource/service/invocation.rb', line 147 def postconditions_unmet failures = service.postconditions.map{|post| post.check(self) }.compact failures.empty? ? nil : failures.join("\n") end |
#response_code ⇒ Object
Getters on response
17 18 19 20 21 |
# File 'lib/webspicy/resource/service/invocation.rb', line 17 def response_code code = response.status code = code.code unless code.is_a?(Integer) code end |
#value_equal(exp, got) ⇒ Object
109 110 111 112 113 114 115 116 117 118 |
# File 'lib/webspicy/resource/service/invocation.rb', line 109 def value_equal(exp, got) case exp when Hash exp.all?{|(k,v)| got[k] == v } else exp == got end end |