committee 1.0.7 → 1.0.8
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.
- data/lib/committee/errors.rb +3 -0
- data/lib/committee/middleware/request_validation.rb +8 -2
- data/lib/committee/middleware/response_validation.rb +3 -0
- data/lib/committee/response_validator.rb +4 -1
- data/lib/committee/router.rb +2 -0
- data/test/middleware/request_validation_test.rb +8 -0
- data/test/middleware/response_validation_test.rb +7 -0
- data/test/response_validator_test.rb +5 -0
- metadata +1 -1
data/lib/committee/errors.rb
CHANGED
@@ -3,6 +3,7 @@ module Committee::Middleware
|
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
5
|
@prefix = options[:prefix]
|
6
|
+
@raise = options[:raise]
|
6
7
|
@strict = options[:strict]
|
7
8
|
|
8
9
|
# deprecated
|
@@ -18,15 +19,20 @@ module Committee::Middleware
|
|
18
19
|
@app.call(env)
|
19
20
|
else
|
20
21
|
if @strict
|
21
|
-
|
22
|
-
"That request method and path combination isn't defined.")
|
22
|
+
raise Committee::NotFound
|
23
23
|
else
|
24
24
|
@app.call(env)
|
25
25
|
end
|
26
26
|
end
|
27
27
|
rescue Committee::BadRequest, Committee::InvalidRequest
|
28
|
+
raise if @raise
|
28
29
|
render_error(400, :bad_request, $!.message)
|
30
|
+
rescue Committee::NotFound
|
31
|
+
raise if @raise
|
32
|
+
render_error(404, :not_found,
|
33
|
+
"That request method and path combination isn't defined.")
|
29
34
|
rescue MultiJson::LoadError
|
35
|
+
raise Committee::InvalidRequest if @raise
|
30
36
|
render_error(400, :bad_request, "Request body wasn't valid JSON.")
|
31
37
|
end
|
32
38
|
end
|
@@ -3,6 +3,7 @@ module Committee::Middleware
|
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
5
|
@prefix = options[:prefix]
|
6
|
+
@raise = options[:raise]
|
6
7
|
end
|
7
8
|
|
8
9
|
def call(env)
|
@@ -15,8 +16,10 @@ module Committee::Middleware
|
|
15
16
|
end
|
16
17
|
[status, headers, response]
|
17
18
|
rescue Committee::InvalidResponse
|
19
|
+
raise if @raise
|
18
20
|
render_error(500, :invalid_response, $!.message)
|
19
21
|
rescue MultiJson::LoadError
|
22
|
+
raise Commitee::InvalidResponse if @raise
|
20
23
|
render_error(500, :invalid_response, "Response wasn't valid JSON.")
|
21
24
|
end
|
22
25
|
end
|
@@ -29,7 +29,10 @@ module Committee
|
|
29
29
|
private
|
30
30
|
|
31
31
|
def check_content_type!(headers)
|
32
|
-
|
32
|
+
match = [%r{application/json}, %r{application/schema\+json}].any? { |m|
|
33
|
+
headers["Content-Type"] =~ m
|
34
|
+
}
|
35
|
+
unless match
|
33
36
|
raise Committee::InvalidResponse,
|
34
37
|
%{"Content-Type" response header must be set to "application/json".}
|
35
38
|
end
|
data/lib/committee/router.rb
CHANGED
@@ -27,6 +27,7 @@ module Committee
|
|
27
27
|
|
28
28
|
schema.links.each do |link|
|
29
29
|
method, href = parse_link(link)
|
30
|
+
next unless method
|
30
31
|
routes[method] ||= []
|
31
32
|
routes[method] << [%r{^#{href}$}, link]
|
32
33
|
end
|
@@ -41,6 +42,7 @@ module Committee
|
|
41
42
|
end
|
42
43
|
|
43
44
|
def parse_link(link)
|
45
|
+
return nil, nil if !link.method || !link.href
|
44
46
|
method = link.method.to_s.upcase
|
45
47
|
# /apps/{id} --> /apps/([^/]+)
|
46
48
|
href = link.href.gsub(/\{(.*?)\}/, "[^/]+")
|
@@ -69,6 +69,14 @@ describe Committee::Middleware::RequestValidation do
|
|
69
69
|
assert_equal 404, last_response.status
|
70
70
|
end
|
71
71
|
|
72
|
+
it "optionally raises an error" do
|
73
|
+
@app = new_rack_app(raise: true)
|
74
|
+
header "Content-Type", "application/json"
|
75
|
+
assert_raises(Committee::InvalidRequest) do
|
76
|
+
post "/apps", "{x:y}"
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
72
80
|
private
|
73
81
|
|
74
82
|
def new_rack_app(options = {})
|
@@ -41,6 +41,13 @@ describe Committee::Middleware::ResponseValidation do
|
|
41
41
|
assert_equal 200, last_response.status
|
42
42
|
end
|
43
43
|
|
44
|
+
it "rescues JSON errors" do
|
45
|
+
@app = new_rack_app("[{x:y}]", {}, raise: true)
|
46
|
+
assert(Committee::InvalidResponse) do
|
47
|
+
get "/apps"
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
44
51
|
private
|
45
52
|
|
46
53
|
def new_rack_app(response, headers = {}, options = {})
|
@@ -41,6 +41,11 @@ describe Committee::ResponseValidator do
|
|
41
41
|
assert_equal message, e.message
|
42
42
|
end
|
43
43
|
|
44
|
+
it "allows application/schema+json in responses as well" do
|
45
|
+
@headers = { "Content-Type" => "application/schema+json" }
|
46
|
+
call
|
47
|
+
end
|
48
|
+
|
44
49
|
it "raises errors generated by json_schema" do
|
45
50
|
@data.merge!("name" => "%@!")
|
46
51
|
e = assert_raises(Committee::InvalidResponse) { call }
|