Skip to content

Commit bc3318c

Browse files
committed
extracted validator tests in their own fiels
1 parent fdd865b commit bc3318c

File tree

4 files changed

+136
-64
lines changed

4 files changed

+136
-64
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Validations::CoerceValidator do
4+
def app; @app; end
5+
6+
before do
7+
@app = Class.new(Grape::API) do
8+
default_format :json
9+
10+
params do
11+
requires :int, :coerce => Integer
12+
optional :arr, :coerce => Array[Integer]
13+
optional :bool, :coerce => Array[Boolean]
14+
end
15+
get '/coerce' do
16+
{
17+
:int => params[:int].class,
18+
:arr => params[:arr] ? params[:arr][0].class : nil,
19+
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
20+
}
21+
end
22+
23+
end
24+
25+
end
26+
27+
# TOOD: Later when virtus can tell us that an input IS invalid
28+
# it "should return an error on malformed input" do
29+
# get '/coerce', :int => "43a"
30+
# last_response.status.should == 400
31+
# end
32+
33+
it 'should coerce inputs' do
34+
get('/coerce', :int => "43")
35+
last_response.status.should == 200
36+
ret = MultiJson.load(last_response.body)
37+
ret["int"].should == "Fixnum"
38+
39+
get('/coerce', :int => "40", :arr => ["1","20","3"], :bool => [1, 0])
40+
last_response.status.should == 200
41+
ret = MultiJson.load(last_response.body)
42+
ret["int"].should == "Fixnum"
43+
ret["arr"].should == "Fixnum"
44+
ret["bool"].should == true
45+
end
46+
47+
end
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Validations::PresenceValidator do
4+
def app; @app; end
5+
6+
before do
7+
@app = Class.new(Grape::API) do
8+
default_format :json
9+
10+
params do
11+
requires :id, :regexp => /^[0-9]+$/
12+
end
13+
14+
post do
15+
{:ret => params[:id]}
16+
end
17+
18+
params do
19+
requires :name, :company
20+
end
21+
22+
get do
23+
"Hello"
24+
end
25+
26+
end
27+
28+
end
29+
30+
it 'validates id' do
31+
post('/')
32+
last_response.status.should == 400
33+
last_response.body.should == "missing parameter: id"
34+
35+
post('/', {}, 'rack.input' => StringIO.new('{"id" : "a56b"}'))
36+
last_response.body.should == 'invalid parameter: id'
37+
last_response.status.should == 400
38+
39+
post('/', {}, 'rack.input' => StringIO.new('{"id" : 56}'))
40+
last_response.body.should == '{"ret":56}'
41+
last_response.status.should == 201
42+
end
43+
44+
it 'validates name, company' do
45+
get('/')
46+
last_response.status.should == 400
47+
last_response.body.should == "missing parameter: name"
48+
49+
get('/', :name => "Bob")
50+
last_response.status.should == 400
51+
last_response.body.should == "missing parameter: company"
52+
53+
get('/', :name => "Bob", :company => "TestCorp")
54+
last_response.status.should == 200
55+
last_response.body.should == "Hello"
56+
end
57+
58+
end
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
require 'spec_helper'
2+
3+
describe Grape::Validations::RegexpValidator do
4+
def app; @app; end
5+
6+
before do
7+
@app = Class.new(Grape::API) do
8+
default_format :json
9+
10+
params do
11+
requires :name, :regexp => /^[a-z]+$/
12+
end
13+
get do
14+
15+
end
16+
17+
end
18+
19+
end
20+
21+
it 'should refuse invalid input' do
22+
get '/', :name => "invalid name"
23+
last_response.status.should == 400
24+
end
25+
26+
it 'should accept valid input' do
27+
get '/', :name => "bob"
28+
last_response.status.should == 200
29+
end
30+
31+
end

spec/grape/validations_spec.rb

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,6 @@ def app; @app; end
77
@app = Class.new(Grape::API) do
88
default_format :json
99

10-
11-
params do
12-
requires :id, :regexp => /^[0-9]+$/
13-
end
14-
15-
post do
16-
{:ret => params[:id]}
17-
end
18-
1910
params do
2011
requires :name, :company
2112
optional :a_number, :regexp => /^[0-9]+$/
@@ -25,51 +16,10 @@ def app; @app; end
2516
"Hello"
2617
end
2718

28-
params do
29-
requires :int, :coerce => Integer
30-
optional :arr, :coerce => Array[Integer]
31-
optional :bool, :coerce => Array[Boolean]
32-
end
33-
get '/coerce' do
34-
{
35-
:int => params[:int].class,
36-
:arr => params[:arr] ? params[:arr][0].class : nil,
37-
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
38-
}
39-
end
40-
4119
end
4220

4321
end
4422

45-
it 'validates id' do
46-
post('/')
47-
last_response.status.should == 400
48-
last_response.body.should == "missing parameter: id"
49-
50-
post('/', {}, 'rack.input' => StringIO.new('{"id" : "a56b"}'))
51-
last_response.body.should == 'invalid parameter: id'
52-
last_response.status.should == 400
53-
54-
post('/', {}, 'rack.input' => StringIO.new('{"id" : 56}'))
55-
last_response.body.should == '{"ret":56}'
56-
last_response.status.should == 201
57-
end
58-
59-
it 'validates name, company' do
60-
get('/')
61-
last_response.status.should == 400
62-
last_response.body.should == "missing parameter: name"
63-
64-
get('/', :name => "Bob")
65-
last_response.status.should == 400
66-
last_response.body.should == "missing parameter: company"
67-
68-
get('/', :name => "Bob", :company => "TestCorp")
69-
last_response.status.should == 200
70-
last_response.body.should == "Hello"
71-
end
72-
7323
it 'validates optional parameter if present' do
7424
get('/', :name => "Bob", :company => "TestCorp", :a_number => "string")
7525
last_response.status.should == 400
@@ -80,18 +30,4 @@ def app; @app; end
8030
last_response.body.should == "Hello"
8131
end
8232

83-
it 'should coerce inputs' do
84-
get('/coerce', :int => "43")
85-
last_response.status.should == 200
86-
ret = MultiJson.load(last_response.body)
87-
ret["int"].should == "Fixnum"
88-
89-
get('/coerce', :int => "40", :arr => ["1","20","3"], :bool => [1, 0])
90-
last_response.status.should == 200
91-
ret = MultiJson.load(last_response.body)
92-
ret["int"].should == "Fixnum"
93-
ret["arr"].should == "Fixnum"
94-
ret["bool"].should == true
95-
end
96-
9733
end

0 commit comments

Comments
 (0)