webspicy 0.15.5 → 0.16.1
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 -13
- data/examples/restful/Gemfile +2 -2
- data/examples/restful/Gemfile.lock +53 -33
- data/examples/restful/Rakefile +0 -1
- data/examples/restful/app.rb +19 -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 +7 -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/restful/webspicy/todo/putTodo.yml +65 -0
- 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 +24 -9
- 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 +88 -45
- data/LICENSE.md +0 -22
- data/lib/webspicy/client.rb +0 -61
- data/lib/webspicy/client/http_client.rb +0 -133
- data/lib/webspicy/client/rack_test_client.rb +0 -168
- 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
@@ -0,0 +1,188 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Tester
|
3
|
+
class RackTestClient < Client
|
4
|
+
|
5
|
+
def self.for(app)
|
6
|
+
Factory.new(app)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(scope, app)
|
10
|
+
super(scope)
|
11
|
+
@api = Api.new(scope, app)
|
12
|
+
end
|
13
|
+
attr_reader :api
|
14
|
+
|
15
|
+
def call(test_case)
|
16
|
+
service, specification = test_case.service, test_case.specification
|
17
|
+
|
18
|
+
# Instantiate the parameters
|
19
|
+
headers = test_case.headers.dup
|
20
|
+
params = test_case.dress_params? ? service.dress_params(test_case.params) : test_case.params
|
21
|
+
body = test_case.body || test_case.located_file_upload
|
22
|
+
|
23
|
+
# Instantiate the url and strip parameters
|
24
|
+
url, params = specification.instantiate_url(params)
|
25
|
+
url = scope.to_real_url(url, test_case){|u,_| u }
|
26
|
+
|
27
|
+
# Invoke the service now
|
28
|
+
api.public_send(service.method.to_s.downcase.to_sym, url, params, headers, body)
|
29
|
+
|
30
|
+
# Return the last response
|
31
|
+
api.last_response
|
32
|
+
end
|
33
|
+
|
34
|
+
class Factory
|
35
|
+
|
36
|
+
def initialize(app)
|
37
|
+
@app = app
|
38
|
+
end
|
39
|
+
attr_reader :app
|
40
|
+
|
41
|
+
def new(scope)
|
42
|
+
RackTestClient.new(scope, app)
|
43
|
+
end
|
44
|
+
|
45
|
+
end # class Factory
|
46
|
+
|
47
|
+
class RackHandler
|
48
|
+
include Rack::Test::Methods
|
49
|
+
|
50
|
+
def initialize(app)
|
51
|
+
@app = app
|
52
|
+
end
|
53
|
+
attr_reader :app
|
54
|
+
|
55
|
+
end # class RackHandler
|
56
|
+
|
57
|
+
class Api
|
58
|
+
include Client::Support
|
59
|
+
|
60
|
+
attr_reader :last_response
|
61
|
+
|
62
|
+
def initialize(scope, app)
|
63
|
+
@scope = scope
|
64
|
+
@app = app
|
65
|
+
end
|
66
|
+
|
67
|
+
def config
|
68
|
+
@scope.config
|
69
|
+
end
|
70
|
+
|
71
|
+
def options(url, params = {}, headers = nil, body = nil)
|
72
|
+
handler = get_handler(headers)
|
73
|
+
|
74
|
+
info_request("OPTIONS", url, params, headers, body)
|
75
|
+
|
76
|
+
handler.options(url, params)
|
77
|
+
@last_response = handler.last_response
|
78
|
+
|
79
|
+
debug_response(@last_response)
|
80
|
+
|
81
|
+
@last_response
|
82
|
+
end
|
83
|
+
|
84
|
+
def get(url, params = {}, headers = nil, body = nil)
|
85
|
+
handler = get_handler(headers)
|
86
|
+
|
87
|
+
params = Hash[params.map{|k,v| [k, v.nil? ? "" : v] }]
|
88
|
+
info_request("GET", url, params, headers, body)
|
89
|
+
|
90
|
+
handler.get(url, params)
|
91
|
+
@last_response = handler.last_response
|
92
|
+
|
93
|
+
debug_response(@last_response)
|
94
|
+
|
95
|
+
@last_response
|
96
|
+
end
|
97
|
+
|
98
|
+
def post(url, params = {}, headers = nil, body = nil)
|
99
|
+
handler = get_handler(headers)
|
100
|
+
|
101
|
+
url = url + "?" + Rack::Utils.build_query(params) if body && !params.empty?
|
102
|
+
|
103
|
+
case body
|
104
|
+
when NilClass
|
105
|
+
info_request("POST", url, params, headers, body)
|
106
|
+
handler.post(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
107
|
+
when FileUpload
|
108
|
+
file = Rack::Test::UploadedFile.new(body.path, body.content_type)
|
109
|
+
info_request("POST", url, params, headers, body)
|
110
|
+
handler.post(url, body.param_name.to_sym => file)
|
111
|
+
else
|
112
|
+
info_request("POST", url, params, headers, body)
|
113
|
+
handler.post(url, body)
|
114
|
+
end
|
115
|
+
@last_response = handler.last_response
|
116
|
+
|
117
|
+
debug_response(@last_response)
|
118
|
+
|
119
|
+
@last_response
|
120
|
+
end
|
121
|
+
|
122
|
+
def patch(url, params = {}, headers = nil, body = nil)
|
123
|
+
handler = get_handler(headers)
|
124
|
+
|
125
|
+
info_request("PATCH", url, params, headers, body)
|
126
|
+
|
127
|
+
handler.patch(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
128
|
+
@last_response = handler.last_response
|
129
|
+
|
130
|
+
debug_response(@last_response)
|
131
|
+
|
132
|
+
@last_response
|
133
|
+
end
|
134
|
+
|
135
|
+
def put(url, params = {}, headers = nil, body = nil)
|
136
|
+
handler = get_handler(headers)
|
137
|
+
|
138
|
+
info_request("PUT", url, params, headers, body)
|
139
|
+
|
140
|
+
handler.put(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
141
|
+
@last_response = handler.last_response
|
142
|
+
|
143
|
+
debug_response(@last_response)
|
144
|
+
|
145
|
+
@last_response
|
146
|
+
end
|
147
|
+
|
148
|
+
def post_form(url, params = {}, headers = nil, body = nil)
|
149
|
+
handler = get_handler(headers)
|
150
|
+
|
151
|
+
info_request("POST", url, params, headers, body)
|
152
|
+
|
153
|
+
handler.post(url, params)
|
154
|
+
@last_response = handler.last_response
|
155
|
+
|
156
|
+
debug_response(@last_response)
|
157
|
+
|
158
|
+
@last_response
|
159
|
+
end
|
160
|
+
|
161
|
+
def delete(url, params = {}, headers = nil, body = nil)
|
162
|
+
handler = get_handler(headers)
|
163
|
+
|
164
|
+
info_request("DELETE", url, params, headers, body)
|
165
|
+
|
166
|
+
handler.delete(url, params.to_json, {"CONTENT_TYPE" => "application/json"})
|
167
|
+
@last_response = handler.last_response
|
168
|
+
|
169
|
+
debug_response(@last_response)
|
170
|
+
|
171
|
+
@last_response
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
def get_handler(hs)
|
177
|
+
handler = RackHandler.new(@app)
|
178
|
+
hs.each_pair do |k,v|
|
179
|
+
handler.header(k,v)
|
180
|
+
end if hs
|
181
|
+
handler
|
182
|
+
end
|
183
|
+
|
184
|
+
end # class Api
|
185
|
+
|
186
|
+
end # class RackTestClient
|
187
|
+
end # class Client
|
188
|
+
end # module Webspicy
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Tester
|
3
|
+
class Client
|
4
|
+
module Support
|
5
|
+
include Webspicy::Support::Colorize
|
6
|
+
|
7
|
+
NONE = Object.new
|
8
|
+
|
9
|
+
def querystring_params(params)
|
10
|
+
Hash[params.each_pair.map{|k,v| [k.to_s,v.to_s] }]
|
11
|
+
end
|
12
|
+
|
13
|
+
def info_request(kind, url, params, headers, body)
|
14
|
+
Webspicy.info(colorize_highlight("~> #{kind} #{url}"))
|
15
|
+
debug(" Req params", json_pretty(params)) if params
|
16
|
+
debug(" Req headers", json_pretty(headers)) if headers
|
17
|
+
debug(" Req body", request_body_to_s(body)) if body
|
18
|
+
end
|
19
|
+
|
20
|
+
def debug_response(response)
|
21
|
+
debug(colorize_highlight("."))
|
22
|
+
debug(" Res status", status_to_s(@last_response.status))
|
23
|
+
debug(" Res headers", json_pretty(last_response.headers.to_h))
|
24
|
+
debug(" Res body", response_body_to_s(last_response))
|
25
|
+
Webspicy.debug("")
|
26
|
+
end
|
27
|
+
|
28
|
+
def debug(what, value = NONE)
|
29
|
+
return Webspicy.debug(" #{what}") if value == NONE
|
30
|
+
Webspicy.debug(" #{what}: " + value_to_s(value))
|
31
|
+
end
|
32
|
+
|
33
|
+
def request_body_to_s(body)
|
34
|
+
body = body.to_info if body.is_a?(Webspicy::FileUpload)
|
35
|
+
json_pretty(body)
|
36
|
+
end
|
37
|
+
|
38
|
+
def response_body_to_s(response)
|
39
|
+
case response.content_type.to_s
|
40
|
+
when /json/
|
41
|
+
json_pretty(JSON.load(response.body))
|
42
|
+
else
|
43
|
+
response.body.to_s
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def value_to_s(value)
|
48
|
+
value.to_s.gsub(/\n/, "\n ")
|
49
|
+
end
|
50
|
+
|
51
|
+
def status_to_s(status)
|
52
|
+
case status
|
53
|
+
when 0 ... 400 then colorize_success(status.to_s)
|
54
|
+
else colorize_error(status.to_s)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def json_pretty(s)
|
59
|
+
JSON.pretty_generate(s)
|
60
|
+
end
|
61
|
+
|
62
|
+
end # module Support
|
63
|
+
end # class Client
|
64
|
+
end # class Tester
|
65
|
+
end # module Webspicy
|
@@ -0,0 +1,218 @@
|
|
1
|
+
module Webspicy
|
2
|
+
class Tester
|
3
|
+
class Invocation
|
4
|
+
|
5
|
+
def initialize(test_case, response, client)
|
6
|
+
@test_case = test_case
|
7
|
+
@response = response
|
8
|
+
@client = client
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :test_case, :response, :client
|
12
|
+
|
13
|
+
def service
|
14
|
+
test_case.service
|
15
|
+
end
|
16
|
+
|
17
|
+
def rspec_assert!(rspec)
|
18
|
+
RSpecAsserter.new(rspec, self).assert!
|
19
|
+
end
|
20
|
+
|
21
|
+
def errors
|
22
|
+
@errors ||= begin
|
23
|
+
errs = [
|
24
|
+
[:expected_status_unmet, true],
|
25
|
+
[:expected_content_type_unmet, !test_case.is_expected_status?(204)],
|
26
|
+
[:expected_headers_unmet, test_case.has_expected_headers?],
|
27
|
+
[:expected_schema_unmet, !test_case.is_expected_status?(204)],
|
28
|
+
[:assertions_unmet, test_case.has_assertions?],
|
29
|
+
[:postconditions_unmet, test_case.service.has_postconditions? && !test_case.counterexample?],
|
30
|
+
[:expected_error_unmet, test_case.has_expected_error?]
|
31
|
+
].map do |(expectation,only_if)|
|
32
|
+
next unless only_if
|
33
|
+
begin
|
34
|
+
self.send(expectation)
|
35
|
+
rescue => ex
|
36
|
+
ex.message
|
37
|
+
end
|
38
|
+
end
|
39
|
+
errs.compact
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def has_error?
|
44
|
+
!errors.empty?
|
45
|
+
end
|
46
|
+
|
47
|
+
### Getters on response
|
48
|
+
|
49
|
+
def response_code
|
50
|
+
code = response.status
|
51
|
+
code = code.code unless code.is_a?(Integer)
|
52
|
+
code
|
53
|
+
end
|
54
|
+
|
55
|
+
### Query methods
|
56
|
+
|
57
|
+
def done?
|
58
|
+
!response.nil?
|
59
|
+
end
|
60
|
+
|
61
|
+
def is_expected_success?
|
62
|
+
test_case.expected_status.to_i >= 200 && test_case.expected_status.to_i < 300
|
63
|
+
end
|
64
|
+
|
65
|
+
def is_success?
|
66
|
+
response_code >= 200 && response_code < 300
|
67
|
+
end
|
68
|
+
|
69
|
+
def is_empty_response?
|
70
|
+
response_code == 204
|
71
|
+
end
|
72
|
+
|
73
|
+
def is_redirect?
|
74
|
+
response_code >= 300 && response_code < 400
|
75
|
+
end
|
76
|
+
|
77
|
+
### Check of HTTP status
|
78
|
+
|
79
|
+
def expected_status_unmet
|
80
|
+
expected = test_case.expected_status
|
81
|
+
got = response.status
|
82
|
+
expected === got ? nil : "[status] #{expected} !== #{got}"
|
83
|
+
end
|
84
|
+
|
85
|
+
def meets_expected_status?
|
86
|
+
expected_status_unmet.nil?
|
87
|
+
end
|
88
|
+
|
89
|
+
### Check of the expected output type
|
90
|
+
|
91
|
+
def expected_content_type_unmet
|
92
|
+
ect = test_case.expected_content_type
|
93
|
+
return nil unless ect
|
94
|
+
got = response.content_type
|
95
|
+
got = got.mime_type if got.respond_to?(:mime_type)
|
96
|
+
if ect.nil?
|
97
|
+
got.nil? ? nil : "[content type] #{ect} != #{got}"
|
98
|
+
else
|
99
|
+
got.to_s.start_with?(ect.to_s) ? nil : "[content type] #{ect} != #{got}"
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
def meets_expected_content_type?
|
104
|
+
expected_content_type_unmet.nil?
|
105
|
+
end
|
106
|
+
|
107
|
+
### Check of output schema
|
108
|
+
|
109
|
+
def expected_schema_unmet
|
110
|
+
if is_empty_response?
|
111
|
+
body = response.body.to_s.strip
|
112
|
+
body.empty? ? nil : "[body] empty vs. #{body}"
|
113
|
+
elsif is_redirect?
|
114
|
+
else
|
115
|
+
case dressed_body
|
116
|
+
when Finitio::TypeError
|
117
|
+
rc = dressed_body.root_cause
|
118
|
+
"#{rc.message} (#{rc.location ? rc.location : 'unknown location'})"
|
119
|
+
when StandardError
|
120
|
+
dressed_body.message
|
121
|
+
else nil
|
122
|
+
end
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
def meets_expected_schema?
|
127
|
+
expected_schema_unmet.nil?
|
128
|
+
end
|
129
|
+
|
130
|
+
### Check of assertions
|
131
|
+
|
132
|
+
def assertions_unmet
|
133
|
+
unmet = []
|
134
|
+
asserter = Tester::Asserter.new(dressed_body)
|
135
|
+
test_case.assert.each do |assert|
|
136
|
+
begin
|
137
|
+
asserter.instance_eval(assert)
|
138
|
+
rescue => ex
|
139
|
+
unmet << ex.message
|
140
|
+
end
|
141
|
+
end
|
142
|
+
unmet.empty? ? nil : unmet.join("\n")
|
143
|
+
end
|
144
|
+
|
145
|
+
def value_equal(exp, got)
|
146
|
+
case exp
|
147
|
+
when Hash
|
148
|
+
exp.all?{|(k,v)|
|
149
|
+
got[k] == v
|
150
|
+
}
|
151
|
+
else
|
152
|
+
exp == got
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
### Check of expected error message
|
157
|
+
|
158
|
+
def expected_error_unmet
|
159
|
+
expected = test_case.expected_error
|
160
|
+
case test_case.expected_content_type
|
161
|
+
when %r{json}
|
162
|
+
got = meets_expected_schema? ? dressed_body[:description] : response.body
|
163
|
+
expected == got ? nil : "[error message] `#{expected}` vs. `#{got}`"
|
164
|
+
else
|
165
|
+
dressed_body.include?(expected) ? nil : "[error message] `#{expected}` not found" unless expected.nil?
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
### Check of expected headers
|
170
|
+
|
171
|
+
def expected_headers_unmet
|
172
|
+
unmet = []
|
173
|
+
expected = test_case.expected_headers
|
174
|
+
expected.each_pair do |k,v|
|
175
|
+
got = response.headers[k]
|
176
|
+
unmet << "[headers] #{v} expected for #{k}, got #{got}" unless (got == v)
|
177
|
+
end
|
178
|
+
unmet.empty? ? nil : unmet.join("\n")
|
179
|
+
end
|
180
|
+
|
181
|
+
### Check of postconditions
|
182
|
+
|
183
|
+
def postconditions_unmet
|
184
|
+
failures = service.postconditions.map{|post|
|
185
|
+
post.check(self)
|
186
|
+
}.compact
|
187
|
+
failures.empty? ? nil : failures.join("\n")
|
188
|
+
end
|
189
|
+
|
190
|
+
def loaded_body
|
191
|
+
case test_case.expected_content_type
|
192
|
+
when %r{json}
|
193
|
+
raise "Body empty while expected" if response.body.to_s.empty?
|
194
|
+
@loaded_body ||= ::JSON.parse(response.body)
|
195
|
+
else
|
196
|
+
response.body.to_s
|
197
|
+
end
|
198
|
+
end
|
199
|
+
|
200
|
+
def dressed_body
|
201
|
+
@dressed_body ||= case test_case.expected_content_type
|
202
|
+
when %r{json}
|
203
|
+
schema = is_expected_success? ? service.output_schema : service.error_schema
|
204
|
+
begin
|
205
|
+
schema.dress(loaded_body)
|
206
|
+
rescue Finitio::TypeError => ex
|
207
|
+
ex
|
208
|
+
end
|
209
|
+
else
|
210
|
+
loaded_body
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
end # class Invocation
|
215
|
+
end # class Tester
|
216
|
+
end # module Webspicy
|
217
|
+
require_relative 'rspec_matchers'
|
218
|
+
require_relative 'rspec_asserter'
|