aboutsummaryrefslogtreecommitdiff
path: root/spec/unit/router/call_spec.rb
blob: 6f5c78c6e06f29d16ad2bdde5c0c5c88177f2ce3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'spec_helper'

describe Zero::Router, '#call' do
  subject { Zero::Router.new(routes) }
  let(:result) { ['success'] }

  let(:wrong_app) do
    lambda {|env| [200, {'Content-Type' => 'text/html'}, 'Wrong'] }
  end
  let(:app) { SpecApp }

  shared_examples_for 'a sample app' do
    it "returns a response" do
      subject.call(env).to_a[2].should eq(result)
    end
  end

  context 'with a single route' do
    let(:routes) {{ '/' => app }}
    let(:env) { EnvGenerator.get('/') }
    it_behaves_like "a sample app"
  end

  context 'with multiple router' do
    let(:routes) {{ '/foo' => app, '/wrong' => wrong_app }}
    let(:env) { EnvGenerator.get('/foo') }
    it_behaves_like "a sample app"
  end

  context 'with nested routes' do
    let(:routes) {{ '/' => wrong_app, '/foo' => app, '/foo/bar' => wrong_app }}
    let(:env) { EnvGenerator.get('/foo') }
    it_behaves_like "a sample app"
  end

  context 'with a route not found' do
    let(:routes) {{ '/foo' => wrong_app, '/foo/bar/baz' => app }}
    let(:env) { EnvGenerator.get('/foo/bar') }
    let(:result) { ['Not found!'] }
    it_behaves_like "a sample app"
  end

  context 'with parameters' do
    let(:routes) {{ '/foo/:id' => app }}
    let(:env) { EnvGenerator.get('/foo/bar') }
    let(:app) do
      lambda do |env|
        [200, {}, [Zero::Request.new(env).params['id']]]
      end
    end
    let(:result) { ['bar'] }

    it_behaves_like 'a sample app'
  end
end