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/endpoint_spec.rb
DELETED
|
@@ -1,605 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Grape::Endpoint do
|
|
4
|
-
subject { Class.new(Grape::API) }
|
|
5
|
-
def app; subject end
|
|
6
|
-
|
|
7
|
-
describe '#initialize' do
|
|
8
|
-
it 'takes a settings stack, options, and a block' do
|
|
9
|
-
p = Proc.new {}
|
|
10
|
-
expect {
|
|
11
|
-
Grape::Endpoint.new(Grape::Util::HashStack.new, {
|
|
12
|
-
:path => '/',
|
|
13
|
-
:method => :get
|
|
14
|
-
}, &p)
|
|
15
|
-
}.not_to raise_error
|
|
16
|
-
end
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
it 'sets itself in the env upon call' do
|
|
20
|
-
subject.get('/'){ "Hello world." }
|
|
21
|
-
get '/'
|
|
22
|
-
last_request.env['api.endpoint'].should be_kind_of(Grape::Endpoint)
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
describe '#status' do
|
|
26
|
-
it 'is callable from within a block' do
|
|
27
|
-
subject.get('/home') do
|
|
28
|
-
status 206
|
|
29
|
-
"Hello"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
get '/home'
|
|
33
|
-
last_response.status.should == 206
|
|
34
|
-
last_response.body.should == "Hello"
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
describe '#header' do
|
|
39
|
-
it 'is callable from within a block' do
|
|
40
|
-
subject.get('/hey') do
|
|
41
|
-
header 'X-Awesome', 'true'
|
|
42
|
-
"Awesome"
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
get '/hey'
|
|
46
|
-
last_response.headers['X-Awesome'].should == 'true'
|
|
47
|
-
end
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
describe '#headers' do
|
|
51
|
-
before do
|
|
52
|
-
subject.get('/headers') do
|
|
53
|
-
headers.to_json
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
it 'includes request headers' do
|
|
57
|
-
get '/headers'
|
|
58
|
-
JSON.parse(last_response.body).should == {
|
|
59
|
-
"Host" => "example.org",
|
|
60
|
-
"Cookie" => ""
|
|
61
|
-
}
|
|
62
|
-
end
|
|
63
|
-
it 'includes additional request headers' do
|
|
64
|
-
get '/headers', nil, { "HTTP_X_GRAPE_CLIENT" => "1" }
|
|
65
|
-
JSON.parse(last_response.body)["X-Grape-Client"].should == "1"
|
|
66
|
-
end
|
|
67
|
-
it 'includes headers passed as symbols' do
|
|
68
|
-
env = Rack::MockRequest.env_for("/headers")
|
|
69
|
-
env[:HTTP_SYMBOL_HEADER] = "Goliath passes symbols"
|
|
70
|
-
body = subject.call(env)[2].body.first
|
|
71
|
-
JSON.parse(body)["Symbol-Header"].should == "Goliath passes symbols"
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
describe '#cookies' do
|
|
76
|
-
it 'is callable from within a block' do
|
|
77
|
-
subject.get('/get/cookies') do
|
|
78
|
-
cookies['my-awesome-cookie1'] = 'is cool'
|
|
79
|
-
cookies['my-awesome-cookie2'] = {
|
|
80
|
-
:value => 'is cool too',
|
|
81
|
-
:domain => 'my.example.com',
|
|
82
|
-
:path => '/',
|
|
83
|
-
:secure => true,
|
|
84
|
-
}
|
|
85
|
-
cookies[:cookie3] = 'symbol'
|
|
86
|
-
cookies['cookie4'] = 'secret code here'
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
get('/get/cookies')
|
|
90
|
-
|
|
91
|
-
last_response.headers['Set-Cookie'].split("\n").sort.should eql [
|
|
92
|
-
"cookie3=symbol",
|
|
93
|
-
"cookie4=secret+code+here",
|
|
94
|
-
"my-awesome-cookie1=is+cool",
|
|
95
|
-
"my-awesome-cookie2=is+cool+too; domain=my.example.com; path=/; secure"
|
|
96
|
-
]
|
|
97
|
-
end
|
|
98
|
-
|
|
99
|
-
it 'sets browser cookies and does not set response cookies' do
|
|
100
|
-
subject.get('/username') do
|
|
101
|
-
cookies[:username]
|
|
102
|
-
end
|
|
103
|
-
get('/username', {}, 'HTTP_COOKIE' => 'username=mrplum; sandbox=true')
|
|
104
|
-
|
|
105
|
-
last_response.body.should == 'mrplum'
|
|
106
|
-
last_response.headers['Set-Cookie'].should_not =~ /username=mrplum/
|
|
107
|
-
last_response.headers['Set-Cookie'].should_not =~ /sandbox=true/
|
|
108
|
-
end
|
|
109
|
-
|
|
110
|
-
it 'sets and update browser cookies' do
|
|
111
|
-
subject.get('/username') do
|
|
112
|
-
cookies[:sandbox] = true if cookies[:sandbox] == 'false'
|
|
113
|
-
cookies[:username] += "_test"
|
|
114
|
-
end
|
|
115
|
-
get('/username', {}, 'HTTP_COOKIE' => 'username=user; sandbox=false')
|
|
116
|
-
last_response.body.should == 'user_test'
|
|
117
|
-
last_response.headers['Set-Cookie'].should =~ /username=user_test/
|
|
118
|
-
last_response.headers['Set-Cookie'].should =~ /sandbox=true/
|
|
119
|
-
end
|
|
120
|
-
|
|
121
|
-
it 'deletes cookie' do
|
|
122
|
-
subject.get('/test') do
|
|
123
|
-
sum = 0
|
|
124
|
-
cookies.each do |name, val|
|
|
125
|
-
sum += val.to_i
|
|
126
|
-
cookies.delete name
|
|
127
|
-
end
|
|
128
|
-
sum
|
|
129
|
-
end
|
|
130
|
-
get('/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2')
|
|
131
|
-
last_response.body.should == '3'
|
|
132
|
-
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
|
|
133
|
-
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
|
134
|
-
[ cookie.name, cookie ]
|
|
135
|
-
end]
|
|
136
|
-
cookies.size.should == 2
|
|
137
|
-
[ "and_this", "delete_this_cookie" ].each do |cookie_name|
|
|
138
|
-
cookie = cookies[cookie_name]
|
|
139
|
-
cookie.should_not be_nil
|
|
140
|
-
cookie.value.should == "deleted"
|
|
141
|
-
cookie.expired?.should be_true
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
|
-
|
|
145
|
-
it 'deletes cookies with path' do
|
|
146
|
-
subject.get('/test') do
|
|
147
|
-
sum = 0
|
|
148
|
-
cookies.each do |name, val|
|
|
149
|
-
sum += val.to_i
|
|
150
|
-
cookies.delete name, { :path => '/test' }
|
|
151
|
-
end
|
|
152
|
-
sum
|
|
153
|
-
end
|
|
154
|
-
get('/test', {}, 'HTTP_COOKIE' => 'delete_this_cookie=1; and_this=2')
|
|
155
|
-
last_response.body.should == '3'
|
|
156
|
-
cookies = Hash[last_response.headers['Set-Cookie'].split("\n").map do |set_cookie|
|
|
157
|
-
cookie = CookieJar::Cookie.from_set_cookie 'http://localhost/test', set_cookie
|
|
158
|
-
[ cookie.name, cookie ]
|
|
159
|
-
end]
|
|
160
|
-
cookies.size.should == 2
|
|
161
|
-
[ "and_this", "delete_this_cookie" ].each do |cookie_name|
|
|
162
|
-
cookie = cookies[cookie_name]
|
|
163
|
-
cookie.should_not be_nil
|
|
164
|
-
cookie.value.should == "deleted"
|
|
165
|
-
cookie.path.should == "/test"
|
|
166
|
-
cookie.expired?.should be_true
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
describe '#declared' do
|
|
172
|
-
before do
|
|
173
|
-
subject.params do
|
|
174
|
-
requires :first
|
|
175
|
-
optional :second
|
|
176
|
-
optional :third, :default => 'third-default'
|
|
177
|
-
group :nested do
|
|
178
|
-
optional :fourth
|
|
179
|
-
end
|
|
180
|
-
end
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
it 'has as many keys as there are declared params' do
|
|
184
|
-
subject.get '/declared' do
|
|
185
|
-
declared(params).keys.size.should == 4
|
|
186
|
-
""
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
get '/declared?first=present'
|
|
190
|
-
last_response.status.should == 200
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
it 'has a optional param with default value all the time' do
|
|
194
|
-
subject.get '/declared' do
|
|
195
|
-
params[:third].should == 'third-default'
|
|
196
|
-
""
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
get '/declared?first=one'
|
|
200
|
-
last_response.status.should == 200
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
it 'builds nested params' do
|
|
204
|
-
subject.get '/declared' do
|
|
205
|
-
declared(params)[:nested].keys.size.should == 1
|
|
206
|
-
""
|
|
207
|
-
end
|
|
208
|
-
|
|
209
|
-
get '/declared?first=present&nested[fourth]=1'
|
|
210
|
-
last_response.status.should == 200
|
|
211
|
-
end
|
|
212
|
-
|
|
213
|
-
it 'builds nested params when given array' do
|
|
214
|
-
subject.get '/declared' do
|
|
215
|
-
declared(params)[:nested].size.should == 2
|
|
216
|
-
""
|
|
217
|
-
end
|
|
218
|
-
|
|
219
|
-
get '/declared?first=present&nested[][fourth]=1&nested[][fourth]=2'
|
|
220
|
-
last_response.status.should == 200
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
it 'filters out any additional params that are given' do
|
|
224
|
-
subject.get '/declared' do
|
|
225
|
-
declared(params).key?(:other).should == false
|
|
226
|
-
""
|
|
227
|
-
end
|
|
228
|
-
|
|
229
|
-
get '/declared?first=one&other=two'
|
|
230
|
-
last_response.status.should == 200
|
|
231
|
-
end
|
|
232
|
-
|
|
233
|
-
it 'stringifies if that option is passed' do
|
|
234
|
-
subject.get '/declared' do
|
|
235
|
-
declared(params, :stringify => true)["first"].should == "one"
|
|
236
|
-
""
|
|
237
|
-
end
|
|
238
|
-
|
|
239
|
-
get '/declared?first=one&other=two'
|
|
240
|
-
last_response.status.should == 200
|
|
241
|
-
end
|
|
242
|
-
|
|
243
|
-
it 'does not include missing attributes if that option is passed' do
|
|
244
|
-
subject.get '/declared' do
|
|
245
|
-
declared(params, :include_missing => false)[:second].should == nil
|
|
246
|
-
""
|
|
247
|
-
end
|
|
248
|
-
|
|
249
|
-
get '/declared?first=one&other=two'
|
|
250
|
-
last_response.status.should == 200
|
|
251
|
-
end
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
describe '#params' do
|
|
255
|
-
it 'is available to the caller' do
|
|
256
|
-
subject.get('/hey') do
|
|
257
|
-
params[:howdy]
|
|
258
|
-
end
|
|
259
|
-
|
|
260
|
-
get '/hey?howdy=hey'
|
|
261
|
-
last_response.body.should == 'hey'
|
|
262
|
-
end
|
|
263
|
-
|
|
264
|
-
it 'parses from path segments' do
|
|
265
|
-
subject.get('/hey/:id') do
|
|
266
|
-
params[:id]
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
get '/hey/12'
|
|
270
|
-
last_response.body.should == '12'
|
|
271
|
-
end
|
|
272
|
-
|
|
273
|
-
it 'deeply converts nested params' do
|
|
274
|
-
subject.get '/location' do
|
|
275
|
-
params[:location][:city]
|
|
276
|
-
end
|
|
277
|
-
get '/location?location[city]=Dallas'
|
|
278
|
-
last_response.body.should == 'Dallas'
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
context 'with special requirements' do
|
|
282
|
-
it 'parses email param with provided requirements for params' do
|
|
283
|
-
subject.get('/:person_email', :requirements => { :person_email => /.*/ }) do
|
|
284
|
-
params[:person_email]
|
|
285
|
-
end
|
|
286
|
-
|
|
287
|
-
get '/someone@example.com'
|
|
288
|
-
last_response.body.should == 'someone@example.com'
|
|
289
|
-
|
|
290
|
-
get 'someone@example.com.pl'
|
|
291
|
-
last_response.body.should == 'someone@example.com.pl'
|
|
292
|
-
end
|
|
293
|
-
|
|
294
|
-
it 'parses many params with provided regexps' do
|
|
295
|
-
subject.get('/:person_email/test/:number',
|
|
296
|
-
:requirements => {
|
|
297
|
-
:person_email => /someone@(.*).com/,
|
|
298
|
-
:number => /[0-9]/ }) do
|
|
299
|
-
params[:person_email] << params[:number]
|
|
300
|
-
end
|
|
301
|
-
|
|
302
|
-
get '/someone@example.com/test/1'
|
|
303
|
-
last_response.body.should == 'someone@example.com1'
|
|
304
|
-
|
|
305
|
-
get '/someone@testing.wrong/test/1'
|
|
306
|
-
last_response.status.should == 404
|
|
307
|
-
|
|
308
|
-
get 'someone@test.com/test/wrong_number'
|
|
309
|
-
last_response.status.should == 404
|
|
310
|
-
|
|
311
|
-
get 'someone@test.com/wrong_middle/1'
|
|
312
|
-
last_response.status.should == 404
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
context 'namespace requirements' do
|
|
316
|
-
before :each do
|
|
317
|
-
subject.namespace :outer, :requirements => { :person_email => /abc@(.*).com/ } do
|
|
318
|
-
get('/:person_email') do
|
|
319
|
-
params[:person_email]
|
|
320
|
-
end
|
|
321
|
-
|
|
322
|
-
namespace :inner, :requirements => {:number => /[0-9]/, :person_email => /someone@(.*).com/ }do
|
|
323
|
-
get '/:person_email/test/:number' do
|
|
324
|
-
params[:person_email] << params[:number]
|
|
325
|
-
end
|
|
326
|
-
end
|
|
327
|
-
end
|
|
328
|
-
end
|
|
329
|
-
it "parse email param with provided requirements for params" do
|
|
330
|
-
get '/outer/abc@example.com'
|
|
331
|
-
last_response.body.should == 'abc@example.com'
|
|
332
|
-
end
|
|
333
|
-
|
|
334
|
-
it "should override outer namespace's requirements" do
|
|
335
|
-
get '/outer/inner/someone@testing.wrong/test/1'
|
|
336
|
-
last_response.status.should == 404
|
|
337
|
-
|
|
338
|
-
get '/outer/inner/someone@testing.com/test/1'
|
|
339
|
-
last_response.status.should == 200
|
|
340
|
-
last_response.body.should == 'someone@testing.com1'
|
|
341
|
-
end
|
|
342
|
-
|
|
343
|
-
end
|
|
344
|
-
end
|
|
345
|
-
|
|
346
|
-
context 'from body parameters' do
|
|
347
|
-
before(:each) do
|
|
348
|
-
subject.post '/request_body' do
|
|
349
|
-
params[:user]
|
|
350
|
-
end
|
|
351
|
-
subject.put '/request_body' do
|
|
352
|
-
params[:user]
|
|
353
|
-
end
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
it 'converts JSON bodies to params' do
|
|
357
|
-
post '/request_body', MultiJson.dump(:user => 'Bobby T.'), {'CONTENT_TYPE' => 'application/json'}
|
|
358
|
-
last_response.body.should == 'Bobby T.'
|
|
359
|
-
end
|
|
360
|
-
|
|
361
|
-
it 'does not convert empty JSON bodies to params' do
|
|
362
|
-
put '/request_body', '', {'CONTENT_TYPE' => 'application/json'}
|
|
363
|
-
last_response.body.should == ''
|
|
364
|
-
end
|
|
365
|
-
|
|
366
|
-
it 'converts XML bodies to params' do
|
|
367
|
-
post '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
|
368
|
-
last_response.body.should == 'Bobby T.'
|
|
369
|
-
end
|
|
370
|
-
|
|
371
|
-
it 'converts XML bodies to params' do
|
|
372
|
-
put '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
|
373
|
-
last_response.body.should == 'Bobby T.'
|
|
374
|
-
end
|
|
375
|
-
|
|
376
|
-
it 'does not include parameters not defined by the body' do
|
|
377
|
-
subject.post '/omitted_params' do
|
|
378
|
-
params[:version].should == nil
|
|
379
|
-
params[:user].should == 'Bob'
|
|
380
|
-
end
|
|
381
|
-
post '/omitted_params', MultiJson.dump(:user => 'Bob'), {'CONTENT_TYPE' => 'application/json'}
|
|
382
|
-
end
|
|
383
|
-
end
|
|
384
|
-
|
|
385
|
-
it "responds with a 406 for an unsupported content-type" do
|
|
386
|
-
subject.format :json
|
|
387
|
-
# subject.content_type :json, "application/json"
|
|
388
|
-
subject.put '/request_body' do
|
|
389
|
-
params[:user]
|
|
390
|
-
end
|
|
391
|
-
put '/request_body', '<user>Bobby T.</user>', {'CONTENT_TYPE' => 'application/xml'}
|
|
392
|
-
last_response.status.should == 406
|
|
393
|
-
last_response.body.should == '{"error":"The requested content-type \'application/xml\' is not supported."}'
|
|
394
|
-
end
|
|
395
|
-
|
|
396
|
-
end
|
|
397
|
-
|
|
398
|
-
describe '#error!' do
|
|
399
|
-
it 'accepts a message' do
|
|
400
|
-
subject.get('/hey') do
|
|
401
|
-
error! "This is not valid."
|
|
402
|
-
"This is valid."
|
|
403
|
-
end
|
|
404
|
-
|
|
405
|
-
get '/hey'
|
|
406
|
-
last_response.status.should == 403
|
|
407
|
-
last_response.body.should == "This is not valid."
|
|
408
|
-
end
|
|
409
|
-
|
|
410
|
-
it 'accepts a code' do
|
|
411
|
-
subject.get('/hey') do
|
|
412
|
-
error! "Unauthorized.", 401
|
|
413
|
-
end
|
|
414
|
-
|
|
415
|
-
get '/hey'
|
|
416
|
-
last_response.status.should == 401
|
|
417
|
-
last_response.body.should == "Unauthorized."
|
|
418
|
-
end
|
|
419
|
-
|
|
420
|
-
it 'accepts an object and render it in format' do
|
|
421
|
-
subject.get '/hey' do
|
|
422
|
-
error!({'dude' => 'rad'}, 403)
|
|
423
|
-
end
|
|
424
|
-
|
|
425
|
-
get '/hey.json'
|
|
426
|
-
last_response.status.should == 403
|
|
427
|
-
last_response.body.should == '{"dude":"rad"}'
|
|
428
|
-
end
|
|
429
|
-
end
|
|
430
|
-
|
|
431
|
-
describe '#redirect' do
|
|
432
|
-
it 'redirects to a url with status 302' do
|
|
433
|
-
subject.get('/hey') do
|
|
434
|
-
redirect "/ha"
|
|
435
|
-
end
|
|
436
|
-
get '/hey'
|
|
437
|
-
last_response.status.should eq 302
|
|
438
|
-
last_response.headers['Location'].should eq "/ha"
|
|
439
|
-
last_response.body.should eq ""
|
|
440
|
-
end
|
|
441
|
-
|
|
442
|
-
it 'has status code 303 if it is not get request and it is http 1.1' do
|
|
443
|
-
subject.post('/hey') do
|
|
444
|
-
redirect "/ha"
|
|
445
|
-
end
|
|
446
|
-
post '/hey', {}, 'HTTP_VERSION' => 'HTTP/1.1'
|
|
447
|
-
last_response.status.should eq 303
|
|
448
|
-
last_response.headers['Location'].should eq "/ha"
|
|
449
|
-
end
|
|
450
|
-
|
|
451
|
-
it 'support permanent redirect' do
|
|
452
|
-
subject.get('/hey') do
|
|
453
|
-
redirect "/ha", :permanent => true
|
|
454
|
-
end
|
|
455
|
-
get '/hey'
|
|
456
|
-
last_response.status.should eq 301
|
|
457
|
-
last_response.headers['Location'].should eq "/ha"
|
|
458
|
-
last_response.body.should eq ""
|
|
459
|
-
end
|
|
460
|
-
end
|
|
461
|
-
|
|
462
|
-
it 'does not persist params between calls' do
|
|
463
|
-
subject.post('/new') do
|
|
464
|
-
params[:text]
|
|
465
|
-
end
|
|
466
|
-
|
|
467
|
-
post '/new', :text => 'abc'
|
|
468
|
-
last_response.body.should == 'abc'
|
|
469
|
-
|
|
470
|
-
post '/new', :text => 'def'
|
|
471
|
-
last_response.body.should == 'def'
|
|
472
|
-
end
|
|
473
|
-
|
|
474
|
-
it 'resets all instance variables (except block) between calls' do
|
|
475
|
-
subject.helpers do
|
|
476
|
-
def memoized
|
|
477
|
-
@memoized ||= params[:howdy]
|
|
478
|
-
end
|
|
479
|
-
end
|
|
480
|
-
|
|
481
|
-
subject.get('/hello') do
|
|
482
|
-
memoized
|
|
483
|
-
end
|
|
484
|
-
|
|
485
|
-
get '/hello?howdy=hey'
|
|
486
|
-
last_response.body.should == 'hey'
|
|
487
|
-
get '/hello?howdy=yo'
|
|
488
|
-
last_response.body.should == 'yo'
|
|
489
|
-
end
|
|
490
|
-
|
|
491
|
-
it 'allows explicit return calls' do
|
|
492
|
-
subject.get('/home') do
|
|
493
|
-
return "Hello"
|
|
494
|
-
end
|
|
495
|
-
|
|
496
|
-
get '/home'
|
|
497
|
-
last_response.status.should == 200
|
|
498
|
-
last_response.body.should == "Hello"
|
|
499
|
-
end
|
|
500
|
-
|
|
501
|
-
describe '.generate_api_method' do
|
|
502
|
-
it 'raises NameError if the method name is already in use' do
|
|
503
|
-
expect {
|
|
504
|
-
Grape::Endpoint.generate_api_method("version", &proc{})
|
|
505
|
-
}.to raise_error(NameError)
|
|
506
|
-
end
|
|
507
|
-
it 'raises ArgumentError if a block is not given' do
|
|
508
|
-
expect {
|
|
509
|
-
Grape::Endpoint.generate_api_method("GET without a block method")
|
|
510
|
-
}.to raise_error(ArgumentError)
|
|
511
|
-
end
|
|
512
|
-
it 'returns a Proc' do
|
|
513
|
-
Grape::Endpoint.generate_api_method("GET test for a proc", &proc{}).should be_a Proc
|
|
514
|
-
end
|
|
515
|
-
end
|
|
516
|
-
|
|
517
|
-
context 'filters' do
|
|
518
|
-
describe 'before filters' do
|
|
519
|
-
it 'runs the before filter if set' do
|
|
520
|
-
subject.before{ env['before_test'] = "OK" }
|
|
521
|
-
subject.get('/before_test'){ env['before_test'] }
|
|
522
|
-
|
|
523
|
-
get '/before_test'
|
|
524
|
-
last_response.body.should == "OK"
|
|
525
|
-
end
|
|
526
|
-
end
|
|
527
|
-
|
|
528
|
-
describe 'after filters' do
|
|
529
|
-
it 'overrides the response body if it sets it' do
|
|
530
|
-
subject.after{ body "after" }
|
|
531
|
-
subject.get('/after_test'){ "during" }
|
|
532
|
-
get '/after_test'
|
|
533
|
-
last_response.body.should == 'after'
|
|
534
|
-
end
|
|
535
|
-
|
|
536
|
-
it 'does not override the response body with its return' do
|
|
537
|
-
subject.after{ "after" }
|
|
538
|
-
subject.get('/after_test'){ "body" }
|
|
539
|
-
get '/after_test'
|
|
540
|
-
last_response.body.should == "body"
|
|
541
|
-
end
|
|
542
|
-
end
|
|
543
|
-
end
|
|
544
|
-
|
|
545
|
-
context 'anchoring' do
|
|
546
|
-
verbs = %w(post get head delete put options patch)
|
|
547
|
-
|
|
548
|
-
verbs.each do |verb|
|
|
549
|
-
it 'allows for the anchoring option with a #{verb.upcase} method' do
|
|
550
|
-
subject.send(verb, '/example', :anchor => true) do
|
|
551
|
-
verb
|
|
552
|
-
end
|
|
553
|
-
send(verb, '/example/and/some/more')
|
|
554
|
-
last_response.status.should eql 404
|
|
555
|
-
end
|
|
556
|
-
|
|
557
|
-
it 'anchors paths by default for the #{verb.upcase} method' do
|
|
558
|
-
subject.send(verb, '/example') do
|
|
559
|
-
verb
|
|
560
|
-
end
|
|
561
|
-
send(verb, '/example/and/some/more')
|
|
562
|
-
last_response.status.should eql 404
|
|
563
|
-
end
|
|
564
|
-
|
|
565
|
-
it 'responds to /example/and/some/more for the non-anchored #{verb.upcase} method' do
|
|
566
|
-
subject.send(verb, '/example', :anchor => false) do
|
|
567
|
-
verb
|
|
568
|
-
end
|
|
569
|
-
send(verb, '/example/and/some/more')
|
|
570
|
-
last_response.status.should eql verb == "post" ? 201 : 200
|
|
571
|
-
last_response.body.should eql verb == 'head' ? '' : verb
|
|
572
|
-
end
|
|
573
|
-
end
|
|
574
|
-
end
|
|
575
|
-
|
|
576
|
-
context 'request' do
|
|
577
|
-
it 'should be set to the url requested' do
|
|
578
|
-
subject.get('/url') do
|
|
579
|
-
request.url
|
|
580
|
-
end
|
|
581
|
-
get '/url'
|
|
582
|
-
last_response.body.should == "http://example.org/url"
|
|
583
|
-
end
|
|
584
|
-
[ 'v1', :v1 ].each do |version|
|
|
585
|
-
it 'should include version #{version}' do
|
|
586
|
-
subject.version version, :using => :path
|
|
587
|
-
subject.get('/url') do
|
|
588
|
-
request.url
|
|
589
|
-
end
|
|
590
|
-
get "/#{version}/url"
|
|
591
|
-
last_response.body.should == "http://example.org/#{version}/url"
|
|
592
|
-
end
|
|
593
|
-
end
|
|
594
|
-
it 'should include prefix' do
|
|
595
|
-
subject.version 'v1', :using => :path
|
|
596
|
-
subject.prefix 'api'
|
|
597
|
-
subject.get('/url') do
|
|
598
|
-
request.url
|
|
599
|
-
end
|
|
600
|
-
get '/api/v1/url'
|
|
601
|
-
last_response.body.should == "http://example.org/api/v1/url"
|
|
602
|
-
end
|
|
603
|
-
end
|
|
604
|
-
|
|
605
|
-
end
|