grape 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grape might be problematic. Click here for more details.
- checksums.yaml +8 -8
- data/.rubocop.yml +65 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +17 -1
- data/Gemfile +1 -0
- data/README.md +16 -8
- data/RELEASING.md +105 -0
- data/grape.gemspec +1 -1
- data/lib/grape.rb +1 -0
- data/lib/grape/api.rb +88 -54
- data/lib/grape/cookies.rb +4 -6
- data/lib/grape/endpoint.rb +81 -69
- data/lib/grape/error_formatter/base.rb +5 -4
- data/lib/grape/error_formatter/json.rb +3 -3
- data/lib/grape/error_formatter/txt.rb +1 -1
- data/lib/grape/error_formatter/xml.rb +4 -4
- data/lib/grape/exceptions/base.rb +7 -7
- data/lib/grape/exceptions/incompatible_option_values.rb +13 -0
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +1 -4
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/missing_option.rb +1 -1
- data/lib/grape/exceptions/missing_vendor_option.rb +1 -1
- data/lib/grape/exceptions/unknown_options.rb +1 -1
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/validation.rb +2 -2
- data/lib/grape/exceptions/validation_errors.rb +1 -1
- data/lib/grape/formatter/base.rb +5 -4
- data/lib/grape/formatter/serializable_hash.rb +7 -6
- data/lib/grape/http/request.rb +2 -2
- data/lib/grape/locale/en.yml +2 -0
- data/lib/grape/middleware/auth/base.rb +3 -3
- data/lib/grape/middleware/auth/basic.rb +1 -1
- data/lib/grape/middleware/auth/oauth2.rb +18 -20
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/middleware/error.rb +19 -19
- data/lib/grape/middleware/filter.rb +3 -3
- data/lib/grape/middleware/formatter.rb +29 -23
- data/lib/grape/middleware/versioner.rb +1 -1
- data/lib/grape/middleware/versioner/accept_version_header.rb +8 -6
- data/lib/grape/middleware/versioner/header.rb +16 -14
- data/lib/grape/middleware/versioner/param.rb +7 -7
- data/lib/grape/middleware/versioner/path.rb +7 -9
- data/lib/grape/parser/base.rb +3 -2
- data/lib/grape/path.rb +1 -1
- data/lib/grape/route.rb +6 -4
- data/lib/grape/util/content_types.rb +2 -1
- data/lib/grape/util/deep_merge.rb +5 -5
- data/lib/grape/util/hash_stack.rb +2 -2
- data/lib/grape/validations.rb +34 -30
- data/lib/grape/validations/coerce.rb +6 -5
- data/lib/grape/validations/default.rb +0 -1
- data/lib/grape/validations/presence.rb +1 -1
- data/lib/grape/validations/regexp.rb +2 -2
- data/lib/grape/validations/values.rb +16 -0
- data/lib/grape/version.rb +1 -1
- data/spec/grape/api_spec.rb +229 -210
- data/spec/grape/endpoint_spec.rb +56 -54
- data/spec/grape/entity_spec.rb +31 -33
- data/spec/grape/exceptions/missing_mime_type_spec.rb +3 -9
- data/spec/grape/middleware/auth/basic_spec.rb +8 -8
- data/spec/grape/middleware/auth/digest_spec.rb +5 -5
- data/spec/grape/middleware/auth/oauth2_spec.rb +23 -23
- data/spec/grape/middleware/base_spec.rb +6 -6
- data/spec/grape/middleware/error_spec.rb +11 -15
- data/spec/grape/middleware/exception_spec.rb +45 -25
- data/spec/grape/middleware/formatter_spec.rb +56 -45
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +25 -25
- data/spec/grape/middleware/versioner/header_spec.rb +54 -54
- data/spec/grape/middleware/versioner/param_spec.rb +17 -18
- data/spec/grape/middleware/versioner/path_spec.rb +6 -6
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/util/hash_stack_spec.rb +26 -27
- data/spec/grape/validations/coerce_spec.rb +39 -34
- data/spec/grape/validations/default_spec.rb +12 -13
- data/spec/grape/validations/presence_spec.rb +18 -22
- data/spec/grape/validations/regexp_spec.rb +9 -9
- data/spec/grape/validations/values_spec.rb +64 -0
- data/spec/grape/validations_spec.rb +127 -70
- data/spec/shared/versioning_examples.rb +5 -5
- data/spec/support/basic_auth_encode_helpers.rb +0 -1
- data/spec/support/versioned_helpers.rb +5 -6
- metadata +10 -4
@@ -1,16 +1,16 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Grape::Middleware::Formatter do
|
4
|
-
subject{ Grape::Middleware::Formatter.new(app) }
|
5
|
-
before{ subject.stub(:dup).and_return(subject) }
|
4
|
+
subject { Grape::Middleware::Formatter.new(app) }
|
5
|
+
before { subject.stub(:dup).and_return(subject) }
|
6
6
|
|
7
|
-
let(:app){ lambda{|env| [200, {}, [@body || { "foo" => "bar" }]]} }
|
7
|
+
let(:app) { lambda { |env| [200, {}, [@body || { "foo" => "bar" }]] } }
|
8
8
|
|
9
9
|
context 'serialization' do
|
10
10
|
it 'looks at the bodies for possibly serializable data' do
|
11
|
-
@body = {"abc" => "def"}
|
12
|
-
|
13
|
-
bodies.each{|b| b.should == MultiJson.dump(@body) }
|
11
|
+
@body = { "abc" => "def" }
|
12
|
+
_, _, bodies = *subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json')
|
13
|
+
bodies.each { |b| b.should == MultiJson.dump(@body) }
|
14
14
|
end
|
15
15
|
|
16
16
|
it 'calls #to_json since default format is json' do
|
@@ -21,7 +21,18 @@ describe Grape::Middleware::Formatter do
|
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
24
|
-
subject.call(
|
24
|
+
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/json').last.each { |b| b.should == '"bar"' }
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'calls #to_json if the content type is jsonapi' do
|
28
|
+
@body = { 'foos' => [{ 'bar' => 'baz' }] }
|
29
|
+
@body.instance_eval do
|
30
|
+
def to_json
|
31
|
+
"{\"foos\":[{\"bar\":\"baz\"}] }"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
subject.call('PATH_INFO' => '/somewhere', 'HTTP_ACCEPT' => 'application/vnd.api+json').last.each { |b| b.should == '{"foos":[{"bar":"baz"}] }' }
|
25
36
|
end
|
26
37
|
|
27
38
|
it 'calls #to_xml if the content type is xml' do
|
@@ -32,7 +43,7 @@ describe Grape::Middleware::Formatter do
|
|
32
43
|
end
|
33
44
|
end
|
34
45
|
|
35
|
-
subject.call(
|
46
|
+
subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json').last.each { |b| b.should == '<bar/>' }
|
36
47
|
end
|
37
48
|
end
|
38
49
|
|
@@ -46,7 +57,7 @@ describe Grape::Middleware::Formatter do
|
|
46
57
|
formatter.stub(:call) { raise Grape::Exceptions::InvalidFormatter.new(String, 'xml') }
|
47
58
|
|
48
59
|
expect {
|
49
|
-
catch(:error){subject.call(
|
60
|
+
catch(:error) { subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json') }
|
50
61
|
}.to_not raise_error
|
51
62
|
end
|
52
63
|
|
@@ -54,7 +65,7 @@ describe Grape::Middleware::Formatter do
|
|
54
65
|
formatter.stub(:call) { raise StandardError }
|
55
66
|
|
56
67
|
expect {
|
57
|
-
catch(:error){subject.call(
|
68
|
+
catch(:error) { subject.call('PATH_INFO' => '/somewhere.xml', 'HTTP_ACCEPT' => 'application/json') }
|
58
69
|
}.to raise_error
|
59
70
|
end
|
60
71
|
end
|
@@ -62,91 +73,91 @@ describe Grape::Middleware::Formatter do
|
|
62
73
|
context 'detection' do
|
63
74
|
|
64
75
|
it 'uses the extension if one is provided' do
|
65
|
-
subject.call(
|
76
|
+
subject.call('PATH_INFO' => '/info.xml')
|
66
77
|
subject.env['api.format'].should == :xml
|
67
|
-
subject.call(
|
78
|
+
subject.call('PATH_INFO' => '/info.json')
|
68
79
|
subject.env['api.format'].should == :json
|
69
80
|
end
|
70
81
|
|
71
82
|
it 'uses the format parameter if one is provided' do
|
72
|
-
subject.call(
|
83
|
+
subject.call('PATH_INFO' => '/info', 'QUERY_STRING' => 'format=json')
|
73
84
|
subject.env['api.format'].should == :json
|
74
|
-
subject.call(
|
85
|
+
subject.call('PATH_INFO' => '/info', 'QUERY_STRING' => 'format=xml')
|
75
86
|
subject.env['api.format'].should == :xml
|
76
87
|
end
|
77
88
|
|
78
89
|
it 'uses the default format if none is provided' do
|
79
|
-
subject.call(
|
90
|
+
subject.call('PATH_INFO' => '/info')
|
80
91
|
subject.env['api.format'].should == :txt
|
81
92
|
end
|
82
93
|
|
83
94
|
it 'uses the requested format if provided in headers' do
|
84
|
-
subject.call(
|
95
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json')
|
85
96
|
subject.env['api.format'].should == :json
|
86
97
|
end
|
87
98
|
|
88
99
|
it 'uses the file extension format if provided before headers' do
|
89
|
-
subject.call(
|
100
|
+
subject.call('PATH_INFO' => '/info.txt', 'HTTP_ACCEPT' => 'application/json')
|
90
101
|
subject.env['api.format'].should == :txt
|
91
102
|
end
|
92
103
|
end
|
93
104
|
|
94
105
|
context 'accept header detection' do
|
95
106
|
it 'detects from the Accept header' do
|
96
|
-
subject.call(
|
107
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/xml')
|
97
108
|
subject.env['api.format'].should == :xml
|
98
109
|
end
|
99
110
|
|
100
111
|
it 'looks for case-indifferent headers' do
|
101
|
-
subject.call(
|
112
|
+
subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml')
|
102
113
|
subject.env['api.format'].should == :xml
|
103
114
|
end
|
104
115
|
|
105
116
|
it 'uses quality rankings to determine formats' do
|
106
|
-
subject.call(
|
117
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; q=0.3,application/xml; q=1.0')
|
107
118
|
subject.env['api.format'].should == :xml
|
108
|
-
subject.call(
|
119
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; q=1.0,application/xml; q=0.3')
|
109
120
|
subject.env['api.format'].should == :json
|
110
121
|
end
|
111
122
|
|
112
123
|
it 'handles quality rankings mixed with nothing' do
|
113
|
-
subject.call(
|
124
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json,application/xml; q=1.0')
|
114
125
|
subject.env['api.format'].should == :xml
|
115
126
|
end
|
116
127
|
|
117
128
|
it 'parses headers with other attributes' do
|
118
|
-
subject.call(
|
129
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/json; abc=2.3; q=1.0,application/xml; q=0.7')
|
119
130
|
subject.env['api.format'].should == :json
|
120
131
|
end
|
121
132
|
|
122
133
|
it 'parses headers with vendor and api version' do
|
123
|
-
subject.call(
|
134
|
+
subject.call('PATH_INFO' => '/info', 'HTTP_ACCEPT' => 'application/vnd.test-v1+xml')
|
124
135
|
subject.env['api.format'].should == :xml
|
125
136
|
end
|
126
137
|
|
127
138
|
it 'parses headers with symbols as hash keys' do
|
128
|
-
subject.call(
|
139
|
+
subject.call('PATH_INFO' => '/info', 'http_accept' => 'application/xml', system_time: '091293')
|
129
140
|
subject.env[:system_time].should == '091293'
|
130
141
|
end
|
131
142
|
end
|
132
143
|
|
133
144
|
context 'content-type' do
|
134
145
|
it 'is set for json' do
|
135
|
-
_, headers, _ = subject.call(
|
146
|
+
_, headers, _ = subject.call('PATH_INFO' => '/info.json')
|
136
147
|
headers['Content-type'].should == 'application/json'
|
137
148
|
end
|
138
149
|
it 'is set for xml' do
|
139
|
-
_, headers, _ = subject.call(
|
150
|
+
_, headers, _ = subject.call('PATH_INFO' => '/info.xml')
|
140
151
|
headers['Content-type'].should == 'application/xml'
|
141
152
|
end
|
142
153
|
it 'is set for txt' do
|
143
|
-
_, headers, _ = subject.call(
|
154
|
+
_, headers, _ = subject.call('PATH_INFO' => '/info.txt')
|
144
155
|
headers['Content-type'].should == 'text/plain'
|
145
156
|
end
|
146
157
|
it 'is set for custom' do
|
147
158
|
subject.options[:content_types] = {}
|
148
159
|
subject.options[:content_types][:custom] = 'application/x-custom'
|
149
|
-
_, headers, _ = subject.call(
|
160
|
+
_, headers, _ = subject.call('PATH_INFO' => '/info.custom')
|
150
161
|
headers['Content-type'].should == 'application/x-custom'
|
151
162
|
end
|
152
163
|
end
|
@@ -156,34 +167,34 @@ describe Grape::Middleware::Formatter do
|
|
156
167
|
subject.options[:content_types] = {}
|
157
168
|
subject.options[:content_types][:custom] = "don't care"
|
158
169
|
subject.options[:formatters][:custom] = lambda { |obj, env| 'CUSTOM FORMAT' }
|
159
|
-
_, _, body = subject.call(
|
170
|
+
_, _, body = subject.call('PATH_INFO' => '/info.custom')
|
160
171
|
body.body.should == ['CUSTOM FORMAT']
|
161
172
|
end
|
162
173
|
it 'uses default json formatter' do
|
163
174
|
@body = ['blah']
|
164
|
-
_, _, body = subject.call(
|
175
|
+
_, _, body = subject.call('PATH_INFO' => '/info.json')
|
165
176
|
body.body.should == ['["blah"]']
|
166
177
|
end
|
167
178
|
it 'uses custom json formatter' do
|
168
179
|
subject.options[:formatters][:json] = lambda { |obj, env| 'CUSTOM JSON FORMAT' }
|
169
|
-
_, _, body = subject.call(
|
180
|
+
_, _, body = subject.call('PATH_INFO' => '/info.json')
|
170
181
|
body.body.should == ['CUSTOM JSON FORMAT']
|
171
182
|
end
|
172
183
|
end
|
173
184
|
|
174
185
|
context 'input' do
|
175
|
-
[
|
176
|
-
[
|
186
|
+
["POST", "PATCH", "PUT", "DELETE"].each do |method|
|
187
|
+
["application/json", "application/json; charset=utf-8"].each do |content_type|
|
177
188
|
context content_type do
|
178
189
|
it 'parses the body from #{method} and copies values into rack.request.form_hash' do
|
179
190
|
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
|
180
|
-
subject.call(
|
191
|
+
subject.call(
|
181
192
|
'PATH_INFO' => '/info',
|
182
193
|
'REQUEST_METHOD' => method,
|
183
194
|
'CONTENT_TYPE' => content_type,
|
184
195
|
'rack.input' => io,
|
185
196
|
'CONTENT_LENGTH' => io.length
|
186
|
-
|
197
|
+
)
|
187
198
|
subject.env['rack.request.form_hash']['is_boolean'].should be_true
|
188
199
|
subject.env['rack.request.form_hash']['string'].should == 'thing'
|
189
200
|
end
|
@@ -191,50 +202,50 @@ describe Grape::Middleware::Formatter do
|
|
191
202
|
end
|
192
203
|
it "parses the chunked body from #{method} and copies values into rack.request.from_hash" do
|
193
204
|
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
|
194
|
-
subject.call(
|
205
|
+
subject.call(
|
195
206
|
'PATH_INFO' => '/infol',
|
196
207
|
'REQUEST_METHOD' => method,
|
197
208
|
'CONTENT_TYPE' => 'application/json',
|
198
209
|
'rack.input' => io,
|
199
210
|
'HTTP_TRANSFER_ENCODING' => 'chunked'
|
200
|
-
|
211
|
+
)
|
201
212
|
subject.env['rack.request.form_hash']['is_boolean'].should be_true
|
202
213
|
subject.env['rack.request.form_hash']['string'].should == 'thing'
|
203
214
|
end
|
204
215
|
it "rewinds IO" do
|
205
216
|
io = StringIO.new('{"is_boolean":true,"string":"thing"}')
|
206
217
|
io.read
|
207
|
-
subject.call(
|
218
|
+
subject.call(
|
208
219
|
'PATH_INFO' => '/infol',
|
209
220
|
'REQUEST_METHOD' => method,
|
210
221
|
'CONTENT_TYPE' => 'application/json',
|
211
222
|
'rack.input' => io,
|
212
223
|
'HTTP_TRANSFER_ENCODING' => 'chunked'
|
213
|
-
|
224
|
+
)
|
214
225
|
subject.env['rack.request.form_hash']['is_boolean'].should be_true
|
215
226
|
subject.env['rack.request.form_hash']['string'].should == 'thing'
|
216
227
|
end
|
217
228
|
it 'parses the body from an xml #{method} and copies values into rack.request.from_hash' do
|
218
229
|
io = StringIO.new('<thing><name>Test</name></thing>')
|
219
|
-
subject.call(
|
230
|
+
subject.call(
|
220
231
|
'PATH_INFO' => '/info.xml',
|
221
232
|
'REQUEST_METHOD' => method,
|
222
233
|
'CONTENT_TYPE' => 'application/xml',
|
223
234
|
'rack.input' => io,
|
224
235
|
'CONTENT_LENGTH' => io.length
|
225
|
-
|
236
|
+
)
|
226
237
|
subject.env['rack.request.form_hash']['thing']['name'].should == 'Test'
|
227
238
|
end
|
228
|
-
[
|
239
|
+
[Rack::Request::FORM_DATA_MEDIA_TYPES, Rack::Request::PARSEABLE_DATA_MEDIA_TYPES].flatten.each do |content_type|
|
229
240
|
it "ignores #{content_type}" do
|
230
241
|
io = StringIO.new('name=Other+Test+Thing')
|
231
|
-
subject.call(
|
242
|
+
subject.call(
|
232
243
|
'PATH_INFO' => '/info',
|
233
244
|
'REQUEST_METHOD' => method,
|
234
245
|
'CONTENT_TYPE' => content_type,
|
235
246
|
'rack.input' => io,
|
236
247
|
'CONTENT_LENGTH' => io.length
|
237
|
-
|
248
|
+
)
|
238
249
|
subject.env['rack.request.form_hash'].should be_nil
|
239
250
|
end
|
240
251
|
end
|
@@ -1,13 +1,13 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Grape::Middleware::Versioner::AcceptVersionHeader do
|
4
|
-
let(:app) { lambda{|env| [200, env, env]} }
|
4
|
+
let(:app) { lambda { |env| [200, env, env] } }
|
5
5
|
subject { Grape::Middleware::Versioner::AcceptVersionHeader.new(app, @options || {}) }
|
6
6
|
|
7
7
|
before do
|
8
8
|
@options = {
|
9
|
-
:
|
10
|
-
:
|
9
|
+
version_options: {
|
10
|
+
using: :accept_version_header
|
11
11
|
},
|
12
12
|
}
|
13
13
|
end
|
@@ -18,25 +18,25 @@ describe Grape::Middleware::Versioner::AcceptVersionHeader do
|
|
18
18
|
end
|
19
19
|
|
20
20
|
it 'is set' do
|
21
|
-
status, _, env =
|
21
|
+
status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
|
22
22
|
env['api.version'].should eql 'v1'
|
23
23
|
status.should == 200
|
24
24
|
end
|
25
25
|
|
26
26
|
it 'is set if format provided' do
|
27
|
-
status, _, env =
|
27
|
+
status, _, env = subject.call('HTTP_ACCEPT_VERSION' => 'v1')
|
28
28
|
env['api.version'].should eql 'v1'
|
29
29
|
status.should == 200
|
30
30
|
end
|
31
31
|
|
32
32
|
it 'fails with 406 Not Acceptable if version is not supported' do
|
33
33
|
expect {
|
34
|
-
|
34
|
+
subject.call('HTTP_ACCEPT_VERSION' => 'v2').last
|
35
35
|
}.to throw_symbol(
|
36
36
|
:error,
|
37
|
-
:
|
38
|
-
:
|
39
|
-
:
|
37
|
+
status: 406,
|
38
|
+
headers: { 'X-Cascade' => 'pass' },
|
39
|
+
message: 'The requested version is not supported.'
|
40
40
|
)
|
41
41
|
end
|
42
42
|
end
|
@@ -60,23 +60,23 @@ describe Grape::Middleware::Versioner::AcceptVersionHeader do
|
|
60
60
|
|
61
61
|
it 'fails with 406 Not Acceptable if header is not set' do
|
62
62
|
expect {
|
63
|
-
|
63
|
+
subject.call({}).last
|
64
64
|
}.to throw_symbol(
|
65
65
|
:error,
|
66
|
-
:
|
67
|
-
:
|
68
|
-
:
|
66
|
+
status: 406,
|
67
|
+
headers: { 'X-Cascade' => 'pass' },
|
68
|
+
message: 'Accept-Version header must be set.'
|
69
69
|
)
|
70
70
|
end
|
71
71
|
|
72
72
|
it 'fails with 406 Not Acceptable if header is empty' do
|
73
73
|
expect {
|
74
|
-
|
74
|
+
subject.call('HTTP_ACCEPT_VERSION' => '').last
|
75
75
|
}.to throw_symbol(
|
76
76
|
:error,
|
77
|
-
:
|
78
|
-
:
|
79
|
-
:
|
77
|
+
status: 406,
|
78
|
+
headers: { 'X-Cascade' => 'pass' },
|
79
|
+
message: 'Accept-Version header must be set.'
|
80
80
|
)
|
81
81
|
end
|
82
82
|
|
@@ -94,23 +94,23 @@ describe Grape::Middleware::Versioner::AcceptVersionHeader do
|
|
94
94
|
|
95
95
|
it 'fails with 406 Not Acceptable if header is not set' do
|
96
96
|
expect {
|
97
|
-
|
97
|
+
subject.call({}).last
|
98
98
|
}.to throw_symbol(
|
99
99
|
:error,
|
100
|
-
:
|
101
|
-
:
|
102
|
-
:
|
100
|
+
status: 406,
|
101
|
+
headers: {},
|
102
|
+
message: 'Accept-Version header must be set.'
|
103
103
|
)
|
104
104
|
end
|
105
105
|
|
106
106
|
it 'fails with 406 Not Acceptable if header is empty' do
|
107
107
|
expect {
|
108
|
-
|
108
|
+
subject.call('HTTP_ACCEPT_VERSION' => '').last
|
109
109
|
}.to throw_symbol(
|
110
110
|
:error,
|
111
|
-
:
|
112
|
-
:
|
113
|
-
:
|
111
|
+
status: 406,
|
112
|
+
headers: {},
|
113
|
+
message: 'Accept-Version header must be set.'
|
114
114
|
)
|
115
115
|
end
|
116
116
|
|
@@ -1,14 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Grape::Middleware::Versioner::Header do
|
4
|
-
let(:app) { lambda{|env| [200, env, env]} }
|
4
|
+
let(:app) { lambda { |env| [200, env, env] } }
|
5
5
|
subject { Grape::Middleware::Versioner::Header.new(app, @options || {}) }
|
6
6
|
|
7
7
|
before do
|
8
8
|
@options = {
|
9
|
-
:
|
10
|
-
:
|
11
|
-
:
|
9
|
+
version_options: {
|
10
|
+
using: :header,
|
11
|
+
vendor: 'vendor',
|
12
12
|
},
|
13
13
|
}
|
14
14
|
end
|
@@ -49,10 +49,10 @@ describe Grape::Middleware::Versioner::Header do
|
|
49
49
|
status.should == 200
|
50
50
|
end
|
51
51
|
|
52
|
-
[
|
53
|
-
context 'when version is set to #{version{' do
|
52
|
+
['v1', :v1].each do |version|
|
53
|
+
context 'when version is set to #{version{ ' do
|
54
54
|
before do
|
55
|
-
@options[:versions] = [
|
55
|
+
@options[:versions] = [version]
|
56
56
|
end
|
57
57
|
|
58
58
|
it 'is set' do
|
@@ -85,12 +85,12 @@ describe Grape::Middleware::Versioner::Header do
|
|
85
85
|
|
86
86
|
it 'fails with 406 Not Acceptable if vendor is invalid' do
|
87
87
|
expect {
|
88
|
-
|
88
|
+
subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor+json').last
|
89
89
|
}.to throw_symbol(
|
90
90
|
:error,
|
91
|
-
:
|
92
|
-
:
|
93
|
-
:
|
91
|
+
status: 406,
|
92
|
+
headers: { 'X-Cascade' => 'pass' },
|
93
|
+
message: 'API vendor or version not found.'
|
94
94
|
)
|
95
95
|
end
|
96
96
|
|
@@ -113,12 +113,12 @@ describe Grape::Middleware::Versioner::Header do
|
|
113
113
|
|
114
114
|
it 'fails with 406 Not Acceptable if vendor is invalid' do
|
115
115
|
expect {
|
116
|
-
|
116
|
+
subject.call('HTTP_ACCEPT' => 'application/vnd.othervendor-v1+json').last
|
117
117
|
}.to throw_symbol(
|
118
118
|
:error,
|
119
|
-
:
|
120
|
-
:
|
121
|
-
:
|
119
|
+
status: 406,
|
120
|
+
headers: { 'X-Cascade' => 'pass' },
|
121
|
+
message: 'API vendor or version not found.'
|
122
122
|
)
|
123
123
|
end
|
124
124
|
end
|
@@ -143,12 +143,12 @@ describe Grape::Middleware::Versioner::Header do
|
|
143
143
|
|
144
144
|
it 'fails with 406 Not Acceptable if version is invalid' do
|
145
145
|
expect {
|
146
|
-
|
146
|
+
subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v2+json').last
|
147
147
|
}.to throw_symbol(
|
148
148
|
:error,
|
149
|
-
:
|
150
|
-
:
|
151
|
-
:
|
149
|
+
status: 406,
|
150
|
+
headers: { 'X-Cascade' => 'pass' },
|
151
|
+
message: 'API vendor or version not found.'
|
152
152
|
)
|
153
153
|
end
|
154
154
|
end
|
@@ -172,45 +172,45 @@ describe Grape::Middleware::Versioner::Header do
|
|
172
172
|
|
173
173
|
it 'fails with 406 Not Acceptable if header is not set' do
|
174
174
|
expect {
|
175
|
-
|
175
|
+
subject.call({}).last
|
176
176
|
}.to throw_symbol(
|
177
177
|
:error,
|
178
|
-
:
|
179
|
-
:
|
180
|
-
:
|
178
|
+
status: 406,
|
179
|
+
headers: { 'X-Cascade' => 'pass' },
|
180
|
+
message: 'Accept header must be set.'
|
181
181
|
)
|
182
182
|
end
|
183
183
|
|
184
184
|
it 'fails with 406 Not Acceptable if header is empty' do
|
185
185
|
expect {
|
186
|
-
|
186
|
+
subject.call('HTTP_ACCEPT' => '').last
|
187
187
|
}.to throw_symbol(
|
188
188
|
:error,
|
189
|
-
:
|
190
|
-
:
|
191
|
-
:
|
189
|
+
status: 406,
|
190
|
+
headers: { 'X-Cascade' => 'pass' },
|
191
|
+
message: 'Accept header must be set.'
|
192
192
|
)
|
193
193
|
end
|
194
194
|
|
195
195
|
it 'fails with 406 Not Acceptable if type is a range' do
|
196
196
|
expect {
|
197
|
-
|
197
|
+
subject.call('HTTP_ACCEPT' => '*/*').last
|
198
198
|
}.to throw_symbol(
|
199
199
|
:error,
|
200
|
-
:
|
201
|
-
:
|
202
|
-
:
|
200
|
+
status: 406,
|
201
|
+
headers: { 'X-Cascade' => 'pass' },
|
202
|
+
message: 'Accept header must not contain ranges ("*").'
|
203
203
|
)
|
204
204
|
end
|
205
205
|
|
206
206
|
it 'fails with 406 Not Acceptable if subtype is a range' do
|
207
207
|
expect {
|
208
|
-
|
208
|
+
subject.call('HTTP_ACCEPT' => 'application/*').last
|
209
209
|
}.to throw_symbol(
|
210
210
|
:error,
|
211
|
-
:
|
212
|
-
:
|
213
|
-
:
|
211
|
+
status: 406,
|
212
|
+
headers: { 'X-Cascade' => 'pass' },
|
213
|
+
message: 'Accept header must not contain ranges ("*").'
|
214
214
|
)
|
215
215
|
end
|
216
216
|
|
@@ -228,45 +228,45 @@ describe Grape::Middleware::Versioner::Header do
|
|
228
228
|
|
229
229
|
it 'fails with 406 Not Acceptable if header is not set' do
|
230
230
|
expect {
|
231
|
-
|
231
|
+
subject.call({}).last
|
232
232
|
}.to throw_symbol(
|
233
233
|
:error,
|
234
|
-
:
|
235
|
-
:
|
236
|
-
:
|
234
|
+
status: 406,
|
235
|
+
headers: {},
|
236
|
+
message: 'Accept header must be set.'
|
237
237
|
)
|
238
238
|
end
|
239
239
|
|
240
240
|
it 'fails with 406 Not Acceptable if header is empty' do
|
241
241
|
expect {
|
242
|
-
|
242
|
+
subject.call('HTTP_ACCEPT' => '').last
|
243
243
|
}.to throw_symbol(
|
244
244
|
:error,
|
245
|
-
:
|
246
|
-
:
|
247
|
-
:
|
245
|
+
status: 406,
|
246
|
+
headers: {},
|
247
|
+
message: 'Accept header must be set.'
|
248
248
|
)
|
249
249
|
end
|
250
250
|
|
251
251
|
it 'fails with 406 Not Acceptable if type is a range' do
|
252
252
|
expect {
|
253
|
-
|
253
|
+
subject.call('HTTP_ACCEPT' => '*/*').last
|
254
254
|
}.to throw_symbol(
|
255
255
|
:error,
|
256
|
-
:
|
257
|
-
:
|
258
|
-
:
|
256
|
+
status: 406,
|
257
|
+
headers: {},
|
258
|
+
message: 'Accept header must not contain ranges ("*").'
|
259
259
|
)
|
260
260
|
end
|
261
261
|
|
262
262
|
it 'fails with 406 Not Acceptable if subtype is a range' do
|
263
263
|
expect {
|
264
|
-
|
264
|
+
subject.call('HTTP_ACCEPT' => 'application/*').last
|
265
265
|
}.to throw_symbol(
|
266
266
|
:error,
|
267
|
-
:
|
268
|
-
:
|
269
|
-
:
|
267
|
+
status: 406,
|
268
|
+
headers: {},
|
269
|
+
message: 'Accept header must not contain ranges ("*").'
|
270
270
|
)
|
271
271
|
end
|
272
272
|
|
@@ -293,9 +293,9 @@ describe Grape::Middleware::Versioner::Header do
|
|
293
293
|
subject.call('HTTP_ACCEPT' => 'application/vnd.vendor-v3+json')
|
294
294
|
}.to throw_symbol(
|
295
295
|
:error,
|
296
|
-
:
|
297
|
-
:
|
298
|
-
:
|
296
|
+
status: 406,
|
297
|
+
headers: { 'X-Cascade' => 'pass' },
|
298
|
+
message: 'API vendor or version not found.'
|
299
299
|
)
|
300
300
|
end
|
301
301
|
end
|