committee 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: eccf6a4dfc52e5076e090765d4a2ef1eb3ad444b
4
- data.tar.gz: 0f8708a47a6ddb941ab7a0a652518b1f91b5c1c8
3
+ metadata.gz: b5866c6d0e099206a30958c836ad4b49473f68cd
4
+ data.tar.gz: d05049dcb5e2970c4451ce7e163790fb8199e95e
5
5
  SHA512:
6
- metadata.gz: dda95c75d4c988448851b8d4021c7401310309b89bbb03ee7b3191f0d96d2762e89ba7a95b586985817301ab141fa855c1bb25eb423eaead9ac584765d8c7bb0
7
- data.tar.gz: c8eb5c8fd08fa815ebb2f174cf395b1b5a3b93f5cb41a21648ea600a1397157ed939498663de7fda1e68ae640d49e4680bbd3161ea13787f8def0cbbccf5b0e2
6
+ metadata.gz: 0c4c4c60032ef9e6b7874e9078f6be8a6d3aee3d32949591abf7b4569057cbd4498c55b1d8d4a10ccd9d0970e7c422dc26933b171bb4efb5a72a9635b90a0247
7
+ data.tar.gz: 20acabc46e24f3eb85dc8b3dcce37794988a0dd559a534c46b2be17bb8861051f1927914df7f6b28bb4271b813a7228f69c03d9051baa0ae724e307d8dce697a
@@ -2,10 +2,11 @@ module Committee::Middleware
2
2
  class RequestValidation < Base
3
3
  def initialize(app, options={})
4
4
  super
5
- @allow_form_params = options.fetch(:allow_form_params, true)
6
- @optimistic_json = options.fetch(:optimistic_json, false)
7
- @raise = options[:raise]
8
- @strict = options[:strict]
5
+ @allow_form_params = options.fetch(:allow_form_params, true)
6
+ @allow_query_params = options.fetch(:allow_query_params, true)
7
+ @optimistic_json = options.fetch(:optimistic_json, false)
8
+ @raise = options[:raise]
9
+ @strict = options[:strict]
9
10
 
10
11
  # deprecated
11
12
  @allow_extra = options[:allow_extra]
@@ -14,8 +15,9 @@ module Committee::Middleware
14
15
  def handle(request)
15
16
  request.env[@params_key] = Committee::RequestUnpacker.new(
16
17
  request,
17
- allow_form_params: @allow_form_params,
18
- optimistic_json: @optimistic_json
18
+ allow_form_params: @allow_form_params,
19
+ allow_query_params: @allow_query_params,
20
+ optimistic_json: @optimistic_json
19
21
  ).call
20
22
 
21
23
  if link = @router.find_request_link(request)
@@ -15,7 +15,7 @@ module Committee::Middleware
15
15
  full_body << chunk
16
16
  end
17
17
  data = MultiJson.decode(full_body)
18
- Committee::ResponseValidator.new(link).call(headers, data)
18
+ Committee::ResponseValidator.new(link).call(status, headers, data)
19
19
  end
20
20
 
21
21
  [status, headers, response]
@@ -3,8 +3,9 @@ module Committee
3
3
  def initialize(request, options={})
4
4
  @request = request
5
5
 
6
- @allow_form_params = options[:allow_form_params]
7
- @optimistic_json = options[:optimistic_json]
6
+ @allow_form_params = options[:allow_form_params]
7
+ @allow_query_params = options[:allow_query_params]
8
+ @optimistic_json = options[:optimistic_json]
8
9
  end
9
10
 
10
11
  def call
@@ -16,7 +17,7 @@ module Committee
16
17
  parse_json rescue MultiJson::LoadError nil
17
18
  end
18
19
 
19
- if params
20
+ params = if params
20
21
  params
21
22
  elsif @allow_form_params && @request.content_type == "application/x-www-form-urlencoded"
22
23
  # Actually, POST means anything in the request body, could be from
@@ -25,6 +26,12 @@ module Committee
25
26
  else
26
27
  {}
27
28
  end
29
+
30
+ if @allow_query_params
31
+ indifferent_params(@request.GET).merge(params)
32
+ else
33
+ params
34
+ end
28
35
  end
29
36
 
30
37
  private
@@ -9,8 +9,10 @@ module Committee
9
9
  @validator = JsonSchema::Validator.new(schema)
10
10
  end
11
11
 
12
- def call(headers, data)
13
- check_content_type!(headers)
12
+ def call(status, headers, data)
13
+ unless status == 204 # 204 No Content
14
+ check_content_type!(headers)
15
+ end
14
16
 
15
17
  if @link.rel == "instances" && !@link.target_schema
16
18
  if !data.is_a?(Array)
@@ -19,7 +19,7 @@ module Committee::Test
19
19
  end
20
20
 
21
21
  data = MultiJson.decode(last_response.body)
22
- Committee::ResponseValidator.new(link).call(last_response.headers, data)
22
+ Committee::ResponseValidator.new(link).call(last_response.status, last_response.headers, data)
23
23
  end
24
24
 
25
25
  def assert_schema_content_type
@@ -72,6 +72,27 @@ describe Committee::RequestUnpacker do
72
72
  assert_equal({ "x" => "y" }, params)
73
73
  end
74
74
 
75
+ it "unpacks form & query params with allow_form_params and allow_query_params" do
76
+ env = {
77
+ "CONTENT_TYPE" => "application/x-www-form-urlencoded",
78
+ "rack.input" => StringIO.new("x=y"),
79
+ "QUERY_STRING" => "a=b"
80
+ }
81
+ request = Rack::Request.new(env)
82
+ params = Committee::RequestUnpacker.new(request, allow_form_params: true, allow_query_params: true).call
83
+ assert_equal({ "x" => "y", "a" => "b" }, params)
84
+ end
85
+
86
+ it "unpacks query params with allow_query_params" do
87
+ env = {
88
+ "rack.input" => StringIO.new(""),
89
+ "QUERY_STRING" => "a=b"
90
+ }
91
+ request = Rack::Request.new(env)
92
+ params = Committee::RequestUnpacker.new(request, allow_query_params: true).call
93
+ assert_equal({ "a" => "b" }, params)
94
+ end
95
+
75
96
  it "errors if JSON is not an object" do
76
97
  env = {
77
98
  "CONTENT_TYPE" => "application/json",
@@ -37,6 +37,18 @@ describe Committee::RequestValidator do
37
37
  assert_equal message, e.message
38
38
  end
39
39
 
40
+ it "detects an missing parameter in GET requests" do
41
+ # GET /apps/search?query=...
42
+ @link = @link = @schema.properties["app"].links[5]
43
+ @request = Rack::Request.new({})
44
+ e = assert_raises(Committee::InvalidRequest) do
45
+ call({})
46
+ end
47
+ message =
48
+ %{Invalid request.\n\n#: failed schema #/definitions/app/links/5/schema: "query" wasn't supplied.}
49
+ assert_equal message, e.message
50
+ end
51
+
40
52
  it "allows an invalid Content-Type with an empty body" do
41
53
  @request =
42
54
  Rack::Request.new({
@@ -53,7 +65,7 @@ describe Committee::RequestValidator do
53
65
  e = assert_raises(Committee::InvalidRequest) do
54
66
  call(data)
55
67
  end
56
- 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: %@!.}
68
+ message = %{Invalid request.\n\n#/name: failed schema #/definitions/app/links/0/schema/properties/name: %@! does not match /^[a-z][a-z0-9-]{3,30}$/.}
57
69
  assert_equal message, e.message
58
70
  end
59
71
 
@@ -2,10 +2,11 @@ require_relative "test_helper"
2
2
 
3
3
  describe Committee::ResponseValidator do
4
4
  before do
5
- @data = ValidApp.dup
5
+ @status = 200
6
6
  @headers = {
7
7
  "Content-Type" => "application/json"
8
8
  }
9
+ @data = ValidApp.dup
9
10
  @schema =
10
11
  JsonSchema.parse!(MultiJson.decode(File.read("./test/data/schema.json")))
11
12
  @schema.expand_references!
@@ -42,6 +43,11 @@ describe Committee::ResponseValidator do
42
43
  assert_equal message, e.message
43
44
  end
44
45
 
46
+ it "allows no Content-Type for 204 No Content" do
47
+ @status, @headers = 204, {}
48
+ call
49
+ end
50
+
45
51
  it "allows application/schema+json in responses as well" do
46
52
  @headers = { "Content-Type" => "application/schema+json" }
47
53
  call
@@ -50,13 +56,13 @@ describe Committee::ResponseValidator do
50
56
  it "raises errors generated by json_schema" do
51
57
  @data.merge!("name" => "%@!")
52
58
  e = assert_raises(Committee::InvalidResponse) { call }
53
- message = %{Invalid response.\n\n#/name: failed schema #/definitions/app/properties/name: Expected string to match pattern "/^[a-z][a-z0-9-]{3,30}$/", value was: %@!.}
59
+ message = %{Invalid response.\n\n#/name: failed schema #/definitions/app/properties/name: %@! does not match /^[a-z][a-z0-9-]{3,30}$/.}
54
60
  assert_equal message, e.message
55
61
  end
56
62
 
57
63
  private
58
64
 
59
65
  def call
60
- Committee::ResponseValidator.new(@link).call(@headers, @data)
66
+ Committee::ResponseValidator.new(@link).call(@status, @headers, @data)
61
67
  end
62
68
  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: 1.5.0
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brandur
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-07 00:00:00.000000000 Z
12
+ date: 2014-10-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json_schema