committee 0.4.7 → 0.4.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.
@@ -1,9 +1,14 @@
1
1
  module Committee::Middleware
2
2
  class RequestValidation < Base
3
+ def initialize(app, options={})
4
+ super
5
+ @prefix = options[:prefix]
6
+ end
7
+
3
8
  def call(env)
4
9
  request = Rack::Request.new(env)
5
10
  env[@params_key] = Committee::RequestUnpacker.new(request).call
6
- link, _ = @router.routes_request?(request)
11
+ link, _ = @router.routes_request?(request, prefix: @prefix)
7
12
  if link
8
13
  Committee::ParamValidator.new(env[@params_key], @schema, link).call
9
14
  end
@@ -1,9 +1,15 @@
1
1
  module Committee::Middleware
2
2
  class ResponseValidation < Base
3
+ def initialize(app, options={})
4
+ super
5
+ @prefix = options[:prefix]
6
+ end
7
+
3
8
  def call(env)
4
9
  status, headers, response = @app.call(env)
5
10
  request = Rack::Request.new(env)
6
- link_schema, type_schema = @router.routes_request?(request)
11
+ link_schema, type_schema =
12
+ @router.routes_request?(request, prefix: @prefix)
7
13
  if type_schema
8
14
  check_content_type!(headers)
9
15
  str = response.reduce("") { |str, s| str << s }
@@ -3,11 +3,12 @@ module Committee::Middleware
3
3
  def initialize(app, options={})
4
4
  super
5
5
  @cache = {}
6
+ @prefix = options[:prefix]
6
7
  end
7
8
 
8
9
  def call(env)
9
10
  request = Rack::Request.new(env)
10
- link_schema, type_schema = @router.routes_request?(request)
11
+ link_schema, type_schema = @router.routes_request?(request, prefix: @prefix)
11
12
  if type_schema
12
13
  str = cache(link_schema["method"], link_schema["href"]) do
13
14
  data = Committee::ResponseGenerator.new(@schema, type_schema, link_schema).call
@@ -3,57 +3,79 @@ require_relative "../test_helper"
3
3
  describe Committee::Middleware::RequestValidation do
4
4
  include Rack::Test::Methods
5
5
 
6
- StubApp = Rack::Builder.new {
7
- use Committee::Middleware::RequestValidation,
8
- schema: File.read("./test/data/schema.json")
9
- run lambda { |_|
10
- [200, {}, []]
11
- }
12
- }
13
-
14
6
  def app
15
- StubApp
16
- end
17
-
18
- before do
19
- header "Content-Type", "application/json"
7
+ @app
20
8
  end
21
9
 
22
10
  it "detects an invalid Content-Type" do
11
+ @app = new_rack_app
23
12
  header "Content-Type", "application/whats-this"
24
13
  post "/account/app-transfers", "{}"
25
14
  assert_equal 400, last_response.status
26
15
  end
27
16
 
28
17
  it "passes through a valid request" do
18
+ @app = new_rack_app
29
19
  params = {
30
20
  "app" => "heroku-api",
31
21
  "recipient" => "owner@heroku.com",
32
22
  }
23
+ header "Content-Type", "application/json"
33
24
  post "/account/app-transfers", MultiJson.encode(params)
34
25
  assert_equal 200, last_response.status
35
26
  end
36
27
 
37
28
  it "detects a missing parameter" do
29
+ @app = new_rack_app
30
+ header "Content-Type", "application/json"
38
31
  post "/account/app-transfers", "{}"
39
32
  assert_equal 422, last_response.status
40
33
  assert_match /require params/i, last_response.body
41
34
  end
42
35
 
43
36
  it "detects an extra parameter" do
37
+ @app = new_rack_app
44
38
  params = {
45
39
  "app" => "heroku-api",
46
40
  "cloud" => "production",
47
41
  "recipient" => "owner@heroku.com",
48
42
  }
43
+ header "Content-Type", "application/json"
49
44
  post "/account/app-transfers", MultiJson.encode(params)
50
45
  assert_equal 422, last_response.status
51
46
  assert_match /unknown params/i, last_response.body
52
47
  end
53
48
 
54
49
  it "rescues JSON errors" do
50
+ @app = new_rack_app
51
+ header "Content-Type", "application/json"
55
52
  post "/account/app-transfers", "{x:y}"
56
53
  assert_equal 400, last_response.status
57
54
  assert_match /valid json/i, last_response.body
58
55
  end
56
+
57
+ it "takes a prefix" do
58
+ @app = new_rack_app(prefix: "/v1")
59
+ params = {
60
+ "app" => "heroku-api",
61
+ "recipient" => "owner@heroku.com",
62
+ }
63
+ header "Content-Type", "application/json"
64
+ post "/v1/account/app-transfers", MultiJson.encode(params)
65
+ assert_equal 200, last_response.status
66
+ end
67
+
68
+ private
69
+
70
+ def new_rack_app(options = {})
71
+ options = {
72
+ schema: File.read("./test/data/schema.json")
73
+ }.merge(options)
74
+ Rack::Builder.new {
75
+ use Committee::Middleware::RequestValidation, options
76
+ run lambda { |_|
77
+ [200, {}, []]
78
+ }
79
+ }
80
+ end
59
81
  end
@@ -14,7 +14,8 @@ describe Committee::Middleware::ResponseValidation do
14
14
  end
15
15
 
16
16
  it "detects an invalid response Content-Type" do
17
- @app = new_rack_app(MultiJson.encode([ValidApp]), {})
17
+ @app = new_rack_app(MultiJson.encode([ValidApp]),
18
+ { "Content-Type" => "application/xml" })
18
19
  get "/apps"
19
20
  assert_equal 500, last_response.status
20
21
  assert_match /response header must be set to/i, last_response.body
@@ -52,10 +53,23 @@ describe Committee::Middleware::ResponseValidation do
52
53
  assert_match /valid json/i, last_response.body
53
54
  end
54
55
 
55
- def new_rack_app(response, headers={ "Content-Type" => "application/json" })
56
+ it "takes a prefix" do
57
+ @app = new_rack_app(MultiJson.encode([ValidApp]), {}, prefix: "/v1")
58
+ get "/v1/apps"
59
+ assert_equal 200, last_response.status
60
+ end
61
+
62
+ private
63
+
64
+ def new_rack_app(response, headers = {}, options = {})
65
+ headers = {
66
+ "Content-Type" => "application/json"
67
+ }.merge(headers)
68
+ options = {
69
+ schema: File.read("./test/data/schema.json")
70
+ }.merge(options)
56
71
  Rack::Builder.new {
57
- use Committee::Middleware::ResponseValidation,
58
- schema: File.read("./test/data/schema.json")
72
+ use Committee::Middleware::ResponseValidation, options
59
73
  run lambda { |_|
60
74
  [200, headers, [response]]
61
75
  }
@@ -3,22 +3,37 @@ require_relative "../test_helper"
3
3
  describe Committee::Middleware::Stub do
4
4
  include Rack::Test::Methods
5
5
 
6
- ParamValidationApp = Rack::Builder.new {
7
- use Committee::Middleware::Stub,
8
- schema: File.read("./test/data/schema.json")
9
- run lambda { |_|
10
- [200, {}, []]
11
- }
12
- }
13
-
14
6
  def app
15
- ParamValidationApp
7
+ @app
16
8
  end
17
9
 
18
10
  it "responds with a stubbed response" do
11
+ @app = new_rack_app
19
12
  get "/apps/heroku-api"
20
13
  assert_equal 200, last_response.status
21
14
  data = MultiJson.decode(last_response.body)
22
15
  assert_equal ValidApp.keys.sort, data.keys.sort
23
16
  end
17
+
18
+ it "takes a prefix" do
19
+ @app = new_rack_app(prefix: "/v1")
20
+ get "/v1/apps/heroku-api"
21
+ assert_equal 200, last_response.status
22
+ data = MultiJson.decode(last_response.body)
23
+ assert_equal ValidApp.keys.sort, data.keys.sort
24
+ end
25
+
26
+ private
27
+
28
+ def new_rack_app(options = {})
29
+ options = {
30
+ schema: File.read("./test/data/schema.json")
31
+ }.merge(options)
32
+ Rack::Builder.new {
33
+ use Committee::Middleware::Stub, options
34
+ run lambda { |_|
35
+ [200, {}, []]
36
+ }
37
+ }
38
+ end
24
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: committee
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.7
4
+ version: 0.4.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2014-04-24 00:00:00.000000000 Z
13
+ date: 2014-04-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json