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/entity_spec.rb
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
require 'grape_entity'
|
|
3
|
-
|
|
4
|
-
describe Grape::Entity do
|
|
5
|
-
subject { Class.new(Grape::API) }
|
|
6
|
-
def app; subject end
|
|
7
|
-
|
|
8
|
-
describe '#present' do
|
|
9
|
-
it 'sets the object as the body if no options are provided' do
|
|
10
|
-
subject.get '/example' do
|
|
11
|
-
present({:abc => 'def'})
|
|
12
|
-
body.should == {:abc => 'def'}
|
|
13
|
-
end
|
|
14
|
-
get '/example'
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
it 'calls through to the provided entity class if one is given' do
|
|
18
|
-
subject.get '/example' do
|
|
19
|
-
entity_mock = Object.new
|
|
20
|
-
entity_mock.should_receive(:represent)
|
|
21
|
-
present Object.new, :with => entity_mock
|
|
22
|
-
end
|
|
23
|
-
get '/example'
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'pulls a representation from the class options if it exists' do
|
|
27
|
-
entity = Class.new(Grape::Entity)
|
|
28
|
-
entity.stub!(:represent).and_return("Hiya")
|
|
29
|
-
|
|
30
|
-
subject.represent Object, :with => entity
|
|
31
|
-
subject.get '/example' do
|
|
32
|
-
present Object.new
|
|
33
|
-
end
|
|
34
|
-
get '/example'
|
|
35
|
-
last_response.body.should == 'Hiya'
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
it 'pulls a representation from the class options if the presented object is a collection of objects' do
|
|
39
|
-
entity = Class.new(Grape::Entity)
|
|
40
|
-
entity.stub!(:represent).and_return("Hiya")
|
|
41
|
-
|
|
42
|
-
class TestObject; end
|
|
43
|
-
class FakeCollection
|
|
44
|
-
def first; TestObject.new; end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
subject.represent TestObject, :with => entity
|
|
48
|
-
subject.get '/example' do
|
|
49
|
-
present [TestObject.new]
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
subject.get '/example2' do
|
|
53
|
-
present FakeCollection.new
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
get '/example'
|
|
57
|
-
last_response.body.should == "Hiya"
|
|
58
|
-
|
|
59
|
-
get '/example2'
|
|
60
|
-
last_response.body.should == "Hiya"
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
it 'pulls a representation from the class ancestor if it exists' do
|
|
64
|
-
entity = Class.new(Grape::Entity)
|
|
65
|
-
entity.stub!(:represent).and_return("Hiya")
|
|
66
|
-
|
|
67
|
-
subclass = Class.new(Object)
|
|
68
|
-
|
|
69
|
-
subject.represent Object, :with => entity
|
|
70
|
-
subject.get '/example' do
|
|
71
|
-
present subclass.new
|
|
72
|
-
end
|
|
73
|
-
get '/example'
|
|
74
|
-
last_response.body.should == 'Hiya'
|
|
75
|
-
end
|
|
76
|
-
|
|
77
|
-
it 'automatically uses Klass::Entity if that exists' do
|
|
78
|
-
some_model = Class.new
|
|
79
|
-
entity = Class.new(Grape::Entity)
|
|
80
|
-
entity.stub!(:represent).and_return("Auto-detect!")
|
|
81
|
-
|
|
82
|
-
some_model.const_set :Entity, entity
|
|
83
|
-
|
|
84
|
-
subject.get '/example' do
|
|
85
|
-
present some_model.new
|
|
86
|
-
end
|
|
87
|
-
get '/example'
|
|
88
|
-
last_response.body.should == 'Auto-detect!'
|
|
89
|
-
end
|
|
90
|
-
|
|
91
|
-
it 'automatically uses Klass::Entity based on the first object in the collection being presented' do
|
|
92
|
-
some_model = Class.new
|
|
93
|
-
entity = Class.new(Grape::Entity)
|
|
94
|
-
entity.stub!(:represent).and_return("Auto-detect!")
|
|
95
|
-
|
|
96
|
-
some_model.const_set :Entity, entity
|
|
97
|
-
|
|
98
|
-
subject.get '/example' do
|
|
99
|
-
present [some_model.new]
|
|
100
|
-
end
|
|
101
|
-
get '/example'
|
|
102
|
-
last_response.body.should == 'Auto-detect!'
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
it 'adds a root key to the output if one is given' do
|
|
106
|
-
subject.get '/example' do
|
|
107
|
-
present({:abc => 'def'}, :root => :root)
|
|
108
|
-
body.should == {:root => {:abc => 'def'}}
|
|
109
|
-
end
|
|
110
|
-
get '/example'
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
[ :json, :serializable_hash ].each do |format|
|
|
114
|
-
|
|
115
|
-
it 'presents with #{format}' do
|
|
116
|
-
entity = Class.new(Grape::Entity)
|
|
117
|
-
entity.root "examples", "example"
|
|
118
|
-
entity.expose :id
|
|
119
|
-
|
|
120
|
-
subject.format format
|
|
121
|
-
subject.get '/example' do
|
|
122
|
-
c = Class.new do
|
|
123
|
-
attr_reader :id
|
|
124
|
-
def initialize(id)
|
|
125
|
-
@id = id
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
present c.new(1), :with => entity
|
|
129
|
-
end
|
|
130
|
-
|
|
131
|
-
get '/example'
|
|
132
|
-
last_response.status.should == 200
|
|
133
|
-
last_response.body.should == '{"example":{"id":1}}'
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
it 'presents with #{format} collection' do
|
|
137
|
-
entity = Class.new(Grape::Entity)
|
|
138
|
-
entity.root "examples", "example"
|
|
139
|
-
entity.expose :id
|
|
140
|
-
|
|
141
|
-
subject.format format
|
|
142
|
-
subject.get '/examples' do
|
|
143
|
-
c = Class.new do
|
|
144
|
-
attr_reader :id
|
|
145
|
-
def initialize(id)
|
|
146
|
-
@id = id
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
examples = [ c.new(1), c.new(2) ]
|
|
150
|
-
present examples, :with => entity
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
get '/examples'
|
|
154
|
-
last_response.status.should == 200
|
|
155
|
-
last_response.body.should == '{"examples":[{"id":1},{"id":2}]}'
|
|
156
|
-
end
|
|
157
|
-
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
it 'presents with xml' do
|
|
161
|
-
entity = Class.new(Grape::Entity)
|
|
162
|
-
entity.root "examples", "example"
|
|
163
|
-
entity.expose :name
|
|
164
|
-
|
|
165
|
-
subject.format :xml
|
|
166
|
-
|
|
167
|
-
subject.get '/example' do
|
|
168
|
-
c = Class.new do
|
|
169
|
-
attr_reader :name
|
|
170
|
-
def initialize(args)
|
|
171
|
-
@name = args[:name] || "no name set"
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
present c.new({:name => "johnnyiller"}), :with => entity
|
|
175
|
-
end
|
|
176
|
-
get '/example'
|
|
177
|
-
last_response.status.should == 200
|
|
178
|
-
last_response.headers['Content-type'].should == "application/xml"
|
|
179
|
-
last_response.body.should == <<-XML
|
|
180
|
-
<?xml version="1.0" encoding="UTF-8"?>
|
|
181
|
-
<hash>
|
|
182
|
-
<example>
|
|
183
|
-
<name>johnnyiller</name>
|
|
184
|
-
</example>
|
|
185
|
-
</hash>
|
|
186
|
-
XML
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
it 'presents with json' do
|
|
190
|
-
entity = Class.new(Grape::Entity)
|
|
191
|
-
entity.root "examples", "example"
|
|
192
|
-
entity.expose :name
|
|
193
|
-
|
|
194
|
-
subject.format :json
|
|
195
|
-
|
|
196
|
-
subject.get '/example' do
|
|
197
|
-
c = Class.new do
|
|
198
|
-
attr_reader :name
|
|
199
|
-
def initialize(args)
|
|
200
|
-
@name = args[:name] || "no name set"
|
|
201
|
-
end
|
|
202
|
-
end
|
|
203
|
-
present c.new({:name => "johnnyiller"}), :with => entity
|
|
204
|
-
end
|
|
205
|
-
get '/example'
|
|
206
|
-
last_response.status.should == 200
|
|
207
|
-
last_response.headers['Content-type'].should == "application/json"
|
|
208
|
-
last_response.body.should == '{"example":{"name":"johnnyiller"}}'
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
it 'presents with jsonp utilising Rack::JSONP' do
|
|
212
|
-
require 'rack/contrib'
|
|
213
|
-
|
|
214
|
-
# Include JSONP middleware
|
|
215
|
-
subject.use Rack::JSONP
|
|
216
|
-
|
|
217
|
-
entity = Class.new(Grape::Entity)
|
|
218
|
-
entity.root "examples", "example"
|
|
219
|
-
entity.expose :name
|
|
220
|
-
|
|
221
|
-
# Rack::JSONP expects a standard JSON response
|
|
222
|
-
subject.format :json
|
|
223
|
-
|
|
224
|
-
subject.get '/example' do
|
|
225
|
-
c = Class.new do
|
|
226
|
-
attr_reader :name
|
|
227
|
-
def initialize(args)
|
|
228
|
-
@name = args[:name] || "no name set"
|
|
229
|
-
end
|
|
230
|
-
end
|
|
231
|
-
|
|
232
|
-
present c.new({:name => "johnnyiller"}), :with => entity
|
|
233
|
-
end
|
|
234
|
-
|
|
235
|
-
get '/example?callback=abcDef'
|
|
236
|
-
last_response.status.should == 200
|
|
237
|
-
last_response.headers['Content-type'].should == "application/javascript"
|
|
238
|
-
last_response.body.should == 'abcDef({"example":{"name":"johnnyiller"}})'
|
|
239
|
-
end
|
|
240
|
-
|
|
241
|
-
context "present with multiple entities" do
|
|
242
|
-
let(:user) do
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
before :each do
|
|
246
|
-
end
|
|
247
|
-
|
|
248
|
-
it "present with multiple entities using optional symbol" do
|
|
249
|
-
user = Class.new do
|
|
250
|
-
attr_reader :name
|
|
251
|
-
def initialize(args)
|
|
252
|
-
@name = args[:name] || "no name set"
|
|
253
|
-
end
|
|
254
|
-
end
|
|
255
|
-
user1 = user.new({:name => 'user1'})
|
|
256
|
-
user2 = user.new({:name => 'user2'})
|
|
257
|
-
|
|
258
|
-
entity = Class.new(Grape::Entity)
|
|
259
|
-
entity.expose :name
|
|
260
|
-
|
|
261
|
-
subject.format :json
|
|
262
|
-
subject.get '/example' do
|
|
263
|
-
present :page, 1
|
|
264
|
-
present :user1, user1, :with => entity
|
|
265
|
-
present :user2, user2, :with => entity
|
|
266
|
-
end
|
|
267
|
-
get '/example'
|
|
268
|
-
expect_response_json = {
|
|
269
|
-
"page" => 1,
|
|
270
|
-
"user1" => {"name" => "user1"},
|
|
271
|
-
"user2" => {"name" => "user2"}
|
|
272
|
-
}
|
|
273
|
-
JSON(last_response.body).should == expect_response_json
|
|
274
|
-
end
|
|
275
|
-
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
end
|
|
280
|
-
|
|
281
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
describe Grape::Exceptions::InvalidFormatter do
|
|
5
|
-
describe "#message" do
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new(String, 'xml')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"cannot convert String to xml"
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
describe Grape::Exceptions::InvalidVersionerOption do
|
|
5
|
-
describe "#message" do
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new("headers")
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"Unknown :using for versioner: headers"
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
describe Grape::Exceptions::MissingMimeType do
|
|
4
|
-
describe "#message" do
|
|
5
|
-
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new("new_json")
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"missing mime type for new_json"
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it "contains the resolution in the message" do
|
|
17
|
-
error.message.should include(
|
|
18
|
-
"or add your own with content_type :new_json, 'application/new_json' "
|
|
19
|
-
)
|
|
20
|
-
end
|
|
21
|
-
end
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
describe Grape::Exceptions::MissingOption do
|
|
5
|
-
describe "#message" do
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new(:path)
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"You must specify :path options."
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
describe Grape::Exceptions::UnknownOptions do
|
|
5
|
-
describe "#message" do
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new([:a, :b])
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"unknown options: "
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
# encoding: utf-8
|
|
2
|
-
require 'spec_helper'
|
|
3
|
-
|
|
4
|
-
describe Grape::Exceptions::UnknownValidator do
|
|
5
|
-
describe "#message" do
|
|
6
|
-
let(:error) do
|
|
7
|
-
described_class.new('gt_10')
|
|
8
|
-
end
|
|
9
|
-
|
|
10
|
-
it "contains the problem in the message" do
|
|
11
|
-
error.message.should include(
|
|
12
|
-
"unknown validator: gt_10"
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
require 'base64'
|
|
4
|
-
|
|
5
|
-
describe Grape::Middleware::Auth::Basic do
|
|
6
|
-
def app
|
|
7
|
-
Rack::Builder.new do |b|
|
|
8
|
-
b.use Grape::Middleware::Error
|
|
9
|
-
b.use(Grape::Middleware::Auth::Basic) do |u,p|
|
|
10
|
-
u && p && u == p
|
|
11
|
-
end
|
|
12
|
-
b.run lambda{|env| [200, {}, ["Hello there."]]}
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'throws a 401 if no auth is given' do
|
|
17
|
-
@proc = lambda{ false }
|
|
18
|
-
get '/whatever'
|
|
19
|
-
last_response.status.should == 401
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'authenticates if given valid creds' do
|
|
23
|
-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin','admin')
|
|
24
|
-
last_response.status.should == 200
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
it 'throws a 401 is wrong auth is given' do
|
|
28
|
-
get '/whatever', {}, 'HTTP_AUTHORIZATION' => encode_basic_auth('admin','wrong')
|
|
29
|
-
last_response.status.should == 401
|
|
30
|
-
end
|
|
31
|
-
end
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
RSpec::Matchers.define :be_challenge do
|
|
4
|
-
match do |actual_response|
|
|
5
|
-
actual_response.status == 401 &&
|
|
6
|
-
actual_response['WWW-Authenticate'] =~ /^Digest / &&
|
|
7
|
-
actual_response.body.empty?
|
|
8
|
-
end
|
|
9
|
-
end
|
|
10
|
-
|
|
11
|
-
class Test < Grape::API
|
|
12
|
-
http_digest({:realm => 'Test Api', :opaque => 'secret'}) do |username|
|
|
13
|
-
{'foo' => 'bar'}[username]
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
get '/test' do
|
|
17
|
-
[{:hey => 'you'},{:there => 'bar'},{:foo => 'baz'}]
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
describe Grape::Middleware::Auth::Digest do
|
|
22
|
-
def app
|
|
23
|
-
Test
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
it 'is a digest authentication challenge' do
|
|
27
|
-
get '/test'
|
|
28
|
-
last_response.should be_challenge
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
it 'throws a 401 if no auth is given' do
|
|
32
|
-
get '/test'
|
|
33
|
-
last_response.status.should == 401
|
|
34
|
-
end
|
|
35
|
-
|
|
36
|
-
it 'authenticates if given valid creds' do
|
|
37
|
-
digest_authorize "foo", "bar"
|
|
38
|
-
get '/test'
|
|
39
|
-
last_response.status.should == 200
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
it 'throws a 401 if given invalid creds' do
|
|
43
|
-
digest_authorize "bar", "foo"
|
|
44
|
-
get '/test'
|
|
45
|
-
last_response.status.should == 401
|
|
46
|
-
end
|
|
47
|
-
end
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Grape::Middleware::Auth::OAuth2 do
|
|
4
|
-
class FakeToken
|
|
5
|
-
def self.verify(token)
|
|
6
|
-
FakeToken.new(token) if %w(g e).include?(token[0..0])
|
|
7
|
-
end
|
|
8
|
-
|
|
9
|
-
def initialize(token)
|
|
10
|
-
self.token = token
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
def expired?
|
|
14
|
-
self.token[0..0] == 'e'
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
def permission_for?(env)
|
|
18
|
-
env['PATH_INFO'] == '/forbidden' ? false : true
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
attr_accessor :token
|
|
22
|
-
end
|
|
23
|
-
|
|
24
|
-
def app
|
|
25
|
-
Rack::Builder.app do
|
|
26
|
-
use Grape::Middleware::Auth::OAuth2, :token_class => 'FakeToken'
|
|
27
|
-
run lambda{|env| [200, {}, [ (env['api.token'].token rescue '') ]]}
|
|
28
|
-
end
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
context 'with the token in the query string' do
|
|
32
|
-
context 'and a valid token' do
|
|
33
|
-
before { get '/awesome?oauth_token=g123' }
|
|
34
|
-
|
|
35
|
-
it 'sets env["api.token"]' do
|
|
36
|
-
last_response.body.should == 'g123'
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
context 'and an invalid token' do
|
|
41
|
-
before do
|
|
42
|
-
@err = catch :error do
|
|
43
|
-
get '/awesome?oauth_token=b123'
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
it 'throws an error' do
|
|
48
|
-
@err[:status].should == 401
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
it 'sets the WWW-Authenticate header in the response' do
|
|
52
|
-
@err[:headers]['WWW-Authenticate'].should == "OAuth realm='OAuth API', error='invalid_token'"
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
end
|
|
56
|
-
|
|
57
|
-
context 'with an expired token' do
|
|
58
|
-
before do
|
|
59
|
-
@err = catch :error do
|
|
60
|
-
get '/awesome?oauth_token=e123'
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
it { @err[:status].should == 401 }
|
|
65
|
-
it { @err[:headers]['WWW-Authenticate'].should == "OAuth realm='OAuth API', error='expired_token'" }
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
%w(HTTP_AUTHORIZATION X_HTTP_AUTHORIZATION X-HTTP_AUTHORIZATION REDIRECT_X_HTTP_AUTHORIZATION).each do |head|
|
|
69
|
-
context 'with the token in the #{head} header' do
|
|
70
|
-
before { get '/awesome', {}, head => 'OAuth g123' }
|
|
71
|
-
it { last_response.body.should == 'g123' }
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
context 'with the token in the POST body' do
|
|
76
|
-
before { post '/awesome', {'oauth_token' => 'g123'} }
|
|
77
|
-
it { last_response.body.should == 'g123'}
|
|
78
|
-
end
|
|
79
|
-
|
|
80
|
-
context 'when accessing something outside its scope' do
|
|
81
|
-
before do
|
|
82
|
-
@err = catch :error do
|
|
83
|
-
get '/forbidden?oauth_token=g123'
|
|
84
|
-
end
|
|
85
|
-
end
|
|
86
|
-
|
|
87
|
-
it { @err[:headers]['WWW-Authenticate'].should == "OAuth realm='OAuth API', error='insufficient_scope'" }
|
|
88
|
-
it { @err[:status].should == 403 }
|
|
89
|
-
end
|
|
90
|
-
end
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Grape::Middleware::Base do
|
|
4
|
-
subject { Grape::Middleware::Base.new( blank_app) }
|
|
5
|
-
let(:blank_app) { lambda{|_| [200, {}, 'Hi there.']} }
|
|
6
|
-
|
|
7
|
-
before do
|
|
8
|
-
# Keep it one object for testing.
|
|
9
|
-
subject.stub!(:dup).and_return(subject)
|
|
10
|
-
end
|
|
11
|
-
|
|
12
|
-
it 'has the app as an accessor' do
|
|
13
|
-
subject.app.should == blank_app
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
it 'is able to access the request' do
|
|
17
|
-
subject.call({})
|
|
18
|
-
subject.request.should be_kind_of(Rack::Request)
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
it 'calls through to the app' do
|
|
22
|
-
subject.call({}).should == [200, {}, 'Hi there.']
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
context 'callbacks' do
|
|
26
|
-
it 'calls #before' do
|
|
27
|
-
subject.should_receive(:before)
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
it 'calls #after' do
|
|
31
|
-
subject.should_receive(:after)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
after{ subject.call!({}) }
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
it 'is able to access the response' do
|
|
38
|
-
subject.call({})
|
|
39
|
-
subject.response.should be_kind_of(Rack::Response)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
context 'options' do
|
|
43
|
-
it 'persists options passed at initialization' do
|
|
44
|
-
Grape::Middleware::Base.new(blank_app, {:abc => true}).options[:abc].should be_true
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
context 'defaults' do
|
|
48
|
-
class ExampleWare < Grape::Middleware::Base
|
|
49
|
-
def default_options
|
|
50
|
-
{:monkey => true}
|
|
51
|
-
end
|
|
52
|
-
end
|
|
53
|
-
|
|
54
|
-
it 'persists the default options' do
|
|
55
|
-
ExampleWare.new(blank_app).options[:monkey].should be_true
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
it 'overrides default options when provided' do
|
|
59
|
-
ExampleWare.new(blank_app, :monkey => false).options[:monkey].should be_false
|
|
60
|
-
end
|
|
61
|
-
end
|
|
62
|
-
end
|
|
63
|
-
end
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
require 'spec_helper'
|
|
2
|
-
|
|
3
|
-
describe Grape::Middleware::Error do
|
|
4
|
-
class ErrApp
|
|
5
|
-
class << self
|
|
6
|
-
attr_accessor :error
|
|
7
|
-
attr_accessor :format
|
|
8
|
-
|
|
9
|
-
def call(env)
|
|
10
|
-
throw :error, self.error
|
|
11
|
-
end
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
def app
|
|
16
|
-
Rack::Builder.app do
|
|
17
|
-
use Grape::Middleware::Error, :default_message => 'Aww, hamburgers.'
|
|
18
|
-
run ErrApp
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
it 'sets the status code appropriately' do
|
|
23
|
-
ErrApp.error = {:status => 410}
|
|
24
|
-
get '/'
|
|
25
|
-
last_response.status.should == 410
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
it 'sets the error message appropriately' do
|
|
29
|
-
ErrApp.error = {:message => 'Awesome stuff.'}
|
|
30
|
-
get '/'
|
|
31
|
-
last_response.body.should == 'Awesome stuff.'
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
it 'defaults to a 403 status' do
|
|
35
|
-
ErrApp.error = {}
|
|
36
|
-
get '/'
|
|
37
|
-
last_response.status.should == 403
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
it 'has a default message' do
|
|
41
|
-
ErrApp.error = {}
|
|
42
|
-
get '/'
|
|
43
|
-
last_response.body.should == 'Aww, hamburgers.'
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
context 'with formatting' do
|
|
47
|
-
|
|
48
|
-
end
|
|
49
|
-
end
|