@@ -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
@@ -32,6 +32,7 @@ class API < Grape::API
3232
3333 params do
3434 requires :int , :coerce => Integer
35+ optional :int2 , :coerce => Integer
3536 optional :arr , :coerce => Array [ Integer ]
3637 optional :bool , :coerce => Array [ Boolean ]
3738 end
@@ -42,52 +43,69 @@ class API < Grape::API
4243 :bool => params [ :bool ] ? ( params [ :bool ] [ 0 ] == true ) && ( params [ :bool ] [ 1 ] == false ) : nil
4344 }
4445 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
4554 end
4655 end
4756 end
48-
57+
4958 def app
5059 ValidationsSpec ::CoerceValidatorSpec ::API
5160 end
52-
61+
5362 it "should return an error on malformed input" do
5463 get '/single' , :int => "43a"
5564 last_response . status . should == 400
56-
65+
5766 get '/single' , :int => "43"
5867 last_response . status . should == 200
5968 end
60-
69+
6170 it "should return an error on malformed input (array)" do
6271 get '/arr' , :ids => [ "1" , "2" , "az" ]
6372 last_response . status . should == 400
64-
73+
6574 get '/arr' , :ids => [ "1" , "2" , "890" ]
6675 last_response . status . should == 200
6776 end
68-
77+
6978 it "should return an error on malformed input (complex object)" do
7079 # this request does raise an error inside Virtus
7180 get '/user' , :user => "32"
7281 last_response . status . should == 400
73-
82+
7483 get '/user' , :user => { :id => 32 , :name => "Bob" }
7584 last_response . status . should == 200
7685 end
77-
86+
7887 it 'should coerce inputs' do
79- get ( '/coerce' , :int => "43" )
88+ get ( '/coerce' , :int => "43" , :int2 => "42" )
8089 last_response . status . should == 200
8190 ret = MultiJson . load ( last_response . body )
8291 ret [ "int" ] . should == "Fixnum"
83-
84- get ( '/coerce' , :int => "40" , :arr => [ "1" , "20" , "3" ] , :bool => [ 1 , 0 ] )
92+
93+ get ( '/coerce' , :int => "40" , :int2 => "42" , : arr => [ "1" , "20" , "3" ] , :bool => [ 1 , 0 ] )
8594 # last_response.body.should == ""
8695 last_response . status . should == 200
8796 ret = MultiJson . load ( last_response . body )
8897 ret [ "int" ] . should == "Fixnum"
8998 ret [ "arr" ] . should == "Fixnum"
9099 ret [ "bool" ] . should == true
91100 end
92-
101+
102+ it 'should not return an error when an optional parameter is nil' do
103+ get ( '/coerce' , :int => "40" )
104+ last_response . status . should == 200
105+ 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
93111end
0 commit comments