@@ -8,7 +8,7 @@ class User
8
8
attribute :id , Integer
9
9
attribute :name , String
10
10
end
11
-
11
+
12
12
class API < Grape ::API
13
13
default_format :json
14
14
@@ -32,6 +32,7 @@ class API < Grape::API
32
32
33
33
params do
34
34
requires :int , :coerce => Integer
35
+ optional :int2 , :coerce => Integer
35
36
optional :arr , :coerce => Array [ Integer ]
36
37
optional :bool , :coerce => Array [ Boolean ]
37
38
end
@@ -42,52 +43,69 @@ class API < Grape::API
42
43
:bool => params [ :bool ] ? ( params [ :bool ] [ 0 ] == true ) && ( params [ :bool ] [ 1 ] == false ) : nil
43
44
}
44
45
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
45
54
end
46
55
end
47
56
end
48
-
57
+
49
58
def app
50
59
ValidationsSpec ::CoerceValidatorSpec ::API
51
60
end
52
-
61
+
53
62
it "should return an error on malformed input" do
54
63
get '/single' , :int => "43a"
55
64
last_response . status . should == 400
56
-
65
+
57
66
get '/single' , :int => "43"
58
67
last_response . status . should == 200
59
68
end
60
-
69
+
61
70
it "should return an error on malformed input (array)" do
62
71
get '/arr' , :ids => [ "1" , "2" , "az" ]
63
72
last_response . status . should == 400
64
-
73
+
65
74
get '/arr' , :ids => [ "1" , "2" , "890" ]
66
75
last_response . status . should == 200
67
76
end
68
-
77
+
69
78
it "should return an error on malformed input (complex object)" do
70
79
# this request does raise an error inside Virtus
71
80
get '/user' , :user => "32"
72
81
last_response . status . should == 400
73
-
82
+
74
83
get '/user' , :user => { :id => 32 , :name => "Bob" }
75
84
last_response . status . should == 200
76
85
end
77
-
86
+
78
87
it 'should coerce inputs' do
79
- get ( '/coerce' , :int => "43" )
88
+ get ( '/coerce' , :int => "43" , :int2 => "42" )
80
89
last_response . status . should == 200
81
90
ret = MultiJson . load ( last_response . body )
82
91
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 ] )
85
94
# last_response.body.should == ""
86
95
last_response . status . should == 200
87
96
ret = MultiJson . load ( last_response . body )
88
97
ret [ "int" ] . should == "Fixnum"
89
98
ret [ "arr" ] . should == "Fixnum"
90
99
ret [ "bool" ] . should == true
91
100
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
93
111
end
0 commit comments