Skip to content

Commit 0d14cd8

Browse files
Add test for file validation
+some cleanup of unnecessary spaces
1 parent f531fda commit 0d14cd8

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

spec/grape/validations/coerce_spec.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class User
88
attribute :id, Integer
99
attribute :name, String
1010
end
11-
11+
1212
class API < Grape::API
1313
default_format :json
1414

@@ -43,39 +43,47 @@ class API < Grape::API
4343
:bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil
4444
}
4545
end
46+
params do
47+
requires :uploaded_file, :type => Rack::Multipart::UploadedFile
48+
end
49+
post '/file' do
50+
{
51+
:dpx_file => params[:uploaded_file]
52+
}
53+
end
4654
end
4755
end
4856
end
49-
57+
5058
def app
5159
ValidationsSpec::CoerceValidatorSpec::API
5260
end
53-
61+
5462
it "should return an error on malformed input" do
5563
get '/single', :int => "43a"
5664
last_response.status.should == 400
57-
65+
5866
get '/single', :int => "43"
5967
last_response.status.should == 200
6068
end
61-
69+
6270
it "should return an error on malformed input (array)" do
6371
get '/arr', :ids => ["1", "2", "az"]
6472
last_response.status.should == 400
65-
73+
6674
get '/arr', :ids => ["1", "2", "890"]
6775
last_response.status.should == 200
6876
end
69-
77+
7078
it "should return an error on malformed input (complex object)" do
7179
# this request does raise an error inside Virtus
7280
get '/user', :user => "32"
7381
last_response.status.should == 400
74-
82+
7583
get '/user', :user => { :id => 32, :name => "Bob"}
7684
last_response.status.should == 200
7785
end
78-
86+
7987
it 'should coerce inputs' do
8088
get('/coerce', :int => "43", :int2 => "42")
8189
last_response.status.should == 200
@@ -90,9 +98,14 @@ def app
9098
ret["arr"].should == "Fixnum"
9199
ret["bool"].should == true
92100
end
93-
101+
94102
it 'should not return an error when an optional parameter is nil' do
95103
get('/coerce', :int => "40")
96104
last_response.status.should == 200
97105
end
106+
107+
it 'should coerce a file' do
108+
post('/file', :uploaded_file => Rack::Test::UploadedFile.new(__FILE__))
109+
last_response.status.should == 201
110+
end
98111
end

0 commit comments

Comments
 (0)