committee 0.4.13 → 0.4.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,22 +1,25 @@
1
1
  module Committee
2
- class BadRequest < StandardError
2
+ class Error < StandardError
3
3
  end
4
4
 
5
- class InvalidPattern < StandardError
5
+ class BadRequest < Error
6
6
  end
7
7
 
8
- class InvalidFormat < StandardError
8
+ class InvalidPattern < Error
9
9
  end
10
10
 
11
- class InvalidType < StandardError
11
+ class InvalidFormat < Error
12
12
  end
13
13
 
14
- class InvalidParams < StandardError
14
+ class InvalidType < Error
15
15
  end
16
16
 
17
- class InvalidResponse < StandardError
17
+ class InvalidParams < Error
18
18
  end
19
19
 
20
- class ReferenceNotFound < StandardError
20
+ class InvalidResponse < Error
21
+ end
22
+
23
+ class ReferenceNotFound < Error
21
24
  end
22
25
  end
@@ -13,7 +13,7 @@ module Committee::Middleware
13
13
 
14
14
  def render_error(status, id, message)
15
15
  [status, { "Content-Type" => "application/json" },
16
- [MultiJson.encode(id: id, error: message)]]
16
+ [MultiJson.encode({ id: id, error: message }, pretty: true)]]
17
17
  end
18
18
  end
19
19
  end
@@ -21,7 +21,7 @@ module Committee::Middleware
21
21
  @app.call(env)
22
22
  rescue Committee::BadRequest
23
23
  render_error(400, :bad_request, $!.message)
24
- rescue Committee::InvalidParams
24
+ rescue Committee::Error
25
25
  render_error(422, :invalid_params, $!.message)
26
26
  rescue MultiJson::LoadError
27
27
  render_error(400, :invalid_params, "Request body wasn't valid JSON.")
@@ -9,12 +9,12 @@ module Committee
9
9
  end
10
10
 
11
11
  def [](type)
12
- @schema["definitions"][type]
12
+ find(@schema["properties"][type]['$ref'])
13
13
  end
14
14
 
15
15
  def each
16
- @schema["definitions"].each do |type, type_schema|
17
- yield(type, type_schema)
16
+ @schema["properties"].each do |type, ref|
17
+ yield(type, find(ref['$ref']))
18
18
  end
19
19
  end
20
20
 
@@ -77,7 +77,7 @@ module Committee
77
77
  end
78
78
 
79
79
  def check_pattern(pattern, value, identifier)
80
- !pattern || value =~ pattern
80
+ !pattern || value =~ Regexp.new(pattern)
81
81
  end
82
82
  end
83
83
  end
@@ -0,0 +1,55 @@
1
+ require "benchmark"
2
+ require_relative "../test_helper"
3
+
4
+ describe Committee::Middleware::RequestValidation do
5
+ include Rack::Test::Methods
6
+
7
+ def app
8
+ @app
9
+ end
10
+
11
+ N = 50000
12
+
13
+ it "is reasonably quick" do
14
+ params = MultiJson.encode({
15
+ "app" => "heroku-api",
16
+ "recipient" => "owner@heroku.com",
17
+ })
18
+
19
+ Benchmark.benchmark(Benchmark::CAPTION, 7, Benchmark::FORMAT, ">overhead:") do |x|
20
+ raw = x.report("raw") do
21
+ N.times do
22
+ @app = new_rack_app(raw: true)
23
+ header "Content-Type", "application/json"
24
+ post "/account/app-transfers", params
25
+ end
26
+ end
27
+
28
+ rich = x.report("rich") do
29
+ N.times do
30
+ @app = new_rack_app
31
+ header "Content-Type", "application/json"
32
+ post "/account/app-transfers", params
33
+ end
34
+ end
35
+
36
+ puts "average overhead = #{(rich.real - raw.real) / N}"
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def new_rack_app(options = {})
43
+ Rack::Builder.new {
44
+ unless options.delete(:raw)
45
+ options = {
46
+ schema: File.read("./test/data/schema.json")
47
+ }.merge(options)
48
+ use Committee::Middleware::RequestValidation, options
49
+ end
50
+ run lambda { |_|
51
+ [200, {}, []]
52
+ }
53
+ }
54
+ end
55
+ 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.13
4
+ version: 0.4.14
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-30 00:00:00.000000000 Z
13
+ date: 2014-05-12 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: multi_json
@@ -44,6 +44,22 @@ dependencies:
44
44
  - - ! '>'
45
45
  - !ruby/object:Gem::Version
46
46
  version: '0.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: minitest
49
+ requirement: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
47
63
  - !ruby/object:Gem::Dependency
48
64
  name: rack-test
49
65
  requirement: !ruby/object:Gem::Requirement
@@ -60,6 +76,22 @@ dependencies:
60
76
  - - ! '>='
61
77
  - !ruby/object:Gem::Version
62
78
  version: '0'
79
+ - !ruby/object:Gem::Dependency
80
+ name: rake
81
+ requirement: !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ! '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ type: :development
88
+ prerelease: false
89
+ version_requirements: !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ! '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
63
95
  description:
64
96
  email:
65
97
  - brandur@mutelight.org
@@ -87,6 +119,7 @@ files:
87
119
  - test/middleware/response_validation_test.rb
88
120
  - test/middleware/stub_test.rb
89
121
  - test/param_validator_test.rb
122
+ - test/performance/request_validation.rb
90
123
  - test/request_unpacker_test.rb
91
124
  - test/response_generator_test.rb
92
125
  - test/response_validator_test.rb