1
0
Fork 0
mirror of https://github.com/sinatra/sinatra synced 2023-03-27 23:18:01 -04:00
sinatra/sinatra-contrib/spec/required_params_spec.rb
2016-05-04 17:19:29 +09:00

68 lines
2.1 KiB
Ruby

require_relative 'spec_helper'
describe Sinatra::RequiredParams do
context "#required_params" do
context "simple keys" do
before do
mock_app do
helpers Sinatra::RequiredParams
get('/') { required_params(:p1, :p2) }
end
end
it 'return 400 if required params do not exist' do
get('/')
expect(last_response.status).to eq(400)
end
it 'return 400 if required params do not exist partially' do
get('/', :p1 => 1)
expect(last_response.status).to eq(400)
end
it 'return 200 if required params exist' do
get('/', :p1 => 1, :p2 => 2)
expect(last_response.status).to eq(200)
end
it 'return 200 if required params exist with array' do
get('/', :p1 => 1, :p2 => [31, 32, 33])
expect(last_response.status).to eq(200)
end
end
context "hash keys" do
before do
mock_app do
helpers Sinatra::RequiredParams
get('/') { required_params(:p1, :p2 => :p21) }
end
end
it 'return 400 if required params do not exist' do
get('/')
expect(last_response.status).to eq(400)
end
it 'return 200 if required params exist' do
get('/', :p1 => 1, :p2 => {:p21 => 21})
expect(last_response.status).to eq(200)
end
end
context "complex keys" do
before do
mock_app do
helpers Sinatra::RequiredParams
get('/') { required_params(:p1 => [:p11, {:p12 => :p121, :p122 => [:p123, {:p124 => :p1241}]}]) }
end
end
it 'return 400 if required params do not exist' do
get('/')
expect(last_response.status).to eq(400)
end
it 'return 200 if required params exist' do
get('/', :p1 => {:p11 => 11, :p12 => {:p121 => 121}, :p122 => {:p123 => 123, :p124 => {:p1241 => 1241}}})
expect(last_response.status).to eq(200)
end
end
end
context "#_required_params" do
it "is invisible" do
expect { _required_params }.to raise_error(NameError)
end
end
end