committee 1.2.2 → 1.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/committee/middleware/base.rb +11 -1
- data/lib/committee/middleware/request_validation.rb +9 -12
- data/lib/committee/middleware/response_validation.rb +4 -5
- data/lib/committee/middleware/stub.rb +7 -9
- data/lib/committee/router.rb +15 -5
- data/lib/committee/test/methods.rb +2 -4
- data/test/middleware/request_validation_test.rb +7 -0
- data/test/router_test.rb +22 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 80631a912a39fd3bfe5334456acb5c6dcaf8a30a
|
4
|
+
data.tar.gz: 61ded3be6affcb5f842a168a27823bc646f00776
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 51cd02c1fedb4fa660e88e3f1d56580a3d3c425e8cb0736903ed6b50a7564ff0e46a4a5aa447af7077132893f1b174e6e4becff245edd80d73da6f961adf04bb
|
7
|
+
data.tar.gz: 735ce3684a203194307b638c35201fbd59aaef38813ffa7e076fc17e6ee3ddc7fc3c954cba93930d5ef7e263149fa90c776c70beeba821e81e077e9f1052a47e
|
@@ -11,7 +11,17 @@ module Committee::Middleware
|
|
11
11
|
end
|
12
12
|
@schema = JsonSchema.parse!(data)
|
13
13
|
@schema.expand_references!
|
14
|
-
@router = Committee::Router.new(@schema)
|
14
|
+
@router = Committee::Router.new(@schema, options)
|
15
|
+
end
|
16
|
+
|
17
|
+
def call(env)
|
18
|
+
request = Rack::Request.new(env)
|
19
|
+
|
20
|
+
if @router.includes_request?(request)
|
21
|
+
handle(request)
|
22
|
+
else
|
23
|
+
@app.call(request.env)
|
24
|
+
end
|
15
25
|
end
|
16
26
|
|
17
27
|
private
|
@@ -2,7 +2,6 @@ module Committee::Middleware
|
|
2
2
|
class RequestValidation < Base
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
|
-
@prefix = options[:prefix]
|
6
5
|
@raise = options[:raise]
|
7
6
|
@strict = options[:strict]
|
8
7
|
|
@@ -10,19 +9,17 @@ module Committee::Middleware
|
|
10
9
|
@allow_extra = options[:allow_extra]
|
11
10
|
end
|
12
11
|
|
13
|
-
def
|
14
|
-
request =
|
15
|
-
|
16
|
-
if link = @router.
|
12
|
+
def handle(request)
|
13
|
+
request.env[@params_key] = Committee::RequestUnpacker.new(request).call
|
14
|
+
|
15
|
+
if link = @router.find_request_link(request)
|
17
16
|
validator = Committee::RequestValidator.new(link)
|
18
|
-
validator.call(request, env[@params_key])
|
19
|
-
@app.call(env)
|
17
|
+
validator.call(request, request.env[@params_key])
|
18
|
+
@app.call(request.env)
|
19
|
+
elsif @strict
|
20
|
+
raise Committee::NotFound
|
20
21
|
else
|
21
|
-
|
22
|
-
raise Committee::NotFound
|
23
|
-
else
|
24
|
-
@app.call(env)
|
25
|
-
end
|
22
|
+
@app.call(request.env)
|
26
23
|
end
|
27
24
|
rescue Committee::BadRequest, Committee::InvalidRequest
|
28
25
|
raise if @raise
|
@@ -2,14 +2,13 @@ module Committee::Middleware
|
|
2
2
|
class ResponseValidation < Base
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
|
-
@prefix = options[:prefix]
|
6
5
|
@raise = options[:raise]
|
7
6
|
end
|
8
7
|
|
9
|
-
def
|
10
|
-
status, headers, response = @app.call(env)
|
11
|
-
|
12
|
-
if link = @router.
|
8
|
+
def handle(request)
|
9
|
+
status, headers, response = @app.call(request.env)
|
10
|
+
|
11
|
+
if link = @router.find_request_link(request)
|
13
12
|
full_body = ""
|
14
13
|
response.each do |chunk|
|
15
14
|
full_body << chunk
|
@@ -3,23 +3,21 @@ module Committee::Middleware
|
|
3
3
|
def initialize(app, options={})
|
4
4
|
super
|
5
5
|
@cache = {}
|
6
|
-
@call
|
7
|
-
@prefix = options[:prefix]
|
6
|
+
@call = options[:call]
|
8
7
|
end
|
9
8
|
|
10
|
-
def
|
11
|
-
|
12
|
-
if link = @router.routes_request?(request, prefix: @prefix)
|
9
|
+
def handle(request)
|
10
|
+
if link = @router.find_request_link(request)
|
13
11
|
headers = { "Content-Type" => "application/json" }
|
14
12
|
data = cache(link.method, link.href) do
|
15
13
|
Committee::ResponseGenerator.new.call(link)
|
16
14
|
end
|
17
15
|
if @call
|
18
|
-
env["committee.response"] = data
|
19
|
-
call_status, call_headers, call_body = @app.call(env)
|
16
|
+
request.env["committee.response"] = data
|
17
|
+
call_status, call_headers, call_body = @app.call(request.env)
|
20
18
|
|
21
19
|
# a committee.suppress signal initiates a direct pass through
|
22
|
-
if env["committee.suppress"] == true
|
20
|
+
if request.env["committee.suppress"] == true
|
23
21
|
return call_status, call_headers, call_body
|
24
22
|
end
|
25
23
|
|
@@ -30,7 +28,7 @@ module Committee::Middleware
|
|
30
28
|
status = link.rel == "create" ? 201 : 200
|
31
29
|
[status, headers, [MultiJson.encode(data, pretty: true)]]
|
32
30
|
else
|
33
|
-
@app.call(env)
|
31
|
+
@app.call(request.env)
|
34
32
|
end
|
35
33
|
end
|
36
34
|
|
data/lib/committee/router.rb
CHANGED
@@ -1,11 +1,21 @@
|
|
1
1
|
module Committee
|
2
2
|
class Router
|
3
|
-
def initialize(schema)
|
3
|
+
def initialize(schema, options = {})
|
4
4
|
@routes = build_routes(schema)
|
5
|
+
@prefix = options[:prefix]
|
6
|
+
@prefix_regexp = /\A#{Regexp.escape(@prefix)}/.freeze if @prefix
|
5
7
|
end
|
6
8
|
|
7
|
-
def
|
8
|
-
|
9
|
+
def includes?(path)
|
10
|
+
!@prefix || path =~ @prefix_regexp
|
11
|
+
end
|
12
|
+
|
13
|
+
def includes_request?(request)
|
14
|
+
includes?(request.path)
|
15
|
+
end
|
16
|
+
|
17
|
+
def find_link(method, path)
|
18
|
+
path = path.gsub(@prefix_regexp, "") if @prefix
|
9
19
|
if method_routes = @routes[method]
|
10
20
|
method_routes.each do |pattern, link|
|
11
21
|
if path =~ pattern
|
@@ -16,8 +26,8 @@ module Committee
|
|
16
26
|
nil
|
17
27
|
end
|
18
28
|
|
19
|
-
def
|
20
|
-
|
29
|
+
def find_request_link(request)
|
30
|
+
find_link(request.request_method, request.path_info)
|
21
31
|
end
|
22
32
|
|
23
33
|
private
|
@@ -11,11 +11,9 @@ module Committee::Test
|
|
11
11
|
schema.expand_references!
|
12
12
|
schema
|
13
13
|
end
|
14
|
-
@router ||= Committee::Router.new(@schema)
|
14
|
+
@router ||= Committee::Router.new(@schema, prefix: schema_url_prefix)
|
15
15
|
|
16
|
-
link =
|
17
|
-
@router.routes_request?(last_request, prefix: schema_url_prefix)
|
18
|
-
unless link
|
16
|
+
unless link = @router.find_request_link(last_request)
|
19
17
|
response = "`#{last_request.request_method} #{last_request.path_info}` undefined in schema."
|
20
18
|
raise Committee::InvalidResponse.new(response)
|
21
19
|
end
|
@@ -46,6 +46,13 @@ describe Committee::Middleware::RequestValidation do
|
|
46
46
|
assert_equal 200, last_response.status
|
47
47
|
end
|
48
48
|
|
49
|
+
it "ignores paths outside the prefix" do
|
50
|
+
@app = new_rack_app(prefix: "/v1")
|
51
|
+
header "Content-Type", "text/html"
|
52
|
+
get "/hello"
|
53
|
+
assert_equal 200, last_response.status
|
54
|
+
end
|
55
|
+
|
49
56
|
it "warns when sending a deprecated string" do
|
50
57
|
mock(Committee).warn_deprecated.with_any_args
|
51
58
|
@app = new_rack_app(schema: File.read("./test/data/schema.json"))
|
data/test/router_test.rb
CHANGED
@@ -1,27 +1,38 @@
|
|
1
1
|
require_relative "test_helper"
|
2
2
|
|
3
3
|
describe Committee::Router do
|
4
|
-
before do
|
5
|
-
data = MultiJson.decode(File.read("./test/data/schema.json"))
|
6
|
-
schema = JsonSchema.parse!(data)
|
7
|
-
schema.expand_references!
|
8
|
-
@router = Committee::Router.new(schema)
|
9
|
-
end
|
10
|
-
|
11
4
|
it "builds routes without parameters" do
|
12
|
-
refute_nil
|
5
|
+
refute_nil router.find_link("GET", "/apps")
|
13
6
|
end
|
14
7
|
|
15
8
|
it "builds routes with parameters" do
|
16
|
-
refute_nil
|
9
|
+
refute_nil router.find_link("GET", "/apps/123")
|
17
10
|
end
|
18
11
|
|
19
12
|
it "doesn't match anything on a /" do
|
20
|
-
assert_nil
|
13
|
+
assert_nil router.find_link("GET", "/")
|
21
14
|
end
|
22
15
|
|
23
16
|
it "takes a prefix" do
|
24
17
|
# this is a sociopathic example
|
25
|
-
|
18
|
+
assert router(prefix: "/kpi").find_link("GET", "/kpi/apps/123")
|
19
|
+
end
|
20
|
+
|
21
|
+
it "includes all paths without a prefix" do
|
22
|
+
assert router.includes?("/")
|
23
|
+
assert router.includes?("/apps")
|
24
|
+
end
|
25
|
+
|
26
|
+
it "only includes the prefix path with a prefix" do
|
27
|
+
refute router(prefix: "/kpi").includes?("/")
|
28
|
+
assert router(prefix: "/kpi").includes?("/kpi")
|
29
|
+
assert router(prefix: "/kpi").includes?("/kpi/apps")
|
30
|
+
end
|
31
|
+
|
32
|
+
def router(options = {})
|
33
|
+
data = MultiJson.decode(File.read("./test/data/schema.json"))
|
34
|
+
schema = JsonSchema.parse!(data)
|
35
|
+
schema.expand_references!
|
36
|
+
Committee::Router.new(schema, options)
|
26
37
|
end
|
27
38
|
end
|