grape 0.5.0 → 3.2.1
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +1223 -164
- data/CONTRIBUTING.md +148 -0
- data/LICENSE +1 -1
- data/README.md +3531 -675
- data/UPGRADING.md +1851 -0
- data/grape.gemspec +26 -32
- data/grape.png +0 -0
- data/lib/grape/api/instance.rb +194 -0
- data/lib/grape/api.rb +110 -485
- data/lib/grape/content_types.rb +28 -0
- data/lib/grape/cookies.rb +32 -25
- data/lib/grape/declared_params_handler.rb +118 -0
- data/lib/grape/dry_types.rb +54 -0
- data/lib/grape/dsl/callbacks.rb +19 -0
- data/lib/grape/dsl/declared.rb +35 -0
- data/lib/grape/dsl/desc.rb +65 -0
- data/lib/grape/dsl/headers.rb +21 -0
- data/lib/grape/dsl/helpers.rb +103 -0
- data/lib/grape/dsl/inside_route.rb +293 -0
- data/lib/grape/dsl/logger.rb +19 -0
- data/lib/grape/dsl/middleware.rb +36 -0
- data/lib/grape/dsl/parameters.rb +239 -0
- data/lib/grape/dsl/request_response.rb +168 -0
- data/lib/grape/dsl/routing.rb +288 -0
- data/lib/grape/dsl/settings.rb +67 -0
- data/lib/grape/dsl/validations.rb +51 -0
- data/lib/grape/endpoint.rb +287 -386
- data/lib/grape/env.rb +20 -0
- data/lib/grape/error_formatter/base.rb +56 -19
- data/lib/grape/error_formatter/json.rb +18 -7
- data/lib/grape/error_formatter/serializable_hash.rb +7 -0
- data/lib/grape/error_formatter/txt.rb +14 -11
- data/lib/grape/error_formatter/xml.rb +5 -11
- data/lib/grape/error_formatter.rb +15 -0
- data/lib/grape/exceptions/base.rb +31 -47
- data/lib/grape/exceptions/incompatible_option_values.rb +11 -0
- data/lib/grape/exceptions/invalid_accept_header.rb +11 -0
- data/lib/grape/exceptions/invalid_formatter.rb +3 -5
- data/lib/grape/exceptions/invalid_message_body.rb +11 -0
- data/lib/grape/exceptions/invalid_response.rb +11 -0
- data/lib/grape/exceptions/invalid_version_header.rb +11 -0
- data/lib/grape/exceptions/invalid_versioner_option.rb +3 -6
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +3 -7
- data/lib/grape/exceptions/method_not_allowed.rb +11 -0
- data/lib/grape/exceptions/missing_group_type.rb +11 -0
- data/lib/grape/exceptions/missing_mime_type.rb +3 -6
- data/lib/grape/exceptions/missing_vendor_option.rb +3 -5
- data/lib/grape/exceptions/request_error.rb +11 -0
- data/lib/grape/exceptions/unknown_auth_strategy.rb +11 -0
- data/lib/grape/exceptions/unknown_parameter.rb +11 -0
- data/lib/grape/exceptions/unknown_params_builder.rb +11 -0
- data/lib/grape/exceptions/unknown_validator.rb +3 -4
- data/lib/grape/exceptions/unsupported_group_type.rb +11 -0
- data/lib/grape/exceptions/validation.rb +20 -8
- data/lib/grape/exceptions/validation_array_errors.rb +14 -0
- data/lib/grape/exceptions/validation_errors.rb +59 -0
- data/lib/grape/formatter/base.rb +9 -26
- data/lib/grape/formatter/json.rb +6 -7
- data/lib/grape/formatter/serializable_hash.rb +24 -20
- data/lib/grape/formatter/txt.rb +5 -7
- data/lib/grape/formatter/xml.rb +6 -7
- data/lib/grape/formatter.rb +17 -0
- data/lib/grape/json.rb +10 -0
- data/lib/grape/locale/en.yml +47 -20
- data/lib/grape/middleware/auth/base.rb +25 -0
- data/lib/grape/middleware/auth/dsl.rb +38 -0
- data/lib/grape/middleware/auth/strategies.rb +25 -0
- data/lib/grape/middleware/auth/strategy_info.rb +15 -0
- data/lib/grape/middleware/base.rb +73 -19
- data/lib/grape/middleware/error.rb +104 -44
- data/lib/grape/middleware/filter.rb +5 -3
- data/lib/grape/middleware/formatter.rb +115 -105
- data/lib/grape/middleware/globals.rb +14 -0
- data/lib/grape/middleware/stack.rb +110 -0
- data/lib/grape/middleware/versioner/accept_version_header.rb +11 -40
- data/lib/grape/middleware/versioner/base.rb +76 -0
- data/lib/grape/middleware/versioner/header.rb +61 -78
- data/lib/grape/middleware/versioner/param.rb +9 -21
- data/lib/grape/middleware/versioner/path.rb +16 -31
- data/lib/grape/middleware/versioner.rb +10 -15
- data/lib/grape/namespace.rb +39 -10
- data/lib/grape/params_builder/base.rb +20 -0
- data/lib/grape/params_builder/hash.rb +11 -0
- data/lib/grape/params_builder/hash_with_indifferent_access.rb +11 -0
- data/lib/grape/params_builder/hashie_mash.rb +11 -0
- data/lib/grape/params_builder.rb +15 -0
- data/lib/grape/parser/base.rb +9 -24
- data/lib/grape/parser/json.rb +8 -7
- data/lib/grape/parser/xml.rb +8 -7
- data/lib/grape/parser.rb +15 -0
- data/lib/grape/path.rb +76 -0
- data/lib/grape/presenters/presenter.rb +11 -0
- data/lib/grape/railtie.rb +9 -0
- data/lib/grape/request.rb +182 -0
- data/lib/grape/router/base_route.rb +48 -0
- data/lib/grape/router/greedy_route.rb +26 -0
- data/lib/grape/router/pattern.rb +67 -0
- data/lib/grape/router/route.rb +58 -0
- data/lib/grape/router.rb +167 -0
- data/lib/grape/serve_stream/file_body.rb +36 -0
- data/lib/grape/serve_stream/sendfile_response.rb +21 -0
- data/lib/grape/serve_stream/stream_response.rb +23 -0
- data/lib/grape/util/api_description.rb +58 -0
- data/lib/grape/util/base_inheritable.rb +46 -0
- data/lib/grape/util/cache.rb +18 -0
- data/lib/grape/util/deep_freeze.rb +35 -0
- data/lib/grape/util/endpoint_configuration.rb +8 -0
- data/lib/grape/util/header.rb +13 -0
- data/lib/grape/util/inheritable_setting.rb +107 -0
- data/lib/grape/util/inheritable_values.rb +29 -0
- data/lib/grape/util/lazy/block.rb +29 -0
- data/lib/grape/util/lazy/value.rb +38 -0
- data/lib/grape/util/lazy/value_array.rb +21 -0
- data/lib/grape/util/lazy/value_enumerable.rb +34 -0
- data/lib/grape/util/lazy/value_hash.rb +21 -0
- data/lib/grape/util/media_type.rb +70 -0
- data/lib/grape/util/registry.rb +27 -0
- data/lib/grape/util/reverse_stackable_values.rb +15 -0
- data/lib/grape/util/stackable_values.rb +36 -0
- data/lib/grape/util/translation.rb +42 -0
- data/lib/grape/validations/attributes_iterator.rb +77 -0
- data/lib/grape/validations/contract_scope.rb +28 -0
- data/lib/grape/validations/multiple_attributes_iterator.rb +13 -0
- data/lib/grape/validations/param_scope_tracker.rb +57 -0
- data/lib/grape/validations/params_documentation.rb +50 -0
- data/lib/grape/validations/params_scope.rb +529 -0
- data/lib/grape/validations/single_attribute_iterator.rb +26 -0
- data/lib/grape/validations/types/array_coercer.rb +60 -0
- data/lib/grape/validations/types/custom_type_coercer.rb +164 -0
- data/lib/grape/validations/types/custom_type_collection_coercer.rb +56 -0
- data/lib/grape/validations/types/dry_type_coercer.rb +59 -0
- data/lib/grape/validations/types/file.rb +31 -0
- data/lib/grape/validations/types/invalid_value.rb +17 -0
- data/lib/grape/validations/types/json.rb +71 -0
- data/lib/grape/validations/types/multiple_type_coercer.rb +57 -0
- data/lib/grape/validations/types/primitive_coercer.rb +46 -0
- data/lib/grape/validations/types/set_coercer.rb +35 -0
- data/lib/grape/validations/types/variant_collection_coercer.rb +51 -0
- data/lib/grape/validations/types.rb +198 -0
- data/lib/grape/validations/validators/all_or_none_of_validator.rb +19 -0
- data/lib/grape/validations/validators/allow_blank_validator.rb +25 -0
- data/lib/grape/validations/validators/as_validator.rb +14 -0
- data/lib/grape/validations/validators/at_least_one_of_validator.rb +18 -0
- data/lib/grape/validations/validators/base.rb +158 -0
- data/lib/grape/validations/validators/coerce_validator.rb +55 -0
- data/lib/grape/validations/validators/contract_scope_validator.rb +45 -0
- data/lib/grape/validations/validators/default_validator.rb +31 -0
- data/lib/grape/validations/validators/exactly_one_of_validator.rb +24 -0
- data/lib/grape/validations/validators/except_values_validator.rb +33 -0
- data/lib/grape/validations/validators/length_validator.rb +48 -0
- data/lib/grape/validations/validators/multiple_params_base.rb +34 -0
- data/lib/grape/validations/validators/mutually_exclusive_validator.rb +18 -0
- data/lib/grape/validations/validators/presence_validator.rb +17 -0
- data/lib/grape/validations/validators/regexp_validator.rb +24 -0
- data/lib/grape/validations/validators/same_as_validator.rb +20 -0
- data/lib/grape/validations/validators/values_validator.rb +68 -0
- data/lib/grape/validations.rb +10 -226
- data/lib/grape/version.rb +4 -1
- data/lib/grape/xml.rb +10 -0
- data/lib/grape.rb +79 -77
- metadata +160 -306
- data/.gitignore +0 -38
- data/.rspec +0 -2
- data/.travis.yml +0 -9
- data/.yardopts +0 -2
- data/Gemfile +0 -19
- data/Guardfile +0 -15
- data/Rakefile +0 -64
- data/lib/grape/exceptions/missing_option.rb +0 -13
- data/lib/grape/exceptions/unknown_options.rb +0 -14
- data/lib/grape/http/request.rb +0 -28
- data/lib/grape/middleware/auth/basic.rb +0 -30
- data/lib/grape/middleware/auth/digest.rb +0 -30
- data/lib/grape/middleware/auth/oauth2.rb +0 -72
- data/lib/grape/route.rb +0 -23
- data/lib/grape/util/content_types.rb +0 -17
- data/lib/grape/util/deep_merge.rb +0 -23
- data/lib/grape/util/hash_stack.rb +0 -120
- data/lib/grape/validations/coerce.rb +0 -62
- data/lib/grape/validations/default.rb +0 -24
- data/lib/grape/validations/presence.rb +0 -12
- data/lib/grape/validations/regexp.rb +0 -14
- data/spec/grape/api_spec.rb +0 -2129
- data/spec/grape/endpoint_spec.rb +0 -605
- data/spec/grape/entity_spec.rb +0 -281
- data/spec/grape/exceptions/invalid_formatter_spec.rb +0 -18
- data/spec/grape/exceptions/invalid_versioner_option_spec.rb +0 -18
- data/spec/grape/exceptions/missing_mime_type_spec.rb +0 -24
- data/spec/grape/exceptions/missing_option_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_options_spec.rb +0 -18
- data/spec/grape/exceptions/unknown_validator_spec.rb +0 -18
- data/spec/grape/middleware/auth/basic_spec.rb +0 -31
- data/spec/grape/middleware/auth/digest_spec.rb +0 -47
- data/spec/grape/middleware/auth/oauth2_spec.rb +0 -90
- data/spec/grape/middleware/base_spec.rb +0 -63
- data/spec/grape/middleware/error_spec.rb +0 -49
- data/spec/grape/middleware/exception_spec.rb +0 -164
- data/spec/grape/middleware/formatter_spec.rb +0 -244
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +0 -121
- data/spec/grape/middleware/versioner/header_spec.rb +0 -277
- data/spec/grape/middleware/versioner/param_spec.rb +0 -58
- data/spec/grape/middleware/versioner/path_spec.rb +0 -44
- data/spec/grape/middleware/versioner_spec.rb +0 -22
- data/spec/grape/util/hash_stack_spec.rb +0 -133
- data/spec/grape/validations/coerce_spec.rb +0 -143
- data/spec/grape/validations/default_spec.rb +0 -67
- data/spec/grape/validations/presence_spec.rb +0 -144
- data/spec/grape/validations/regexp_spec.rb +0 -33
- data/spec/grape/validations/zh-CN.yml +0 -9
- data/spec/grape/validations_spec.rb +0 -217
- data/spec/shared/versioning_examples.rb +0 -121
- data/spec/spec_helper.rb +0 -25
- data/spec/support/basic_auth_encode_helpers.rb +0 -4
- data/spec/support/versioned_helpers.rb +0 -48
data/spec/grape/api_spec.rb
DELETED
|
@@ -1,2129 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'shared/versioning_examples'
|
|
3
|
-
|
|
4
|
-
describe Grape::API do
|
|
5
|
-
subject { Class.new(Grape::API) }
|
|
6
|
-
|
|
7
|
-
def app; subject end
|
|
8
|
-
|
|
9
|
-
describe '.prefix' do
|
|
10
|
-
|
|
11
|
-
it 'routes root through with the prefix' do
|
|
12
|
-
subject.prefix 'awesome/sauce'
|
|
13
|
-
subject.get do
|
|
14
|
-
"Hello there."
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
get 'awesome/sauce/'
|
|
18
|
-
last_response.body.should eql "Hello there."
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
it 'routes through with the prefix' do
|
|
22
|
-
subject.prefix 'awesome/sauce'
|
|
23
|
-
subject.get :hello do
|
|
24
|
-
"Hello there."
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
get 'awesome/sauce/hello'
|
|
28
|
-
last_response.body.should eql "Hello there."
|
|
29
|
-
|
|
30
|
-
get '/hello'
|
|
31
|
-
last_response.status.should eql 404
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
describe '.version' do
|
|
37
|
-
context 'when defined' do
|
|
38
|
-
it 'returns version value' do
|
|
39
|
-
subject.version 'v1'
|
|
40
|
-
subject.version.should == 'v1'
|
|
41
|
-
end
|
|
42
|
-
end
|
|
43
|
-
|
|
44
|
-
context 'when not defined' do
|
|
45
|
-
it 'returns nil' do
|
|
46
|
-
subject.version.should be_nil
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
describe '.version using path' do
|
|
52
|
-
it_should_behave_like 'versioning' do
|
|
53
|
-
let(:macro_options) do
|
|
54
|
-
{
|
|
55
|
-
:using => :path
|
|
56
|
-
}
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
describe '.version using param' do
|
|
62
|
-
it_should_behave_like 'versioning' do
|
|
63
|
-
let(:macro_options) do
|
|
64
|
-
{
|
|
65
|
-
:using => :param,
|
|
66
|
-
:parameter => "apiver"
|
|
67
|
-
}
|
|
68
|
-
end
|
|
69
|
-
end
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
describe '.version using header' do
|
|
73
|
-
it_should_behave_like 'versioning' do
|
|
74
|
-
let(:macro_options) do
|
|
75
|
-
{
|
|
76
|
-
:using => :header,
|
|
77
|
-
:vendor => 'mycompany',
|
|
78
|
-
:format => 'json'
|
|
79
|
-
}
|
|
80
|
-
end
|
|
81
|
-
end
|
|
82
|
-
|
|
83
|
-
# Behavior as defined by rfc2616 when no header is defined
|
|
84
|
-
# http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
|
|
85
|
-
describe 'no specified accept header' do
|
|
86
|
-
# subject.version 'v1', :using => :header
|
|
87
|
-
# subject.get '/hello' do
|
|
88
|
-
# 'hello'
|
|
89
|
-
# end
|
|
90
|
-
|
|
91
|
-
# it 'routes' do
|
|
92
|
-
# get '/hello'
|
|
93
|
-
# last_response.status.should eql 200
|
|
94
|
-
# end
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
# pending 'routes if any media type is allowed'
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
describe '.version using accept_version_header' do
|
|
101
|
-
it_should_behave_like 'versioning' do
|
|
102
|
-
let(:macro_options) do
|
|
103
|
-
{
|
|
104
|
-
:using => :accept_version_header
|
|
105
|
-
}
|
|
106
|
-
end
|
|
107
|
-
end
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
describe '.represent' do
|
|
111
|
-
it 'requires a :with option' do
|
|
112
|
-
expect{ subject.represent Object, {} }.to raise_error(Grape::Exceptions::InvalidWithOptionForRepresent)
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
it 'adds the association to the :representations setting' do
|
|
116
|
-
klass = Class.new
|
|
117
|
-
subject.represent Object, :with => klass
|
|
118
|
-
subject.settings[:representations][Object].should == klass
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
describe '.namespace' do
|
|
123
|
-
it 'is retrievable and converted to a path' do
|
|
124
|
-
subject.namespace :awesome do
|
|
125
|
-
namespace.should == '/awesome'
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
it 'comes after the prefix and version' do
|
|
130
|
-
subject.prefix :rad
|
|
131
|
-
subject.version 'v1', :using => :path
|
|
132
|
-
|
|
133
|
-
subject.namespace :awesome do
|
|
134
|
-
get('/hello'){ "worked" }
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
get "/rad/v1/awesome/hello"
|
|
138
|
-
last_response.body.should == "worked"
|
|
139
|
-
end
|
|
140
|
-
|
|
141
|
-
it 'cancels itself after the block is over' do
|
|
142
|
-
subject.namespace :awesome do
|
|
143
|
-
namespace.should == '/awesome'
|
|
144
|
-
end
|
|
145
|
-
|
|
146
|
-
subject.namespace.should == '/'
|
|
147
|
-
end
|
|
148
|
-
|
|
149
|
-
it 'is stackable' do
|
|
150
|
-
subject.namespace :awesome do
|
|
151
|
-
namespace :rad do
|
|
152
|
-
namespace.should == '/awesome/rad'
|
|
153
|
-
end
|
|
154
|
-
namespace.should == '/awesome'
|
|
155
|
-
end
|
|
156
|
-
subject.namespace.should == '/'
|
|
157
|
-
end
|
|
158
|
-
|
|
159
|
-
it 'accepts path segments correctly' do
|
|
160
|
-
subject.namespace :members do
|
|
161
|
-
namespace '/:member_id' do
|
|
162
|
-
namespace.should == '/members/:member_id'
|
|
163
|
-
get '/' do
|
|
164
|
-
params[:member_id]
|
|
165
|
-
end
|
|
166
|
-
end
|
|
167
|
-
end
|
|
168
|
-
get '/members/23'
|
|
169
|
-
last_response.body.should == "23"
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
it 'is callable with nil just to push onto the stack' do
|
|
173
|
-
subject.namespace do
|
|
174
|
-
version 'v2', :using => :path
|
|
175
|
-
get('/hello'){ "inner" }
|
|
176
|
-
end
|
|
177
|
-
subject.get('/hello'){ "outer" }
|
|
178
|
-
|
|
179
|
-
get '/v2/hello'
|
|
180
|
-
last_response.body.should == "inner"
|
|
181
|
-
get '/hello'
|
|
182
|
-
last_response.body.should == "outer"
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
%w(group resource resources segment).each do |als|
|
|
186
|
-
it '`.#{als}` is an alias' do
|
|
187
|
-
subject.send(als, :awesome) do
|
|
188
|
-
namespace.should == "/awesome"
|
|
189
|
-
end
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
describe '.route_param' do
|
|
195
|
-
it 'adds a parameterized route segment namespace' do
|
|
196
|
-
subject.namespace :users do
|
|
197
|
-
route_param :id do
|
|
198
|
-
get do
|
|
199
|
-
params[:id]
|
|
200
|
-
end
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
|
|
204
|
-
get '/users/23'
|
|
205
|
-
last_response.body.should == '23'
|
|
206
|
-
end
|
|
207
|
-
|
|
208
|
-
it 'should be able to define requirements with a single hash' do
|
|
209
|
-
subject.namespace :users do
|
|
210
|
-
route_param :id, :requirements => /[0-9]+/ do
|
|
211
|
-
get do
|
|
212
|
-
params[:id]
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
get '/users/michael'
|
|
218
|
-
last_response.status.should == 404
|
|
219
|
-
get '/users/23'
|
|
220
|
-
last_response.status.should == 200
|
|
221
|
-
end
|
|
222
|
-
end
|
|
223
|
-
|
|
224
|
-
describe '.route' do
|
|
225
|
-
it 'allows for no path' do
|
|
226
|
-
subject.namespace :votes do
|
|
227
|
-
get do
|
|
228
|
-
"Votes"
|
|
229
|
-
end
|
|
230
|
-
post do
|
|
231
|
-
"Created a Vote"
|
|
232
|
-
end
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
get '/votes'
|
|
236
|
-
last_response.body.should eql 'Votes'
|
|
237
|
-
post '/votes'
|
|
238
|
-
last_response.body.should eql 'Created a Vote'
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
it 'handles empty calls' do
|
|
242
|
-
subject.get "/"
|
|
243
|
-
get "/"
|
|
244
|
-
last_response.body.should eql ""
|
|
245
|
-
end
|
|
246
|
-
|
|
247
|
-
describe 'root routes should work with' do
|
|
248
|
-
before do
|
|
249
|
-
subject.format :txt
|
|
250
|
-
def subject.enable_root_route!
|
|
251
|
-
self.get("/") { "root" }
|
|
252
|
-
end
|
|
253
|
-
end
|
|
254
|
-
|
|
255
|
-
after do
|
|
256
|
-
last_response.body.should eql "root"
|
|
257
|
-
end
|
|
258
|
-
|
|
259
|
-
describe 'path versioned APIs' do
|
|
260
|
-
before do
|
|
261
|
-
subject.version 'v1', :using => :path
|
|
262
|
-
subject.enable_root_route!
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
it 'without a format' do
|
|
266
|
-
versioned_get "/", "v1", :using => :path
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
it 'with a format' do
|
|
270
|
-
get "/v1/.json"
|
|
271
|
-
end
|
|
272
|
-
end
|
|
273
|
-
|
|
274
|
-
it 'header versioned APIs' do
|
|
275
|
-
subject.version 'v1', :using => :header, :vendor => 'test'
|
|
276
|
-
subject.enable_root_route!
|
|
277
|
-
|
|
278
|
-
versioned_get "/", "v1", :using => :header
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
it 'param versioned APIs' do
|
|
282
|
-
subject.version 'v1', :using => :param
|
|
283
|
-
subject.enable_root_route!
|
|
284
|
-
|
|
285
|
-
versioned_get "/", "v1", :using => :param
|
|
286
|
-
end
|
|
287
|
-
|
|
288
|
-
it 'Accept-Version header versioned APIs' do
|
|
289
|
-
subject.version 'v1', :using => :accept_version_header
|
|
290
|
-
subject.enable_root_route!
|
|
291
|
-
|
|
292
|
-
versioned_get "/", "v1", :using => :accept_version_header
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
it 'unversioned APIs' do
|
|
296
|
-
subject.enable_root_route!
|
|
297
|
-
|
|
298
|
-
get "/"
|
|
299
|
-
end
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
it 'allows for multiple paths' do
|
|
303
|
-
subject.get(["/abc", "/def"]) do
|
|
304
|
-
"foo"
|
|
305
|
-
end
|
|
306
|
-
|
|
307
|
-
get '/abc'
|
|
308
|
-
last_response.body.should eql 'foo'
|
|
309
|
-
get '/def'
|
|
310
|
-
last_response.body.should eql 'foo'
|
|
311
|
-
end
|
|
312
|
-
|
|
313
|
-
context 'format' do
|
|
314
|
-
before(:each) do
|
|
315
|
-
subject.get("/abc") do
|
|
316
|
-
RSpec::Mocks::Mock.new(:to_json => 'abc', :to_txt => 'def')
|
|
317
|
-
end
|
|
318
|
-
end
|
|
319
|
-
|
|
320
|
-
it 'allows .json' do
|
|
321
|
-
get '/abc.json'
|
|
322
|
-
last_response.status.should == 200
|
|
323
|
-
last_response.body.should eql 'abc' # json-encoded symbol
|
|
324
|
-
end
|
|
325
|
-
|
|
326
|
-
it 'allows .txt' do
|
|
327
|
-
get '/abc.txt'
|
|
328
|
-
last_response.status.should == 200
|
|
329
|
-
last_response.body.should eql 'def' # raw text
|
|
330
|
-
end
|
|
331
|
-
end
|
|
332
|
-
|
|
333
|
-
it 'allows for format without corrupting a param' do
|
|
334
|
-
subject.get('/:id') do
|
|
335
|
-
{"id" => params[:id]}
|
|
336
|
-
end
|
|
337
|
-
|
|
338
|
-
get '/awesome.json'
|
|
339
|
-
last_response.body.should eql '{"id":"awesome"}'
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
it 'allows for format in namespace with no path' do
|
|
343
|
-
subject.namespace :abc do
|
|
344
|
-
get do
|
|
345
|
-
["json"]
|
|
346
|
-
end
|
|
347
|
-
end
|
|
348
|
-
|
|
349
|
-
get '/abc.json'
|
|
350
|
-
last_response.body.should eql '["json"]'
|
|
351
|
-
end
|
|
352
|
-
|
|
353
|
-
it 'allows for multiple verbs' do
|
|
354
|
-
subject.route([:get, :post], '/abc') do
|
|
355
|
-
"hiya"
|
|
356
|
-
end
|
|
357
|
-
|
|
358
|
-
subject.endpoints.first.routes.each do |route|
|
|
359
|
-
route.route_path.should eql '/abc(.:format)'
|
|
360
|
-
end
|
|
361
|
-
|
|
362
|
-
get '/abc'
|
|
363
|
-
last_response.body.should eql 'hiya'
|
|
364
|
-
post '/abc'
|
|
365
|
-
last_response.body.should eql 'hiya'
|
|
366
|
-
end
|
|
367
|
-
|
|
368
|
-
[ :put, :post ].each do |verb|
|
|
369
|
-
context verb do
|
|
370
|
-
[ 'string', :symbol, 1, -1.1, {}, [], true, false, nil ].each do |object|
|
|
371
|
-
it "allows a(n) #{object.class} json object in params" do
|
|
372
|
-
subject.format :json
|
|
373
|
-
subject.send(verb) do
|
|
374
|
-
env['api.request.body']
|
|
375
|
-
end
|
|
376
|
-
send verb, '/', MultiJson.dump(object), { 'CONTENT_TYPE' => 'application/json' }
|
|
377
|
-
last_response.status.should == (verb == :post ? 201 : 200)
|
|
378
|
-
last_response.body.should eql MultiJson.dump(object)
|
|
379
|
-
last_request.params.should eql Hash.new
|
|
380
|
-
end
|
|
381
|
-
it "stores input in api.request.input" do
|
|
382
|
-
subject.format :json
|
|
383
|
-
subject.send(verb) do
|
|
384
|
-
env['api.request.input']
|
|
385
|
-
end
|
|
386
|
-
send verb, '/', MultiJson.dump(object), { 'CONTENT_TYPE' => 'application/json' }
|
|
387
|
-
last_response.status.should == (verb == :post ? 201 : 200)
|
|
388
|
-
last_response.body.should eql MultiJson.dump(object).to_json
|
|
389
|
-
end
|
|
390
|
-
context "chunked transfer encoding" do
|
|
391
|
-
it "stores input in api.request.input" do
|
|
392
|
-
subject.format :json
|
|
393
|
-
subject.send(verb) do
|
|
394
|
-
env['api.request.input']
|
|
395
|
-
end
|
|
396
|
-
send verb, '/', MultiJson.dump(object), { 'CONTENT_TYPE' => 'application/json', 'HTTP_TRANSFER_ENCODING' => 'chunked', 'CONTENT_LENGTH' => nil }
|
|
397
|
-
last_response.status.should == (verb == :post ? 201 : 200)
|
|
398
|
-
last_response.body.should eql MultiJson.dump(object).to_json
|
|
399
|
-
end
|
|
400
|
-
end
|
|
401
|
-
end
|
|
402
|
-
end
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
it 'allows for multipart paths' do
|
|
406
|
-
|
|
407
|
-
subject.route([:get, :post], '/:id/first') do
|
|
408
|
-
"first"
|
|
409
|
-
end
|
|
410
|
-
|
|
411
|
-
subject.route([:get, :post], '/:id') do
|
|
412
|
-
"ola"
|
|
413
|
-
end
|
|
414
|
-
subject.route([:get, :post], '/:id/first/second') do
|
|
415
|
-
"second"
|
|
416
|
-
end
|
|
417
|
-
|
|
418
|
-
get '/1'
|
|
419
|
-
last_response.body.should eql 'ola'
|
|
420
|
-
post '/1'
|
|
421
|
-
last_response.body.should eql 'ola'
|
|
422
|
-
get '/1/first'
|
|
423
|
-
last_response.body.should eql 'first'
|
|
424
|
-
post '/1/first'
|
|
425
|
-
last_response.body.should eql 'first'
|
|
426
|
-
get '/1/first/second'
|
|
427
|
-
last_response.body.should eql 'second'
|
|
428
|
-
|
|
429
|
-
end
|
|
430
|
-
|
|
431
|
-
it 'allows for :any as a verb' do
|
|
432
|
-
subject.route(:any, '/abc') do
|
|
433
|
-
"lol"
|
|
434
|
-
end
|
|
435
|
-
|
|
436
|
-
%w(get post put delete options patch).each do |m|
|
|
437
|
-
send(m, '/abc')
|
|
438
|
-
last_response.body.should eql 'lol'
|
|
439
|
-
end
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
verbs = %w(post get head delete put options patch)
|
|
443
|
-
verbs.each do |verb|
|
|
444
|
-
it 'allows and properly constrain a #{verb.upcase} method' do
|
|
445
|
-
subject.send(verb, '/example') do
|
|
446
|
-
verb
|
|
447
|
-
end
|
|
448
|
-
send(verb, '/example')
|
|
449
|
-
last_response.body.should eql verb == 'head' ? '' : verb
|
|
450
|
-
# Call it with a method other than the properly constrained one.
|
|
451
|
-
send(used_verb = verbs[(verbs.index(verb) + 2) % verbs.size], '/example')
|
|
452
|
-
last_response.status.should eql used_verb == 'options' ? 204 :405
|
|
453
|
-
end
|
|
454
|
-
end
|
|
455
|
-
|
|
456
|
-
it 'returns a 201 response code for POST by default' do
|
|
457
|
-
subject.post('example') do
|
|
458
|
-
"Created"
|
|
459
|
-
end
|
|
460
|
-
|
|
461
|
-
post '/example'
|
|
462
|
-
last_response.status.should eql 201
|
|
463
|
-
last_response.body.should eql 'Created'
|
|
464
|
-
end
|
|
465
|
-
|
|
466
|
-
it 'returns a 405 for an unsupported method' do
|
|
467
|
-
subject.get 'example' do
|
|
468
|
-
"example"
|
|
469
|
-
end
|
|
470
|
-
put '/example'
|
|
471
|
-
last_response.status.should eql 405
|
|
472
|
-
last_response.body.should eql ''
|
|
473
|
-
end
|
|
474
|
-
|
|
475
|
-
specify '405 responses includes an Allow header specifying supported methods' do
|
|
476
|
-
subject.get 'example' do
|
|
477
|
-
"example"
|
|
478
|
-
end
|
|
479
|
-
subject.post 'example' do
|
|
480
|
-
"example"
|
|
481
|
-
end
|
|
482
|
-
put '/example'
|
|
483
|
-
last_response.headers['Allow'].should eql 'OPTIONS, GET, POST, HEAD'
|
|
484
|
-
end
|
|
485
|
-
|
|
486
|
-
specify '405 responses includes an Content-Type header' do
|
|
487
|
-
subject.get 'example' do
|
|
488
|
-
"example"
|
|
489
|
-
end
|
|
490
|
-
subject.post 'example' do
|
|
491
|
-
"example"
|
|
492
|
-
end
|
|
493
|
-
put '/example'
|
|
494
|
-
last_response.headers['Content-Type'].should eql 'text/plain'
|
|
495
|
-
end
|
|
496
|
-
|
|
497
|
-
it 'adds an OPTIONS route that returns a 204 and an Allow header' do
|
|
498
|
-
subject.get 'example' do
|
|
499
|
-
"example"
|
|
500
|
-
end
|
|
501
|
-
options '/example'
|
|
502
|
-
last_response.status.should eql 204
|
|
503
|
-
last_response.body.should eql ''
|
|
504
|
-
last_response.headers['Allow'].should eql 'OPTIONS, GET, HEAD'
|
|
505
|
-
end
|
|
506
|
-
|
|
507
|
-
it 'allows HEAD on a GET request' do
|
|
508
|
-
subject.get 'example' do
|
|
509
|
-
"example"
|
|
510
|
-
end
|
|
511
|
-
head '/example'
|
|
512
|
-
last_response.status.should eql 200
|
|
513
|
-
last_response.body.should eql ''
|
|
514
|
-
end
|
|
515
|
-
|
|
516
|
-
it 'overwrites the default HEAD request' do
|
|
517
|
-
subject.head 'example' do
|
|
518
|
-
error! 'nothing to see here', 400
|
|
519
|
-
end
|
|
520
|
-
subject.get 'example' do
|
|
521
|
-
"example"
|
|
522
|
-
end
|
|
523
|
-
head '/example'
|
|
524
|
-
last_response.status.should eql 400
|
|
525
|
-
end
|
|
526
|
-
end
|
|
527
|
-
|
|
528
|
-
context "do_not_route_head!" do
|
|
529
|
-
before :each do
|
|
530
|
-
subject.do_not_route_head!
|
|
531
|
-
subject.get 'example' do
|
|
532
|
-
"example"
|
|
533
|
-
end
|
|
534
|
-
end
|
|
535
|
-
it 'options does not contain HEAD' do
|
|
536
|
-
options '/example'
|
|
537
|
-
last_response.status.should eql 204
|
|
538
|
-
last_response.body.should eql ''
|
|
539
|
-
last_response.headers['Allow'].should eql 'OPTIONS, GET'
|
|
540
|
-
end
|
|
541
|
-
it 'does not allow HEAD on a GET request' do
|
|
542
|
-
head '/example'
|
|
543
|
-
last_response.status.should eql 405
|
|
544
|
-
end
|
|
545
|
-
end
|
|
546
|
-
|
|
547
|
-
context "do_not_route_options!" do
|
|
548
|
-
before :each do
|
|
549
|
-
subject.do_not_route_options!
|
|
550
|
-
subject.get 'example' do
|
|
551
|
-
"example"
|
|
552
|
-
end
|
|
553
|
-
end
|
|
554
|
-
it 'options does not exist' do
|
|
555
|
-
options '/example'
|
|
556
|
-
last_response.status.should eql 405
|
|
557
|
-
end
|
|
558
|
-
end
|
|
559
|
-
|
|
560
|
-
describe 'filters' do
|
|
561
|
-
it 'adds a before filter' do
|
|
562
|
-
subject.before { @foo = 'first' }
|
|
563
|
-
subject.before { @bar = 'second' }
|
|
564
|
-
subject.get '/' do
|
|
565
|
-
"#{@foo} #{@bar}"
|
|
566
|
-
end
|
|
567
|
-
|
|
568
|
-
get '/'
|
|
569
|
-
last_response.body.should eql 'first second'
|
|
570
|
-
end
|
|
571
|
-
|
|
572
|
-
it 'adds a after_validation filter' do
|
|
573
|
-
subject.after_validation { @foo = "first #{params[:id]}:#{params[:id].class}" }
|
|
574
|
-
subject.after_validation { @bar = 'second' }
|
|
575
|
-
subject.params do
|
|
576
|
-
requires :id, :type => Integer
|
|
577
|
-
end
|
|
578
|
-
subject.get '/' do
|
|
579
|
-
"#{@foo} #{@bar}"
|
|
580
|
-
end
|
|
581
|
-
|
|
582
|
-
get '/', :id => "32"
|
|
583
|
-
last_response.body.should eql 'first 32:Fixnum second'
|
|
584
|
-
end
|
|
585
|
-
|
|
586
|
-
it 'adds a after filter' do
|
|
587
|
-
m = double('after mock')
|
|
588
|
-
subject.after { m.do_something! }
|
|
589
|
-
subject.after { m.do_something! }
|
|
590
|
-
subject.get '/' do
|
|
591
|
-
@var ||= 'default'
|
|
592
|
-
end
|
|
593
|
-
|
|
594
|
-
m.should_receive(:do_something!).exactly(2).times
|
|
595
|
-
get '/'
|
|
596
|
-
last_response.body.should eql 'default'
|
|
597
|
-
end
|
|
598
|
-
end
|
|
599
|
-
|
|
600
|
-
context 'format' do
|
|
601
|
-
before do
|
|
602
|
-
subject.get("/foo") { "bar" }
|
|
603
|
-
end
|
|
604
|
-
|
|
605
|
-
it 'sets content type for txt format' do
|
|
606
|
-
get '/foo'
|
|
607
|
-
last_response.headers['Content-Type'].should eql 'text/plain'
|
|
608
|
-
end
|
|
609
|
-
|
|
610
|
-
it 'sets content type for json' do
|
|
611
|
-
get '/foo.json'
|
|
612
|
-
last_response.headers['Content-Type'].should eql 'application/json'
|
|
613
|
-
end
|
|
614
|
-
|
|
615
|
-
it 'sets content type for error' do
|
|
616
|
-
subject.get('/error') { error!('error in plain text', 500) }
|
|
617
|
-
get '/error'
|
|
618
|
-
last_response.headers['Content-Type'].should eql 'text/plain'
|
|
619
|
-
end
|
|
620
|
-
|
|
621
|
-
it 'sets content type for error' do
|
|
622
|
-
subject.format :json
|
|
623
|
-
subject.get('/error') { error!('error in json', 500) }
|
|
624
|
-
get '/error.json'
|
|
625
|
-
last_response.headers['Content-Type'].should eql 'application/json'
|
|
626
|
-
end
|
|
627
|
-
|
|
628
|
-
it 'sets content type for xml' do
|
|
629
|
-
subject.format :xml
|
|
630
|
-
subject.get('/error') { error!('error in xml', 500) }
|
|
631
|
-
get '/error.xml'
|
|
632
|
-
last_response.headers['Content-Type'].should eql 'application/xml'
|
|
633
|
-
end
|
|
634
|
-
end
|
|
635
|
-
|
|
636
|
-
context 'custom middleware' do
|
|
637
|
-
module ApiSpec
|
|
638
|
-
class PhonyMiddleware
|
|
639
|
-
def initialize(app, *args)
|
|
640
|
-
@args = args
|
|
641
|
-
@app = app
|
|
642
|
-
@block = true if block_given?
|
|
643
|
-
end
|
|
644
|
-
|
|
645
|
-
def call(env)
|
|
646
|
-
env['phony.args'] ||= []
|
|
647
|
-
env['phony.args'] << @args
|
|
648
|
-
env['phony.block'] = true if @block
|
|
649
|
-
@app.call(env)
|
|
650
|
-
end
|
|
651
|
-
end
|
|
652
|
-
end
|
|
653
|
-
|
|
654
|
-
describe '.middleware' do
|
|
655
|
-
it 'includes middleware arguments from settings' do
|
|
656
|
-
settings = Grape::Util::HashStack.new
|
|
657
|
-
settings.stub!(:stack).and_return([{:middleware => [[ApiSpec::PhonyMiddleware, 'abc', 123]]}])
|
|
658
|
-
subject.stub!(:settings).and_return(settings)
|
|
659
|
-
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 'abc', 123]]
|
|
660
|
-
end
|
|
661
|
-
|
|
662
|
-
it 'includes all middleware from stacked settings' do
|
|
663
|
-
settings = Grape::Util::HashStack.new
|
|
664
|
-
settings.stub!(:stack).and_return [
|
|
665
|
-
{:middleware => [[ApiSpec::PhonyMiddleware, 123],[ApiSpec::PhonyMiddleware, 'abc']]},
|
|
666
|
-
{:middleware => [[ApiSpec::PhonyMiddleware, 'foo']]}
|
|
667
|
-
]
|
|
668
|
-
subject.stub!(:settings).and_return(settings)
|
|
669
|
-
|
|
670
|
-
subject.middleware.should eql [
|
|
671
|
-
[ApiSpec::PhonyMiddleware, 123],
|
|
672
|
-
[ApiSpec::PhonyMiddleware, 'abc'],
|
|
673
|
-
[ApiSpec::PhonyMiddleware, 'foo']
|
|
674
|
-
]
|
|
675
|
-
end
|
|
676
|
-
end
|
|
677
|
-
|
|
678
|
-
describe '.use' do
|
|
679
|
-
it 'adds middleware' do
|
|
680
|
-
subject.use ApiSpec::PhonyMiddleware, 123
|
|
681
|
-
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 123]]
|
|
682
|
-
end
|
|
683
|
-
|
|
684
|
-
it 'does not show up outside the namespace' do
|
|
685
|
-
subject.use ApiSpec::PhonyMiddleware, 123
|
|
686
|
-
subject.namespace :awesome do
|
|
687
|
-
use ApiSpec::PhonyMiddleware, 'abc'
|
|
688
|
-
middleware.should == [[ApiSpec::PhonyMiddleware, 123],[ApiSpec::PhonyMiddleware, 'abc']]
|
|
689
|
-
end
|
|
690
|
-
|
|
691
|
-
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, 123]]
|
|
692
|
-
end
|
|
693
|
-
|
|
694
|
-
it 'calls the middleware' do
|
|
695
|
-
subject.use ApiSpec::PhonyMiddleware, 'hello'
|
|
696
|
-
subject.get '/' do
|
|
697
|
-
env['phony.args'].first.first
|
|
698
|
-
end
|
|
699
|
-
|
|
700
|
-
get '/'
|
|
701
|
-
last_response.body.should eql 'hello'
|
|
702
|
-
end
|
|
703
|
-
|
|
704
|
-
it 'adds a block if one is given' do
|
|
705
|
-
block = lambda{ }
|
|
706
|
-
subject.use ApiSpec::PhonyMiddleware, &block
|
|
707
|
-
subject.middleware.should eql [[ApiSpec::PhonyMiddleware, block]]
|
|
708
|
-
end
|
|
709
|
-
|
|
710
|
-
it 'uses a block if one is given' do
|
|
711
|
-
block = lambda{ }
|
|
712
|
-
subject.use ApiSpec::PhonyMiddleware, &block
|
|
713
|
-
subject.get '/' do
|
|
714
|
-
env['phony.block'].inspect
|
|
715
|
-
end
|
|
716
|
-
|
|
717
|
-
get '/'
|
|
718
|
-
last_response.body.should == 'true'
|
|
719
|
-
end
|
|
720
|
-
|
|
721
|
-
it 'does not destroy the middleware settings on multiple runs' do
|
|
722
|
-
block = lambda{ }
|
|
723
|
-
subject.use ApiSpec::PhonyMiddleware, &block
|
|
724
|
-
subject.get '/' do
|
|
725
|
-
env['phony.block'].inspect
|
|
726
|
-
end
|
|
727
|
-
|
|
728
|
-
2.times do
|
|
729
|
-
get '/'
|
|
730
|
-
last_response.body.should == 'true'
|
|
731
|
-
end
|
|
732
|
-
end
|
|
733
|
-
|
|
734
|
-
it 'mounts behind error middleware' do
|
|
735
|
-
m = Class.new(Grape::Middleware::Base) do
|
|
736
|
-
def before
|
|
737
|
-
throw :error, :message => "Caught in the Net", :status => 400
|
|
738
|
-
end
|
|
739
|
-
end
|
|
740
|
-
subject.use m
|
|
741
|
-
subject.get "/" do
|
|
742
|
-
end
|
|
743
|
-
get "/"
|
|
744
|
-
last_response.status.should == 400
|
|
745
|
-
last_response.body.should == "Caught in the Net"
|
|
746
|
-
end
|
|
747
|
-
end
|
|
748
|
-
end
|
|
749
|
-
describe '.basic' do
|
|
750
|
-
it 'protects any resources on the same scope' do
|
|
751
|
-
subject.http_basic do |u,p|
|
|
752
|
-
u == 'allow'
|
|
753
|
-
end
|
|
754
|
-
subject.get(:hello){ "Hello, world."}
|
|
755
|
-
get '/hello'
|
|
756
|
-
last_response.status.should eql 401
|
|
757
|
-
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow','whatever')
|
|
758
|
-
last_response.status.should eql 200
|
|
759
|
-
end
|
|
760
|
-
|
|
761
|
-
it 'is scopable' do
|
|
762
|
-
subject.get(:hello){ "Hello, world."}
|
|
763
|
-
subject.namespace :admin do
|
|
764
|
-
http_basic do |u,p|
|
|
765
|
-
u == 'allow'
|
|
766
|
-
end
|
|
767
|
-
|
|
768
|
-
get(:hello){ "Hello, world." }
|
|
769
|
-
end
|
|
770
|
-
|
|
771
|
-
get '/hello'
|
|
772
|
-
last_response.status.should eql 200
|
|
773
|
-
get '/admin/hello'
|
|
774
|
-
last_response.status.should eql 401
|
|
775
|
-
end
|
|
776
|
-
|
|
777
|
-
it 'is callable via .auth as well' do
|
|
778
|
-
subject.auth :http_basic do |u,p|
|
|
779
|
-
u == 'allow'
|
|
780
|
-
end
|
|
781
|
-
|
|
782
|
-
subject.get(:hello){ "Hello, world."}
|
|
783
|
-
get '/hello'
|
|
784
|
-
last_response.status.should eql 401
|
|
785
|
-
get '/hello', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('allow','whatever')
|
|
786
|
-
last_response.status.should eql 200
|
|
787
|
-
end
|
|
788
|
-
end
|
|
789
|
-
|
|
790
|
-
describe '.logger' do
|
|
791
|
-
it 'returns an instance of Logger class by default' do
|
|
792
|
-
subject.logger.class.should eql Logger
|
|
793
|
-
end
|
|
794
|
-
|
|
795
|
-
it 'allows setting a custom logger' do
|
|
796
|
-
mylogger = Class.new
|
|
797
|
-
subject.logger mylogger
|
|
798
|
-
mylogger.should_receive(:info).exactly(1).times
|
|
799
|
-
subject.logger.info "this will be logged"
|
|
800
|
-
end
|
|
801
|
-
|
|
802
|
-
it "defaults to a standard logger log format" do
|
|
803
|
-
t = Time.at(100)
|
|
804
|
-
Time.stub(:now).and_return(t)
|
|
805
|
-
STDOUT.should_receive(:write).with("I, [#{Logger::Formatter.new.send(:format_datetime, t)}\##{Process.pid}] INFO -- : this will be logged\n")
|
|
806
|
-
subject.logger.info "this will be logged"
|
|
807
|
-
end
|
|
808
|
-
end
|
|
809
|
-
|
|
810
|
-
describe '.helpers' do
|
|
811
|
-
it 'is accessible from the endpoint' do
|
|
812
|
-
subject.helpers do
|
|
813
|
-
def hello
|
|
814
|
-
"Hello, world."
|
|
815
|
-
end
|
|
816
|
-
end
|
|
817
|
-
|
|
818
|
-
subject.get '/howdy' do
|
|
819
|
-
hello
|
|
820
|
-
end
|
|
821
|
-
|
|
822
|
-
get '/howdy'
|
|
823
|
-
last_response.body.should eql 'Hello, world.'
|
|
824
|
-
end
|
|
825
|
-
|
|
826
|
-
it 'is scopable' do
|
|
827
|
-
subject.helpers do
|
|
828
|
-
def generic
|
|
829
|
-
'always there'
|
|
830
|
-
end
|
|
831
|
-
end
|
|
832
|
-
|
|
833
|
-
subject.namespace :admin do
|
|
834
|
-
helpers do
|
|
835
|
-
def secret
|
|
836
|
-
'only in admin'
|
|
837
|
-
end
|
|
838
|
-
end
|
|
839
|
-
|
|
840
|
-
get '/secret' do
|
|
841
|
-
[generic, secret].join ':'
|
|
842
|
-
end
|
|
843
|
-
end
|
|
844
|
-
|
|
845
|
-
subject.get '/generic' do
|
|
846
|
-
[generic, respond_to?(:secret)].join ':'
|
|
847
|
-
end
|
|
848
|
-
|
|
849
|
-
get '/generic'
|
|
850
|
-
last_response.body.should eql 'always there:false'
|
|
851
|
-
get '/admin/secret'
|
|
852
|
-
last_response.body.should eql 'always there:only in admin'
|
|
853
|
-
end
|
|
854
|
-
|
|
855
|
-
it 'is reopenable' do
|
|
856
|
-
subject.helpers do
|
|
857
|
-
def one
|
|
858
|
-
1
|
|
859
|
-
end
|
|
860
|
-
end
|
|
861
|
-
|
|
862
|
-
subject.helpers do
|
|
863
|
-
def two
|
|
864
|
-
2
|
|
865
|
-
end
|
|
866
|
-
end
|
|
867
|
-
|
|
868
|
-
subject.get 'howdy' do
|
|
869
|
-
[one, two]
|
|
870
|
-
end
|
|
871
|
-
|
|
872
|
-
lambda{get '/howdy'}.should_not raise_error
|
|
873
|
-
end
|
|
874
|
-
|
|
875
|
-
it 'allows for modules' do
|
|
876
|
-
mod = Module.new do
|
|
877
|
-
def hello
|
|
878
|
-
"Hello, world."
|
|
879
|
-
end
|
|
880
|
-
end
|
|
881
|
-
subject.helpers mod
|
|
882
|
-
|
|
883
|
-
subject.get '/howdy' do
|
|
884
|
-
hello
|
|
885
|
-
end
|
|
886
|
-
|
|
887
|
-
get '/howdy'
|
|
888
|
-
last_response.body.should eql 'Hello, world.'
|
|
889
|
-
end
|
|
890
|
-
|
|
891
|
-
it 'allows multiple calls with modules and blocks' do
|
|
892
|
-
subject.helpers Module.new do
|
|
893
|
-
def one
|
|
894
|
-
1
|
|
895
|
-
end
|
|
896
|
-
end
|
|
897
|
-
subject.helpers Module.new do
|
|
898
|
-
def two
|
|
899
|
-
2
|
|
900
|
-
end
|
|
901
|
-
end
|
|
902
|
-
subject.helpers do
|
|
903
|
-
def three
|
|
904
|
-
3
|
|
905
|
-
end
|
|
906
|
-
end
|
|
907
|
-
subject.get 'howdy' do
|
|
908
|
-
[one, two, three]
|
|
909
|
-
end
|
|
910
|
-
lambda{get '/howdy'}.should_not raise_error
|
|
911
|
-
end
|
|
912
|
-
end
|
|
913
|
-
|
|
914
|
-
describe '.scope' do
|
|
915
|
-
# TODO: refactor this to not be tied to versioning. How about a generic
|
|
916
|
-
# .setting macro?
|
|
917
|
-
it 'scopes the various settings' do
|
|
918
|
-
subject.prefix 'new'
|
|
919
|
-
|
|
920
|
-
subject.scope :legacy do
|
|
921
|
-
prefix 'legacy'
|
|
922
|
-
get '/abc' do
|
|
923
|
-
'abc'
|
|
924
|
-
end
|
|
925
|
-
end
|
|
926
|
-
|
|
927
|
-
subject.get '/def' do
|
|
928
|
-
'def'
|
|
929
|
-
end
|
|
930
|
-
|
|
931
|
-
get '/new/abc'
|
|
932
|
-
last_response.status.should eql 404
|
|
933
|
-
get '/legacy/abc'
|
|
934
|
-
last_response.status.should eql 200
|
|
935
|
-
get '/legacy/def'
|
|
936
|
-
last_response.status.should eql 404
|
|
937
|
-
get '/new/def'
|
|
938
|
-
last_response.status.should eql 200
|
|
939
|
-
end
|
|
940
|
-
end
|
|
941
|
-
|
|
942
|
-
describe '.rescue_from' do
|
|
943
|
-
it 'does not rescue errors when rescue_from is not set' do
|
|
944
|
-
subject.get '/exception' do
|
|
945
|
-
raise "rain!"
|
|
946
|
-
end
|
|
947
|
-
lambda { get '/exception' }.should raise_error
|
|
948
|
-
end
|
|
949
|
-
|
|
950
|
-
it 'rescues all errors if rescue_from :all is called' do
|
|
951
|
-
subject.rescue_from :all
|
|
952
|
-
subject.get '/exception' do
|
|
953
|
-
raise "rain!"
|
|
954
|
-
end
|
|
955
|
-
get '/exception'
|
|
956
|
-
last_response.status.should eql 403
|
|
957
|
-
end
|
|
958
|
-
|
|
959
|
-
it 'rescues only certain errors if rescue_from is called with specific errors' do
|
|
960
|
-
subject.rescue_from ArgumentError
|
|
961
|
-
subject.get('/rescued'){ raise ArgumentError }
|
|
962
|
-
subject.get('/unrescued'){ raise "beefcake" }
|
|
963
|
-
|
|
964
|
-
get '/rescued'
|
|
965
|
-
last_response.status.should eql 403
|
|
966
|
-
|
|
967
|
-
lambda{ get '/unrescued' }.should raise_error
|
|
968
|
-
end
|
|
969
|
-
|
|
970
|
-
it 'does not re-raise exceptions of type Grape::Exception::Base' do
|
|
971
|
-
class CustomError < Grape::Exceptions::Base; end
|
|
972
|
-
subject.get('/custom_exception'){ raise CustomError }
|
|
973
|
-
|
|
974
|
-
lambda{ get '/custom_exception' }.should_not raise_error
|
|
975
|
-
end
|
|
976
|
-
|
|
977
|
-
it 'rescues custom grape exceptions' do
|
|
978
|
-
class CustomError < Grape::Exceptions::Base; end
|
|
979
|
-
subject.rescue_from CustomError do |e|
|
|
980
|
-
rack_response('New Error', e.status)
|
|
981
|
-
end
|
|
982
|
-
subject.get '/custom_error' do
|
|
983
|
-
raise CustomError, :status => 400, :message => 'Custom Error'
|
|
984
|
-
end
|
|
985
|
-
|
|
986
|
-
get '/custom_error'
|
|
987
|
-
last_response.status.should == 400
|
|
988
|
-
last_response.body.should == 'New Error'
|
|
989
|
-
end
|
|
990
|
-
|
|
991
|
-
it 'can rescue exceptions raised in the formatter' do
|
|
992
|
-
formatter = stub(:formatter)
|
|
993
|
-
formatter.stub(:call) { raise StandardError }
|
|
994
|
-
Grape::Formatter::Base.stub(:formatter_for) { formatter }
|
|
995
|
-
|
|
996
|
-
subject.rescue_from :all do |e|
|
|
997
|
-
rack_response('Formatter Error', 500)
|
|
998
|
-
end
|
|
999
|
-
subject.get('/formatter_exception') { 'Hello world' }
|
|
1000
|
-
|
|
1001
|
-
get '/formatter_exception'
|
|
1002
|
-
last_response.status.should eql 500
|
|
1003
|
-
last_response.body.should == 'Formatter Error'
|
|
1004
|
-
end
|
|
1005
|
-
end
|
|
1006
|
-
|
|
1007
|
-
describe '.rescue_from klass, block' do
|
|
1008
|
-
it 'rescues Exception' do
|
|
1009
|
-
subject.rescue_from RuntimeError do |e|
|
|
1010
|
-
rack_response("rescued from #{e.message}", 202)
|
|
1011
|
-
end
|
|
1012
|
-
subject.get '/exception' do
|
|
1013
|
-
raise "rain!"
|
|
1014
|
-
end
|
|
1015
|
-
get '/exception'
|
|
1016
|
-
last_response.status.should eql 202
|
|
1017
|
-
last_response.body.should == 'rescued from rain!'
|
|
1018
|
-
end
|
|
1019
|
-
it 'rescues an error via rescue_from :all' do
|
|
1020
|
-
class ConnectionError < RuntimeError; end
|
|
1021
|
-
subject.rescue_from :all do |e|
|
|
1022
|
-
rack_response("rescued from #{e.class.name}", 500)
|
|
1023
|
-
end
|
|
1024
|
-
subject.get '/exception' do
|
|
1025
|
-
raise ConnectionError
|
|
1026
|
-
end
|
|
1027
|
-
get '/exception'
|
|
1028
|
-
last_response.status.should eql 500
|
|
1029
|
-
last_response.body.should == 'rescued from ConnectionError'
|
|
1030
|
-
end
|
|
1031
|
-
it 'rescues a specific error' do
|
|
1032
|
-
class ConnectionError < RuntimeError; end
|
|
1033
|
-
subject.rescue_from ConnectionError do |e|
|
|
1034
|
-
rack_response("rescued from #{e.class.name}", 500)
|
|
1035
|
-
end
|
|
1036
|
-
subject.get '/exception' do
|
|
1037
|
-
raise ConnectionError
|
|
1038
|
-
end
|
|
1039
|
-
get '/exception'
|
|
1040
|
-
last_response.status.should eql 500
|
|
1041
|
-
last_response.body.should == 'rescued from ConnectionError'
|
|
1042
|
-
end
|
|
1043
|
-
it 'rescues multiple specific errors' do
|
|
1044
|
-
class ConnectionError < RuntimeError; end
|
|
1045
|
-
class DatabaseError < RuntimeError; end
|
|
1046
|
-
subject.rescue_from ConnectionError do |e|
|
|
1047
|
-
rack_response("rescued from #{e.class.name}", 500)
|
|
1048
|
-
end
|
|
1049
|
-
subject.rescue_from DatabaseError do |e|
|
|
1050
|
-
rack_response("rescued from #{e.class.name}", 500)
|
|
1051
|
-
end
|
|
1052
|
-
subject.get '/connection' do
|
|
1053
|
-
raise ConnectionError
|
|
1054
|
-
end
|
|
1055
|
-
subject.get '/database' do
|
|
1056
|
-
raise DatabaseError
|
|
1057
|
-
end
|
|
1058
|
-
get '/connection'
|
|
1059
|
-
last_response.status.should eql 500
|
|
1060
|
-
last_response.body.should == 'rescued from ConnectionError'
|
|
1061
|
-
get '/database'
|
|
1062
|
-
last_response.status.should eql 500
|
|
1063
|
-
last_response.body.should == 'rescued from DatabaseError'
|
|
1064
|
-
end
|
|
1065
|
-
it 'does not rescue a different error' do
|
|
1066
|
-
class CommunicationError < RuntimeError; end
|
|
1067
|
-
subject.rescue_from RuntimeError do |e|
|
|
1068
|
-
rack_response("rescued from #{e.class.name}", 500)
|
|
1069
|
-
end
|
|
1070
|
-
subject.get '/uncaught' do
|
|
1071
|
-
raise CommunicationError
|
|
1072
|
-
end
|
|
1073
|
-
lambda { get '/uncaught' }.should raise_error(CommunicationError)
|
|
1074
|
-
end
|
|
1075
|
-
end
|
|
1076
|
-
|
|
1077
|
-
describe '.error_format' do
|
|
1078
|
-
it 'rescues all errors and return :txt' do
|
|
1079
|
-
subject.rescue_from :all
|
|
1080
|
-
subject.format :txt
|
|
1081
|
-
subject.get '/exception' do
|
|
1082
|
-
raise "rain!"
|
|
1083
|
-
end
|
|
1084
|
-
get '/exception'
|
|
1085
|
-
last_response.body.should eql "rain!"
|
|
1086
|
-
end
|
|
1087
|
-
|
|
1088
|
-
it 'rescues all errors and return :txt with backtrace' do
|
|
1089
|
-
subject.rescue_from :all, :backtrace => true
|
|
1090
|
-
subject.format :txt
|
|
1091
|
-
subject.get '/exception' do
|
|
1092
|
-
raise "rain!"
|
|
1093
|
-
end
|
|
1094
|
-
get '/exception'
|
|
1095
|
-
last_response.body.start_with?("rain!\r\n").should be_true
|
|
1096
|
-
end
|
|
1097
|
-
|
|
1098
|
-
it 'rescues all errors with a default formatter' do
|
|
1099
|
-
subject.default_format :foo
|
|
1100
|
-
subject.content_type :foo, "text/foo"
|
|
1101
|
-
subject.rescue_from :all
|
|
1102
|
-
subject.get '/exception' do
|
|
1103
|
-
raise "rain!"
|
|
1104
|
-
end
|
|
1105
|
-
get '/exception.foo'
|
|
1106
|
-
last_response.body.should start_with "rain!"
|
|
1107
|
-
end
|
|
1108
|
-
|
|
1109
|
-
it 'defaults the error formatter to format' do
|
|
1110
|
-
subject.format :json
|
|
1111
|
-
subject.rescue_from :all
|
|
1112
|
-
subject.content_type :json, "application/json"
|
|
1113
|
-
subject.content_type :foo, "text/foo"
|
|
1114
|
-
subject.get '/exception' do
|
|
1115
|
-
raise "rain!"
|
|
1116
|
-
end
|
|
1117
|
-
get '/exception.json'
|
|
1118
|
-
last_response.body.should == '{"error":"rain!"}'
|
|
1119
|
-
get '/exception.foo'
|
|
1120
|
-
last_response.body.should == '{"error":"rain!"}'
|
|
1121
|
-
end
|
|
1122
|
-
|
|
1123
|
-
context 'class' do
|
|
1124
|
-
before :each do
|
|
1125
|
-
class CustomErrorFormatter
|
|
1126
|
-
def self.call(message, backtrace, options, env)
|
|
1127
|
-
"message: #{message} @backtrace"
|
|
1128
|
-
end
|
|
1129
|
-
end
|
|
1130
|
-
end
|
|
1131
|
-
it 'returns a custom error format' do
|
|
1132
|
-
subject.rescue_from :all, :backtrace => true
|
|
1133
|
-
subject.error_formatter :txt, CustomErrorFormatter
|
|
1134
|
-
subject.get '/exception' do
|
|
1135
|
-
raise "rain!"
|
|
1136
|
-
end
|
|
1137
|
-
get '/exception'
|
|
1138
|
-
last_response.body.should == "message: rain! @backtrace"
|
|
1139
|
-
end
|
|
1140
|
-
end
|
|
1141
|
-
|
|
1142
|
-
it 'rescues all errors and return :json' do
|
|
1143
|
-
subject.rescue_from :all
|
|
1144
|
-
subject.format :json
|
|
1145
|
-
subject.get '/exception' do
|
|
1146
|
-
raise "rain!"
|
|
1147
|
-
end
|
|
1148
|
-
get '/exception'
|
|
1149
|
-
last_response.body.should eql '{"error":"rain!"}'
|
|
1150
|
-
end
|
|
1151
|
-
it 'rescues all errors and return :json with backtrace' do
|
|
1152
|
-
subject.rescue_from :all, :backtrace => true
|
|
1153
|
-
subject.format :json
|
|
1154
|
-
subject.get '/exception' do
|
|
1155
|
-
raise "rain!"
|
|
1156
|
-
end
|
|
1157
|
-
get '/exception'
|
|
1158
|
-
json = MultiJson.load(last_response.body)
|
|
1159
|
-
json["error"].should eql 'rain!'
|
|
1160
|
-
json["backtrace"].length.should > 0
|
|
1161
|
-
end
|
|
1162
|
-
it 'rescues error! and return txt' do
|
|
1163
|
-
subject.format :txt
|
|
1164
|
-
subject.get '/error' do
|
|
1165
|
-
error!("Access Denied", 401)
|
|
1166
|
-
end
|
|
1167
|
-
get '/error'
|
|
1168
|
-
last_response.body.should eql "Access Denied"
|
|
1169
|
-
end
|
|
1170
|
-
it 'rescues error! and return json' do
|
|
1171
|
-
subject.format :json
|
|
1172
|
-
subject.get '/error' do
|
|
1173
|
-
error!("Access Denied", 401)
|
|
1174
|
-
end
|
|
1175
|
-
get '/error'
|
|
1176
|
-
last_response.body.should eql '{"error":"Access Denied"}'
|
|
1177
|
-
end
|
|
1178
|
-
end
|
|
1179
|
-
|
|
1180
|
-
describe '.content_type' do
|
|
1181
|
-
it 'sets additional content-type' do
|
|
1182
|
-
subject.content_type :xls, "application/vnd.ms-excel"
|
|
1183
|
-
subject.get :excel do
|
|
1184
|
-
"some binary content"
|
|
1185
|
-
end
|
|
1186
|
-
get '/excel.xls'
|
|
1187
|
-
last_response.content_type.should == "application/vnd.ms-excel"
|
|
1188
|
-
end
|
|
1189
|
-
it 'allows to override content-type' do
|
|
1190
|
-
subject.get :content do
|
|
1191
|
-
content_type "text/javascript"
|
|
1192
|
-
"var x = 1;"
|
|
1193
|
-
end
|
|
1194
|
-
get '/content'
|
|
1195
|
-
last_response.content_type.should == "text/javascript"
|
|
1196
|
-
end
|
|
1197
|
-
it 'removes existing content types' do
|
|
1198
|
-
subject.content_type :xls, "application/vnd.ms-excel"
|
|
1199
|
-
subject.get :excel do
|
|
1200
|
-
"some binary content"
|
|
1201
|
-
end
|
|
1202
|
-
get '/excel.json'
|
|
1203
|
-
last_response.status.should == 406
|
|
1204
|
-
last_response.body.should == "The requested format 'txt' is not supported."
|
|
1205
|
-
end
|
|
1206
|
-
end
|
|
1207
|
-
|
|
1208
|
-
describe '.formatter' do
|
|
1209
|
-
context 'multiple formatters' do
|
|
1210
|
-
before :each do
|
|
1211
|
-
subject.formatter :json, lambda { |object, env| "{\"custom_formatter\":\"#{object[:some]}\"}" }
|
|
1212
|
-
subject.formatter :txt, lambda { |object, env| "custom_formatter: #{object[:some]}" }
|
|
1213
|
-
subject.get :simple do
|
|
1214
|
-
{:some => 'hash'}
|
|
1215
|
-
end
|
|
1216
|
-
end
|
|
1217
|
-
it 'sets one formatter' do
|
|
1218
|
-
get '/simple.json'
|
|
1219
|
-
last_response.body.should eql '{"custom_formatter":"hash"}'
|
|
1220
|
-
end
|
|
1221
|
-
it 'sets another formatter' do
|
|
1222
|
-
get '/simple.txt'
|
|
1223
|
-
last_response.body.should eql 'custom_formatter: hash'
|
|
1224
|
-
end
|
|
1225
|
-
end
|
|
1226
|
-
context 'custom formatter' do
|
|
1227
|
-
before :each do
|
|
1228
|
-
subject.content_type :json, 'application/json'
|
|
1229
|
-
subject.content_type :custom, 'application/custom'
|
|
1230
|
-
subject.formatter :custom, lambda { |object, env| "{\"custom_formatter\":\"#{object[:some]}\"}" }
|
|
1231
|
-
subject.get :simple do
|
|
1232
|
-
{:some => 'hash'}
|
|
1233
|
-
end
|
|
1234
|
-
end
|
|
1235
|
-
it 'uses json' do
|
|
1236
|
-
get '/simple.json'
|
|
1237
|
-
last_response.body.should eql '{"some":"hash"}'
|
|
1238
|
-
end
|
|
1239
|
-
it 'uses custom formatter' do
|
|
1240
|
-
get '/simple.custom', { 'HTTP_ACCEPT' => 'application/custom' }
|
|
1241
|
-
last_response.body.should eql '{"custom_formatter":"hash"}'
|
|
1242
|
-
end
|
|
1243
|
-
end
|
|
1244
|
-
context 'custom formatter class' do
|
|
1245
|
-
module CustomFormatter
|
|
1246
|
-
def self.call(object, env)
|
|
1247
|
-
"{\"custom_formatter\":\"#{object[:some]}\"}"
|
|
1248
|
-
end
|
|
1249
|
-
end
|
|
1250
|
-
before :each do
|
|
1251
|
-
subject.content_type :json, 'application/json'
|
|
1252
|
-
subject.content_type :custom, 'application/custom'
|
|
1253
|
-
subject.formatter :custom, CustomFormatter
|
|
1254
|
-
subject.get :simple do
|
|
1255
|
-
{:some => 'hash'}
|
|
1256
|
-
end
|
|
1257
|
-
end
|
|
1258
|
-
it 'uses json' do
|
|
1259
|
-
get '/simple.json'
|
|
1260
|
-
last_response.body.should eql '{"some":"hash"}'
|
|
1261
|
-
end
|
|
1262
|
-
it 'uses custom formatter' do
|
|
1263
|
-
get '/simple.custom', { 'HTTP_ACCEPT' => 'application/custom' }
|
|
1264
|
-
last_response.body.should eql '{"custom_formatter":"hash"}'
|
|
1265
|
-
end
|
|
1266
|
-
end
|
|
1267
|
-
end
|
|
1268
|
-
|
|
1269
|
-
describe '.parser' do
|
|
1270
|
-
it 'parses data in format requested by content-type' do
|
|
1271
|
-
subject.format :json
|
|
1272
|
-
subject.post '/data' do
|
|
1273
|
-
{ :x => params[:x] }
|
|
1274
|
-
end
|
|
1275
|
-
post "/data", '{"x":42}', { 'CONTENT_TYPE' => 'application/json' }
|
|
1276
|
-
last_response.status.should == 201
|
|
1277
|
-
last_response.body.should == '{"x":42}'
|
|
1278
|
-
end
|
|
1279
|
-
context 'lambda parser' do
|
|
1280
|
-
before :each do
|
|
1281
|
-
subject.content_type :txt, "text/plain"
|
|
1282
|
-
subject.content_type :custom, "text/custom"
|
|
1283
|
-
subject.parser :custom, lambda { |object, env| { object.to_sym => object.to_s.reverse } }
|
|
1284
|
-
subject.put :simple do
|
|
1285
|
-
params[:simple]
|
|
1286
|
-
end
|
|
1287
|
-
end
|
|
1288
|
-
[ "text/custom", "text/custom; charset=UTF-8" ].each do |content_type|
|
|
1289
|
-
it "uses parser for #{content_type}" do
|
|
1290
|
-
put '/simple', "simple", "CONTENT_TYPE" => content_type
|
|
1291
|
-
last_response.status.should == 200
|
|
1292
|
-
last_response.body.should eql "elpmis"
|
|
1293
|
-
end
|
|
1294
|
-
end
|
|
1295
|
-
end
|
|
1296
|
-
context 'custom parser class' do
|
|
1297
|
-
module CustomParser
|
|
1298
|
-
def self.call(object, env)
|
|
1299
|
-
{ object.to_sym => object.to_s.reverse }
|
|
1300
|
-
end
|
|
1301
|
-
end
|
|
1302
|
-
before :each do
|
|
1303
|
-
subject.content_type :txt, "text/plain"
|
|
1304
|
-
subject.content_type :custom, "text/custom"
|
|
1305
|
-
subject.parser :custom, CustomParser
|
|
1306
|
-
subject.put :simple do
|
|
1307
|
-
params[:simple]
|
|
1308
|
-
end
|
|
1309
|
-
end
|
|
1310
|
-
it 'uses custom parser' do
|
|
1311
|
-
put '/simple', "simple", "CONTENT_TYPE" => "text/custom"
|
|
1312
|
-
last_response.status.should == 200
|
|
1313
|
-
last_response.body.should eql "elpmis"
|
|
1314
|
-
end
|
|
1315
|
-
end
|
|
1316
|
-
context "multi_xml" do
|
|
1317
|
-
it "doesn't parse yaml" do
|
|
1318
|
-
subject.put :yaml do
|
|
1319
|
-
params[:tag]
|
|
1320
|
-
end
|
|
1321
|
-
put '/yaml', '<tag type="symbol">a123</tag>', "CONTENT_TYPE" => "application/xml"
|
|
1322
|
-
last_response.status.should == 400
|
|
1323
|
-
last_response.body.should eql 'Disallowed type attribute: "symbol"'
|
|
1324
|
-
end
|
|
1325
|
-
end
|
|
1326
|
-
context "none parser class" do
|
|
1327
|
-
before :each do
|
|
1328
|
-
subject.parser :json, nil
|
|
1329
|
-
subject.put "data" do
|
|
1330
|
-
"body: #{env['api.request.body']}"
|
|
1331
|
-
end
|
|
1332
|
-
end
|
|
1333
|
-
it "does not parse data" do
|
|
1334
|
-
put '/data', 'not valid json', "CONTENT_TYPE" => "application/json"
|
|
1335
|
-
last_response.status.should == 200
|
|
1336
|
-
last_response.body.should == "body: not valid json"
|
|
1337
|
-
end
|
|
1338
|
-
end
|
|
1339
|
-
end
|
|
1340
|
-
|
|
1341
|
-
describe '.default_format' do
|
|
1342
|
-
before :each do
|
|
1343
|
-
subject.format :json
|
|
1344
|
-
subject.default_format :json
|
|
1345
|
-
end
|
|
1346
|
-
it 'returns data in default format' do
|
|
1347
|
-
subject.get '/data' do
|
|
1348
|
-
{ :x => 42 }
|
|
1349
|
-
end
|
|
1350
|
-
get "/data"
|
|
1351
|
-
last_response.status.should == 200
|
|
1352
|
-
last_response.body.should == '{"x":42}'
|
|
1353
|
-
end
|
|
1354
|
-
it 'parses data in default format' do
|
|
1355
|
-
subject.post '/data' do
|
|
1356
|
-
{ :x => params[:x] }
|
|
1357
|
-
end
|
|
1358
|
-
post "/data", '{"x":42}', "CONTENT_TYPE" => ""
|
|
1359
|
-
last_response.status.should == 201
|
|
1360
|
-
last_response.body.should == '{"x":42}'
|
|
1361
|
-
end
|
|
1362
|
-
end
|
|
1363
|
-
|
|
1364
|
-
describe '.default_error_status' do
|
|
1365
|
-
it 'allows setting default_error_status' do
|
|
1366
|
-
subject.rescue_from :all
|
|
1367
|
-
subject.default_error_status 200
|
|
1368
|
-
subject.get '/exception' do
|
|
1369
|
-
raise "rain!"
|
|
1370
|
-
end
|
|
1371
|
-
get '/exception'
|
|
1372
|
-
last_response.status.should eql 200
|
|
1373
|
-
end
|
|
1374
|
-
it 'has a default error status' do
|
|
1375
|
-
subject.rescue_from :all
|
|
1376
|
-
subject.get '/exception' do
|
|
1377
|
-
raise "rain!"
|
|
1378
|
-
end
|
|
1379
|
-
get '/exception'
|
|
1380
|
-
last_response.status.should eql 403
|
|
1381
|
-
end
|
|
1382
|
-
end
|
|
1383
|
-
|
|
1384
|
-
context 'routes' do
|
|
1385
|
-
describe 'empty api structure' do
|
|
1386
|
-
it 'returns an empty array of routes' do
|
|
1387
|
-
subject.routes.should == []
|
|
1388
|
-
end
|
|
1389
|
-
end
|
|
1390
|
-
describe 'single method api structure' do
|
|
1391
|
-
before(:each) do
|
|
1392
|
-
subject.get :ping do
|
|
1393
|
-
'pong'
|
|
1394
|
-
end
|
|
1395
|
-
end
|
|
1396
|
-
it 'returns one route' do
|
|
1397
|
-
subject.routes.size.should == 1
|
|
1398
|
-
route = subject.routes[0]
|
|
1399
|
-
route.route_version.should be_nil
|
|
1400
|
-
route.route_path.should == "/ping(.:format)"
|
|
1401
|
-
route.route_method.should == "GET"
|
|
1402
|
-
end
|
|
1403
|
-
end
|
|
1404
|
-
describe 'api structure with two versions and a namespace' do
|
|
1405
|
-
before :each do
|
|
1406
|
-
subject.version 'v1', :using => :path
|
|
1407
|
-
subject.get 'version' do
|
|
1408
|
-
api.version
|
|
1409
|
-
end
|
|
1410
|
-
# version v2
|
|
1411
|
-
subject.version 'v2', :using => :path
|
|
1412
|
-
subject.prefix 'p'
|
|
1413
|
-
subject.namespace 'n1' do
|
|
1414
|
-
namespace 'n2' do
|
|
1415
|
-
get 'version' do
|
|
1416
|
-
api.version
|
|
1417
|
-
end
|
|
1418
|
-
end
|
|
1419
|
-
end
|
|
1420
|
-
end
|
|
1421
|
-
it 'returns the latest version set' do
|
|
1422
|
-
subject.version.should == 'v2'
|
|
1423
|
-
end
|
|
1424
|
-
it 'returns versions' do
|
|
1425
|
-
subject.versions.should == [ 'v1', 'v2' ]
|
|
1426
|
-
end
|
|
1427
|
-
it 'sets route paths' do
|
|
1428
|
-
subject.routes.size.should >= 2
|
|
1429
|
-
subject.routes[0].route_path.should == "/:version/version(.:format)"
|
|
1430
|
-
subject.routes[1].route_path.should == "/p/:version/n1/n2/version(.:format)"
|
|
1431
|
-
end
|
|
1432
|
-
it 'sets route versions' do
|
|
1433
|
-
subject.routes[0].route_version.should == 'v1'
|
|
1434
|
-
subject.routes[1].route_version.should == 'v2'
|
|
1435
|
-
end
|
|
1436
|
-
it 'sets a nested namespace' do
|
|
1437
|
-
subject.routes[1].route_namespace.should == "/n1/n2"
|
|
1438
|
-
end
|
|
1439
|
-
it 'sets prefix' do
|
|
1440
|
-
subject.routes[1].route_prefix.should == 'p'
|
|
1441
|
-
end
|
|
1442
|
-
end
|
|
1443
|
-
describe 'api structure with additional parameters' do
|
|
1444
|
-
before(:each) do
|
|
1445
|
-
subject.get 'split/:string', { :params => { "token" => "a token" }, :optional_params => { "limit" => "the limit" } } do
|
|
1446
|
-
params[:string].split(params[:token], (params[:limit] || 0).to_i)
|
|
1447
|
-
end
|
|
1448
|
-
end
|
|
1449
|
-
it 'splits a string' do
|
|
1450
|
-
get "/split/a,b,c.json", :token => ','
|
|
1451
|
-
last_response.body.should == '["a","b","c"]'
|
|
1452
|
-
end
|
|
1453
|
-
it 'splits a string with limit' do
|
|
1454
|
-
get "/split/a,b,c.json", :token => ',', :limit => '2'
|
|
1455
|
-
last_response.body.should == '["a","b,c"]'
|
|
1456
|
-
end
|
|
1457
|
-
it 'sets route_params' do
|
|
1458
|
-
subject.routes.map { |route|
|
|
1459
|
-
{ :params => route.route_params, :optional_params => route.route_optional_params }
|
|
1460
|
-
}.should eq [
|
|
1461
|
-
{ :params => { "string" => "", "token" => "a token" }, :optional_params => { "limit" => "the limit" } }
|
|
1462
|
-
]
|
|
1463
|
-
end
|
|
1464
|
-
end
|
|
1465
|
-
end
|
|
1466
|
-
|
|
1467
|
-
context 'desc' do
|
|
1468
|
-
it 'empty array of routes' do
|
|
1469
|
-
subject.routes.should == []
|
|
1470
|
-
end
|
|
1471
|
-
it 'empty array of routes' do
|
|
1472
|
-
subject.desc "grape api"
|
|
1473
|
-
subject.routes.should == []
|
|
1474
|
-
end
|
|
1475
|
-
it 'describes a method' do
|
|
1476
|
-
subject.desc "first method"
|
|
1477
|
-
subject.get :first do ; end
|
|
1478
|
-
subject.routes.length.should == 1
|
|
1479
|
-
route = subject.routes.first
|
|
1480
|
-
route.route_description.should == "first method"
|
|
1481
|
-
route.route_foo.should be_nil
|
|
1482
|
-
route.route_params.should == { }
|
|
1483
|
-
end
|
|
1484
|
-
it 'describes methods separately' do
|
|
1485
|
-
subject.desc "first method"
|
|
1486
|
-
subject.get :first do ; end
|
|
1487
|
-
subject.desc "second method"
|
|
1488
|
-
subject.get :second do ; end
|
|
1489
|
-
subject.routes.count.should == 2
|
|
1490
|
-
subject.routes.map { |route|
|
|
1491
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1492
|
-
}.should eq [
|
|
1493
|
-
{ :description => "first method", :params => {} },
|
|
1494
|
-
{ :description => "second method", :params => {} }
|
|
1495
|
-
]
|
|
1496
|
-
end
|
|
1497
|
-
it 'resets desc' do
|
|
1498
|
-
subject.desc "first method"
|
|
1499
|
-
subject.get :first do ; end
|
|
1500
|
-
subject.get :second do ; end
|
|
1501
|
-
subject.routes.map { |route|
|
|
1502
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1503
|
-
}.should eq [
|
|
1504
|
-
{ :description => "first method", :params => {} },
|
|
1505
|
-
{ :description => nil, :params => {} }
|
|
1506
|
-
]
|
|
1507
|
-
end
|
|
1508
|
-
it 'namespaces and describe arbitrary parameters' do
|
|
1509
|
-
subject.namespace 'ns' do
|
|
1510
|
-
desc "ns second", :foo => "bar"
|
|
1511
|
-
get 'second' do ; end
|
|
1512
|
-
end
|
|
1513
|
-
subject.routes.map { |route|
|
|
1514
|
-
{ :description => route.route_description, :foo => route.route_foo, :params => route.route_params }
|
|
1515
|
-
}.should eq [
|
|
1516
|
-
{ :description => "ns second", :foo => "bar", :params => {} },
|
|
1517
|
-
]
|
|
1518
|
-
end
|
|
1519
|
-
it 'includes details' do
|
|
1520
|
-
subject.desc "method", :details => "method details"
|
|
1521
|
-
subject.get 'method' do ; end
|
|
1522
|
-
subject.routes.map { |route|
|
|
1523
|
-
{ :description => route.route_description, :details => route.route_details, :params => route.route_params }
|
|
1524
|
-
}.should eq [
|
|
1525
|
-
{ :description => "method", :details => "method details", :params => {} },
|
|
1526
|
-
]
|
|
1527
|
-
end
|
|
1528
|
-
it 'describes a method with parameters' do
|
|
1529
|
-
subject.desc "Reverses a string.", { :params =>
|
|
1530
|
-
{ "s" => { :desc => "string to reverse", :type => "string" }}
|
|
1531
|
-
}
|
|
1532
|
-
subject.get 'reverse' do
|
|
1533
|
-
params[:s].reverse
|
|
1534
|
-
end
|
|
1535
|
-
subject.routes.map { |route|
|
|
1536
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1537
|
-
}.should eq [
|
|
1538
|
-
{ :description => "Reverses a string.", :params => { "s" => { :desc => "string to reverse", :type => "string" } } }
|
|
1539
|
-
]
|
|
1540
|
-
end
|
|
1541
|
-
it 'merges the parameters of the namespace with the parameters of the method' do
|
|
1542
|
-
subject.desc "namespace"
|
|
1543
|
-
subject.params do
|
|
1544
|
-
requires :ns_param, :desc => "namespace parameter"
|
|
1545
|
-
end
|
|
1546
|
-
subject.namespace 'ns' do
|
|
1547
|
-
desc "method"
|
|
1548
|
-
params do
|
|
1549
|
-
optional :method_param, :desc => "method parameter"
|
|
1550
|
-
end
|
|
1551
|
-
get 'method' do ; end
|
|
1552
|
-
end
|
|
1553
|
-
subject.routes.map { |route|
|
|
1554
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1555
|
-
}.should eq [
|
|
1556
|
-
{ :description => "method",
|
|
1557
|
-
:params => {
|
|
1558
|
-
"ns_param" => { :required => true, :desc => "namespace parameter" },
|
|
1559
|
-
"method_param" => { :required => false, :desc => "method parameter" }
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
]
|
|
1563
|
-
end
|
|
1564
|
-
it 'merges the parameters of nested namespaces' do
|
|
1565
|
-
subject.desc "ns1"
|
|
1566
|
-
subject.params do
|
|
1567
|
-
optional :ns_param, :desc => "ns param 1"
|
|
1568
|
-
requires :ns1_param, :desc => "ns1 param"
|
|
1569
|
-
end
|
|
1570
|
-
subject.namespace 'ns1' do
|
|
1571
|
-
desc "ns2"
|
|
1572
|
-
params do
|
|
1573
|
-
requires :ns_param, :desc => "ns param 2"
|
|
1574
|
-
requires :ns2_param, :desc => "ns2 param"
|
|
1575
|
-
end
|
|
1576
|
-
namespace 'ns2' do
|
|
1577
|
-
desc "method"
|
|
1578
|
-
params do
|
|
1579
|
-
optional :method_param, :desc => "method param"
|
|
1580
|
-
end
|
|
1581
|
-
get 'method' do ; end
|
|
1582
|
-
end
|
|
1583
|
-
end
|
|
1584
|
-
subject.routes.map { |route|
|
|
1585
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1586
|
-
}.should eq [
|
|
1587
|
-
{ :description => "method",
|
|
1588
|
-
:params => {
|
|
1589
|
-
"ns_param" => { :required => true, :desc => "ns param 2" },
|
|
1590
|
-
"ns1_param" => { :required => true, :desc => "ns1 param" },
|
|
1591
|
-
"ns2_param" => { :required => true, :desc => "ns2 param" },
|
|
1592
|
-
"method_param" => { :required => false, :desc => "method param" }
|
|
1593
|
-
}
|
|
1594
|
-
}
|
|
1595
|
-
]
|
|
1596
|
-
end
|
|
1597
|
-
it "groups nested params and prevents overwriting of params with same name in different groups" do
|
|
1598
|
-
subject.desc "method"
|
|
1599
|
-
subject.params do
|
|
1600
|
-
group :group1 do
|
|
1601
|
-
optional :param1, :desc => "group1 param1 desc"
|
|
1602
|
-
requires :param2, :desc => "group1 param2 desc"
|
|
1603
|
-
end
|
|
1604
|
-
group :group2 do
|
|
1605
|
-
optional :param1, :desc => "group2 param1 desc"
|
|
1606
|
-
requires :param2, :desc => "group2 param2 desc"
|
|
1607
|
-
end
|
|
1608
|
-
end
|
|
1609
|
-
subject.get "method" do ; end
|
|
1610
|
-
|
|
1611
|
-
subject.routes.map { |route|
|
|
1612
|
-
route.route_params
|
|
1613
|
-
}.should eq [{
|
|
1614
|
-
"group1[param1]" => { :required => false, :desc => "group1 param1 desc" },
|
|
1615
|
-
"group1[param2]" => { :required => true, :desc => "group1 param2 desc" },
|
|
1616
|
-
"group2[param1]" => { :required => false, :desc => "group2 param1 desc" },
|
|
1617
|
-
"group2[param2]" => { :required => true, :desc => "group2 param2 desc" }
|
|
1618
|
-
}]
|
|
1619
|
-
end
|
|
1620
|
-
it 'uses full name of parameters in nested groups' do
|
|
1621
|
-
subject.desc "nesting"
|
|
1622
|
-
subject.params do
|
|
1623
|
-
requires :root_param, :desc => "root param"
|
|
1624
|
-
group :nested do
|
|
1625
|
-
requires :nested_param, :desc => "nested param"
|
|
1626
|
-
end
|
|
1627
|
-
end
|
|
1628
|
-
subject.get 'method' do ; end
|
|
1629
|
-
subject.routes.map { |route|
|
|
1630
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1631
|
-
}.should eq [
|
|
1632
|
-
{ :description => "nesting",
|
|
1633
|
-
:params => {
|
|
1634
|
-
"root_param" => { :required => true, :desc => "root param" },
|
|
1635
|
-
"nested[nested_param]" => { :required => true, :desc => "nested param" }
|
|
1636
|
-
}
|
|
1637
|
-
}
|
|
1638
|
-
]
|
|
1639
|
-
end
|
|
1640
|
-
it 'parses parameters when no description is given' do
|
|
1641
|
-
subject.params do
|
|
1642
|
-
requires :one_param, :desc => "one param"
|
|
1643
|
-
end
|
|
1644
|
-
subject.get 'method' do ; end
|
|
1645
|
-
subject.routes.map { |route|
|
|
1646
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1647
|
-
}.should eq [
|
|
1648
|
-
{ :description => nil, :params => { "one_param" => { :required => true, :desc => "one param" } } }
|
|
1649
|
-
]
|
|
1650
|
-
end
|
|
1651
|
-
it 'does not symbolize params' do
|
|
1652
|
-
subject.desc "Reverses a string.", { :params =>
|
|
1653
|
-
{ "s" => { :desc => "string to reverse", :type => "string" }}
|
|
1654
|
-
}
|
|
1655
|
-
subject.get 'reverse/:s' do
|
|
1656
|
-
params[:s].reverse
|
|
1657
|
-
end
|
|
1658
|
-
subject.routes.map { |route|
|
|
1659
|
-
{ :description => route.route_description, :params => route.route_params }
|
|
1660
|
-
}.should eq [
|
|
1661
|
-
{ :description => "Reverses a string.", :params => { "s" => { :desc => "string to reverse", :type => "string" } } }
|
|
1662
|
-
]
|
|
1663
|
-
end
|
|
1664
|
-
end
|
|
1665
|
-
|
|
1666
|
-
describe '.mount' do
|
|
1667
|
-
let(:mounted_app){ lambda{|env| [200, {}, ["MOUNTED"]]} }
|
|
1668
|
-
|
|
1669
|
-
context 'with a bare rack app' do
|
|
1670
|
-
before do
|
|
1671
|
-
subject.mount mounted_app => '/mounty'
|
|
1672
|
-
end
|
|
1673
|
-
|
|
1674
|
-
it 'makes a bare Rack app available at the endpoint' do
|
|
1675
|
-
get '/mounty'
|
|
1676
|
-
last_response.body.should == 'MOUNTED'
|
|
1677
|
-
end
|
|
1678
|
-
|
|
1679
|
-
it 'anchors the routes, passing all subroutes to it' do
|
|
1680
|
-
get '/mounty/awesome'
|
|
1681
|
-
last_response.body.should == 'MOUNTED'
|
|
1682
|
-
end
|
|
1683
|
-
|
|
1684
|
-
it 'is able to cascade' do
|
|
1685
|
-
subject.mount lambda{ |env|
|
|
1686
|
-
headers = {}
|
|
1687
|
-
headers['X-Cascade'] == 'pass' unless env['PATH_INFO'].include?('boo')
|
|
1688
|
-
[200, headers, ["Farfegnugen"]]
|
|
1689
|
-
} => '/'
|
|
1690
|
-
|
|
1691
|
-
get '/boo'
|
|
1692
|
-
last_response.body.should == 'Farfegnugen'
|
|
1693
|
-
get '/mounty'
|
|
1694
|
-
last_response.body.should == 'MOUNTED'
|
|
1695
|
-
end
|
|
1696
|
-
end
|
|
1697
|
-
|
|
1698
|
-
context 'without a hash' do
|
|
1699
|
-
it 'calls through setting the route to "/"' do
|
|
1700
|
-
subject.mount mounted_app
|
|
1701
|
-
get '/'
|
|
1702
|
-
last_response.body.should == 'MOUNTED'
|
|
1703
|
-
end
|
|
1704
|
-
end
|
|
1705
|
-
|
|
1706
|
-
context 'mounting an API' do
|
|
1707
|
-
it 'applies the settings of the mounting api' do
|
|
1708
|
-
subject.version 'v1', :using => :path
|
|
1709
|
-
|
|
1710
|
-
subject.namespace :cool do
|
|
1711
|
-
app = Class.new(Grape::API)
|
|
1712
|
-
app.get('/awesome') do
|
|
1713
|
-
"yo"
|
|
1714
|
-
end
|
|
1715
|
-
|
|
1716
|
-
mount app
|
|
1717
|
-
end
|
|
1718
|
-
|
|
1719
|
-
get '/v1/cool/awesome'
|
|
1720
|
-
last_response.body.should == 'yo'
|
|
1721
|
-
end
|
|
1722
|
-
|
|
1723
|
-
it 'applies the settings to nested mounted apis' do
|
|
1724
|
-
subject.version 'v1', :using => :path
|
|
1725
|
-
|
|
1726
|
-
subject.namespace :cool do
|
|
1727
|
-
inner_app = Class.new(Grape::API)
|
|
1728
|
-
inner_app.get('/awesome') do
|
|
1729
|
-
"yo"
|
|
1730
|
-
end
|
|
1731
|
-
|
|
1732
|
-
app = Class.new(Grape::API)
|
|
1733
|
-
app.mount inner_app
|
|
1734
|
-
mount app
|
|
1735
|
-
end
|
|
1736
|
-
|
|
1737
|
-
get '/v1/cool/awesome'
|
|
1738
|
-
last_response.body.should == 'yo'
|
|
1739
|
-
end
|
|
1740
|
-
|
|
1741
|
-
it 'inherits rescues even when some defined by mounted' do
|
|
1742
|
-
subject.rescue_from :all do |e|
|
|
1743
|
-
rack_response("rescued from #{e.message}", 202)
|
|
1744
|
-
end
|
|
1745
|
-
subject.namespace :mounted do
|
|
1746
|
-
app = Class.new(Grape::API)
|
|
1747
|
-
app.rescue_from ArgumentError
|
|
1748
|
-
app.get('/fail') { raise "doh!" }
|
|
1749
|
-
mount app
|
|
1750
|
-
end
|
|
1751
|
-
get '/mounted/fail'
|
|
1752
|
-
last_response.status.should eql 202
|
|
1753
|
-
last_response.body.should == 'rescued from doh!'
|
|
1754
|
-
end
|
|
1755
|
-
|
|
1756
|
-
it 'collects the routes of the mounted api' do
|
|
1757
|
-
subject.namespace :cool do
|
|
1758
|
-
app = Class.new(Grape::API)
|
|
1759
|
-
app.get('/awesome') {}
|
|
1760
|
-
app.post('/sauce') {}
|
|
1761
|
-
mount app
|
|
1762
|
-
end
|
|
1763
|
-
subject.routes.size.should == 2
|
|
1764
|
-
subject.routes.first.route_path.should =~ /\/cool\/awesome/
|
|
1765
|
-
subject.routes.last.route_path.should =~ /\/cool\/sauce/
|
|
1766
|
-
end
|
|
1767
|
-
|
|
1768
|
-
it 'mounts on a path' do
|
|
1769
|
-
subject.namespace :cool do
|
|
1770
|
-
app = Class.new(Grape::API)
|
|
1771
|
-
app.get '/awesome' do
|
|
1772
|
-
"sauce"
|
|
1773
|
-
end
|
|
1774
|
-
mount app => '/mounted'
|
|
1775
|
-
end
|
|
1776
|
-
get "/mounted/cool/awesome"
|
|
1777
|
-
last_response.status.should == 200
|
|
1778
|
-
last_response.body.should == "sauce"
|
|
1779
|
-
end
|
|
1780
|
-
|
|
1781
|
-
it 'mounts on a nested path' do
|
|
1782
|
-
app1 = Class.new(Grape::API)
|
|
1783
|
-
app2 = Class.new(Grape::API)
|
|
1784
|
-
app2.get '/nice' do
|
|
1785
|
-
"play"
|
|
1786
|
-
end
|
|
1787
|
-
# note that the reverse won't work, mount from outside-in
|
|
1788
|
-
subject.mount app1 => '/app1'
|
|
1789
|
-
app1.mount app2 => '/app2'
|
|
1790
|
-
get "/app1/app2/nice"
|
|
1791
|
-
last_response.status.should == 200
|
|
1792
|
-
last_response.body.should == "play"
|
|
1793
|
-
end
|
|
1794
|
-
|
|
1795
|
-
end
|
|
1796
|
-
end
|
|
1797
|
-
|
|
1798
|
-
describe '.endpoints' do
|
|
1799
|
-
it 'adds one for each route created' do
|
|
1800
|
-
subject.get '/'
|
|
1801
|
-
subject.post '/'
|
|
1802
|
-
subject.endpoints.size.should == 2
|
|
1803
|
-
end
|
|
1804
|
-
end
|
|
1805
|
-
|
|
1806
|
-
describe '.compile' do
|
|
1807
|
-
it 'sets the instance' do
|
|
1808
|
-
subject.instance.should be_nil
|
|
1809
|
-
subject.compile
|
|
1810
|
-
subject.instance.should be_kind_of(subject)
|
|
1811
|
-
end
|
|
1812
|
-
end
|
|
1813
|
-
|
|
1814
|
-
describe '.change!' do
|
|
1815
|
-
it 'invalidates any compiled instance' do
|
|
1816
|
-
subject.compile
|
|
1817
|
-
subject.change!
|
|
1818
|
-
subject.instance.should be_nil
|
|
1819
|
-
end
|
|
1820
|
-
end
|
|
1821
|
-
|
|
1822
|
-
describe ".endpoint" do
|
|
1823
|
-
before(:each) do
|
|
1824
|
-
subject.format :json
|
|
1825
|
-
subject.get '/endpoint/options' do
|
|
1826
|
-
{
|
|
1827
|
-
:path => options[:path],
|
|
1828
|
-
:source_location => source.source_location
|
|
1829
|
-
}
|
|
1830
|
-
end
|
|
1831
|
-
end
|
|
1832
|
-
it 'path' do
|
|
1833
|
-
get '/endpoint/options'
|
|
1834
|
-
options = MultiJson.load(last_response.body)
|
|
1835
|
-
options["path"].should == ["/endpoint/options"]
|
|
1836
|
-
options["source_location"][0].should include "api_spec.rb"
|
|
1837
|
-
options["source_location"][1].to_i.should > 0
|
|
1838
|
-
end
|
|
1839
|
-
end
|
|
1840
|
-
|
|
1841
|
-
describe '.route' do
|
|
1842
|
-
context 'plain' do
|
|
1843
|
-
before(:each) do
|
|
1844
|
-
subject.get '/' do
|
|
1845
|
-
route.route_path
|
|
1846
|
-
end
|
|
1847
|
-
subject.get '/path' do
|
|
1848
|
-
route.route_path
|
|
1849
|
-
end
|
|
1850
|
-
end
|
|
1851
|
-
it 'provides access to route info' do
|
|
1852
|
-
get '/'
|
|
1853
|
-
last_response.body.should == "/(.:format)"
|
|
1854
|
-
get '/path'
|
|
1855
|
-
last_response.body.should == "/path(.:format)"
|
|
1856
|
-
end
|
|
1857
|
-
end
|
|
1858
|
-
context 'with desc' do
|
|
1859
|
-
before(:each) do
|
|
1860
|
-
subject.desc 'returns description'
|
|
1861
|
-
subject.get '/description' do
|
|
1862
|
-
route.route_description
|
|
1863
|
-
end
|
|
1864
|
-
subject.desc 'returns parameters', { :params => { "x" => "y" }}
|
|
1865
|
-
subject.get '/params/:id' do
|
|
1866
|
-
route.route_params[params[:id]]
|
|
1867
|
-
end
|
|
1868
|
-
end
|
|
1869
|
-
it 'returns route description' do
|
|
1870
|
-
get '/description'
|
|
1871
|
-
last_response.body.should == "returns description"
|
|
1872
|
-
end
|
|
1873
|
-
it 'returns route parameters' do
|
|
1874
|
-
get '/params/x'
|
|
1875
|
-
last_response.body.should == "y"
|
|
1876
|
-
end
|
|
1877
|
-
end
|
|
1878
|
-
end
|
|
1879
|
-
describe '.format' do
|
|
1880
|
-
context ':txt' do
|
|
1881
|
-
before(:each) do
|
|
1882
|
-
subject.format :txt
|
|
1883
|
-
subject.content_type :json, "application/json"
|
|
1884
|
-
subject.get '/meaning_of_life' do
|
|
1885
|
-
{ :meaning_of_life => 42 }
|
|
1886
|
-
end
|
|
1887
|
-
end
|
|
1888
|
-
it 'forces txt without an extension' do
|
|
1889
|
-
get '/meaning_of_life'
|
|
1890
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1891
|
-
end
|
|
1892
|
-
it 'does not force txt with an extension' do
|
|
1893
|
-
get '/meaning_of_life.json'
|
|
1894
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_json
|
|
1895
|
-
end
|
|
1896
|
-
it 'forces txt from a non-accepting header' do
|
|
1897
|
-
get '/meaning_of_life', {}, { 'HTTP_ACCEPT' => 'application/json' }
|
|
1898
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1899
|
-
end
|
|
1900
|
-
end
|
|
1901
|
-
context ':txt only' do
|
|
1902
|
-
before(:each) do
|
|
1903
|
-
subject.format :txt
|
|
1904
|
-
subject.get '/meaning_of_life' do
|
|
1905
|
-
{ :meaning_of_life => 42 }
|
|
1906
|
-
end
|
|
1907
|
-
end
|
|
1908
|
-
it 'forces txt without an extension' do
|
|
1909
|
-
get '/meaning_of_life'
|
|
1910
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1911
|
-
end
|
|
1912
|
-
it 'forces txt with the wrong extension' do
|
|
1913
|
-
get '/meaning_of_life.json'
|
|
1914
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1915
|
-
end
|
|
1916
|
-
it 'forces txt from a non-accepting header' do
|
|
1917
|
-
get '/meaning_of_life', {}, { 'HTTP_ACCEPT' => 'application/json' }
|
|
1918
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1919
|
-
end
|
|
1920
|
-
end
|
|
1921
|
-
context ':json' do
|
|
1922
|
-
before(:each) do
|
|
1923
|
-
subject.format :json
|
|
1924
|
-
subject.content_type :txt, "text/plain"
|
|
1925
|
-
subject.get '/meaning_of_life' do
|
|
1926
|
-
{ :meaning_of_life => 42 }
|
|
1927
|
-
end
|
|
1928
|
-
end
|
|
1929
|
-
it 'forces json without an extension' do
|
|
1930
|
-
get '/meaning_of_life'
|
|
1931
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_json
|
|
1932
|
-
end
|
|
1933
|
-
it 'does not force json with an extension' do
|
|
1934
|
-
get '/meaning_of_life.txt'
|
|
1935
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1936
|
-
end
|
|
1937
|
-
it 'forces json from a non-accepting header' do
|
|
1938
|
-
get '/meaning_of_life', {}, { 'HTTP_ACCEPT' => 'text/html' }
|
|
1939
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_json
|
|
1940
|
-
end
|
|
1941
|
-
it 'can be overwritten with an explicit content type' do
|
|
1942
|
-
subject.get '/meaning_of_life_with_content_type' do
|
|
1943
|
-
content_type "text/plain"
|
|
1944
|
-
{ :meaning_of_life => 42 }.to_s
|
|
1945
|
-
end
|
|
1946
|
-
get '/meaning_of_life_with_content_type'
|
|
1947
|
-
last_response.body.should == { :meaning_of_life => 42 }.to_s
|
|
1948
|
-
end
|
|
1949
|
-
it 'raised :error from middleware' do
|
|
1950
|
-
middleware = Class.new(Grape::Middleware::Base) do
|
|
1951
|
-
def before
|
|
1952
|
-
throw :error, :message => "Unauthorized", :status => 42
|
|
1953
|
-
end
|
|
1954
|
-
end
|
|
1955
|
-
subject.use middleware
|
|
1956
|
-
subject.get do
|
|
1957
|
-
|
|
1958
|
-
end
|
|
1959
|
-
get "/"
|
|
1960
|
-
last_response.status.should == 42
|
|
1961
|
-
last_response.body.should == { :error => "Unauthorized" }.to_json
|
|
1962
|
-
end
|
|
1963
|
-
|
|
1964
|
-
end
|
|
1965
|
-
context ':serializable_hash' do
|
|
1966
|
-
before(:each) do
|
|
1967
|
-
class SerializableHashExample
|
|
1968
|
-
def serializable_hash
|
|
1969
|
-
{ :abc => 'def' }
|
|
1970
|
-
end
|
|
1971
|
-
end
|
|
1972
|
-
subject.format :serializable_hash
|
|
1973
|
-
end
|
|
1974
|
-
it 'instance' do
|
|
1975
|
-
subject.get '/example' do
|
|
1976
|
-
SerializableHashExample.new
|
|
1977
|
-
end
|
|
1978
|
-
get '/example'
|
|
1979
|
-
last_response.body.should == '{"abc":"def"}'
|
|
1980
|
-
end
|
|
1981
|
-
it 'root' do
|
|
1982
|
-
subject.get '/example' do
|
|
1983
|
-
{ "root" => SerializableHashExample.new }
|
|
1984
|
-
end
|
|
1985
|
-
get '/example'
|
|
1986
|
-
last_response.body.should == '{"root":{"abc":"def"}}'
|
|
1987
|
-
end
|
|
1988
|
-
it 'array' do
|
|
1989
|
-
subject.get '/examples' do
|
|
1990
|
-
[ SerializableHashExample.new, SerializableHashExample.new ]
|
|
1991
|
-
end
|
|
1992
|
-
get '/examples'
|
|
1993
|
-
last_response.body.should == '[{"abc":"def"},{"abc":"def"}]'
|
|
1994
|
-
end
|
|
1995
|
-
end
|
|
1996
|
-
context ":xml" do
|
|
1997
|
-
before(:each) do
|
|
1998
|
-
subject.format :xml
|
|
1999
|
-
end
|
|
2000
|
-
it 'string' do
|
|
2001
|
-
subject.get "/example" do
|
|
2002
|
-
"example"
|
|
2003
|
-
end
|
|
2004
|
-
get '/example'
|
|
2005
|
-
last_response.status.should == 500
|
|
2006
|
-
last_response.body.should == <<-XML
|
|
2007
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2008
|
-
<error>
|
|
2009
|
-
<message>cannot convert String to xml</message>
|
|
2010
|
-
</error>
|
|
2011
|
-
XML
|
|
2012
|
-
end
|
|
2013
|
-
it 'hash' do
|
|
2014
|
-
subject.get "/example" do
|
|
2015
|
-
ActiveSupport::OrderedHash[
|
|
2016
|
-
:example1, "example1",
|
|
2017
|
-
:example2, "example2"
|
|
2018
|
-
]
|
|
2019
|
-
end
|
|
2020
|
-
get '/example'
|
|
2021
|
-
last_response.status.should == 200
|
|
2022
|
-
last_response.body.should == <<-XML
|
|
2023
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2024
|
-
<hash>
|
|
2025
|
-
<example1>example1</example1>
|
|
2026
|
-
<example2>example2</example2>
|
|
2027
|
-
</hash>
|
|
2028
|
-
XML
|
|
2029
|
-
end
|
|
2030
|
-
it 'array' do
|
|
2031
|
-
subject.get "/example" do
|
|
2032
|
-
[ "example1", "example2" ]
|
|
2033
|
-
end
|
|
2034
|
-
get '/example'
|
|
2035
|
-
last_response.status.should == 200
|
|
2036
|
-
last_response.body.should == <<-XML
|
|
2037
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2038
|
-
<strings type="array">
|
|
2039
|
-
<string>example1</string>
|
|
2040
|
-
<string>example2</string>
|
|
2041
|
-
</strings>
|
|
2042
|
-
XML
|
|
2043
|
-
end
|
|
2044
|
-
it 'raised :error from middleware' do
|
|
2045
|
-
middleware = Class.new(Grape::Middleware::Base) do
|
|
2046
|
-
def before
|
|
2047
|
-
throw :error, :message => "Unauthorized", :status => 42
|
|
2048
|
-
end
|
|
2049
|
-
end
|
|
2050
|
-
subject.use middleware
|
|
2051
|
-
subject.get do
|
|
2052
|
-
|
|
2053
|
-
end
|
|
2054
|
-
get "/"
|
|
2055
|
-
last_response.status.should == 42
|
|
2056
|
-
last_response.body.should == <<-XML
|
|
2057
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
2058
|
-
<error>
|
|
2059
|
-
<message>Unauthorized</message>
|
|
2060
|
-
</error>
|
|
2061
|
-
XML
|
|
2062
|
-
end
|
|
2063
|
-
end
|
|
2064
|
-
end
|
|
2065
|
-
|
|
2066
|
-
context "catch-all" do
|
|
2067
|
-
before do
|
|
2068
|
-
api1 = Class.new(Grape::API)
|
|
2069
|
-
api1.version 'v1', :using => :path
|
|
2070
|
-
api1.get "hello" do
|
|
2071
|
-
"v1"
|
|
2072
|
-
end
|
|
2073
|
-
api2 = Class.new(Grape::API)
|
|
2074
|
-
api2.version 'v2', :using => :path
|
|
2075
|
-
api2.get "hello" do
|
|
2076
|
-
"v2"
|
|
2077
|
-
end
|
|
2078
|
-
subject.mount api1
|
|
2079
|
-
subject.mount api2
|
|
2080
|
-
end
|
|
2081
|
-
[ true, false ].each do |anchor|
|
|
2082
|
-
it "anchor=#{anchor}" do
|
|
2083
|
-
subject.route :any, '*path', :anchor => anchor do
|
|
2084
|
-
error!("Unrecognized request path: #{params[:path]} - #{env['PATH_INFO']}#{env['SCRIPT_NAME']}", 404)
|
|
2085
|
-
end
|
|
2086
|
-
get "/v1/hello"
|
|
2087
|
-
last_response.status.should == 200
|
|
2088
|
-
last_response.body.should == "v1"
|
|
2089
|
-
get "/v2/hello"
|
|
2090
|
-
last_response.status.should == 200
|
|
2091
|
-
last_response.body.should == "v2"
|
|
2092
|
-
get "/foobar"
|
|
2093
|
-
last_response.status.should == 404
|
|
2094
|
-
last_response.body.should == "Unrecognized request path: foobar - /foobar"
|
|
2095
|
-
end
|
|
2096
|
-
end
|
|
2097
|
-
end
|
|
2098
|
-
|
|
2099
|
-
context "cascading" do
|
|
2100
|
-
context "via version" do
|
|
2101
|
-
it "cascades" do
|
|
2102
|
-
subject.version 'v1', :using => :path, :cascade => true
|
|
2103
|
-
get "/v1/hello"
|
|
2104
|
-
last_response.status.should == 404
|
|
2105
|
-
last_response.headers["X-Cascade"].should == "pass"
|
|
2106
|
-
end
|
|
2107
|
-
it "does not cascade" do
|
|
2108
|
-
subject.version 'v2', :using => :path, :cascade => false
|
|
2109
|
-
get "/v2/hello"
|
|
2110
|
-
last_response.status.should == 404
|
|
2111
|
-
last_response.headers.keys.should_not include "X-Cascade"
|
|
2112
|
-
end
|
|
2113
|
-
end
|
|
2114
|
-
context "via endpoint" do
|
|
2115
|
-
it "cascades" do
|
|
2116
|
-
subject.cascade true
|
|
2117
|
-
get "/hello"
|
|
2118
|
-
last_response.status.should == 404
|
|
2119
|
-
last_response.headers["X-Cascade"].should == "pass"
|
|
2120
|
-
end
|
|
2121
|
-
it "does not cascade" do
|
|
2122
|
-
subject.cascade false
|
|
2123
|
-
get "/hello"
|
|
2124
|
-
last_response.status.should == 404
|
|
2125
|
-
last_response.headers.keys.should_not include "X-Cascade"
|
|
2126
|
-
end
|
|
2127
|
-
end
|
|
2128
|
-
end
|
|
2129
|
-
end
|