committee 1.0.3 → 1.0.4
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.
@@ -10,9 +10,10 @@ module Committee::Middleware
|
|
10
10
|
|
11
11
|
def call(env)
|
12
12
|
request = Rack::Request.new(env)
|
13
|
-
env[@params_key] = Committee::RequestUnpacker.new(request).call
|
14
13
|
if link = @router.routes_request?(request, prefix: @prefix)
|
15
|
-
Committee::RequestValidator.new
|
14
|
+
validator = Committee::RequestValidator.new(link)
|
15
|
+
validator.call(request)
|
16
|
+
env[@params_key] = validator.data
|
16
17
|
end
|
17
18
|
@app.call(env)
|
18
19
|
rescue Committee::BadRequest, Committee::InvalidRequest
|
@@ -1,16 +1,30 @@
|
|
1
1
|
module Committee
|
2
2
|
class RequestValidator
|
3
|
-
|
3
|
+
attr_accessor :data
|
4
|
+
|
5
|
+
def initialize(link, options = {})
|
6
|
+
@link = link
|
4
7
|
end
|
5
8
|
|
6
|
-
def call(
|
7
|
-
|
8
|
-
|
9
|
+
def call(request)
|
10
|
+
check_content_type!(request)
|
11
|
+
@data = Committee::RequestUnpacker.new(request).call
|
12
|
+
if @link.schema
|
13
|
+
valid, errors = @link.schema.validate(@data)
|
9
14
|
if !valid
|
10
15
|
errors = JsonSchema::SchemaError.aggregate(errors).join("\n")
|
11
16
|
raise InvalidRequest, "Invalid request.\n\n#{errors}"
|
12
17
|
end
|
13
18
|
end
|
14
19
|
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def check_content_type!(request)
|
24
|
+
unless Rack::Mime.match?(@link.enc_type, request.content_type)
|
25
|
+
raise Committee::InvalidRequest,
|
26
|
+
%{"Content-Type" request header must be set to "#{@link.enc_type}".}
|
27
|
+
end
|
28
|
+
end
|
15
29
|
end
|
16
30
|
end
|
@@ -25,7 +25,7 @@ module Committee::Test
|
|
25
25
|
end
|
26
26
|
|
27
27
|
def assert_schema_content_type
|
28
|
-
Committee.warn_deprecated("
|
28
|
+
Committee.warn_deprecated("Committee: use of #assert_schema_content_type is deprecated; use #assert_schema_conform instead.")
|
29
29
|
end
|
30
30
|
|
31
31
|
# can be overridden alternatively to #schema_path in case the schema is
|
@@ -28,17 +28,6 @@ describe Committee::Middleware::RequestValidation do
|
|
28
28
|
assert_match /invalid request/i, last_response.body
|
29
29
|
end
|
30
30
|
|
31
|
-
it "detects an invalid Content-Type" do
|
32
|
-
@app = new_rack_app
|
33
|
-
header "Content-Type", "application/whats-this"
|
34
|
-
params = {
|
35
|
-
"name" => "cloudnasium"
|
36
|
-
}
|
37
|
-
post "/apps", MultiJson.encode(params)
|
38
|
-
assert_equal 400, last_response.status
|
39
|
-
assert_match /unsupported content-type/i, last_response.body
|
40
|
-
end
|
41
|
-
|
42
31
|
it "rescues JSON errors" do
|
43
32
|
@app = new_rack_app
|
44
33
|
header "Content-Type", "application/json"
|
@@ -1,5 +1,7 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
|
+
require "stringio"
|
4
|
+
|
3
5
|
describe Committee::RequestValidator do
|
4
6
|
before do
|
5
7
|
@schema =
|
@@ -13,7 +15,16 @@ describe Committee::RequestValidator do
|
|
13
15
|
params = {
|
14
16
|
"name" => "heroku-api",
|
15
17
|
}
|
16
|
-
call(params)
|
18
|
+
call(request(params))
|
19
|
+
end
|
20
|
+
|
21
|
+
it "detects an invalid request Content-Type" do
|
22
|
+
e = assert_raises(Committee::InvalidRequest) {
|
23
|
+
call(Rack::Request.new("CONTENT_TYPE" => "application/x-www-form-urlencoded"))
|
24
|
+
}
|
25
|
+
message =
|
26
|
+
%{"Content-Type" request header must be set to "application/json".}
|
27
|
+
assert_equal message, e.message
|
17
28
|
end
|
18
29
|
|
19
30
|
it "detects a parameter of the wrong pattern" do
|
@@ -21,7 +32,7 @@ describe Committee::RequestValidator do
|
|
21
32
|
"name" => "%@!"
|
22
33
|
}
|
23
34
|
e = assert_raises(Committee::InvalidRequest) do
|
24
|
-
call(params)
|
35
|
+
call(request(params))
|
25
36
|
end
|
26
37
|
message = %{Invalid request.\n\n#/name: failed schema #/definitions/app/links/0/schema/properties/name: Expected string to match pattern "/^[a-z][a-z0-9-]{3,30}$/", value was: %@!.}
|
27
38
|
assert_equal message, e.message
|
@@ -29,7 +40,15 @@ describe Committee::RequestValidator do
|
|
29
40
|
|
30
41
|
private
|
31
42
|
|
32
|
-
def call(
|
33
|
-
Committee::RequestValidator.new
|
43
|
+
def call(request)
|
44
|
+
Committee::RequestValidator.new(@link).call(request)
|
45
|
+
end
|
46
|
+
|
47
|
+
def request(params)
|
48
|
+
env = {
|
49
|
+
"CONTENT_TYPE" => "application/json",
|
50
|
+
"rack.input" => StringIO.new(MultiJson.encode(params))
|
51
|
+
}
|
52
|
+
Rack::Request.new(env)
|
34
53
|
end
|
35
54
|
end
|