0
0
zero/spec/unit/router/call_spec.rb

61 lines
1.6 KiB
Ruby
Raw Normal View History

2012-11-16 10:50:49 +01:00
require 'spec_helper'
describe Zero::Router, '#call' do
subject { Zero::Router.new(routes) }
let(:result) { ['success'] }
2013-01-06 19:09:44 +01:00
let(:content_type) { {'Content-Type' => 'text/html'} }
let(:status_code) { 200 }
2012-11-16 10:50:49 +01:00
let(:wrong_app) do
lambda {|env| [200, {'Content-Type' => 'text/html'}, 'Wrong'] }
end
let(:app) { SpecApp }
2012-11-29 20:02:28 +01:00
shared_examples_for 'a sample app' do
2012-11-16 10:50:49 +01:00
it "returns a response" do
2013-01-06 19:09:44 +01:00
subject.call(env).to_a[0].should eq(status_code)
subject.call(env).to_a[1].should eq(content_type)
2012-11-16 10:50:49 +01:00
subject.call(env).to_a[2].should eq(result)
end
end
2012-11-29 20:02:28 +01:00
context 'with a single route' do
2012-11-16 10:50:49 +01:00
let(:routes) {{ '/' => app }}
let(:env) { EnvGenerator.get('/') }
it_behaves_like "a sample app"
end
2012-11-29 20:02:28 +01:00
context 'with multiple router' do
2012-11-16 10:50:49 +01:00
let(:routes) {{ '/foo' => app, '/wrong' => wrong_app }}
let(:env) { EnvGenerator.get('/foo') }
it_behaves_like "a sample app"
end
2012-11-29 20:02:28 +01:00
context 'with nested routes' do
2012-11-16 10:50:49 +01:00
let(:routes) {{ '/' => wrong_app, '/foo' => app, '/foo/bar' => wrong_app }}
let(:env) { EnvGenerator.get('/foo') }
it_behaves_like "a sample app"
end
2012-11-29 20:02:28 +01:00
context 'with a route not found' do
2012-11-16 10:50:49 +01:00
let(:routes) {{ '/foo' => wrong_app, '/foo/bar/baz' => app }}
let(:env) { EnvGenerator.get('/foo/bar') }
let(:result) { ['Not found!'] }
2013-01-06 19:09:44 +01:00
let(:status_code) { 404 }
2012-11-16 10:50:49 +01:00
it_behaves_like "a sample app"
end
2012-11-29 20:02:28 +01:00
context 'with parameters' do
let(:routes) {{ '/foo/:id' => app }}
let(:env) { EnvGenerator.get('/foo/bar') }
let(:app) do
lambda do |env|
2013-01-06 19:09:44 +01:00
[200, content_type, [Zero::Request.new(env).params['id']]]
2012-11-29 20:02:28 +01:00
end
end
let(:result) { ['bar'] }
it_behaves_like 'a sample app'
end
2012-11-16 10:50:49 +01:00
end