committee 5.6.1 → 5.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/committee/drivers/open_api_3/driver.rb +7 -1
- data/lib/committee/drivers.rb +2 -3
- data/lib/committee/errors.rb +11 -0
- data/lib/committee/middleware/base.rb +11 -5
- data/lib/committee/middleware/options/base.rb +107 -0
- data/lib/committee/middleware/options/request_validation.rb +80 -0
- data/lib/committee/middleware/options/response_validation.rb +46 -0
- data/lib/committee/middleware/options.rb +12 -0
- data/lib/committee/middleware/request_validation.rb +7 -1
- data/lib/committee/middleware/response_validation.rb +6 -2
- data/lib/committee/middleware.rb +1 -0
- data/lib/committee/schema_validator/hyper_schema/response_generator.rb +1 -3
- data/lib/committee/schema_validator/hyper_schema/response_validator.rb +14 -1
- data/lib/committee/schema_validator/hyper_schema/router.rb +1 -1
- data/lib/committee/schema_validator/hyper_schema.rb +3 -14
- data/lib/committee/schema_validator/open_api_3/operation_wrapper.rb +43 -13
- data/lib/committee/schema_validator/open_api_3/parameter_deserializer.rb +556 -0
- data/lib/committee/schema_validator/open_api_3/response_validator.rb +16 -2
- data/lib/committee/schema_validator/open_api_3.rb +19 -13
- data/lib/committee/schema_validator/option.rb +6 -17
- data/lib/committee/schema_validator.rb +12 -1
- data/lib/committee/test/except_parameter.rb +416 -0
- data/lib/committee/test/methods.rb +38 -2
- data/lib/committee/version.rb +1 -1
- data/lib/committee.rb +1 -1
- data/test/drivers/open_api_2/driver_test.rb +4 -16
- data/test/drivers/open_api_2/parameter_schema_builder_test.rb +4 -50
- data/test/drivers_test.rb +35 -21
- data/test/middleware/options/base_test.rb +120 -0
- data/test/middleware/options/request_validation_test.rb +177 -0
- data/test/middleware/options/response_validation_test.rb +121 -0
- data/test/middleware/request_validation_open_api_3_test.rb +200 -80
- data/test/middleware/request_validation_test.rb +13 -70
- data/test/middleware/response_validation_open_api_3_test.rb +40 -17
- data/test/middleware/response_validation_test.rb +3 -14
- data/test/request_unpacker_test.rb +2 -10
- data/test/schema_validator/hyper_schema/parameter_coercer_test.rb +1 -37
- data/test/schema_validator/hyper_schema/request_validator_test.rb +6 -30
- data/test/schema_validator/hyper_schema/router_test.rb +5 -0
- data/test/schema_validator/hyper_schema/string_params_coercer_test.rb +1 -37
- data/test/schema_validator/open_api_3/operation_wrapper_test.rb +58 -43
- data/test/schema_validator/open_api_3/parameter_deserializer_test.rb +457 -0
- data/test/schema_validator/open_api_3/request_validator_test.rb +1 -2
- data/test/schema_validator/open_api_3/response_validator_test.rb +3 -11
- data/test/schema_validator_test.rb +41 -0
- data/test/test/methods_test.rb +238 -105
- data/test/test/schema_coverage_test.rb +8 -155
- metadata +11 -1
|
@@ -38,20 +38,7 @@ describe Committee::Drivers::OpenAPI2::ParameterSchemaBuilder do
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
it "reflects an array with an items schema into a schema" do
|
|
41
|
-
data = {
|
|
42
|
-
"parameters" => [
|
|
43
|
-
{
|
|
44
|
-
"name" => "tags",
|
|
45
|
-
"type" => "array",
|
|
46
|
-
"minItems" => 1,
|
|
47
|
-
"maxItems" => 10,
|
|
48
|
-
"uniqueItems" => true,
|
|
49
|
-
"items" => {
|
|
50
|
-
"type" => "string"
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
]
|
|
54
|
-
}
|
|
41
|
+
data = { "parameters" => [{ "name" => "tags", "type" => "array", "minItems" => 1, "maxItems" => 10, "uniqueItems" => true, "items" => { "type" => "string" } }] }
|
|
55
42
|
schema, schema_data = call(data)
|
|
56
43
|
|
|
57
44
|
assert_nil schema_data
|
|
@@ -71,18 +58,7 @@ describe Committee::Drivers::OpenAPI2::ParameterSchemaBuilder do
|
|
|
71
58
|
end
|
|
72
59
|
|
|
73
60
|
it "reflects string properties into a schema" do
|
|
74
|
-
data = {
|
|
75
|
-
"parameters" => [
|
|
76
|
-
{
|
|
77
|
-
"name" => "password",
|
|
78
|
-
"type" => "string",
|
|
79
|
-
"format" => "password",
|
|
80
|
-
"pattern" => "[a-zA-Z0-9]+",
|
|
81
|
-
"minLength" => 6,
|
|
82
|
-
"maxLength" => 30
|
|
83
|
-
}
|
|
84
|
-
]
|
|
85
|
-
}
|
|
61
|
+
data = { "parameters" => [{ "name" => "password", "type" => "string", "format" => "password", "pattern" => "[a-zA-Z0-9]+", "minLength" => 6, "maxLength" => 30 }] }
|
|
86
62
|
schema, schema_data = call(data)
|
|
87
63
|
|
|
88
64
|
assert_nil schema_data
|
|
@@ -93,19 +69,7 @@ describe Committee::Drivers::OpenAPI2::ParameterSchemaBuilder do
|
|
|
93
69
|
end
|
|
94
70
|
|
|
95
71
|
it "reflects number properties into a schema" do
|
|
96
|
-
data = {
|
|
97
|
-
"parameters" => [
|
|
98
|
-
{
|
|
99
|
-
"name" => "limit",
|
|
100
|
-
"type" => "integer",
|
|
101
|
-
"minimum" => 20,
|
|
102
|
-
"exclusiveMinimum" => true,
|
|
103
|
-
"maximum" => 100,
|
|
104
|
-
"exclusiveMaximum" => false,
|
|
105
|
-
"multipleOf" => 10
|
|
106
|
-
}
|
|
107
|
-
]
|
|
108
|
-
}
|
|
72
|
+
data = { "parameters" => [{ "name" => "limit", "type" => "integer", "minimum" => 20, "exclusiveMinimum" => true, "maximum" => 100, "exclusiveMaximum" => false, "multipleOf" => 10 }] }
|
|
109
73
|
schema, schema_data = call(data)
|
|
110
74
|
|
|
111
75
|
assert_nil schema_data
|
|
@@ -117,17 +81,7 @@ describe Committee::Drivers::OpenAPI2::ParameterSchemaBuilder do
|
|
|
117
81
|
end
|
|
118
82
|
|
|
119
83
|
it "returns schema data for a body parameter" do
|
|
120
|
-
data = {
|
|
121
|
-
"parameters" => [
|
|
122
|
-
{
|
|
123
|
-
"name" => "payload",
|
|
124
|
-
"in" => "body",
|
|
125
|
-
"schema" => {
|
|
126
|
-
"$ref" => "#/definitions/foo",
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
]
|
|
130
|
-
}
|
|
84
|
+
data = { "parameters" => [{ "name" => "payload", "in" => "body", "schema" => { "$ref" => "#/definitions/foo", } }] }
|
|
131
85
|
schema, schema_data = call(data)
|
|
132
86
|
|
|
133
87
|
assert_nil schema
|
data/test/drivers_test.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "test_helper"
|
|
4
|
+
require "tmpdir"
|
|
4
5
|
|
|
5
6
|
describe Committee::Drivers do
|
|
6
7
|
DRIVERS = [:hyper_schema, :open_api_2, :open_api_3,].freeze
|
|
@@ -74,30 +75,51 @@ describe Committee::Drivers do
|
|
|
74
75
|
describe 'cache behavior' do
|
|
75
76
|
describe 'when loading the same file' do
|
|
76
77
|
it 'returns the same object when the options are identical' do
|
|
77
|
-
assert_equal(
|
|
78
|
-
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id,
|
|
79
|
-
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id,
|
|
80
|
-
)
|
|
78
|
+
assert_equal(Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id, Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id,)
|
|
81
79
|
end
|
|
82
80
|
|
|
83
81
|
it 'returns different objects if the options are different' do
|
|
84
|
-
refute_equal(
|
|
85
|
-
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id,
|
|
86
|
-
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: false }).object_id,
|
|
87
|
-
)
|
|
82
|
+
refute_equal(Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id, Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: false }).object_id,)
|
|
88
83
|
end
|
|
89
84
|
|
|
90
85
|
it 'returns different objects if the file contents have changed' do
|
|
91
86
|
object_id = Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id
|
|
92
87
|
original_file_contents = File.read(open_api_3_schema_path)
|
|
93
88
|
File.write(open_api_3_schema_path, original_file_contents + "\n")
|
|
94
|
-
refute_equal(
|
|
95
|
-
Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id,
|
|
96
|
-
object_id,
|
|
97
|
-
)
|
|
89
|
+
refute_equal(Committee::Drivers.load_from_file(open_api_3_schema_path, parser_options: { strict_reference_validation: true }).object_id, object_id,)
|
|
98
90
|
File.write(open_api_3_schema_path, original_file_contents)
|
|
99
91
|
end
|
|
100
92
|
end
|
|
93
|
+
|
|
94
|
+
describe 'when loading different files with identical root schema content' do
|
|
95
|
+
it 'returns different objects because relative references are resolved from schema_path' do
|
|
96
|
+
parser_options = { strict_reference_validation: true }
|
|
97
|
+
|
|
98
|
+
Dir.mktmpdir("committee-cache-key-test") do |tmpdir|
|
|
99
|
+
first_dir = File.join(tmpdir, "first")
|
|
100
|
+
second_dir = File.join(tmpdir, "second")
|
|
101
|
+
Dir.mkdir(first_dir)
|
|
102
|
+
Dir.mkdir(second_dir)
|
|
103
|
+
|
|
104
|
+
root_schema = File.read(open_api_3_schema_path)
|
|
105
|
+
original_referee = File.read("test/data/openapi3/referee.yaml")
|
|
106
|
+
changed_referee = original_referee.sub("type: string", "type: integer")
|
|
107
|
+
|
|
108
|
+
first_root_path = File.join(first_dir, "normal.yaml")
|
|
109
|
+
second_root_path = File.join(second_dir, "normal.yaml")
|
|
110
|
+
|
|
111
|
+
File.write(first_root_path, root_schema)
|
|
112
|
+
File.write(second_root_path, root_schema)
|
|
113
|
+
File.write(File.join(first_dir, "referee.yaml"), original_referee)
|
|
114
|
+
File.write(File.join(second_dir, "referee.yaml"), changed_referee)
|
|
115
|
+
|
|
116
|
+
first_schema = Committee::Drivers.load_from_file(first_root_path, parser_options: parser_options)
|
|
117
|
+
second_schema = Committee::Drivers.load_from_file(second_root_path, parser_options: parser_options)
|
|
118
|
+
|
|
119
|
+
refute_equal(first_schema.object_id, second_schema.object_id)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
101
123
|
end
|
|
102
124
|
end
|
|
103
125
|
|
|
@@ -152,15 +174,7 @@ describe Committee::Drivers do
|
|
|
152
174
|
end
|
|
153
175
|
|
|
154
176
|
describe Committee::Drivers::Driver do
|
|
155
|
-
DRIVER_METHODS = {
|
|
156
|
-
default_allow_get_body: [],
|
|
157
|
-
default_coerce_form_params: [],
|
|
158
|
-
default_path_params: [],
|
|
159
|
-
default_query_params: [],
|
|
160
|
-
name: [],
|
|
161
|
-
parse: [nil],
|
|
162
|
-
schema_class: [],
|
|
163
|
-
}
|
|
177
|
+
DRIVER_METHODS = { default_allow_get_body: [], default_coerce_form_params: [], default_path_params: [], default_query_params: [], name: [], parse: [nil], schema_class: [], }
|
|
164
178
|
|
|
165
179
|
it "has a set of abstract methods" do
|
|
166
180
|
driver = Committee::Drivers::Driver.new
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
describe Committee::Middleware::Options::Base do
|
|
6
|
+
describe "#initialize" do
|
|
7
|
+
it "accepts valid options with schema" do
|
|
8
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema)
|
|
9
|
+
assert_equal hyper_schema, options.schema
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it "accepts valid options with schema_path" do
|
|
13
|
+
options = Committee::Middleware::Options::Base.new(schema_path: hyper_schema_schema_path)
|
|
14
|
+
assert_equal hyper_schema_schema_path, options.schema_path
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it "raises error when neither schema nor schema_path is provided" do
|
|
18
|
+
e = assert_raises(ArgumentError) do
|
|
19
|
+
Committee::Middleware::Options::Base.new({})
|
|
20
|
+
end
|
|
21
|
+
assert_equal "Committee: need option `schema` or `schema_path`", e.message
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it "raises error when error_class is not a Class" do
|
|
25
|
+
e = assert_raises(ArgumentError) do
|
|
26
|
+
Committee::Middleware::Options::Base.new(schema: hyper_schema, error_class: "not a class")
|
|
27
|
+
end
|
|
28
|
+
assert_equal "error_class must be a Class", e.message
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "raises error when accept_request_filter is not callable" do
|
|
32
|
+
e = assert_raises(ArgumentError) do
|
|
33
|
+
Committee::Middleware::Options::Base.new(schema: hyper_schema, accept_request_filter: "not callable")
|
|
34
|
+
end
|
|
35
|
+
assert_equal "accept_request_filter must respond to #call", e.message
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "uses default error_class when not provided" do
|
|
39
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema)
|
|
40
|
+
assert_equal Committee::ValidationError, options.error_class
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "uses provided error_class" do
|
|
44
|
+
custom_error = Class.new(StandardError)
|
|
45
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, error_class: custom_error)
|
|
46
|
+
assert_equal custom_error, options.error_class
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it "sets raise_error from :raise option" do
|
|
50
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, raise: true)
|
|
51
|
+
assert_equal true, options.raise_error
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it "sets prefix option" do
|
|
55
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, prefix: "/api/v1")
|
|
56
|
+
assert_equal "/api/v1", options.prefix
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
it "sets accept_request_filter option" do
|
|
60
|
+
filter = ->(request) { request.path.start_with?("/api") }
|
|
61
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, accept_request_filter: filter)
|
|
62
|
+
assert_equal filter, options.accept_request_filter
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
it "sets strict_reference_validation option" do
|
|
66
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, strict_reference_validation: true)
|
|
67
|
+
assert_equal true, options.strict_reference_validation
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
describe "#[]" do
|
|
72
|
+
it "provides Hash-like access" do
|
|
73
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, prefix: "/api")
|
|
74
|
+
assert_equal "/api", options[:prefix]
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
describe "#fetch" do
|
|
79
|
+
it "returns value for existing key" do
|
|
80
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, prefix: "/api")
|
|
81
|
+
assert_equal "/api", options.fetch(:prefix, nil)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "returns default for missing key" do
|
|
85
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema)
|
|
86
|
+
assert_equal "default", options.fetch(:nonexistent, "default")
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
describe "#to_h" do
|
|
91
|
+
it "returns Hash representation of options" do
|
|
92
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema, prefix: "/api")
|
|
93
|
+
hash = options.to_h
|
|
94
|
+
assert_kind_of Hash, hash
|
|
95
|
+
assert_equal hyper_schema, hash[:schema]
|
|
96
|
+
assert_equal "/api", hash[:prefix]
|
|
97
|
+
end
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
describe ".from" do
|
|
101
|
+
it "returns the same object if already an Options instance" do
|
|
102
|
+
options = Committee::Middleware::Options::Base.new(schema: hyper_schema)
|
|
103
|
+
assert_same options, Committee::Middleware::Options::Base.from(options)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
it "creates new instance from Hash" do
|
|
107
|
+
hash = { schema: hyper_schema, prefix: "/api" }
|
|
108
|
+
options = Committee::Middleware::Options::Base.from(hash)
|
|
109
|
+
assert_kind_of Committee::Middleware::Options::Base, options
|
|
110
|
+
assert_equal "/api", options.prefix
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
it "raises error for invalid input" do
|
|
114
|
+
e = assert_raises(ArgumentError) do
|
|
115
|
+
Committee::Middleware::Options::Base.from("invalid")
|
|
116
|
+
end
|
|
117
|
+
assert_match(/options must be a Hash or/, e.message)
|
|
118
|
+
end
|
|
119
|
+
end
|
|
120
|
+
end
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
describe Committee::Middleware::Options::RequestValidation do
|
|
6
|
+
describe "#initialize" do
|
|
7
|
+
it "inherits from Base" do
|
|
8
|
+
assert Committee::Middleware::Options::RequestValidation < Committee::Middleware::Options::Base
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "accepts valid options" do
|
|
12
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
13
|
+
assert_equal hyper_schema, options.schema
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "sets strict option with default false" do
|
|
17
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
18
|
+
assert_equal false, options.strict
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "sets strict option when provided" do
|
|
22
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, strict: true)
|
|
23
|
+
assert_equal true, options.strict
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "sets ignore_error option with default false" do
|
|
27
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
28
|
+
assert_equal false, options.ignore_error
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "sets ignore_error option when provided" do
|
|
32
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, ignore_error: true)
|
|
33
|
+
assert_equal true, options.ignore_error
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "sets coerce_date_times option" do
|
|
37
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, coerce_date_times: true)
|
|
38
|
+
assert_equal true, options.coerce_date_times
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "sets coerce_recursive option with default true" do
|
|
42
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
43
|
+
assert_equal true, options.coerce_recursive
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "sets coerce_form_params option" do
|
|
47
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, coerce_form_params: true)
|
|
48
|
+
assert_equal true, options.coerce_form_params
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "sets coerce_path_params option" do
|
|
52
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, coerce_path_params: true)
|
|
53
|
+
assert_equal true, options.coerce_path_params
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "sets coerce_query_params option" do
|
|
57
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, coerce_query_params: true)
|
|
58
|
+
assert_equal true, options.coerce_query_params
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "sets allow_form_params option with default true" do
|
|
62
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
63
|
+
assert_equal true, options.allow_form_params
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "sets allow_query_params option with default true" do
|
|
67
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
68
|
+
assert_equal true, options.allow_query_params
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
it "sets allow_get_body option with default false" do
|
|
72
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
73
|
+
assert_equal false, options.allow_get_body
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "sets allow_non_get_query_params option with default false" do
|
|
77
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
78
|
+
assert_equal false, options.allow_non_get_query_params
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
it "sets check_content_type option with default true" do
|
|
82
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
83
|
+
assert_equal true, options.check_content_type
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it "sets check_header option with default true" do
|
|
87
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
88
|
+
assert_equal true, options.check_header
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "sets optimistic_json option with default false" do
|
|
92
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
93
|
+
assert_equal false, options.optimistic_json
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
it "sets error_handler option" do
|
|
97
|
+
handler = ->(e, env) {}
|
|
98
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, error_handler: handler)
|
|
99
|
+
assert_equal handler, options.error_handler
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
it "raises error when error_handler is not callable" do
|
|
103
|
+
e = assert_raises(ArgumentError) do
|
|
104
|
+
Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, error_handler: "not callable")
|
|
105
|
+
end
|
|
106
|
+
assert_equal "error_handler must respond to #call", e.message
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
it "sets request_body_hash_key option with default" do
|
|
110
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
111
|
+
assert_equal "committee.request_body_hash", options.request_body_hash_key
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it "sets query_hash_key option with default" do
|
|
115
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
116
|
+
assert_equal "committee.query_hash", options.query_hash_key
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
it "sets path_hash_key option with default" do
|
|
120
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
121
|
+
assert_equal "committee.path_hash", options.path_hash_key
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
it "sets headers_key option with default" do
|
|
125
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
126
|
+
assert_equal "committee.headers", options.headers_key
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
it "sets params_key option with default" do
|
|
130
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
131
|
+
assert_equal "committee.params", options.params_key
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
it "sets allow_blank_structures option with default false" do
|
|
135
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
136
|
+
assert_equal false, options.allow_blank_structures
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
it "sets allow_empty_date_and_datetime option with default false" do
|
|
140
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
141
|
+
assert_equal false, options.allow_empty_date_and_datetime
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
it "sets parameter_overwrite_by_rails_rule option with default true" do
|
|
145
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
146
|
+
assert_equal true, options.parameter_overwrite_by_rails_rule
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
it "sets deserialize_parameters option" do
|
|
150
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, deserialize_parameters: true)
|
|
151
|
+
assert_equal true, options.deserialize_parameters
|
|
152
|
+
end
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
describe "#to_h" do
|
|
156
|
+
it "includes RequestValidation specific options" do
|
|
157
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema, strict: true, coerce_date_times: true)
|
|
158
|
+
hash = options.to_h
|
|
159
|
+
assert_equal true, hash[:strict]
|
|
160
|
+
assert_equal true, hash[:coerce_date_times]
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
describe ".from" do
|
|
165
|
+
it "returns the same object if already an Options instance" do
|
|
166
|
+
options = Committee::Middleware::Options::RequestValidation.new(schema: hyper_schema)
|
|
167
|
+
assert_same options, Committee::Middleware::Options::RequestValidation.from(options)
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
it "creates new instance from Hash" do
|
|
171
|
+
hash = { schema: hyper_schema, strict: true }
|
|
172
|
+
options = Committee::Middleware::Options::RequestValidation.from(hash)
|
|
173
|
+
assert_kind_of Committee::Middleware::Options::RequestValidation, options
|
|
174
|
+
assert_equal true, options.strict
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "test_helper"
|
|
4
|
+
|
|
5
|
+
describe Committee::Middleware::Options::ResponseValidation do
|
|
6
|
+
describe "#initialize" do
|
|
7
|
+
it "inherits from Base" do
|
|
8
|
+
assert Committee::Middleware::Options::ResponseValidation < Committee::Middleware::Options::Base
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it "accepts valid options" do
|
|
12
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
13
|
+
assert_equal hyper_schema, options.schema
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it "sets strict option with default false" do
|
|
17
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
18
|
+
assert_equal false, options.strict
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it "sets strict option when provided" do
|
|
22
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, strict: true)
|
|
23
|
+
assert_equal true, options.strict
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it "sets ignore_error option with default false" do
|
|
27
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
28
|
+
assert_equal false, options.ignore_error
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it "sets ignore_error option when provided" do
|
|
32
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, ignore_error: true)
|
|
33
|
+
assert_equal true, options.ignore_error
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it "sets validate_success_only option with default true" do
|
|
37
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
38
|
+
assert_equal true, options.validate_success_only
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
it "sets validate_success_only option when provided" do
|
|
42
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, validate_success_only: false)
|
|
43
|
+
assert_equal false, options.validate_success_only
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
it "sets parse_response_by_content_type option with default true" do
|
|
47
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
48
|
+
assert_equal true, options.parse_response_by_content_type
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it "sets parse_response_by_content_type option when provided" do
|
|
52
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, parse_response_by_content_type: false)
|
|
53
|
+
assert_equal false, options.parse_response_by_content_type
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
it "sets coerce_response_values option with default false" do
|
|
57
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
58
|
+
assert_equal false, options.coerce_response_values
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it "sets coerce_response_values option when provided" do
|
|
62
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, coerce_response_values: true)
|
|
63
|
+
assert_equal true, options.coerce_response_values
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
it "sets error_handler option" do
|
|
67
|
+
handler = ->(e, env) {}
|
|
68
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, error_handler: handler)
|
|
69
|
+
assert_equal handler, options.error_handler
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
it "raises error when error_handler is not callable" do
|
|
73
|
+
e = assert_raises(ArgumentError) do
|
|
74
|
+
Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, error_handler: "not callable")
|
|
75
|
+
end
|
|
76
|
+
assert_equal "error_handler must respond to #call", e.message
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
it "sets streaming_content_parsers option with default empty hash" do
|
|
80
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
81
|
+
assert_equal({}, options.streaming_content_parsers)
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
it "sets streaming_content_parsers option when provided" do
|
|
85
|
+
parser = ->(body) { JSON.parse(body) }
|
|
86
|
+
parsers = { "application/json" => parser }
|
|
87
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, streaming_content_parsers: parsers)
|
|
88
|
+
assert_equal parsers, options.streaming_content_parsers
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
it "raises error when streaming_content_parsers is not a Hash" do
|
|
92
|
+
e = assert_raises(ArgumentError) do
|
|
93
|
+
Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, streaming_content_parsers: "not a hash")
|
|
94
|
+
end
|
|
95
|
+
assert_equal "streaming_content_parsers must be a Hash", e.message
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
describe "#to_h" do
|
|
100
|
+
it "includes ResponseValidation specific options" do
|
|
101
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema, strict: true, validate_success_only: false)
|
|
102
|
+
hash = options.to_h
|
|
103
|
+
assert_equal true, hash[:strict]
|
|
104
|
+
assert_equal false, hash[:validate_success_only]
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
describe ".from" do
|
|
109
|
+
it "returns the same object if already an Options instance" do
|
|
110
|
+
options = Committee::Middleware::Options::ResponseValidation.new(schema: hyper_schema)
|
|
111
|
+
assert_same options, Committee::Middleware::Options::ResponseValidation.from(options)
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
it "creates new instance from Hash" do
|
|
115
|
+
hash = { schema: hyper_schema, strict: true }
|
|
116
|
+
options = Committee::Middleware::Options::ResponseValidation.from(hash)
|
|
117
|
+
assert_kind_of Committee::Middleware::Options::ResponseValidation, options
|
|
118
|
+
assert_equal true, options.strict
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
end
|