|
| 1 | +require 'spec_helper' |
| 2 | + |
| 3 | +describe Grape::Validations do |
| 4 | + def app; @app; end |
| 5 | + |
| 6 | + before do |
| 7 | + @app = Class.new(Grape::API) do |
| 8 | + default_format :json |
| 9 | + |
| 10 | + requires :id, :regexp => /^[0-9]+$/ |
| 11 | + post do |
| 12 | + {:ret => params[:id]} |
| 13 | + end |
| 14 | + |
| 15 | + requires :name, :company |
| 16 | + optional :a_number, :regexp => /^[0-9]+$/ |
| 17 | + get do |
| 18 | + "Hello" |
| 19 | + end |
| 20 | + |
| 21 | + requires :int, :coerce => Integer |
| 22 | + optional :arr, :coerce => Array[Integer] |
| 23 | + optional :bool, :coerce => Array[Boolean] |
| 24 | + get '/coerce' do |
| 25 | + { |
| 26 | + :int => params[:int].class, |
| 27 | + :arr => params[:arr] ? params[:arr][0].class : nil, |
| 28 | + :bool => params[:bool] ? (params[:bool][0] == true) && (params[:bool][1] == false) : nil |
| 29 | + } |
| 30 | + end |
| 31 | + |
| 32 | + end |
| 33 | + |
| 34 | + end |
| 35 | + |
| 36 | + it 'validates id' do |
| 37 | + post('/') |
| 38 | + last_response.status.should == 400 |
| 39 | + last_response.body.should == "missing parameter: id" |
| 40 | + |
| 41 | + post('/', {}, 'rack.input' => StringIO.new('{"id" : "a56b"}')) |
| 42 | + last_response.body.should == 'invalid parameter: id' |
| 43 | + last_response.status.should == 400 |
| 44 | + |
| 45 | + post('/', {}, 'rack.input' => StringIO.new('{"id" : 56}')) |
| 46 | + last_response.body.should == '{"ret":56}' |
| 47 | + last_response.status.should == 201 |
| 48 | + end |
| 49 | + |
| 50 | + it 'validates name, company' do |
| 51 | + get('/') |
| 52 | + last_response.status.should == 400 |
| 53 | + last_response.body.should == "missing parameter: name" |
| 54 | + |
| 55 | + get('/', :name => "Bob") |
| 56 | + last_response.status.should == 400 |
| 57 | + last_response.body.should == "missing parameter: company" |
| 58 | + |
| 59 | + get('/', :name => "Bob", :company => "TestCorp") |
| 60 | + last_response.status.should == 200 |
| 61 | + last_response.body.should == "Hello" |
| 62 | + end |
| 63 | + |
| 64 | + it 'validates optional parameter if present' do |
| 65 | + get('/', :name => "Bob", :company => "TestCorp", :a_number => "string") |
| 66 | + last_response.status.should == 400 |
| 67 | + last_response.body.should == "invalid parameter: a_number" |
| 68 | + |
| 69 | + get('/', :name => "Bob", :company => "TestCorp", :a_number => 45) |
| 70 | + last_response.status.should == 200 |
| 71 | + last_response.body.should == "Hello" |
| 72 | + end |
| 73 | + |
| 74 | + it 'should coerce inputs' do |
| 75 | + get('/coerce', :int => "43") |
| 76 | + last_response.status.should == 200 |
| 77 | + ret = MultiJson.load(last_response.body) |
| 78 | + ret["int"].should == "Fixnum" |
| 79 | + |
| 80 | + get('/coerce', :int => "40", :arr => ["1","20","3"], :bool => [1, 0]) |
| 81 | + last_response.status.should == 200 |
| 82 | + ret = MultiJson.load(last_response.body) |
| 83 | + ret["int"].should == "Fixnum" |
| 84 | + ret["arr"].should == "Fixnum" |
| 85 | + ret["bool"].should == true |
| 86 | + end |
| 87 | + |
| 88 | +end |
0 commit comments