File: rack_mount_compat_spec.rb

package info (click to toggle)
ruby-addressable 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 712 kB
  • ctags: 211
  • sloc: ruby: 9,132; makefile: 10
file content (104 lines) | stat: -rw-r--r-- 3,326 bytes parent folder | download
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# coding: utf-8
# Copyright (C) 2006-2015 Bob Aman
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.


require "spec_helper"

require "addressable/uri"
require "addressable/template"
require "rack/mount"

describe Rack::Mount do
  let(:app_one) do
    proc { |env| [200, {'Content-Type' => 'text/plain'}, 'Route 1'] }
  end
  let(:app_two) do
    proc { |env| [200, {'Content-Type' => 'text/plain'}, 'Route 2'] }
  end
  let(:app_three) do
    proc { |env| [200, {'Content-Type' => 'text/plain'}, 'Route 3'] }
  end
  let(:routes) do
    s = Rack::Mount::RouteSet.new do |set|
      set.add_route(app_one, {
        :request_method => 'GET',
        :path_info => Addressable::Template.new('/one/{id}/')
      }, {:id => 'unidentified'}, :one)
      set.add_route(app_two, {
        :request_method => 'GET',
        :path_info => Addressable::Template.new('/two/')
      }, {:id => 'unidentified'}, :two)
      set.add_route(app_three, {
        :request_method => 'GET',
        :path_info => Addressable::Template.new('/three/{id}/').to_regexp
      }, {:id => 'unidentified'}, :three)
    end
    s.rehash
    s
  end

  it "should generate from routes with Addressable::Template" do
    path, _ = routes.generate(:path_info, :one, {:id => '123'})
    expect(path).to eq '/one/123/'
  end

  it "should generate from routes with Addressable::Template using defaults" do
    path, _ = routes.generate(:path_info, :one, {})
    expect(path).to eq '/one/unidentified/'
  end

  it "should recognize routes with Addressable::Template" do
    request = Rack::Request.new(
      'REQUEST_METHOD' => 'GET',
      'PATH_INFO' => '/one/123/'
    )
    route, _, params = routes.recognize(request)
    expect(route).not_to be_nil
    expect(route.app).to eq app_one
    expect(params).to eq({id: '123'})
  end

  it "should generate from routes with Addressable::Template" do
    path, _ = routes.generate(:path_info, :two, {:id => '654'})
    expect(path).to eq '/two/'
  end

  it "should generate from routes with Addressable::Template using defaults" do
    path, _ = routes.generate(:path_info, :two, {})
    expect(path).to eq '/two/'
  end

  it "should recognize routes with Addressable::Template" do
    request = Rack::Request.new(
      'REQUEST_METHOD' => 'GET',
      'PATH_INFO' => '/two/'
    )
    route, _, params = routes.recognize(request)
    expect(route).not_to be_nil
    expect(route.app).to eq app_two
    expect(params).to eq({id: 'unidentified'})
  end

  it "should recognize routes with derived Regexp" do
    request = Rack::Request.new(
      'REQUEST_METHOD' => 'GET',
      'PATH_INFO' => '/three/789/'
    )
    route, _, params = routes.recognize(request)
    expect(route).not_to be_nil
    expect(route.app).to eq app_three
    expect(params).to eq({id: '789'})
  end
end