grape 0.6.0 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of grape might be problematic. Click here for more details.
- checksums.yaml +8 -8
- data/.rubocop.yml +65 -0
- data/.travis.yml +4 -0
- data/CHANGELOG.md +17 -1
- data/Gemfile +1 -0
- data/README.md +16 -8
- data/RELEASING.md +105 -0
- data/grape.gemspec +1 -1
- data/lib/grape.rb +1 -0
- data/lib/grape/api.rb +88 -54
- data/lib/grape/cookies.rb +4 -6
- data/lib/grape/endpoint.rb +81 -69
- data/lib/grape/error_formatter/base.rb +5 -4
- data/lib/grape/error_formatter/json.rb +3 -3
- data/lib/grape/error_formatter/txt.rb +1 -1
- data/lib/grape/error_formatter/xml.rb +4 -4
- data/lib/grape/exceptions/base.rb +7 -7
- data/lib/grape/exceptions/incompatible_option_values.rb +13 -0
- data/lib/grape/exceptions/invalid_formatter.rb +1 -1
- data/lib/grape/exceptions/invalid_versioner_option.rb +1 -1
- data/lib/grape/exceptions/invalid_with_option_for_represent.rb +1 -4
- data/lib/grape/exceptions/missing_mime_type.rb +1 -1
- data/lib/grape/exceptions/missing_option.rb +1 -1
- data/lib/grape/exceptions/missing_vendor_option.rb +1 -1
- data/lib/grape/exceptions/unknown_options.rb +1 -1
- data/lib/grape/exceptions/unknown_validator.rb +1 -1
- data/lib/grape/exceptions/validation.rb +2 -2
- data/lib/grape/exceptions/validation_errors.rb +1 -1
- data/lib/grape/formatter/base.rb +5 -4
- data/lib/grape/formatter/serializable_hash.rb +7 -6
- data/lib/grape/http/request.rb +2 -2
- data/lib/grape/locale/en.yml +2 -0
- data/lib/grape/middleware/auth/base.rb +3 -3
- data/lib/grape/middleware/auth/basic.rb +1 -1
- data/lib/grape/middleware/auth/oauth2.rb +18 -20
- data/lib/grape/middleware/base.rb +1 -1
- data/lib/grape/middleware/error.rb +19 -19
- data/lib/grape/middleware/filter.rb +3 -3
- data/lib/grape/middleware/formatter.rb +29 -23
- data/lib/grape/middleware/versioner.rb +1 -1
- data/lib/grape/middleware/versioner/accept_version_header.rb +8 -6
- data/lib/grape/middleware/versioner/header.rb +16 -14
- data/lib/grape/middleware/versioner/param.rb +7 -7
- data/lib/grape/middleware/versioner/path.rb +7 -9
- data/lib/grape/parser/base.rb +3 -2
- data/lib/grape/path.rb +1 -1
- data/lib/grape/route.rb +6 -4
- data/lib/grape/util/content_types.rb +2 -1
- data/lib/grape/util/deep_merge.rb +5 -5
- data/lib/grape/util/hash_stack.rb +2 -2
- data/lib/grape/validations.rb +34 -30
- data/lib/grape/validations/coerce.rb +6 -5
- data/lib/grape/validations/default.rb +0 -1
- data/lib/grape/validations/presence.rb +1 -1
- data/lib/grape/validations/regexp.rb +2 -2
- data/lib/grape/validations/values.rb +16 -0
- data/lib/grape/version.rb +1 -1
- data/spec/grape/api_spec.rb +229 -210
- data/spec/grape/endpoint_spec.rb +56 -54
- data/spec/grape/entity_spec.rb +31 -33
- data/spec/grape/exceptions/missing_mime_type_spec.rb +3 -9
- data/spec/grape/middleware/auth/basic_spec.rb +8 -8
- data/spec/grape/middleware/auth/digest_spec.rb +5 -5
- data/spec/grape/middleware/auth/oauth2_spec.rb +23 -23
- data/spec/grape/middleware/base_spec.rb +6 -6
- data/spec/grape/middleware/error_spec.rb +11 -15
- data/spec/grape/middleware/exception_spec.rb +45 -25
- data/spec/grape/middleware/formatter_spec.rb +56 -45
- data/spec/grape/middleware/versioner/accept_version_header_spec.rb +25 -25
- data/spec/grape/middleware/versioner/header_spec.rb +54 -54
- data/spec/grape/middleware/versioner/param_spec.rb +17 -18
- data/spec/grape/middleware/versioner/path_spec.rb +6 -6
- data/spec/grape/middleware/versioner_spec.rb +1 -1
- data/spec/grape/util/hash_stack_spec.rb +26 -27
- data/spec/grape/validations/coerce_spec.rb +39 -34
- data/spec/grape/validations/default_spec.rb +12 -13
- data/spec/grape/validations/presence_spec.rb +18 -22
- data/spec/grape/validations/regexp_spec.rb +9 -9
- data/spec/grape/validations/values_spec.rb +64 -0
- data/spec/grape/validations_spec.rb +127 -70
- data/spec/shared/versioning_examples.rb +5 -5
- data/spec/support/basic_auth_encode_helpers.rb +0 -1
- data/spec/support/versioned_helpers.rb +5 -6
- metadata +10 -4
@@ -2,16 +2,16 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Grape::Middleware::Versioner::Param do
|
4
4
|
|
5
|
-
let(:app) { lambda{|env| [200, env, env['api.version']]} }
|
5
|
+
let(:app) { lambda { |env| [200, env, env['api.version']] } }
|
6
6
|
subject { Grape::Middleware::Versioner::Param.new(app, @options || {}) }
|
7
7
|
|
8
8
|
it 'sets the API version based on the default param (apiver)' do
|
9
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
9
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "apiver" => "v1" } })
|
10
10
|
subject.call(env)[1]["api.version"].should == 'v1'
|
11
11
|
end
|
12
12
|
|
13
|
-
it 'cuts (only) the version out of the params', :
|
14
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
13
|
+
it 'cuts (only) the version out of the params', focus: true do
|
14
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "apiver" => "v1", "other_param" => "5" } })
|
15
15
|
subject.call(env)[1]['rack.request.query_hash']["apiver"].should be_nil
|
16
16
|
subject.call(env)[1]['rack.request.query_hash']["other_param"].should == "5"
|
17
17
|
end
|
@@ -22,37 +22,36 @@ describe Grape::Middleware::Versioner::Param do
|
|
22
22
|
end
|
23
23
|
|
24
24
|
context 'with specified parameter name' do
|
25
|
-
before{ @options = {:
|
25
|
+
before { @options = { parameter: 'v' } }
|
26
26
|
it 'sets the API version based on the custom parameter name' do
|
27
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
28
|
-
|
27
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "v" => "v1" } })
|
28
|
+
subject.call(env)[1]["api.version"].should == "v1"
|
29
29
|
end
|
30
30
|
it 'does not set the API version based on the default param' do
|
31
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
32
|
-
|
31
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "apiver" => "v1" } })
|
32
|
+
subject.call(env)[1]["api.version"].should be_nil
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
context 'with specified versions' do
|
37
|
-
before{ @options = {:
|
37
|
+
before { @options = { versions: ['v1', 'v2'] } }
|
38
38
|
it 'throws an error if a non-allowed version is specified' do
|
39
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
40
|
-
catch(:error){subject.call(env)}[:status].should == 404
|
39
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "apiver" => "v3" } })
|
40
|
+
catch(:error) { subject.call(env) }[:status].should == 404
|
41
41
|
end
|
42
|
-
|
43
42
|
it 'allows versions that have been specified' do
|
44
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
43
|
+
env = Rack::MockRequest.env_for("/awesome", { params: { "apiver" => "v1" } })
|
45
44
|
subject.call(env)[1]["api.version"].should == 'v1'
|
46
45
|
end
|
47
46
|
end
|
48
47
|
|
49
48
|
it 'returns a 200 when no version is set (matches the first version found)' do
|
50
49
|
@options = {
|
51
|
-
:
|
52
|
-
:
|
50
|
+
versions: ['v1'],
|
51
|
+
version_options: { using: :header }
|
53
52
|
}
|
54
|
-
env = Rack::MockRequest.env_for("/awesome", {:
|
53
|
+
env = Rack::MockRequest.env_for("/awesome", { params: {} })
|
55
54
|
subject.call(env).first.should == 200
|
56
55
|
end
|
57
56
|
|
58
|
-
end
|
57
|
+
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Grape::Middleware::Versioner::Path do
|
4
|
-
let(:app) { lambda{|env| [200, env, env['api.version']]} }
|
4
|
+
let(:app) { lambda { |env| [200, env, env['api.version']] } }
|
5
5
|
subject { Grape::Middleware::Versioner::Path.new(app, @options || {}) }
|
6
6
|
|
7
7
|
it 'sets the API version based on the first path' do
|
@@ -17,7 +17,7 @@ describe Grape::Middleware::Versioner::Path do
|
|
17
17
|
end
|
18
18
|
|
19
19
|
context 'with a pattern' do
|
20
|
-
before { @options = {:
|
20
|
+
before { @options = { pattern: /v./i } }
|
21
21
|
it 'sets the version if it matches' do
|
22
22
|
subject.call('PATH_INFO' => '/v1/awesome').last.should == 'v1'
|
23
23
|
end
|
@@ -27,12 +27,12 @@ describe Grape::Middleware::Versioner::Path do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
[
|
30
|
+
[['v1', 'v2'], [:v1, :v2], [:v1, 'v2'], ['v1', :v2]].each do |versions|
|
31
31
|
context 'with specified versions as #{versions}' do
|
32
|
-
before { @options = { :
|
32
|
+
before { @options = { versions: versions } }
|
33
33
|
|
34
34
|
it 'throws an error if a non-allowed version is specified' do
|
35
|
-
catch(:error){subject.call('PATH_INFO' => '/v3/awesome')}[:status].should == 404
|
35
|
+
catch(:error) { subject.call('PATH_INFO' => '/v3/awesome') }[:status].should == 404
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'allows versions that have been specified' do
|
@@ -41,4 +41,4 @@ describe Grape::Middleware::Versioner::Path do
|
|
41
41
|
end
|
42
42
|
end
|
43
43
|
|
44
|
-
end
|
44
|
+
end
|
@@ -1,12 +1,11 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Grape::Util::HashStack do
|
4
|
-
let(:klass){ Grape::Util::HashStack }
|
5
4
|
|
6
5
|
describe '#get' do
|
7
6
|
it 'finds the first available key' do
|
8
7
|
subject[:abc] = 123
|
9
|
-
subject.push(:
|
8
|
+
subject.push(abc: 345)
|
10
9
|
subject.get(:abc).should == 345
|
11
10
|
end
|
12
11
|
|
@@ -32,9 +31,9 @@ describe Grape::Util::HashStack do
|
|
32
31
|
end
|
33
32
|
|
34
33
|
it 'merges a hash that is passed' do
|
35
|
-
subject[:abc] = {:
|
36
|
-
subject.imbue(:abc, {:
|
37
|
-
subject[:abc].should == {:
|
34
|
+
subject[:abc] = { foo: 'bar' }
|
35
|
+
subject.imbue(:abc, { baz: 'wich' })
|
36
|
+
subject[:abc].should == { foo: 'bar', baz: 'wich' }
|
38
37
|
end
|
39
38
|
|
40
39
|
it 'sets the value if not a hash or array' do
|
@@ -45,86 +44,86 @@ describe Grape::Util::HashStack do
|
|
45
44
|
it 'is able to imbue an array without explicit setting' do
|
46
45
|
subject.imbue(:arr, [1])
|
47
46
|
subject.imbue(:arr, [2])
|
48
|
-
subject[:arr].should == [1,2]
|
47
|
+
subject[:arr].should == [1, 2]
|
49
48
|
end
|
50
49
|
|
51
50
|
it 'is able to imbue a hash without explicit setting' do
|
52
|
-
subject.imbue(:hash, :
|
53
|
-
subject.imbue(:hash, :
|
54
|
-
subject[:hash].should == {:
|
51
|
+
subject.imbue(:hash, foo: 'bar')
|
52
|
+
subject.imbue(:hash, baz: 'wich')
|
53
|
+
subject[:hash].should == { foo: 'bar', baz: 'wich' }
|
55
54
|
end
|
56
55
|
end
|
57
56
|
|
58
57
|
describe '#push' do
|
59
58
|
it 'returns a HashStack' do
|
60
|
-
subject.push(
|
59
|
+
subject.push(Grape::Util::HashStack.new).should be_kind_of(Grape::Util::HashStack)
|
61
60
|
end
|
62
61
|
|
63
62
|
it 'places the passed value on the top of the stack' do
|
64
|
-
subject.push(:
|
65
|
-
subject.stack.should == [{}, {:
|
63
|
+
subject.push(abc: 123)
|
64
|
+
subject.stack.should == [{}, { abc: 123 }]
|
66
65
|
end
|
67
66
|
|
68
67
|
it 'pushes an empty hash by default' do
|
69
68
|
subject[:abc] = 123
|
70
69
|
subject.push
|
71
|
-
subject.stack.should == [{:
|
70
|
+
subject.stack.should == [{ abc: 123 }, {}]
|
72
71
|
end
|
73
72
|
end
|
74
73
|
|
75
74
|
describe '#pop' do
|
76
75
|
it 'removes and return the top frame' do
|
77
|
-
subject.push(:
|
78
|
-
subject.pop.should == {:
|
76
|
+
subject.push(abc: 123)
|
77
|
+
subject.pop.should == { abc: 123 }
|
79
78
|
subject.stack.size.should == 1
|
80
79
|
end
|
81
80
|
end
|
82
81
|
|
83
82
|
describe '#peek' do
|
84
83
|
it 'returns the top frame without removing it' do
|
85
|
-
subject.push(:
|
86
|
-
subject.peek.should == {:
|
84
|
+
subject.push(abc: 123)
|
85
|
+
subject.peek.should == { abc: 123 }
|
87
86
|
subject.stack.size.should == 2
|
88
87
|
end
|
89
88
|
end
|
90
89
|
|
91
90
|
describe '#prepend' do
|
92
91
|
it 'returns a HashStack' do
|
93
|
-
subject.prepend(
|
92
|
+
subject.prepend(Grape::Util::HashStack.new).should be_kind_of(Grape::Util::HashStack)
|
94
93
|
end
|
95
94
|
|
96
95
|
it "prepends a HashStack's stack onto its own stack" do
|
97
|
-
other =
|
98
|
-
subject.prepend(other).stack.should == [{}, {:
|
96
|
+
other = Grape::Util::HashStack.new.push(abc: 123)
|
97
|
+
subject.prepend(other).stack.should == [{}, { abc: 123 }, {}]
|
99
98
|
end
|
100
99
|
end
|
101
100
|
|
102
101
|
describe '#concat' do
|
103
102
|
it 'returns a HashStack' do
|
104
|
-
subject.concat(
|
103
|
+
subject.concat(Grape::Util::HashStack.new).should be_kind_of(Grape::Util::HashStack)
|
105
104
|
end
|
106
105
|
|
107
106
|
it "appends a HashStack's stack onto its own stack" do
|
108
|
-
other =
|
109
|
-
subject.concat(other).stack.should == [{}, {}, {:
|
107
|
+
other = Grape::Util::HashStack.new.push(abc: 123)
|
108
|
+
subject.concat(other).stack.should == [{}, {}, { abc: 123 }]
|
110
109
|
end
|
111
110
|
end
|
112
111
|
|
113
112
|
describe '#update' do
|
114
113
|
it 'merges! into the top frame' do
|
115
|
-
subject.update(:
|
116
|
-
subject.stack.should == [{:
|
114
|
+
subject.update(abc: 123)
|
115
|
+
subject.stack.should == [{ abc: 123 }]
|
117
116
|
end
|
118
117
|
|
119
118
|
it 'returns a HashStack' do
|
120
|
-
subject.update(:
|
119
|
+
subject.update(abc: 123).should be_kind_of(Grape::Util::HashStack)
|
121
120
|
end
|
122
121
|
end
|
123
122
|
|
124
123
|
describe '#clone' do
|
125
124
|
it 'performs a deep copy' do
|
126
125
|
subject[:abc] = 123
|
127
|
-
subject.push :
|
126
|
+
subject.push def: 234
|
128
127
|
clone = subject.clone
|
129
128
|
clone[:def] = 345
|
130
129
|
subject[:def].should == 234
|
@@ -2,8 +2,13 @@
|
|
2
2
|
require 'spec_helper'
|
3
3
|
|
4
4
|
describe Grape::Validations::CoerceValidator do
|
5
|
-
subject
|
6
|
-
|
5
|
+
subject do
|
6
|
+
Class.new(Grape::API)
|
7
|
+
end
|
8
|
+
|
9
|
+
def app
|
10
|
+
subject
|
11
|
+
end
|
7
12
|
|
8
13
|
describe 'coerce' do
|
9
14
|
|
@@ -14,23 +19,23 @@ describe Grape::Validations::CoerceValidator do
|
|
14
19
|
end
|
15
20
|
|
16
21
|
it "i18n error on malformed input" do
|
17
|
-
I18n.load_path << File.expand_path('../zh-CN.yml',__FILE__)
|
22
|
+
I18n.load_path << File.expand_path('../zh-CN.yml', __FILE__)
|
18
23
|
I18n.reload!
|
19
|
-
I18n.locale =
|
20
|
-
subject.params { requires :age, :
|
24
|
+
I18n.locale = 'zh-CN'.to_sym
|
25
|
+
subject.params { requires :age, type: Integer }
|
21
26
|
subject.get '/single' do 'int works'; end
|
22
27
|
|
23
|
-
get '/single', :
|
28
|
+
get '/single', age: '43a'
|
24
29
|
last_response.status.should == 400
|
25
30
|
last_response.body.should == '年龄格式不正确'
|
26
31
|
end
|
27
32
|
|
28
33
|
it 'gives an english fallback error when default locale message is blank' do
|
29
|
-
I18n.locale =
|
30
|
-
subject.params { requires :age, :
|
34
|
+
I18n.locale = 'pt-BR'.to_sym
|
35
|
+
subject.params { requires :age, type: Integer }
|
31
36
|
subject.get '/single' do 'int works'; end
|
32
37
|
|
33
|
-
get '/single', :
|
38
|
+
get '/single', age: '43a'
|
34
39
|
last_response.status.should == 400
|
35
40
|
last_response.body.should == 'age is invalid'
|
36
41
|
end
|
@@ -38,27 +43,27 @@ describe Grape::Validations::CoerceValidator do
|
|
38
43
|
end
|
39
44
|
|
40
45
|
it 'error on malformed input' do
|
41
|
-
subject.params { requires :int, :
|
46
|
+
subject.params { requires :int, type: Integer }
|
42
47
|
subject.get '/single' do 'int works'; end
|
43
48
|
|
44
|
-
get '/single', :
|
49
|
+
get '/single', int: '43a'
|
45
50
|
last_response.status.should == 400
|
46
51
|
last_response.body.should == 'int is invalid'
|
47
52
|
|
48
|
-
get '/single', :
|
53
|
+
get '/single', int: '43'
|
49
54
|
last_response.status.should == 200
|
50
55
|
last_response.body.should == 'int works'
|
51
56
|
end
|
52
57
|
|
53
58
|
it 'error on malformed input (Array)' do
|
54
|
-
subject.params { requires :ids, :
|
59
|
+
subject.params { requires :ids, type: Array[Integer] }
|
55
60
|
subject.get '/array' do 'array int works'; end
|
56
61
|
|
57
|
-
get 'array', { :
|
62
|
+
get 'array', { ids: ['1', '2', 'az'] }
|
58
63
|
last_response.status.should == 400
|
59
64
|
last_response.body.should == 'ids is invalid'
|
60
65
|
|
61
|
-
get 'array', { :
|
66
|
+
get 'array', { ids: ['1', '2', '890'] }
|
62
67
|
last_response.status.should == 200
|
63
68
|
last_response.body.should == 'array int works'
|
64
69
|
end
|
@@ -66,21 +71,21 @@ describe Grape::Validations::CoerceValidator do
|
|
66
71
|
context 'complex objects' do
|
67
72
|
module CoerceValidatorSpec
|
68
73
|
class User
|
69
|
-
include Virtus
|
74
|
+
include Virtus.model
|
70
75
|
attribute :id, Integer
|
71
76
|
attribute :name, String
|
72
77
|
end
|
73
78
|
end
|
74
79
|
|
75
80
|
it 'error on malformed input for complex objects' do
|
76
|
-
subject.params { requires :user, :
|
81
|
+
subject.params { requires :user, type: CoerceValidatorSpec::User }
|
77
82
|
subject.get '/user' do 'complex works'; end
|
78
83
|
|
79
|
-
get '/user', :
|
84
|
+
get '/user', user: "32"
|
80
85
|
last_response.status.should == 400
|
81
86
|
last_response.body.should == 'user is invalid'
|
82
87
|
|
83
|
-
get '/user', :
|
88
|
+
get '/user', user: { id: 32, name: 'Bob' }
|
84
89
|
last_response.status.should == 200
|
85
90
|
last_response.body.should == 'complex works'
|
86
91
|
end
|
@@ -88,58 +93,58 @@ describe Grape::Validations::CoerceValidator do
|
|
88
93
|
|
89
94
|
context 'coerces' do
|
90
95
|
it 'Integer' do
|
91
|
-
subject.params { requires :int, :
|
96
|
+
subject.params { requires :int, coerce: Integer }
|
92
97
|
subject.get '/int' do params[:int].class; end
|
93
98
|
|
94
|
-
get '/int', { :
|
99
|
+
get '/int', { int: "45" }
|
95
100
|
last_response.status.should == 200
|
96
101
|
last_response.body.should == 'Fixnum'
|
97
102
|
end
|
98
103
|
|
99
104
|
it 'Array of Integers' do
|
100
|
-
subject.params { requires :arry, :
|
105
|
+
subject.params { requires :arry, coerce: Array[Integer] }
|
101
106
|
subject.get '/array' do params[:arry][0].class; end
|
102
107
|
|
103
|
-
get '/array', { :
|
108
|
+
get '/array', { arry: ['1', '2', '3'] }
|
104
109
|
last_response.status.should == 200
|
105
110
|
last_response.body.should == 'Fixnum'
|
106
111
|
end
|
107
112
|
|
108
113
|
it 'Array of Bools' do
|
109
|
-
subject.params { requires :arry, :
|
114
|
+
subject.params { requires :arry, coerce: Array[Virtus::Attribute::Boolean] }
|
110
115
|
subject.get '/array' do params[:arry][0].class; end
|
111
116
|
|
112
|
-
get 'array', { :
|
117
|
+
get 'array', { arry: [1, 0] }
|
113
118
|
last_response.status.should == 200
|
114
119
|
last_response.body.should == 'TrueClass'
|
115
120
|
end
|
116
121
|
|
117
122
|
it 'Bool' do
|
118
|
-
subject.params { requires :bool, :
|
123
|
+
subject.params { requires :bool, coerce: Virtus::Attribute::Boolean }
|
119
124
|
subject.get '/bool' do params[:bool].class; end
|
120
125
|
|
121
|
-
get '/bool', { :
|
126
|
+
get '/bool', { bool: 1 }
|
122
127
|
last_response.status.should == 200
|
123
128
|
last_response.body.should == 'TrueClass'
|
124
129
|
|
125
|
-
get '/bool', { :
|
130
|
+
get '/bool', { bool: 0 }
|
126
131
|
last_response.status.should == 200
|
127
132
|
last_response.body.should == 'FalseClass'
|
128
133
|
|
129
|
-
get '/bool', { :
|
134
|
+
get '/bool', { bool: 'false' }
|
130
135
|
last_response.status.should == 200
|
131
136
|
last_response.body.should == 'FalseClass'
|
132
137
|
|
133
|
-
get '/bool', { :
|
138
|
+
get '/bool', { bool: 'true' }
|
134
139
|
last_response.status.should == 200
|
135
140
|
last_response.body.should == 'TrueClass'
|
136
141
|
end
|
137
142
|
|
138
143
|
it 'file' do
|
139
|
-
subject.params { requires :file, :
|
144
|
+
subject.params { requires :file, coerce: Rack::Multipart::UploadedFile }
|
140
145
|
subject.post '/upload' do params[:file].filename; end
|
141
146
|
|
142
|
-
post '/upload', { :
|
147
|
+
post '/upload', { file: Rack::Test::UploadedFile.new(__FILE__) }
|
143
148
|
last_response.status.should == 201
|
144
149
|
last_response.body.should == File.basename(__FILE__).to_s
|
145
150
|
end
|
@@ -147,12 +152,12 @@ describe Grape::Validations::CoerceValidator do
|
|
147
152
|
it 'Nests integers' do
|
148
153
|
subject.params do
|
149
154
|
group :integers do
|
150
|
-
requires :int, :
|
155
|
+
requires :int, coerce: Integer
|
151
156
|
end
|
152
157
|
end
|
153
158
|
subject.get '/int' do params[:integers][:int].class; end
|
154
159
|
|
155
|
-
get '/int', { :
|
160
|
+
get '/int', { integers: { int: "45" } }
|
156
161
|
last_response.status.should == 200
|
157
162
|
last_response.body.should == 'Fixnum'
|
158
163
|
end
|
@@ -9,28 +9,28 @@ describe Grape::Validations::DefaultValidator do
|
|
9
9
|
|
10
10
|
params do
|
11
11
|
optional :id
|
12
|
-
optional :type, :
|
12
|
+
optional :type, default: 'default-type'
|
13
13
|
end
|
14
14
|
get '/' do
|
15
|
-
{ :
|
15
|
+
{ id: params[:id], type: params[:type] }
|
16
16
|
end
|
17
17
|
|
18
18
|
params do
|
19
|
-
optional :type1, :
|
20
|
-
optional :type2, :
|
19
|
+
optional :type1, default: 'default-type1'
|
20
|
+
optional :type2, default: 'default-type2'
|
21
21
|
end
|
22
22
|
get '/user' do
|
23
|
-
{ :
|
23
|
+
{ type1: params[:type1], type2: params[:type2] }
|
24
24
|
end
|
25
25
|
|
26
26
|
params do
|
27
27
|
requires :id
|
28
|
-
optional :type1, :
|
29
|
-
optional :type2, :
|
28
|
+
optional :type1, default: 'default-type1'
|
29
|
+
optional :type2, default: 'default-type2'
|
30
30
|
end
|
31
31
|
|
32
32
|
get '/message' do
|
33
|
-
{ :
|
33
|
+
{ id: params[:id], type1: params[:type1], type2: params[:type2] }
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
@@ -43,25 +43,24 @@ describe Grape::Validations::DefaultValidator do
|
|
43
43
|
it 'set default value for optional param' do
|
44
44
|
get("/")
|
45
45
|
last_response.status.should == 200
|
46
|
-
last_response.body.should == { :
|
46
|
+
last_response.body.should == { id: nil, type: 'default-type' }.to_json
|
47
47
|
end
|
48
48
|
|
49
49
|
it 'set default values for optional params' do
|
50
50
|
get("/user")
|
51
51
|
last_response.status.should == 200
|
52
|
-
last_response.body.should == { :
|
52
|
+
last_response.body.should == { type1: 'default-type1', type2: 'default-type2' }.to_json
|
53
53
|
end
|
54
54
|
|
55
55
|
it 'set default values for missing params in the request' do
|
56
56
|
get("/user?type2=value2")
|
57
57
|
last_response.status.should == 200
|
58
|
-
last_response.body.should == { :
|
58
|
+
last_response.body.should == { type1: 'default-type1', type2: 'value2' }.to_json
|
59
59
|
end
|
60
60
|
|
61
61
|
it 'set default values for optional params and allow to use required fields in the same time' do
|
62
62
|
get("/message?id=1")
|
63
63
|
last_response.status.should == 200
|
64
|
-
last_response.body.should == { :
|
64
|
+
last_response.body.should == { id: '1', type1: 'default-type1', type2: 'default-type2' }.to_json
|
65
65
|
end
|
66
66
|
end
|
67
|
-
|