|
1 | 1 | require 'spec_helper'
|
2 | 2 |
|
3 | 3 | describe Grape::Validations::CoerceValidator do
|
4 |
| - module ValidationsSpec |
5 |
| - module CoerceValidatorSpec |
6 |
| - class User |
7 |
| - include Virtus |
8 |
| - attribute :id, Integer |
9 |
| - attribute :name, String |
10 |
| - end |
| 4 | + subject { Class.new(Grape::API) } |
| 5 | + def app; subject end |
11 | 6 |
|
12 |
| - class API < Grape::API |
13 |
| - default_format :json |
| 7 | + describe 'coerce' do |
| 8 | + it 'error on malformed input' do |
| 9 | + subject.params { requires :int, :type => Integer } |
| 10 | + subject.get '/single' do 'int works'; end |
14 | 11 |
|
15 |
| - params do |
16 |
| - requires :int, :coerce => Integer |
17 |
| - end |
18 |
| - get '/single' do |
19 |
| - end |
| 12 | + get '/single', :int => '43a' |
| 13 | + last_response.status.should == 400 |
| 14 | + last_response.body.should == 'invalid parameter: int' |
20 | 15 |
|
21 |
| - params do |
22 |
| - requires :ids, :type => Array[Integer] |
23 |
| - end |
24 |
| - get '/arr' do |
25 |
| - end |
| 16 | + get '/single', :int => '43' |
| 17 | + last_response.status.should == 200 |
| 18 | + last_response.body.should == 'int works' |
| 19 | + end |
26 | 20 |
|
27 |
| - params do |
28 |
| - requires :user, :type => ValidationsSpec::CoerceValidatorSpec::User |
29 |
| - end |
30 |
| - get '/user' do |
31 |
| - end |
| 21 | + it 'error on malformed input (Array)' do |
| 22 | + subject.params { requires :ids, :type => Array[Integer] } |
| 23 | + subject.get '/array' do 'array int works'; end |
32 | 24 |
|
33 |
| - params do |
34 |
| - requires :int, :coerce => Integer |
35 |
| - optional :int2, :coerce => Integer |
36 |
| - optional :arr, :coerce => Array[Integer] |
37 |
| - optional :bool, :coerce => Array[Boolean] |
38 |
| - end |
39 |
| - get '/coerce' do |
40 |
| - { |
41 |
| - :int => params[:int].class, |
42 |
| - :arr => params[:arr] ? params[:arr][0].class : nil, |
43 |
| - :bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil |
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 |
| - } |
| 25 | + get 'array', { :ids => ['1', '2', 'az'] } |
| 26 | + last_response.status.should == 400 |
| 27 | + last_response.body.should == 'invalid parameter: ids' |
| 28 | + |
| 29 | + get 'array', { :ids => ['1', '2', '890'] } |
| 30 | + last_response.status.should == 200 |
| 31 | + last_response.body.should == 'array int works' |
| 32 | + end |
| 33 | + |
| 34 | + context 'complex objects' do |
| 35 | + module CoerceValidatorSpec |
| 36 | + class User |
| 37 | + include Virtus |
| 38 | + attribute :id, Integer |
| 39 | + attribute :name, String |
53 | 40 | end
|
54 | 41 | end
|
55 |
| - end |
56 |
| - end |
57 | 42 |
|
58 |
| - def app |
59 |
| - ValidationsSpec::CoerceValidatorSpec::API |
60 |
| - end |
| 43 | + it 'error on malformed input for complex objects' do |
| 44 | + subject.params { requires :user, :type => CoerceValidatorSpec::User } |
| 45 | + subject.get '/user' do 'complex works'; end |
61 | 46 |
|
62 |
| - it "should return an error on malformed input" do |
63 |
| - get '/single', :int => "43a" |
64 |
| - last_response.status.should == 400 |
| 47 | + get '/user', :user => "32" |
| 48 | + last_response.status.should == 400 |
| 49 | + last_response.body.should == 'invalid parameter: user' |
65 | 50 |
|
66 |
| - get '/single', :int => "43" |
67 |
| - last_response.status.should == 200 |
68 |
| - end |
| 51 | + get '/user', :user => { :id => 32, :name => 'Bob' } |
| 52 | + last_response.status.should == 200 |
| 53 | + last_response.body.should == 'complex works' |
| 54 | + end |
| 55 | + end |
69 | 56 |
|
70 |
| - it "should return an error on malformed input (array)" do |
71 |
| - get '/arr', :ids => ["1", "2", "az"] |
72 |
| - last_response.status.should == 400 |
| 57 | + context 'coerces' do |
| 58 | + it 'Integer' do |
| 59 | + subject.params { requires :int, :coerce => Integer } |
| 60 | + subject.get '/int' do params[:int].class; end |
73 | 61 |
|
74 |
| - get '/arr', :ids => ["1", "2", "890"] |
75 |
| - last_response.status.should == 200 |
76 |
| - end |
| 62 | + get '/int', { :int => "45" } |
| 63 | + last_response.status.should == 200 |
| 64 | + last_response.body.should == 'Fixnum' |
| 65 | + end |
77 | 66 |
|
78 |
| - it "should return an error on malformed input (complex object)" do |
79 |
| - # this request does raise an error inside Virtus |
80 |
| - get '/user', :user => "32" |
81 |
| - last_response.status.should == 400 |
| 67 | + it 'Array of Integers' do |
| 68 | + subject.params { requires :arry, :coerce => Array[Integer] } |
| 69 | + subject.get '/array' do params[:arry][0].class; end |
82 | 70 |
|
83 |
| - get '/user', :user => { :id => 32, :name => "Bob"} |
84 |
| - last_response.status.should == 200 |
85 |
| - end |
| 71 | + get '/array', { :arry => [ '1', '2', '3' ] } |
| 72 | + last_response.status.should == 200 |
| 73 | + last_response.body.should == 'Fixnum' |
| 74 | + end |
86 | 75 |
|
87 |
| - it 'should coerce inputs' do |
88 |
| - get('/coerce', :int => "43", :int2 => "42") |
89 |
| - last_response.status.should == 200 |
90 |
| - ret = MultiJson.load(last_response.body) |
91 |
| - ret["int"].should == "Fixnum" |
92 |
| - |
93 |
| - get('/coerce', :int => "40", :int2 => "42", :arr => ["1","20","3"], :bool => [1, 0]) |
94 |
| - # last_response.body.should == "" |
95 |
| - last_response.status.should == 200 |
96 |
| - ret = MultiJson.load(last_response.body) |
97 |
| - ret["int"].should == "Fixnum" |
98 |
| - ret["arr"].should == "Fixnum" |
99 |
| - ret["bool"].should == true |
100 |
| - end |
| 76 | + it 'Array of Bools' do |
| 77 | + subject.params { requires :arry, :coerce => Array[Virtus::Attribute::Boolean] } |
| 78 | + subject.get '/array' do params[:arry][0].class; end |
101 | 79 |
|
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 |
| 80 | + get 'array', { :arry => [1, 0] } |
| 81 | + last_response.status.should == 200 |
| 82 | + last_response.body.should == 'TrueClass' |
| 83 | + end |
| 84 | + |
| 85 | + it 'Bool' do |
| 86 | + subject.params { requires :bool, :coerce => Virtus::Attribute::Boolean } |
| 87 | + subject.get '/bool' do params[:bool].class; end |
| 88 | + |
| 89 | + get '/bool', { :bool => 1 } |
| 90 | + last_response.status.should == 200 |
| 91 | + last_response.body.should == 'TrueClass' |
| 92 | + |
| 93 | + get '/bool', { :bool => 0 } |
| 94 | + last_response.status.should == 200 |
| 95 | + last_response.body.should == 'FalseClass' |
| 96 | + |
| 97 | + get '/bool', { :bool => 'false' } |
| 98 | + last_response.status.should == 200 |
| 99 | + last_response.body.should == 'FalseClass' |
| 100 | + |
| 101 | + get '/bool', { :bool => 'true' } |
| 102 | + last_response.status.should == 200 |
| 103 | + last_response.body.should == 'TrueClass' |
| 104 | + end |
| 105 | + |
| 106 | + it 'file' do |
| 107 | + subject.params { requires :file, :coerce => Rack::Multipart::UploadedFile } |
| 108 | + subject.post '/upload' do params[:file].filename; end |
106 | 109 |
|
107 |
| - it 'should coerce a file' do |
108 |
| - post('/file', :uploaded_file => Rack::Test::UploadedFile.new(__FILE__)) |
109 |
| - last_response.status.should == 201 |
| 110 | + post '/upload', { :file => Rack::Test::UploadedFile.new(__FILE__) } |
| 111 | + last_response.status.should == 201 |
| 112 | + last_response.body.should == File.basename(__FILE__).to_s |
| 113 | + end |
| 114 | + end |
110 | 115 | end
|
111 | 116 | end
|
0 commit comments