committee 5.5.0 → 5.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/committee/bin/committee_stub.rb +1 -6
- data/lib/committee/drivers/open_api_2/driver.rb +21 -33
- data/lib/committee/drivers/open_api_2/link.rb +8 -1
- data/lib/committee/drivers/open_api_2/parameter_schema_builder.rb +1 -2
- data/lib/committee/drivers/open_api_2/schema_builder.rb +2 -5
- data/lib/committee/drivers.rb +1 -1
- data/lib/committee/errors.rb +2 -2
- data/lib/committee/middleware/base.rb +2 -2
- data/lib/committee/middleware/request_validation.rb +1 -1
- data/lib/committee/middleware/stub.rb +1 -1
- data/lib/committee/request_unpacker.rb +3 -4
- data/lib/committee/schema_validator/hyper_schema/parameter_coercer.rb +41 -41
- data/lib/committee/schema_validator/hyper_schema/request_validator.rb +1 -2
- data/lib/committee/schema_validator/hyper_schema/response_validator.rb +15 -7
- data/lib/committee/schema_validator/hyper_schema/string_params_coercer.rb +60 -60
- data/lib/committee/schema_validator/hyper_schema.rb +64 -59
- data/lib/committee/schema_validator/open_api_3/request_validator.rb +1 -1
- data/lib/committee/schema_validator/open_api_3.rb +10 -5
- data/lib/committee/schema_validator/option.rb +1 -6
- data/lib/committee/test/schema_coverage.rb +1 -7
- data/lib/committee/utils.rb +20 -20
- data/lib/committee/version.rb +1 -1
- data/test/bin/committee_stub_test.rb +1 -5
- data/test/committee_test.rb +0 -1
- data/test/drivers/hyper_schema/driver_test.rb +0 -1
- data/test/drivers/open_api_2/driver_test.rb +1 -3
- data/test/drivers/open_api_2/header_schema_builder_test.rb +1 -9
- data/test/drivers/open_api_2/link_test.rb +1 -2
- data/test/drivers/open_api_2/parameter_schema_builder_test.rb +6 -45
- data/test/drivers/open_api_3/driver_test.rb +5 -5
- data/test/drivers_test.rb +19 -28
- data/test/middleware/base_test.rb +13 -36
- data/test/middleware/request_validation_open_api_3_test.rb +24 -47
- data/test/middleware/request_validation_test.rb +19 -53
- data/test/middleware/response_validation_open_api_3_test.rb +33 -25
- data/test/middleware/response_validation_test.rb +24 -15
- data/test/middleware/stub_test.rb +5 -12
- data/test/request_unpacker_test.rb +19 -66
- data/test/schema_validator/hyper_schema/parameter_coercer_test.rb +3 -3
- data/test/schema_validator/hyper_schema/request_validator_test.rb +7 -17
- data/test/schema_validator/hyper_schema/response_generator_test.rb +24 -18
- data/test/schema_validator/hyper_schema/response_validator_test.rb +3 -7
- data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +2 -2
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +16 -17
- data/test/schema_validator/open_api_3/request_validator_test.rb +8 -19
- data/test/schema_validator/open_api_3/response_validator_test.rb +2 -4
- data/test/test/methods_new_version_test.rb +2 -2
- data/test/test/methods_test.rb +5 -5
- data/test/test/schema_coverage_test.rb +4 -17
- data/test/test_helper.rb +6 -13
- data/test/validation_error_test.rb +1 -5
- metadata +3 -17
@@ -15,6 +15,22 @@ describe Committee::Middleware::ResponseValidation do
|
|
15
15
|
assert_equal 200, last_response.status
|
16
16
|
end
|
17
17
|
|
18
|
+
it "passes through a valid response with content-type (lower-case)" do
|
19
|
+
options = { schema: hyper_schema }
|
20
|
+
status = 200
|
21
|
+
headers = { 'content-type' => 'application/json' }
|
22
|
+
response = JSON.generate([ValidApp])
|
23
|
+
@app = Rack::Builder.new {
|
24
|
+
use Committee::Middleware::ResponseValidation, options
|
25
|
+
run lambda { |_|
|
26
|
+
[status, headers, [response]]
|
27
|
+
}
|
28
|
+
}
|
29
|
+
|
30
|
+
get "/apps"
|
31
|
+
assert_equal 200, last_response.status
|
32
|
+
end
|
33
|
+
|
18
34
|
it "doesn't call error_handler (has a arg) when response is valid" do
|
19
35
|
called = false
|
20
36
|
pr = ->(_e) { called = true }
|
@@ -106,8 +122,7 @@ describe Committee::Middleware::ResponseValidation do
|
|
106
122
|
end
|
107
123
|
|
108
124
|
it "takes a prefix" do
|
109
|
-
@app = new_rack_app(JSON.generate([ValidApp]), {}, prefix: "/v1",
|
110
|
-
schema: hyper_schema)
|
125
|
+
@app = new_rack_app(JSON.generate([ValidApp]), {}, prefix: "/v1", schema: hyper_schema)
|
111
126
|
get "/v1/apps"
|
112
127
|
assert_equal 200, last_response.status
|
113
128
|
end
|
@@ -130,30 +145,26 @@ describe Committee::Middleware::ResponseValidation do
|
|
130
145
|
end
|
131
146
|
|
132
147
|
it "passes through a valid response for OpenAPI" do
|
133
|
-
@app = new_rack_app(JSON.generate([ValidPet]), {},
|
134
|
-
schema: open_api_2_schema)
|
148
|
+
@app = new_rack_app(JSON.generate([ValidPet]), {}, schema: open_api_2_schema)
|
135
149
|
get "/api/pets"
|
136
150
|
assert_equal 200, last_response.status
|
137
151
|
end
|
138
152
|
|
139
153
|
it "passes through a valid response for OpenAPI when data=nil, target_schema=empty, allow_blank_structures=true" do
|
140
|
-
@app = new_rack_app("null", {},
|
141
|
-
allow_blank_structures: true, schema: open_api_2_schema)
|
154
|
+
@app = new_rack_app("null", {}, allow_blank_structures: true, schema: open_api_2_schema)
|
142
155
|
get "/api/pets/cat"
|
143
156
|
assert_equal 200, last_response.status
|
144
157
|
end
|
145
158
|
|
146
159
|
it "invalid responses for OpenAPI when data=nil, target_schema=empty, allow_blank_structures=false" do
|
147
|
-
@app = new_rack_app("null", {},
|
148
|
-
allow_blank_structures: false, schema: open_api_2_schema)
|
160
|
+
@app = new_rack_app("null", {}, allow_blank_structures: false, schema: open_api_2_schema)
|
149
161
|
get "/api/pets/cat"
|
150
162
|
assert_equal 500, last_response.status
|
151
163
|
assert_match(/Invalid response/i, last_response.body)
|
152
164
|
end
|
153
165
|
|
154
166
|
it "passes through a valid response for OpenAPI when data=nil, target_schema=present, allow_blank_structures=true" do
|
155
|
-
@app = new_rack_app("null", {},
|
156
|
-
allow_blank_structures: true, schema: open_api_2_schema)
|
167
|
+
@app = new_rack_app("null", {}, allow_blank_structures: true, schema: open_api_2_schema)
|
157
168
|
get "/api/pets/dog"
|
158
169
|
assert_equal 500, last_response.status
|
159
170
|
assert_match(/nil is not an array/i, last_response.body)
|
@@ -169,8 +180,8 @@ describe Committee::Middleware::ResponseValidation do
|
|
169
180
|
describe ':accept_request_filter' do
|
170
181
|
[
|
171
182
|
{ description: 'when not specified, includes everything', accept_request_filter: nil, expected: { status: 500 } },
|
172
|
-
{ description: 'when predicate matches, performs validation', accept_request_filter: ->
|
173
|
-
{ description: 'when predicate does not match, skips validation', accept_request_filter: ->
|
183
|
+
{ description: 'when predicate matches, performs validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/a') }, expected: { status: 500 } },
|
184
|
+
{ description: 'when predicate does not match, skips validation', accept_request_filter: ->(request) { request.path.start_with?('/v1/x') }, expected: { status: 200 } },
|
174
185
|
].each do |h|
|
175
186
|
description = h[:description]
|
176
187
|
accept_request_filter = h[:accept_request_filter]
|
@@ -188,9 +199,7 @@ describe Committee::Middleware::ResponseValidation do
|
|
188
199
|
private
|
189
200
|
|
190
201
|
def new_rack_app(response, headers = {}, options = {})
|
191
|
-
headers = {
|
192
|
-
"Content-Type" => "application/json"
|
193
|
-
}.merge(headers)
|
202
|
+
headers = { "Content-Type" => "application/json" }.merge(headers)
|
194
203
|
Rack::Builder.new {
|
195
204
|
use Committee::Middleware::ResponseValidation, options
|
196
205
|
run lambda { |_|
|
@@ -27,16 +27,14 @@ describe Committee::Middleware::Stub do
|
|
27
27
|
@app = new_rack_app(call: true, schema: hyper_schema)
|
28
28
|
get "/apps/heroku-api"
|
29
29
|
assert_equal 200, last_response.status
|
30
|
-
assert_equal ValidApp,
|
31
|
-
JSON.parse(last_response.headers["Committee-Response"])
|
30
|
+
assert_equal ValidApp, JSON.parse(last_response.headers["Committee-Response"])
|
32
31
|
end
|
33
32
|
|
34
33
|
it "optionally returns the application's response" do
|
35
34
|
@app = new_rack_app(call: true, schema: hyper_schema, suppress: true)
|
36
35
|
get "/apps/heroku-api"
|
37
36
|
assert_equal 429, last_response.status
|
38
|
-
assert_equal ValidApp,
|
39
|
-
JSON.parse(last_response.headers["Committee-Response"])
|
37
|
+
assert_equal ValidApp, JSON.parse(last_response.headers["Committee-Response"])
|
40
38
|
assert_equal "", last_response.body
|
41
39
|
end
|
42
40
|
|
@@ -44,8 +42,7 @@ describe Committee::Middleware::Stub do
|
|
44
42
|
@app = new_rack_app(call: true, schema: hyper_schema, suppress: true)
|
45
43
|
get "/apps/heroku-api"
|
46
44
|
assert_equal 429, last_response.status
|
47
|
-
assert_equal "#/definitions/app/links/2/targetSchema",
|
48
|
-
last_response.headers["Committee-Response-Schema"]
|
45
|
+
assert_equal "#/definitions/app/links/2/targetSchema", last_response.headers["Committee-Response-Schema"]
|
49
46
|
assert_equal "", last_response.body
|
50
47
|
end
|
51
48
|
|
@@ -122,15 +119,11 @@ describe Committee::Middleware::Stub do
|
|
122
119
|
|
123
120
|
headers = {}
|
124
121
|
if res = env["committee.response"]
|
125
|
-
headers.merge!({
|
126
|
-
"Committee-Response" => JSON.generate(res)
|
127
|
-
})
|
122
|
+
headers.merge!({ "Committee-Response" => JSON.generate(res) })
|
128
123
|
end
|
129
124
|
|
130
125
|
if res = env["committee.response_schema"]
|
131
|
-
headers.merge!({
|
132
|
-
"Committee-Response-Schema" => res.pointer,
|
133
|
-
})
|
126
|
+
headers.merge!({ "Committee-Response-Schema" => res.pointer, })
|
134
127
|
end
|
135
128
|
|
136
129
|
env["committee.suppress"] = suppress
|
@@ -4,39 +4,28 @@ require "test_helper"
|
|
4
4
|
|
5
5
|
describe Committee::RequestUnpacker do
|
6
6
|
it "unpacks JSON on Content-Type: application/json" do
|
7
|
-
env = {
|
8
|
-
"CONTENT_TYPE" => "application/json",
|
9
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
10
|
-
}
|
7
|
+
env = { "CONTENT_TYPE" => "application/json", "rack.input" => StringIO.new('{"x":"y"}'), }
|
11
8
|
request = Rack::Request.new(env)
|
12
9
|
unpacker = Committee::RequestUnpacker.new
|
13
10
|
assert_equal([{ "x" => "y" }, false], unpacker.unpack_request_params(request))
|
14
11
|
end
|
15
12
|
|
16
13
|
it "unpacks JSON on Content-Type: application/vnd.api+json" do
|
17
|
-
env = {
|
18
|
-
"CONTENT_TYPE" => "application/vnd.api+json",
|
19
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
20
|
-
}
|
14
|
+
env = { "CONTENT_TYPE" => "application/vnd.api+json", "rack.input" => StringIO.new('{"x":"y"}'), }
|
21
15
|
request = Rack::Request.new(env)
|
22
16
|
unpacker = Committee::RequestUnpacker.new
|
23
17
|
assert_equal([{ "x" => "y" }, false], unpacker.unpack_request_params(request))
|
24
18
|
end
|
25
19
|
|
26
20
|
it "unpacks JSON on no Content-Type" do
|
27
|
-
env = {
|
28
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
29
|
-
}
|
21
|
+
env = { "rack.input" => StringIO.new('{"x":"y"}'), }
|
30
22
|
request = Rack::Request.new(env)
|
31
23
|
unpacker = Committee::RequestUnpacker.new
|
32
24
|
assert_equal([{ "x" => "y" }, false], unpacker.unpack_request_params(request))
|
33
25
|
end
|
34
26
|
|
35
27
|
it "doesn't unpack JSON on application/x-ndjson" do
|
36
|
-
env = {
|
37
|
-
"CONTENT_TYPE" => "application/x-ndjson",
|
38
|
-
"rack.input" => StringIO.new('{"x":"y"}\n{"a":"b"}'),
|
39
|
-
}
|
28
|
+
env = { "CONTENT_TYPE" => "application/x-ndjson", "rack.input" => StringIO.new('{"x":"y"}\n{"a":"b"}'), }
|
40
29
|
request = Rack::Request.new(env)
|
41
30
|
unpacker = Committee::RequestUnpacker.new
|
42
31
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -44,10 +33,7 @@ describe Committee::RequestUnpacker do
|
|
44
33
|
|
45
34
|
it "doesn't unpack JSON under other Content-Types" do
|
46
35
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
47
|
-
env = {
|
48
|
-
"CONTENT_TYPE" => content_type,
|
49
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
50
|
-
}
|
36
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new('{"x":"y"}'), }
|
51
37
|
request = Rack::Request.new(env)
|
52
38
|
unpacker = Committee::RequestUnpacker.new
|
53
39
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -56,10 +42,7 @@ describe Committee::RequestUnpacker do
|
|
56
42
|
|
57
43
|
it "unpacks JSON under other Content-Types with optimistic_json" do
|
58
44
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
59
|
-
env = {
|
60
|
-
"CONTENT_TYPE" => content_type,
|
61
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
62
|
-
}
|
45
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new('{"x":"y"}'), }
|
63
46
|
request = Rack::Request.new(env)
|
64
47
|
unpacker = Committee::RequestUnpacker.new(optimistic_json: true)
|
65
48
|
assert_equal([{ "x" => "y" }, false], unpacker.unpack_request_params(request))
|
@@ -68,10 +51,7 @@ describe Committee::RequestUnpacker do
|
|
68
51
|
|
69
52
|
it "returns {} when unpacking non-JSON with optimistic_json" do
|
70
53
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
71
|
-
env = {
|
72
|
-
"CONTENT_TYPE" => content_type,
|
73
|
-
"rack.input" => StringIO.new('x=y&foo=42'),
|
74
|
-
}
|
54
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new('x=y&foo=42'), }
|
75
55
|
request = Rack::Request.new(env)
|
76
56
|
unpacker = Committee::RequestUnpacker.new(optimistic_json: true)
|
77
57
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -79,10 +59,7 @@ describe Committee::RequestUnpacker do
|
|
79
59
|
end
|
80
60
|
|
81
61
|
it "unpacks an empty hash on an empty request body" do
|
82
|
-
env = {
|
83
|
-
"CONTENT_TYPE" => "application/json",
|
84
|
-
"rack.input" => StringIO.new(""),
|
85
|
-
}
|
62
|
+
env = { "CONTENT_TYPE" => "application/json", "rack.input" => StringIO.new(""), }
|
86
63
|
request = Rack::Request.new(env)
|
87
64
|
unpacker = Committee::RequestUnpacker.new
|
88
65
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -90,10 +67,7 @@ describe Committee::RequestUnpacker do
|
|
90
67
|
|
91
68
|
it "doesn't unpack form params" do
|
92
69
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
93
|
-
env = {
|
94
|
-
"CONTENT_TYPE" => content_type,
|
95
|
-
"rack.input" => StringIO.new("x=y"),
|
96
|
-
}
|
70
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new("x=y"), }
|
97
71
|
request = Rack::Request.new(env)
|
98
72
|
unpacker = Committee::RequestUnpacker.new
|
99
73
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -102,10 +76,7 @@ describe Committee::RequestUnpacker do
|
|
102
76
|
|
103
77
|
it "unpacks form params with allow_form_params" do
|
104
78
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
105
|
-
env = {
|
106
|
-
"CONTENT_TYPE" => content_type,
|
107
|
-
"rack.input" => StringIO.new("x=y"),
|
108
|
-
}
|
79
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new("x=y"), }
|
109
80
|
request = Rack::Request.new(env)
|
110
81
|
unpacker = Committee::RequestUnpacker.new(allow_form_params: true)
|
111
82
|
assert_equal([{ "x" => "y" }, true], unpacker.unpack_request_params(request))
|
@@ -114,32 +85,22 @@ describe Committee::RequestUnpacker do
|
|
114
85
|
|
115
86
|
it "unpacks form & query params with allow_form_params and allow_query_params" do
|
116
87
|
%w[application/x-www-form-urlencoded multipart/form-data].each do |content_type|
|
117
|
-
env = {
|
118
|
-
"CONTENT_TYPE" => content_type,
|
119
|
-
"rack.input" => StringIO.new("x=y"),
|
120
|
-
"QUERY_STRING" => "a=b"
|
121
|
-
}
|
88
|
+
env = { "CONTENT_TYPE" => content_type, "rack.input" => StringIO.new("x=y"), "QUERY_STRING" => "a=b" }
|
122
89
|
request = Rack::Request.new(env)
|
123
90
|
unpacker = Committee::RequestUnpacker.new(allow_form_params: true, allow_query_params: true)
|
124
|
-
assert_equal([
|
91
|
+
assert_equal([{ "x" => "y" }, true], unpacker.unpack_request_params(request))
|
125
92
|
end
|
126
93
|
end
|
127
94
|
|
128
95
|
it "unpacks query params with allow_query_params" do
|
129
|
-
env = {
|
130
|
-
"rack.input" => StringIO.new(""),
|
131
|
-
"QUERY_STRING" => "a=b"
|
132
|
-
}
|
96
|
+
env = { "rack.input" => StringIO.new(""), "QUERY_STRING" => "a=b" }
|
133
97
|
request = Rack::Request.new(env)
|
134
98
|
unpacker = Committee::RequestUnpacker.new(allow_query_params: true)
|
135
99
|
assert_equal({ "a" => "b" }, unpacker.unpack_query_params(request))
|
136
100
|
end
|
137
101
|
|
138
102
|
it "errors if JSON is not an object" do
|
139
|
-
env = {
|
140
|
-
"CONTENT_TYPE" => "application/json",
|
141
|
-
"rack.input" => StringIO.new('[2]'),
|
142
|
-
}
|
103
|
+
env = { "CONTENT_TYPE" => "application/json", "rack.input" => StringIO.new('[2]'), }
|
143
104
|
request = Rack::Request.new(env)
|
144
105
|
assert_raises(Committee::BadRequest) do
|
145
106
|
Committee::RequestUnpacker.new.unpack_request_params(request)
|
@@ -147,10 +108,7 @@ describe Committee::RequestUnpacker do
|
|
147
108
|
end
|
148
109
|
|
149
110
|
it "errors on an unknown Content-Type" do
|
150
|
-
env = {
|
151
|
-
"CONTENT_TYPE" => "application/whats-this",
|
152
|
-
"rack.input" => StringIO.new('{"x":"y"}'),
|
153
|
-
}
|
111
|
+
env = { "CONTENT_TYPE" => "application/whats-this", "rack.input" => StringIO.new('{"x":"y"}'), }
|
154
112
|
request = Rack::Request.new(env)
|
155
113
|
unpacker = Committee::RequestUnpacker.new
|
156
114
|
assert_equal([{}, false], unpacker.unpack_request_params(request))
|
@@ -158,19 +116,14 @@ describe Committee::RequestUnpacker do
|
|
158
116
|
|
159
117
|
# this is mostly here for line coverage
|
160
118
|
it "unpacks JSON containing an array" do
|
161
|
-
env = {
|
162
|
-
"rack.input" => StringIO.new('{"x":[]}'),
|
163
|
-
}
|
119
|
+
env = { "rack.input" => StringIO.new('{"x":[]}'), }
|
164
120
|
request = Rack::Request.new(env)
|
165
121
|
unpacker = Committee::RequestUnpacker.new
|
166
122
|
assert_equal([{ "x" => [] }, false], unpacker.unpack_request_params(request))
|
167
123
|
end
|
168
124
|
|
169
125
|
it "unpacks http header" do
|
170
|
-
env = {
|
171
|
-
"HTTP_FOO_BAR" => "some header value",
|
172
|
-
"rack.input" => StringIO.new(""),
|
173
|
-
}
|
126
|
+
env = { "HTTP_FOO_BAR" => "some header value", "rack.input" => StringIO.new(""), }
|
174
127
|
request = Rack::Request.new(env)
|
175
128
|
unpacker = Committee::RequestUnpacker.new({ allow_header_params: true })
|
176
129
|
assert_equal({ "FOO-BAR" => "some header value" }, unpacker.unpack_headers(request))
|
@@ -180,7 +133,7 @@ describe Committee::RequestUnpacker do
|
|
180
133
|
env = {
|
181
134
|
"rack.input" => StringIO.new('{"x":1, "y":2}'),
|
182
135
|
"REQUEST_METHOD" => "GET",
|
183
|
-
"QUERY_STRING"=>"data=value&x=aaa",
|
136
|
+
"QUERY_STRING" => "data=value&x=aaa",
|
184
137
|
}
|
185
138
|
request = Rack::Request.new(env)
|
186
139
|
unpacker = Committee::RequestUnpacker.new({ allow_query_params: true, allow_get_body: true })
|
@@ -191,7 +144,7 @@ describe Committee::RequestUnpacker do
|
|
191
144
|
env = {
|
192
145
|
"rack.input" => StringIO.new('{"x":1, "y":2}'),
|
193
146
|
"REQUEST_METHOD" => "GET",
|
194
|
-
"QUERY_STRING"=>"data=value&x=aaa",
|
147
|
+
"QUERY_STRING" => "data=value&x=aaa",
|
195
148
|
}
|
196
149
|
request = Rack::Request.new(env)
|
197
150
|
unpacker = Committee::RequestUnpacker.new({ allow_query_params: true, use_get_body: false })
|
@@ -11,7 +11,7 @@ describe Committee::SchemaValidator::HyperSchema::ParameterCoercer do
|
|
11
11
|
end
|
12
12
|
|
13
13
|
it "pass datetime string" do
|
14
|
-
params = { "update_time" => "2016-04-01T16:00:00.000+09:00"}
|
14
|
+
params = { "update_time" => "2016-04-01T16:00:00.000+09:00" }
|
15
15
|
call(params, coerce_date_times: true)
|
16
16
|
|
17
17
|
assert_kind_of DateTime, params["update_time"]
|
@@ -28,7 +28,7 @@ describe Committee::SchemaValidator::HyperSchema::ParameterCoercer do
|
|
28
28
|
|
29
29
|
it "pass invalid datetime string, not convert" do
|
30
30
|
invalid_datetime = "llmfllmf"
|
31
|
-
params = { "update_time" => invalid_datetime}
|
31
|
+
params = { "update_time" => invalid_datetime }
|
32
32
|
call(params, coerce_date_times: true)
|
33
33
|
|
34
34
|
assert_equal invalid_datetime, params["update_time"]
|
@@ -104,7 +104,7 @@ describe Committee::SchemaValidator::HyperSchema::ParameterCoercer do
|
|
104
104
|
|
105
105
|
class HashLikeObject < Hash; end
|
106
106
|
|
107
|
-
def call(params, options={})
|
107
|
+
def call(params, options = {})
|
108
108
|
link = Committee::Drivers::HyperSchema::Link.new(@link)
|
109
109
|
Committee::SchemaValidator::HyperSchema::ParameterCoercer.new(params, link.schema, options).call!
|
110
110
|
end
|
@@ -24,9 +24,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
24
24
|
end
|
25
25
|
|
26
26
|
it "passes through a valid request" do
|
27
|
-
data = {
|
28
|
-
"name" => "heroku-api",
|
29
|
-
}
|
27
|
+
data = { "name" => "heroku-api", }
|
30
28
|
call(data)
|
31
29
|
end
|
32
30
|
|
@@ -36,9 +34,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
36
34
|
"CONTENT_TYPE" => "application/json; charset=utf-8",
|
37
35
|
"rack.input" => StringIO.new("{}"),
|
38
36
|
})
|
39
|
-
data = {
|
40
|
-
"name" => "heroku-api",
|
41
|
-
}
|
37
|
+
data = { "name" => "heroku-api", }
|
42
38
|
call(data)
|
43
39
|
end
|
44
40
|
|
@@ -51,8 +47,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
51
47
|
})
|
52
48
|
call({})
|
53
49
|
}
|
54
|
-
message =
|
55
|
-
%{"Content-Type" request header must be set to "application/json".}
|
50
|
+
message = %{"Content-Type" request header must be set to "application/json".}
|
56
51
|
assert_equal message, e.message
|
57
52
|
end
|
58
53
|
|
@@ -87,9 +82,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
87
82
|
end
|
88
83
|
|
89
84
|
it "detects a parameter of the wrong pattern" do
|
90
|
-
data = {
|
91
|
-
"name" => "%@!"
|
92
|
-
}
|
85
|
+
data = { "name" => "%@!" }
|
93
86
|
e = assert_raises(Committee::InvalidRequest) do
|
94
87
|
call(data)
|
95
88
|
end
|
@@ -99,7 +92,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
99
92
|
|
100
93
|
private
|
101
94
|
|
102
|
-
def call(data, headers={}, options={})
|
95
|
+
def call(data, headers = {}, options = {})
|
103
96
|
# hyper-schema link should be dropped into driver wrapper before it's used
|
104
97
|
link = Committee::Drivers::HyperSchema::Link.new(@link)
|
105
98
|
Committee::SchemaValidator::HyperSchema::RequestValidator.new(link, options).call(@request, data, headers)
|
@@ -117,11 +110,8 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
117
110
|
})
|
118
111
|
end
|
119
112
|
|
120
|
-
|
121
113
|
it "passes through a valid request" do
|
122
|
-
headers = {
|
123
|
-
"AUTH-TOKEN" => "xxx"
|
124
|
-
}
|
114
|
+
headers = { "AUTH-TOKEN" => "xxx" }
|
125
115
|
call({}, headers)
|
126
116
|
end
|
127
117
|
|
@@ -143,7 +133,7 @@ describe Committee::SchemaValidator::HyperSchema::RequestValidator do
|
|
143
133
|
|
144
134
|
private
|
145
135
|
|
146
|
-
def call(data, headers={}, options={})
|
136
|
+
def call(data, headers = {}, options = {})
|
147
137
|
# hyper-schema link should be dropped into driver wrapper before it's used
|
148
138
|
Committee::SchemaValidator::HyperSchema::RequestValidator.new(@link, options).call(@request, data, headers)
|
149
139
|
end
|
@@ -65,8 +65,8 @@ describe Committee::SchemaValidator::HyperSchema::ResponseGenerator do
|
|
65
65
|
end
|
66
66
|
|
67
67
|
expected = <<-eos.gsub(/\n +/, "").strip
|
68
|
-
At "get /apps/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fname)}"
|
69
|
-
"#/definitions/app/properties/maintenance": no "example" attribute and
|
68
|
+
At "get /apps/{(%23%2Fdefinitions%2Fapp%2Fdefinitions%2Fname)}"#{' '}
|
69
|
+
"#/definitions/app/properties/maintenance": no "example" attribute and#{' '}
|
70
70
|
"null" is not allowed; don't know how to generate property.
|
71
71
|
eos
|
72
72
|
assert_equal expected, e.message
|
@@ -74,52 +74,57 @@ describe Committee::SchemaValidator::HyperSchema::ResponseGenerator do
|
|
74
74
|
|
75
75
|
it "generates first enum value for a schema with enum" do
|
76
76
|
link = Committee::Drivers::OpenAPI2::Link.new
|
77
|
-
|
78
|
-
|
79
|
-
|
77
|
+
target_schema = JsonSchema::Schema.new
|
78
|
+
target_schema.enum = ["foo"]
|
79
|
+
target_schema.type = ["string"]
|
80
|
+
link.target_schemas = { 200 => target_schema }
|
80
81
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
81
82
|
assert_equal("foo", data)
|
82
83
|
end
|
83
84
|
|
84
85
|
it "generates basic types" do
|
85
86
|
link = Committee::Drivers::OpenAPI2::Link.new
|
86
|
-
|
87
|
+
target_schema = JsonSchema::Schema.new
|
88
|
+
link.target_schemas = { 200 => target_schema }
|
87
89
|
|
88
|
-
|
90
|
+
target_schema.type = ["integer"]
|
89
91
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
90
92
|
assert_equal 0, data
|
91
93
|
|
92
|
-
|
94
|
+
target_schema.type = ["null"]
|
93
95
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
94
96
|
assert_nil data
|
95
97
|
|
96
|
-
|
98
|
+
target_schema.type = ["string"]
|
97
99
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
98
100
|
assert_equal "", data
|
99
101
|
end
|
100
102
|
|
101
103
|
it "generates an empty array for an array type" do
|
102
104
|
link = Committee::Drivers::OpenAPI2::Link.new
|
103
|
-
|
104
|
-
link.
|
105
|
+
target_schema = JsonSchema::Schema.new
|
106
|
+
link.target_schemas = { 200 => target_schema }
|
107
|
+
target_schema.type = ["array"]
|
105
108
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
106
109
|
assert_equal([], data)
|
107
110
|
end
|
108
111
|
|
109
112
|
it "generates an empty object for an object with no fields" do
|
110
113
|
link = Committee::Drivers::OpenAPI2::Link.new
|
111
|
-
|
112
|
-
link.
|
114
|
+
target_schema = JsonSchema::Schema.new
|
115
|
+
link.target_schemas = { 200 => target_schema }
|
116
|
+
target_schema.type = ["object"]
|
113
117
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
114
118
|
assert_equal({}, data)
|
115
119
|
end
|
116
120
|
|
117
121
|
it "prefers an example to a built-in value" do
|
118
122
|
link = Committee::Drivers::OpenAPI2::Link.new
|
119
|
-
|
123
|
+
target_schema = JsonSchema::Schema.new
|
124
|
+
link.target_schemas = { 200 => target_schema }
|
120
125
|
|
121
|
-
|
122
|
-
|
126
|
+
target_schema.data = { "example" => 123 }
|
127
|
+
target_schema.type = ["integer"]
|
123
128
|
|
124
129
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
125
130
|
assert_equal 123, data
|
@@ -127,9 +132,10 @@ describe Committee::SchemaValidator::HyperSchema::ResponseGenerator do
|
|
127
132
|
|
128
133
|
it "prefers non-null types to null types" do
|
129
134
|
link = Committee::Drivers::OpenAPI2::Link.new
|
130
|
-
|
135
|
+
target_schema = JsonSchema::Schema.new
|
136
|
+
link.target_schemas = { 200 => target_schema }
|
131
137
|
|
132
|
-
|
138
|
+
target_schema.type = ["null", "integer"]
|
133
139
|
data, _schema = Committee::SchemaValidator::HyperSchema::ResponseGenerator.new.call(link)
|
134
140
|
assert_equal 0, data
|
135
141
|
end
|
@@ -5,9 +5,7 @@ require "test_helper"
|
|
5
5
|
describe Committee::SchemaValidator::HyperSchema::ResponseValidator do
|
6
6
|
before do
|
7
7
|
@status = 200
|
8
|
-
@headers = {
|
9
|
-
"Content-Type" => "application/json"
|
10
|
-
}
|
8
|
+
@headers = { "Content-Type" => "application/json" }
|
11
9
|
@data = ValidApp.dup
|
12
10
|
@schema = JsonSchema.parse!(hyper_schema_data)
|
13
11
|
@schema.expand_references!
|
@@ -78,16 +76,14 @@ describe Committee::SchemaValidator::HyperSchema::ResponseValidator do
|
|
78
76
|
it "detects a blank response Content-Type" do
|
79
77
|
@headers = {}
|
80
78
|
e = assert_raises(Committee::InvalidResponse) { call }
|
81
|
-
message =
|
82
|
-
%{"Content-Type" response header must be set to "#{@link.enc_type}".}
|
79
|
+
message = %{"Content-Type" response header must be set to "#{@link.enc_type}".}
|
83
80
|
assert_equal message, e.message
|
84
81
|
end
|
85
82
|
|
86
83
|
it "detects an invalid response Content-Type" do
|
87
84
|
@headers = { "Content-Type" => "text/html" }
|
88
85
|
e = assert_raises(Committee::InvalidResponse) { call }
|
89
|
-
message =
|
90
|
-
%{"Content-Type" response header must be set to "#{@link.enc_type}".}
|
86
|
+
message = %{"Content-Type" response header must be set to "#{@link.enc_type}".}
|
91
87
|
assert_equal message, e.message
|
92
88
|
end
|
93
89
|
|
@@ -121,7 +121,7 @@ describe Committee::SchemaValidator::HyperSchema::StringParamsCoercer do
|
|
121
121
|
private
|
122
122
|
|
123
123
|
def check_convert(key, before_value, after_value)
|
124
|
-
data = {key => before_value}
|
124
|
+
data = { key => before_value }
|
125
125
|
call(data)
|
126
126
|
|
127
127
|
if !after_value.nil?
|
@@ -131,7 +131,7 @@ describe Committee::SchemaValidator::HyperSchema::StringParamsCoercer do
|
|
131
131
|
end
|
132
132
|
end
|
133
133
|
|
134
|
-
def call(data, options={})
|
134
|
+
def call(data, options = {})
|
135
135
|
Committee::SchemaValidator::HyperSchema::StringParamsCoercer.new(data, @link.schema, options).call!
|
136
136
|
end
|
137
137
|
end
|