webspicy 0.15.8 → 0.16.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +71 -24
- data/bin/webspicy +30 -14
- data/examples/restful/Gemfile.lock +38 -23
- data/examples/restful/Rakefile +0 -1
- data/examples/restful/app.rb +4 -1
- data/examples/restful/webspicy/config.rb +8 -0
- data/examples/restful/webspicy/rack.rb +1 -1
- data/examples/restful/webspicy/real.rb +1 -1
- data/examples/restful/webspicy/schema.fio +2 -2
- data/examples/restful/webspicy/support/must_be_authenticated.rb +2 -2
- data/examples/restful/webspicy/support/todo_removed.rb +18 -0
- data/examples/restful/webspicy/todo/deleteTodo.yml +4 -1
- data/examples/restful/webspicy/todo/getTodoSingleServiceFormat.yml +46 -0
- data/examples/restful/webspicy/todo/options.yml +1 -1
- data/examples/restful/webspicy/todo/patchTodo.yml +3 -0
- data/examples/restful/webspicy/todo/postFile.yml +1 -1
- data/examples/single_spec/spec.yml +59 -0
- data/examples/website/config.rb +2 -0
- data/examples/website/schema.fio +1 -0
- data/examples/website/specification/get-http.yml +34 -0
- data/examples/website/specification/get-https.yml +34 -0
- data/lib/finitio/webspicy/scalars.fio +25 -0
- data/lib/webspicy.rb +48 -17
- data/lib/webspicy/checker.rb +2 -2
- data/lib/webspicy/configuration.rb +70 -14
- data/lib/webspicy/configuration/scope.rb +162 -0
- data/lib/webspicy/configuration/single_url.rb +58 -0
- data/lib/webspicy/configuration/single_yml_file.rb +30 -0
- data/lib/webspicy/formaldoc.fio +23 -8
- data/lib/webspicy/mocker.rb +8 -8
- data/lib/webspicy/openapi.rb +1 -0
- data/lib/webspicy/openapi/generator.rb +127 -0
- data/lib/webspicy/{resource.rb → specification.rb} +26 -5
- data/lib/webspicy/specification/file_upload.rb +37 -0
- data/lib/webspicy/specification/postcondition.rb +16 -0
- data/lib/webspicy/specification/precondition.rb +19 -0
- data/lib/webspicy/specification/precondition/global_request_headers.rb +35 -0
- data/lib/webspicy/specification/precondition/robust_to_invalid_input.rb +68 -0
- data/lib/webspicy/{resource → specification}/service.rb +11 -6
- data/lib/webspicy/specification/test_case.rb +139 -0
- data/lib/webspicy/support.rb +1 -0
- data/lib/webspicy/support/colorize.rb +28 -0
- data/lib/webspicy/support/status_range.rb +6 -1
- data/lib/webspicy/tester.rb +16 -11
- data/lib/webspicy/tester/asserter.rb +3 -2
- data/lib/webspicy/tester/assertions.rb +5 -1
- data/lib/webspicy/tester/client.rb +63 -0
- data/lib/webspicy/tester/client/http_client.rb +154 -0
- data/lib/webspicy/tester/client/rack_test_client.rb +188 -0
- data/lib/webspicy/tester/client/support.rb +65 -0
- data/lib/webspicy/tester/invocation.rb +218 -0
- data/lib/webspicy/tester/rspec_asserter.rb +108 -0
- data/lib/webspicy/tester/rspec_matchers.rb +104 -0
- data/lib/webspicy/version.rb +2 -2
- data/spec/{unit/spec_helper.rb → spec_helper.rb} +0 -0
- data/spec/unit/configuration/scope/test_each_service.rb +49 -0
- data/spec/unit/configuration/scope/test_each_specification.rb +68 -0
- data/spec/unit/configuration/scope/test_expand_example.rb +65 -0
- data/spec/unit/configuration/scope/test_to_real_url.rb +82 -0
- data/spec/unit/openapi/test_generator.rb +28 -0
- data/spec/unit/specification/precondition/test_global_request_headers.rb +42 -0
- data/spec/unit/{resource → specification}/service/test_dress_params.rb +2 -2
- data/spec/unit/specification/test_case/test_mutate.rb +24 -0
- data/spec/unit/{resource → specification}/test_instantiate_url.rb +5 -5
- data/spec/unit/{resource → specification}/test_url_placeholders.rb +4 -4
- data/spec/unit/test_configuration.rb +24 -7
- data/spec/unit/tester/client/test_around.rb +61 -0
- data/spec/unit/tester/test_asserter.rb +51 -0
- data/spec/unit/tester/test_assertions.rb +4 -4
- data/tasks/test.rake +3 -1
- metadata +83 -34
- data/lib/webspicy/client.rb +0 -61
- data/lib/webspicy/client/http_client.rb +0 -145
- data/lib/webspicy/client/rack_test_client.rb +0 -181
- data/lib/webspicy/client/support.rb +0 -48
- data/lib/webspicy/file_upload.rb +0 -35
- data/lib/webspicy/postcondition.rb +0 -14
- data/lib/webspicy/precondition.rb +0 -15
- data/lib/webspicy/resource/service/invocation.rb +0 -212
- data/lib/webspicy/resource/service/test_case.rb +0 -132
- data/lib/webspicy/scope.rb +0 -160
- data/spec/unit/client/test_around.rb +0 -59
- data/spec/unit/scope/test_each_resource.rb +0 -66
- data/spec/unit/scope/test_each_service.rb +0 -47
- data/spec/unit/scope/test_expand_example.rb +0 -63
- data/spec/unit/scope/test_to_real_url.rb +0 -80
@@ -1,145 +0,0 @@
|
|
1
|
-
module Webspicy
|
2
|
-
class HttpClient < Client
|
3
|
-
|
4
|
-
class ::HTTP::Request
|
5
|
-
|
6
|
-
# We monkey patch the URI normalization on Http because
|
7
|
-
# we don't want it to interfere with URIs that are encoded
|
8
|
-
# in tests, especially security tests.
|
9
|
-
def normalize_uri(uri)
|
10
|
-
uri
|
11
|
-
end
|
12
|
-
|
13
|
-
end # class ::HTTP::Request
|
14
|
-
|
15
|
-
def initialize(scope)
|
16
|
-
super(scope)
|
17
|
-
@api = Api.new
|
18
|
-
end
|
19
|
-
attr_reader :api
|
20
|
-
|
21
|
-
def call(test_case)
|
22
|
-
service, resource = test_case.service, test_case.resource
|
23
|
-
|
24
|
-
# Instantiate the parameters
|
25
|
-
headers = test_case.headers
|
26
|
-
params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
|
27
|
-
body = test_case.body || test_case.located_file_upload
|
28
|
-
|
29
|
-
# Instantiate the url and strip parameters
|
30
|
-
url, params = resource.instantiate_url(params)
|
31
|
-
|
32
|
-
# Globalize the URL if required
|
33
|
-
url = scope.to_real_url(url, test_case)
|
34
|
-
|
35
|
-
# Invoke the service now
|
36
|
-
api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
|
37
|
-
|
38
|
-
# Return the result
|
39
|
-
Resource::Service::Invocation.new(service, test_case, api.last_response, self)
|
40
|
-
end
|
41
|
-
|
42
|
-
class Api
|
43
|
-
include Client::Support
|
44
|
-
|
45
|
-
attr_reader :last_response
|
46
|
-
|
47
|
-
def options(url, params = {}, headers = nil, body = nil)
|
48
|
-
info_request("OPTIONS", url, params, headers, body)
|
49
|
-
|
50
|
-
params = querystring_params(params)
|
51
|
-
@last_response = HTTP[headers || {}].options(url, params: params)
|
52
|
-
|
53
|
-
debug_response(@last_response)
|
54
|
-
|
55
|
-
@last_response
|
56
|
-
end
|
57
|
-
|
58
|
-
def get(url, params = {}, headers = nil, body = nil)
|
59
|
-
info_request("GET", url, params, headers, body)
|
60
|
-
|
61
|
-
params = querystring_params(params)
|
62
|
-
@last_response = HTTP[headers || {}].get(url, params: params)
|
63
|
-
|
64
|
-
debug_response(@last_response)
|
65
|
-
|
66
|
-
@last_response
|
67
|
-
end
|
68
|
-
|
69
|
-
def post(url, params = {}, headers = nil, body = nil)
|
70
|
-
info_request("POST", url, params, headers, body)
|
71
|
-
|
72
|
-
url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
|
73
|
-
|
74
|
-
headers ||= {}
|
75
|
-
|
76
|
-
case body
|
77
|
-
when NilClass
|
78
|
-
headers['Content-Type'] ||= 'application/json'
|
79
|
-
@last_response = HTTP[headers].post(url, body: params.to_json)
|
80
|
-
when FileUpload
|
81
|
-
file = HTTP::FormData::File.new(body.path.to_s, {
|
82
|
-
content_type: body.content_type,
|
83
|
-
filename: body.path.basename.to_s
|
84
|
-
})
|
85
|
-
@last_response = HTTP[headers].post(url, form: {
|
86
|
-
body.param_name.to_sym => file
|
87
|
-
})
|
88
|
-
else
|
89
|
-
headers['Content-Type'] ||= 'application/json'
|
90
|
-
@last_response = HTTP[headers].post(url, body: body)
|
91
|
-
end
|
92
|
-
|
93
|
-
debug_response(@last_response)
|
94
|
-
|
95
|
-
@last_response
|
96
|
-
end
|
97
|
-
|
98
|
-
def patch(url, params = {}, headers = nil, body = nil)
|
99
|
-
info_request("PATCH", url, params, headers, body)
|
100
|
-
|
101
|
-
headers ||= {}
|
102
|
-
headers['Content-Type'] ||= 'application/json'
|
103
|
-
@last_response = HTTP[headers].patch(url, body: params.to_json)
|
104
|
-
|
105
|
-
debug_response(@last_response)
|
106
|
-
|
107
|
-
@last_response
|
108
|
-
end
|
109
|
-
|
110
|
-
def put(url, params = {}, headers = nil, body = nil)
|
111
|
-
info_request("PUT", url, params, headers, body)
|
112
|
-
|
113
|
-
headers ||= {}
|
114
|
-
headers['Content-Type'] ||= 'application/json'
|
115
|
-
@last_response = HTTP[headers].put(url, body: params.to_json)
|
116
|
-
|
117
|
-
debug_response(@last_response)
|
118
|
-
|
119
|
-
@last_response
|
120
|
-
end
|
121
|
-
|
122
|
-
def post_form(url, params = {}, headers = nil, body = nil)
|
123
|
-
info_request("POST", url, params, headers, body)
|
124
|
-
|
125
|
-
@last_response = HTTP[headers || {}].post(url, form: params)
|
126
|
-
|
127
|
-
debug_response(@last_response)
|
128
|
-
|
129
|
-
@last_response
|
130
|
-
end
|
131
|
-
|
132
|
-
def delete(url, params = {}, headers = nil, body = nil)
|
133
|
-
info_request("DELETE", url, params, headers, body)
|
134
|
-
|
135
|
-
@last_response = HTTP[headers || {}].delete(url, body: params.to_json)
|
136
|
-
|
137
|
-
debug_response(@last_response)
|
138
|
-
|
139
|
-
@last_response
|
140
|
-
end
|
141
|
-
|
142
|
-
end
|
143
|
-
|
144
|
-
end
|
145
|
-
end
|
@@ -1,181 +0,0 @@
|
|
1
|
-
module Webspicy
|
2
|
-
class RackTestClient < Client
|
3
|
-
|
4
|
-
def self.for(app)
|
5
|
-
Factory.new(app)
|
6
|
-
end
|
7
|
-
|
8
|
-
def initialize(scope, app)
|
9
|
-
super(scope)
|
10
|
-
@api = Api.new(app)
|
11
|
-
end
|
12
|
-
attr_reader :api
|
13
|
-
|
14
|
-
def call(test_case)
|
15
|
-
service, resource = test_case.service, test_case.resource
|
16
|
-
|
17
|
-
# Instantiate the parameters
|
18
|
-
headers = test_case.headers.dup
|
19
|
-
params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
|
20
|
-
body = test_case.body || test_case.located_file_upload
|
21
|
-
|
22
|
-
# Instantiate the url and strip parameters
|
23
|
-
url, params = resource.instantiate_url(params)
|
24
|
-
url = scope.to_real_url(url, test_case){|u,_| u }
|
25
|
-
|
26
|
-
# Invoke the service now
|
27
|
-
api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
|
28
|
-
|
29
|
-
# Return the result
|
30
|
-
Resource::Service::Invocation.new(service, test_case, api.last_response, self)
|
31
|
-
end
|
32
|
-
|
33
|
-
class Factory
|
34
|
-
|
35
|
-
def initialize(app)
|
36
|
-
@app = app
|
37
|
-
end
|
38
|
-
attr_reader :app
|
39
|
-
|
40
|
-
def new(scope)
|
41
|
-
RackTestClient.new(scope, app)
|
42
|
-
end
|
43
|
-
|
44
|
-
end # class Factory
|
45
|
-
|
46
|
-
class RackHandler
|
47
|
-
include Rack::Test::Methods
|
48
|
-
|
49
|
-
def initialize(app)
|
50
|
-
@app = app
|
51
|
-
end
|
52
|
-
attr_reader :app
|
53
|
-
|
54
|
-
end # class RackHandler
|
55
|
-
|
56
|
-
class Api
|
57
|
-
include Client::Support
|
58
|
-
|
59
|
-
attr_reader :last_response
|
60
|
-
|
61
|
-
def initialize(app)
|
62
|
-
@app = app
|
63
|
-
end
|
64
|
-
|
65
|
-
def options(url, params = {}, headers = nil, body = nil)
|
66
|
-
handler = get_handler(headers)
|
67
|
-
|
68
|
-
info_request("OPTIONS", url, params, headers, body)
|
69
|
-
|
70
|
-
handler.options(url, params)
|
71
|
-
@last_response = handler.last_response
|
72
|
-
|
73
|
-
debug_response(@last_response)
|
74
|
-
|
75
|
-
@last_response
|
76
|
-
end
|
77
|
-
|
78
|
-
def get(url, params = {}, headers = nil, body = nil)
|
79
|
-
handler = get_handler(headers)
|
80
|
-
|
81
|
-
params = Hash[params.map{|k,v| [k, v.nil? ? "" : v] }]
|
82
|
-
info_request("GET", url, params, headers, body)
|
83
|
-
|
84
|
-
handler.get(url, params)
|
85
|
-
@last_response = handler.last_response
|
86
|
-
|
87
|
-
debug_response(@last_response)
|
88
|
-
|
89
|
-
@last_response
|
90
|
-
end
|
91
|
-
|
92
|
-
def post(url, params = {}, headers = nil, body = nil)
|
93
|
-
handler = get_handler(headers)
|
94
|
-
|
95
|
-
url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
|
96
|
-
|
97
|
-
case body
|
98
|
-
when NilClass
|
99
|
-
info_request("POST", url, params, headers, body)
|
100
|
-
handler.post(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
101
|
-
when FileUpload
|
102
|
-
file = Rack::Test::UploadedFile.new(body.path, body.content_type)
|
103
|
-
info_request("POST", url, params, headers, body)
|
104
|
-
handler.post(url, body.param_name.to_sym => file)
|
105
|
-
else
|
106
|
-
info_request("POST", url, params, headers, body)
|
107
|
-
handler.post(url, body)
|
108
|
-
end
|
109
|
-
@last_response = handler.last_response
|
110
|
-
|
111
|
-
debug_response(@last_response)
|
112
|
-
|
113
|
-
@last_response
|
114
|
-
end
|
115
|
-
|
116
|
-
def patch(url, params = {}, headers = nil, body = nil)
|
117
|
-
handler = get_handler(headers)
|
118
|
-
|
119
|
-
info_request("PATCH", url, params, headers, body)
|
120
|
-
|
121
|
-
handler.patch(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
122
|
-
@last_response = handler.last_response
|
123
|
-
|
124
|
-
debug_response(@last_response)
|
125
|
-
|
126
|
-
@last_response
|
127
|
-
end
|
128
|
-
|
129
|
-
def put(url, params = {}, headers = nil, body = nil)
|
130
|
-
handler = get_handler(headers)
|
131
|
-
|
132
|
-
info_request("PUT", url, params, headers, body)
|
133
|
-
|
134
|
-
handler.put(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
135
|
-
@last_response = handler.last_response
|
136
|
-
|
137
|
-
debug_response(@last_response)
|
138
|
-
|
139
|
-
@last_response
|
140
|
-
end
|
141
|
-
|
142
|
-
def post_form(url, params = {}, headers = nil, body = nil)
|
143
|
-
handler = get_handler(headers)
|
144
|
-
|
145
|
-
info_request("POST", url, params, headers, body)
|
146
|
-
|
147
|
-
handler.post(url, params)
|
148
|
-
@last_response = handler.last_response
|
149
|
-
|
150
|
-
debug_response(@last_response)
|
151
|
-
|
152
|
-
@last_response
|
153
|
-
end
|
154
|
-
|
155
|
-
def delete(url, params = {}, headers = nil, body = nil)
|
156
|
-
handler = get_handler(headers)
|
157
|
-
|
158
|
-
info_request("DELETE", url, params, headers, body)
|
159
|
-
|
160
|
-
handler.delete(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
161
|
-
@last_response = handler.last_response
|
162
|
-
|
163
|
-
debug_response(@last_response)
|
164
|
-
|
165
|
-
@last_response
|
166
|
-
end
|
167
|
-
|
168
|
-
private
|
169
|
-
|
170
|
-
def get_handler(hs)
|
171
|
-
handler = RackHandler.new(@app)
|
172
|
-
hs.each_pair do |k,v|
|
173
|
-
handler.header(k,v)
|
174
|
-
end if hs
|
175
|
-
handler
|
176
|
-
end
|
177
|
-
|
178
|
-
end # class Api
|
179
|
-
|
180
|
-
end # class RackTestClient
|
181
|
-
end # module Webspicy
|
@@ -1,48 +0,0 @@
|
|
1
|
-
module Webspicy
|
2
|
-
class Client
|
3
|
-
module Support
|
4
|
-
|
5
|
-
def querystring_params(params)
|
6
|
-
Hash[params.each_pair.map{|k,v| [k.to_s,v.to_s] }]
|
7
|
-
end
|
8
|
-
|
9
|
-
def info_request(kind, url, params, headers, body)
|
10
|
-
Webspicy.info("#{kind} #{url}")
|
11
|
-
debug("Req params", JSON.pretty_generate(params)) if params
|
12
|
-
debug("Req headers", JSON.pretty_generate(headers)) if headers
|
13
|
-
debug("Req body", request_body_to_s(body)) if body
|
14
|
-
Webspicy.debug("")
|
15
|
-
end
|
16
|
-
|
17
|
-
def debug_response(response)
|
18
|
-
debug("Res status", @last_response.status)
|
19
|
-
debug("Res headers", JSON.pretty_generate(last_response.headers.to_h))
|
20
|
-
debug("Res body", response_body_to_s(last_response))
|
21
|
-
Webspicy.debug("")
|
22
|
-
end
|
23
|
-
|
24
|
-
def debug(what, value)
|
25
|
-
Webspicy.debug(" #{what}: " + value_to_s(value))
|
26
|
-
end
|
27
|
-
|
28
|
-
def request_body_to_s(body)
|
29
|
-
body = body.to_info if body.is_a?(Webspicy::FileUpload)
|
30
|
-
JSON.pretty_generate(body)
|
31
|
-
end
|
32
|
-
|
33
|
-
def response_body_to_s(response)
|
34
|
-
case response.content_type.to_s
|
35
|
-
when /json/
|
36
|
-
JSON.pretty_generate(JSON.load(response.body))
|
37
|
-
else
|
38
|
-
response.body.to_s
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def value_to_s(value)
|
43
|
-
value.to_s.gsub(/\n/, "\n ")
|
44
|
-
end
|
45
|
-
|
46
|
-
end # module Support
|
47
|
-
end # class Client
|
48
|
-
end # module Webspicy
|
data/lib/webspicy/file_upload.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
module Webspicy
|
2
|
-
class FileUpload
|
3
|
-
|
4
|
-
def initialize(raw)
|
5
|
-
@path = raw[:path]
|
6
|
-
@content_type = raw[:content_type]
|
7
|
-
@param_name = raw[:param_name] || "file"
|
8
|
-
end
|
9
|
-
|
10
|
-
attr_reader :path, :content_type, :param_name
|
11
|
-
|
12
|
-
def self.info(raw)
|
13
|
-
new(raw)
|
14
|
-
end
|
15
|
-
|
16
|
-
def locate(resource)
|
17
|
-
FileUpload.new({
|
18
|
-
path: resource.locate(path),
|
19
|
-
content_type: content_type
|
20
|
-
})
|
21
|
-
end
|
22
|
-
|
23
|
-
def to_info
|
24
|
-
{ path: path.to_s,
|
25
|
-
content_type: content_type,
|
26
|
-
param_name: param_name }
|
27
|
-
end
|
28
|
-
|
29
|
-
def to_s
|
30
|
-
"FileUpload(#{to_info})"
|
31
|
-
end
|
32
|
-
alias :inspect :to_s
|
33
|
-
|
34
|
-
end # class FileUpload
|
35
|
-
end # module Webspicy
|
@@ -1,212 +0,0 @@
|
|
1
|
-
module Webspicy
|
2
|
-
class Resource
|
3
|
-
class Service
|
4
|
-
class Invocation
|
5
|
-
|
6
|
-
def initialize(service, test_case, response, client)
|
7
|
-
@service = service
|
8
|
-
@test_case = test_case
|
9
|
-
@response = response
|
10
|
-
@client = client
|
11
|
-
end
|
12
|
-
|
13
|
-
attr_reader :service, :test_case, :response, :client
|
14
|
-
|
15
|
-
def errors
|
16
|
-
@errors ||= begin
|
17
|
-
errs = [
|
18
|
-
[:expected_status_unmet, true],
|
19
|
-
[:expected_content_type_unmet, !test_case.is_expected_status?(204)],
|
20
|
-
[:expected_headers_unmet, test_case.has_expected_headers?],
|
21
|
-
[:expected_schema_unmet, !test_case.is_expected_status?(204)],
|
22
|
-
[:assertions_unmet, test_case.has_assertions?],
|
23
|
-
[:postconditions_unmet, test_case.service.has_postconditions? && !test_case.counterexample?],
|
24
|
-
[:expected_error_unmet, test_case.has_expected_error?]
|
25
|
-
].map do |(expectation,only_if)|
|
26
|
-
next unless only_if
|
27
|
-
begin
|
28
|
-
self.send(expectation)
|
29
|
-
rescue => ex
|
30
|
-
ex.message
|
31
|
-
end
|
32
|
-
end
|
33
|
-
errs.compact
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def has_error?
|
38
|
-
!errors.empty?
|
39
|
-
end
|
40
|
-
|
41
|
-
### Getters on response
|
42
|
-
|
43
|
-
def response_code
|
44
|
-
code = response.status
|
45
|
-
code = code.code unless code.is_a?(Integer)
|
46
|
-
code
|
47
|
-
end
|
48
|
-
|
49
|
-
### Query methods
|
50
|
-
|
51
|
-
def done?
|
52
|
-
!response.nil?
|
53
|
-
end
|
54
|
-
|
55
|
-
def is_expected_success?
|
56
|
-
test_case.expected_status.to_i >= 200 && test_case.expected_status.to_i < 300
|
57
|
-
end
|
58
|
-
|
59
|
-
def is_success?
|
60
|
-
response_code >= 200 && response_code < 300
|
61
|
-
end
|
62
|
-
|
63
|
-
def is_empty_response?
|
64
|
-
response_code == 204
|
65
|
-
end
|
66
|
-
|
67
|
-
def is_redirect?
|
68
|
-
response_code >= 300 && response_code < 400
|
69
|
-
end
|
70
|
-
|
71
|
-
### Check of HTTP status
|
72
|
-
|
73
|
-
def expected_status_unmet
|
74
|
-
expected = test_case.expected_status
|
75
|
-
got = response.status
|
76
|
-
expected === got ? nil : "#{expected} !== #{got}"
|
77
|
-
end
|
78
|
-
|
79
|
-
def meets_expected_status?
|
80
|
-
expected_status_unmet.nil?
|
81
|
-
end
|
82
|
-
|
83
|
-
### Check of the expected output type
|
84
|
-
|
85
|
-
def expected_content_type_unmet
|
86
|
-
ect = test_case.expected_content_type
|
87
|
-
got = response.content_type
|
88
|
-
got = got.mime_type if got.respond_to?(:mime_type)
|
89
|
-
if ect.nil?
|
90
|
-
got.nil? ? nil : "#{ect} != #{got}"
|
91
|
-
else
|
92
|
-
got.to_s.start_with?(ect.to_s) ? nil : "#{ect} != #{got}"
|
93
|
-
end
|
94
|
-
end
|
95
|
-
|
96
|
-
def meets_expected_content_type?
|
97
|
-
expected_content_type_unmet.nil?
|
98
|
-
end
|
99
|
-
|
100
|
-
### Check of output schema
|
101
|
-
|
102
|
-
def expected_schema_unmet
|
103
|
-
if is_empty_response?
|
104
|
-
body = response.body.to_s.strip
|
105
|
-
body.empty? ? nil : "Expected empty body, got #{body}"
|
106
|
-
elsif is_redirect?
|
107
|
-
else
|
108
|
-
case dressed_body
|
109
|
-
when Finitio::TypeError
|
110
|
-
rc = dressed_body.root_cause
|
111
|
-
"#{rc.message} (#{rc.location ? rc.location : 'unknown location'})"
|
112
|
-
when StandardError
|
113
|
-
dressed_body.message
|
114
|
-
else nil
|
115
|
-
end
|
116
|
-
end
|
117
|
-
end
|
118
|
-
|
119
|
-
def meets_expected_schema?
|
120
|
-
expected_schema_unmet.nil?
|
121
|
-
end
|
122
|
-
|
123
|
-
### Check of assertions
|
124
|
-
|
125
|
-
def assertions_unmet
|
126
|
-
unmet = []
|
127
|
-
asserter = Tester::Asserter.new(dressed_body)
|
128
|
-
test_case.assert.each do |assert|
|
129
|
-
begin
|
130
|
-
asserter.instance_eval(assert)
|
131
|
-
rescue => ex
|
132
|
-
unmet << ex.message
|
133
|
-
end
|
134
|
-
end
|
135
|
-
unmet.empty? ? nil : unmet.join("\n")
|
136
|
-
end
|
137
|
-
|
138
|
-
def value_equal(exp, got)
|
139
|
-
case exp
|
140
|
-
when Hash
|
141
|
-
exp.all?{|(k,v)|
|
142
|
-
got[k] == v
|
143
|
-
}
|
144
|
-
else
|
145
|
-
exp == got
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
### Check of expected error message
|
150
|
-
|
151
|
-
def expected_error_unmet
|
152
|
-
expected = test_case.expected_error
|
153
|
-
case test_case.expected_content_type
|
154
|
-
when %r{json}
|
155
|
-
got = meets_expected_schema? ? dressed_body[:description] : response.body
|
156
|
-
expected == got ? nil : "`#{expected}` vs. `#{got}`"
|
157
|
-
else
|
158
|
-
dressed_body.include?(expected) ? nil : "#{expected} not found" unless expected.nil?
|
159
|
-
end
|
160
|
-
end
|
161
|
-
|
162
|
-
### Check of expected headers
|
163
|
-
|
164
|
-
def expected_headers_unmet
|
165
|
-
unmet = []
|
166
|
-
expected = test_case.expected_headers
|
167
|
-
expected.each_pair do |k,v|
|
168
|
-
got = response.headers[k]
|
169
|
-
unmet << "#{v} expected for #{k}, got #{got}" unless (got == v)
|
170
|
-
end
|
171
|
-
unmet.empty? ? nil : unmet.join("\n")
|
172
|
-
end
|
173
|
-
|
174
|
-
### Check of postconditions
|
175
|
-
|
176
|
-
def postconditions_unmet
|
177
|
-
failures = service.postconditions.map{|post|
|
178
|
-
post.check(self)
|
179
|
-
}.compact
|
180
|
-
failures.empty? ? nil : failures.join("\n")
|
181
|
-
end
|
182
|
-
|
183
|
-
private
|
184
|
-
|
185
|
-
def loaded_body
|
186
|
-
case test_case.expected_content_type
|
187
|
-
when %r{json}
|
188
|
-
raise "Body empty while expected" if response.body.to_s.empty?
|
189
|
-
@loaded_body ||= ::JSON.parse(response.body)
|
190
|
-
else
|
191
|
-
response.body.to_s
|
192
|
-
end
|
193
|
-
end
|
194
|
-
|
195
|
-
def dressed_body
|
196
|
-
@dressed_body ||= case test_case.expected_content_type
|
197
|
-
when %r{json}
|
198
|
-
schema = is_expected_success? ? service.output_schema : service.error_schema
|
199
|
-
begin
|
200
|
-
schema.dress(loaded_body)
|
201
|
-
rescue Finitio::TypeError => ex
|
202
|
-
ex
|
203
|
-
end
|
204
|
-
else
|
205
|
-
loaded_body
|
206
|
-
end
|
207
|
-
end
|
208
|
-
|
209
|
-
end # class Invocation
|
210
|
-
end # class Service
|
211
|
-
end # class Resource
|
212
|
-
end # module Webspicy
|