rtomayko-sinatra 0.9.0 → 0.9.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +40 -0
- data/CHANGES +189 -0
- data/README.rdoc +142 -95
- data/Rakefile +14 -49
- data/compat/events_test.rb +10 -7
- data/compat/helper.rb +13 -1
- data/lib/sinatra.rb +5 -0
- data/lib/sinatra/base.rb +138 -47
- data/lib/sinatra/compat.rb +142 -44
- data/lib/sinatra/test.rb +92 -91
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +9 -0
- data/lib/sinatra/test/spec.rb +7 -0
- data/lib/sinatra/test/unit.rb +2 -2
- data/sinatra.gemspec +8 -5
- data/test/base_test.rb +33 -14
- data/test/builder_test.rb +12 -16
- data/test/erb_test.rb +11 -16
- data/test/filter_test.rb +48 -12
- data/test/haml_test.rb +14 -18
- data/test/helper.rb +25 -0
- data/test/helpers_test.rb +55 -62
- data/test/mapped_error_test.rb +43 -24
- data/test/middleware_test.rb +8 -13
- data/test/options_test.rb +29 -35
- data/test/reload_test.rb +16 -20
- data/test/request_test.rb +12 -5
- data/test/result_test.rb +16 -20
- data/test/routing_test.rb +150 -64
- data/test/sass_test.rb +8 -12
- data/test/sinatra_test.rb +2 -4
- data/test/static_test.rb +16 -19
- data/test/templates_test.rb +23 -19
- metadata +8 -6
- data/ChangeLog +0 -96
data/test/middleware_test.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe "Middleware" do
|
6
|
-
include Sinatra::Test
|
7
|
-
|
8
4
|
before do
|
9
5
|
@app = mock_app(Sinatra::Default) {
|
10
6
|
get '/*' do
|
@@ -31,8 +27,8 @@ describe "Middleware" do
|
|
31
27
|
it "is added with Sinatra::Application.use" do
|
32
28
|
@app.use UpcaseMiddleware
|
33
29
|
get '/hello-world'
|
34
|
-
|
35
|
-
|
30
|
+
assert ok?
|
31
|
+
assert_equal '/HELLO-WORLD', body
|
36
32
|
end
|
37
33
|
|
38
34
|
class DowncaseMiddleware < MockMiddleware
|
@@ -46,18 +42,17 @@ describe "Middleware" do
|
|
46
42
|
@app.use UpcaseMiddleware
|
47
43
|
@app.use DowncaseMiddleware
|
48
44
|
get '/Foo'
|
49
|
-
|
50
|
-
response['X-Tests']
|
45
|
+
assert_equal "/foo", body
|
46
|
+
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
51
47
|
end
|
52
48
|
|
53
49
|
specify "resets the prebuilt pipeline when new middleware is added" do
|
54
50
|
@app.use UpcaseMiddleware
|
55
51
|
get '/Foo'
|
56
|
-
|
52
|
+
assert_equal "/FOO", body
|
57
53
|
@app.use DowncaseMiddleware
|
58
54
|
get '/Foo'
|
59
|
-
|
60
|
-
response['X-Tests']
|
55
|
+
assert_equal '/foo', body
|
56
|
+
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
61
57
|
end
|
62
|
-
|
63
58
|
end
|
data/test/options_test.rb
CHANGED
@@ -1,33 +1,27 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe 'Options' do
|
6
|
-
|
7
|
-
|
8
|
-
before do
|
9
|
-
@app = Class.new(Sinatra::Base)
|
10
|
-
end
|
4
|
+
before { @app = Class.new(Sinatra::Base) }
|
11
5
|
|
12
6
|
it 'sets options to literal values' do
|
13
7
|
@app.set(:foo, 'bar')
|
14
|
-
@app.
|
15
|
-
@app.foo
|
8
|
+
assert @app.respond_to?(:foo)
|
9
|
+
assert_equal 'bar', @app.foo
|
16
10
|
end
|
17
11
|
|
18
12
|
it 'sets options to Procs' do
|
19
13
|
@app.set(:foo, Proc.new { 'baz' })
|
20
|
-
@app.
|
21
|
-
@app.foo
|
14
|
+
assert @app.respond_to?(:foo)
|
15
|
+
assert_equal 'baz', @app.foo
|
22
16
|
end
|
23
17
|
|
24
18
|
it "sets multiple options with a Hash" do
|
25
19
|
@app.set :foo => 1234,
|
26
20
|
:bar => 'Hello World',
|
27
21
|
:baz => Proc.new { 'bizzle' }
|
28
|
-
@app.foo
|
29
|
-
|
30
|
-
@app.baz
|
22
|
+
assert_equal 1234, @app.foo
|
23
|
+
assert_equal 'Hello World', @app.bar
|
24
|
+
assert_equal 'bizzle', @app.baz
|
31
25
|
end
|
32
26
|
|
33
27
|
it 'inherits option methods when subclassed' do
|
@@ -35,10 +29,10 @@ describe 'Options' do
|
|
35
29
|
@app.set :biz, Proc.new { 'baz' }
|
36
30
|
|
37
31
|
sub = Class.new(@app)
|
38
|
-
sub.
|
39
|
-
|
40
|
-
sub.
|
41
|
-
|
32
|
+
assert sub.respond_to?(:foo)
|
33
|
+
assert_equal 'bar', sub.foo
|
34
|
+
assert sub.respond_to?(:biz)
|
35
|
+
assert_equal 'baz', sub.biz
|
42
36
|
end
|
43
37
|
|
44
38
|
it 'overrides options in subclass' do
|
@@ -46,23 +40,23 @@ describe 'Options' do
|
|
46
40
|
@app.set :biz, Proc.new { 'baz' }
|
47
41
|
sub = Class.new(@app)
|
48
42
|
sub.set :foo, 'bling'
|
49
|
-
|
50
|
-
@app.foo
|
43
|
+
assert_equal 'bling', sub.foo
|
44
|
+
assert_equal 'bar', @app.foo
|
51
45
|
end
|
52
46
|
|
53
47
|
it 'creates setter methods when first defined' do
|
54
48
|
@app.set :foo, 'bar'
|
55
|
-
@app.
|
49
|
+
assert @app.respond_to?('foo=')
|
56
50
|
@app.foo = 'biz'
|
57
|
-
@app.foo
|
51
|
+
assert_equal 'biz', @app.foo
|
58
52
|
end
|
59
53
|
|
60
54
|
it 'creates predicate methods when first defined' do
|
61
55
|
@app.set :foo, 'hello world'
|
62
|
-
@app.
|
63
|
-
@app.foo
|
56
|
+
assert @app.respond_to?(:foo?)
|
57
|
+
assert @app.foo?
|
64
58
|
@app.set :foo, nil
|
65
|
-
|
59
|
+
assert !@app.foo?
|
66
60
|
end
|
67
61
|
|
68
62
|
it 'uses existing setter methods if detected' do
|
@@ -76,28 +70,28 @@ describe 'Options' do
|
|
76
70
|
end
|
77
71
|
|
78
72
|
@app.set :foo, 'bam'
|
79
|
-
@app.foo
|
73
|
+
assert_equal 'oops', @app.foo
|
80
74
|
end
|
81
75
|
|
82
76
|
it "sets multiple options to true with #enable" do
|
83
77
|
@app.enable :sessions, :foo, :bar
|
84
|
-
@app.sessions
|
85
|
-
@app.foo
|
86
|
-
@app.bar
|
78
|
+
assert @app.sessions
|
79
|
+
assert @app.foo
|
80
|
+
assert @app.bar
|
87
81
|
end
|
88
82
|
|
89
83
|
it "sets multiple options to false with #disable" do
|
90
84
|
@app.disable :sessions, :foo, :bar
|
91
|
-
|
92
|
-
|
93
|
-
|
85
|
+
assert !@app.sessions
|
86
|
+
assert !@app.foo
|
87
|
+
assert !@app.bar
|
94
88
|
end
|
95
89
|
|
96
90
|
it 'enables MethodOverride middleware when :methodoverride is enabled' do
|
97
91
|
@app.set :methodoverride, true
|
98
92
|
@app.put('/') { 'okay' }
|
99
93
|
post '/', {'_method'=>'PUT'}, {}
|
100
|
-
|
101
|
-
|
94
|
+
assert_equal 200, status
|
95
|
+
assert_equal 'okay', body
|
102
96
|
end
|
103
97
|
end
|
data/test/reload_test.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
$reload_count = 0
|
6
4
|
$reload_app = nil
|
7
5
|
|
8
6
|
describe "Reloading" do
|
9
|
-
include Sinatra::Test
|
10
|
-
|
11
7
|
before {
|
12
8
|
@app = mock_app(Sinatra::Default)
|
13
9
|
$reload_app = @app
|
@@ -20,31 +16,31 @@ describe "Reloading" do
|
|
20
16
|
it 'is enabled by default when in development and the app_file is set' do
|
21
17
|
@app.set :app_file, __FILE__
|
22
18
|
@app.set :environment, :development
|
23
|
-
@app.reload
|
24
|
-
@app.reload
|
19
|
+
assert_same true, @app.reload
|
20
|
+
assert_same true, @app.reload?
|
25
21
|
end
|
26
22
|
|
27
23
|
it 'is disabled by default when running in non-development environment' do
|
28
24
|
@app.set :app_file, __FILE__
|
29
25
|
@app.set :environment, :test
|
30
|
-
|
31
|
-
@app.reload
|
26
|
+
assert !@app.reload
|
27
|
+
assert_same false, @app.reload?
|
32
28
|
end
|
33
29
|
|
34
30
|
it 'is disabled by default when no app_file is available' do
|
35
31
|
@app.set :app_file, nil
|
36
32
|
@app.set :environment, :development
|
37
|
-
|
38
|
-
@app.reload
|
33
|
+
assert !@app.reload
|
34
|
+
assert_same false, @app.reload?
|
39
35
|
end
|
40
36
|
|
41
37
|
it 'can be turned off explicitly' do
|
42
38
|
@app.set :app_file, __FILE__
|
43
39
|
@app.set :environment, :development
|
44
|
-
@app.reload
|
40
|
+
assert_same true, @app.reload
|
45
41
|
@app.set :reload, false
|
46
|
-
@app.reload
|
47
|
-
@app.reload
|
42
|
+
assert_same false, @app.reload
|
43
|
+
assert_same false, @app.reload?
|
48
44
|
end
|
49
45
|
|
50
46
|
it 'reloads the app_file each time a request is made' do
|
@@ -53,13 +49,13 @@ describe "Reloading" do
|
|
53
49
|
@app.get('/') { 'Hello World' }
|
54
50
|
|
55
51
|
get '/'
|
56
|
-
|
57
|
-
|
58
|
-
$reload_count
|
52
|
+
assert_equal 200, status
|
53
|
+
assert_equal 'Hello from reload file', body
|
54
|
+
assert_equal 1, $reload_count
|
59
55
|
|
60
56
|
get '/'
|
61
|
-
|
62
|
-
|
63
|
-
$reload_count
|
57
|
+
assert_equal 200, status
|
58
|
+
assert_equal 'Hello from reload file', body
|
59
|
+
assert_equal 2, $reload_count
|
64
60
|
end
|
65
61
|
end
|
data/test/request_test.rb
CHANGED
@@ -1,11 +1,18 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe 'Sinatra::Request' do
|
6
4
|
it 'responds to #user_agent' do
|
7
5
|
request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
|
8
|
-
request.
|
9
|
-
|
6
|
+
assert request.respond_to?(:user_agent)
|
7
|
+
assert_equal 'Test', request.user_agent
|
8
|
+
end
|
9
|
+
|
10
|
+
it 'parses POST params when Content-Type is form-dataish' do
|
11
|
+
request = Sinatra::Request.new(
|
12
|
+
'REQUEST_METHOD' => 'PUT',
|
13
|
+
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
|
14
|
+
'rack.input' => StringIO.new('foo=bar')
|
15
|
+
)
|
16
|
+
assert_equal 'bar', request.params['foo']
|
10
17
|
end
|
11
18
|
end
|
data/test/result_test.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe 'Result Handling' do
|
6
|
-
include Sinatra::Test
|
7
|
-
|
8
4
|
it "sets response.body when result is a String" do
|
9
5
|
mock_app {
|
10
6
|
get '/' do
|
@@ -13,8 +9,8 @@ describe 'Result Handling' do
|
|
13
9
|
}
|
14
10
|
|
15
11
|
get '/'
|
16
|
-
|
17
|
-
|
12
|
+
assert ok?
|
13
|
+
assert_equal 'Hello World', body
|
18
14
|
end
|
19
15
|
|
20
16
|
it "sets response.body when result is an Array of Strings" do
|
@@ -25,8 +21,8 @@ describe 'Result Handling' do
|
|
25
21
|
}
|
26
22
|
|
27
23
|
get '/'
|
28
|
-
|
29
|
-
|
24
|
+
assert ok?
|
25
|
+
assert_equal 'HelloWorld', body
|
30
26
|
end
|
31
27
|
|
32
28
|
it "sets response.body when result responds to #each" do
|
@@ -39,8 +35,8 @@ describe 'Result Handling' do
|
|
39
35
|
}
|
40
36
|
|
41
37
|
get '/'
|
42
|
-
|
43
|
-
|
38
|
+
assert ok?
|
39
|
+
assert_equal 'Hello World', body
|
44
40
|
end
|
45
41
|
|
46
42
|
it "sets response.body to [] when result is nil" do
|
@@ -51,8 +47,8 @@ describe 'Result Handling' do
|
|
51
47
|
}
|
52
48
|
|
53
49
|
get '/'
|
54
|
-
|
55
|
-
|
50
|
+
assert ok?
|
51
|
+
assert_equal '', body
|
56
52
|
end
|
57
53
|
|
58
54
|
it "sets status, headers, and body when result is a Rack response tuple" do
|
@@ -63,9 +59,9 @@ describe 'Result Handling' do
|
|
63
59
|
}
|
64
60
|
|
65
61
|
get '/'
|
66
|
-
|
67
|
-
response['Content-Type']
|
68
|
-
|
62
|
+
assert_equal 205, status
|
63
|
+
assert_equal 'foo/bar', response['Content-Type']
|
64
|
+
assert_equal 'Hello World', body
|
69
65
|
end
|
70
66
|
|
71
67
|
it "sets status and body when result is a two-tuple" do
|
@@ -76,8 +72,8 @@ describe 'Result Handling' do
|
|
76
72
|
}
|
77
73
|
|
78
74
|
get '/'
|
79
|
-
|
80
|
-
|
75
|
+
assert_equal 409, status
|
76
|
+
assert_equal 'formula of', body
|
81
77
|
end
|
82
78
|
|
83
79
|
it "sets status when result is a Fixnum status code" do
|
@@ -86,7 +82,7 @@ describe 'Result Handling' do
|
|
86
82
|
}
|
87
83
|
|
88
84
|
get '/'
|
89
|
-
|
90
|
-
body
|
85
|
+
assert_equal 205, status
|
86
|
+
assert_equal '', body
|
91
87
|
end
|
92
88
|
end
|
data/test/routing_test.rb
CHANGED
@@ -1,10 +1,6 @@
|
|
1
|
-
require '
|
2
|
-
require 'sinatra/base'
|
3
|
-
require 'sinatra/test'
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
4
2
|
|
5
3
|
describe "Routing" do
|
6
|
-
include Sinatra::Test
|
7
|
-
|
8
4
|
%w[get put post delete head].each do |verb|
|
9
5
|
it "defines #{verb.upcase} request handlers with #{verb}" do
|
10
6
|
mock_app {
|
@@ -15,8 +11,8 @@ describe "Routing" do
|
|
15
11
|
|
16
12
|
request = Rack::MockRequest.new(@app)
|
17
13
|
response = request.request(verb.upcase, '/hello', {})
|
18
|
-
response.
|
19
|
-
|
14
|
+
assert response.ok?
|
15
|
+
assert_equal 'Hello World', response.body
|
20
16
|
end
|
21
17
|
end
|
22
18
|
|
@@ -25,30 +21,30 @@ describe "Routing" do
|
|
25
21
|
get('/foo') { }
|
26
22
|
}
|
27
23
|
get '/bar'
|
28
|
-
|
24
|
+
assert_equal 404, status
|
29
25
|
end
|
30
26
|
|
31
27
|
it "exposes params with indifferent hash" do
|
32
28
|
mock_app {
|
33
29
|
get '/:foo' do
|
34
|
-
params['foo']
|
35
|
-
params[:foo]
|
30
|
+
assert_equal 'bar', params['foo']
|
31
|
+
assert_equal 'bar', params[:foo]
|
36
32
|
'well, alright'
|
37
33
|
end
|
38
34
|
}
|
39
35
|
get '/bar'
|
40
|
-
|
36
|
+
assert_equal 'well, alright', body
|
41
37
|
end
|
42
38
|
|
43
39
|
it "merges named params and query string params in params" do
|
44
40
|
mock_app {
|
45
41
|
get '/:foo' do
|
46
|
-
params['foo']
|
47
|
-
params['baz']
|
42
|
+
assert_equal 'bar', params['foo']
|
43
|
+
assert_equal 'biz', params['baz']
|
48
44
|
end
|
49
45
|
}
|
50
46
|
get '/bar?baz=biz'
|
51
|
-
|
47
|
+
assert ok?
|
52
48
|
end
|
53
49
|
|
54
50
|
it "supports named params like /hello/:person" do
|
@@ -58,7 +54,7 @@ describe "Routing" do
|
|
58
54
|
end
|
59
55
|
}
|
60
56
|
get '/hello/Frank'
|
61
|
-
|
57
|
+
assert_equal 'Hello Frank', body
|
62
58
|
end
|
63
59
|
|
64
60
|
it "supports optional named params like /?:foo?/?:bar?" do
|
@@ -69,58 +65,115 @@ describe "Routing" do
|
|
69
65
|
}
|
70
66
|
|
71
67
|
get '/hello/world'
|
72
|
-
|
73
|
-
|
68
|
+
assert ok?
|
69
|
+
assert_equal "foo=hello;bar=world", body
|
74
70
|
|
75
71
|
get '/hello'
|
76
|
-
|
77
|
-
|
72
|
+
assert ok?
|
73
|
+
assert_equal "foo=hello;bar=", body
|
78
74
|
|
79
75
|
get '/'
|
80
|
-
|
81
|
-
|
76
|
+
assert ok?
|
77
|
+
assert_equal "foo=;bar=", body
|
82
78
|
end
|
83
79
|
|
84
80
|
it "supports single splat params like /*" do
|
85
81
|
mock_app {
|
86
82
|
get '/*' do
|
87
|
-
params['splat'].
|
83
|
+
assert params['splat'].kind_of?(Array)
|
88
84
|
params['splat'].join "\n"
|
89
85
|
end
|
90
86
|
}
|
91
87
|
|
92
88
|
get '/foo'
|
93
|
-
|
89
|
+
assert_equal "foo", body
|
94
90
|
|
95
91
|
get '/foo/bar/baz'
|
96
|
-
|
92
|
+
assert_equal "foo/bar/baz", body
|
97
93
|
end
|
98
94
|
|
99
95
|
it "supports mixing multiple splat params like /*/foo/*/*" do
|
100
96
|
mock_app {
|
101
97
|
get '/*/foo/*/*' do
|
102
|
-
params['splat'].
|
98
|
+
assert params['splat'].kind_of?(Array)
|
103
99
|
params['splat'].join "\n"
|
104
100
|
end
|
105
101
|
}
|
106
102
|
|
107
103
|
get '/bar/foo/bling/baz/boom'
|
108
|
-
|
104
|
+
assert_equal "bar\nbling\nbaz/boom", body
|
109
105
|
|
110
106
|
get '/bar/foo/baz'
|
111
|
-
|
107
|
+
assert not_found?
|
112
108
|
end
|
113
109
|
|
114
110
|
it "supports mixing named and splat params like /:foo/*" do
|
115
111
|
mock_app {
|
116
112
|
get '/:foo/*' do
|
117
|
-
|
118
|
-
|
113
|
+
assert_equal 'foo', params['foo']
|
114
|
+
assert_equal ['bar/baz'], params['splat']
|
119
115
|
end
|
120
116
|
}
|
121
117
|
|
122
118
|
get '/foo/bar/baz'
|
123
|
-
|
119
|
+
assert ok?
|
120
|
+
end
|
121
|
+
|
122
|
+
it "supports basic nested params" do
|
123
|
+
mock_app {
|
124
|
+
get '/hi' do
|
125
|
+
params["person"]["name"]
|
126
|
+
end
|
127
|
+
}
|
128
|
+
|
129
|
+
get "/hi?person[name]=John+Doe"
|
130
|
+
assert ok?
|
131
|
+
assert_equal "John Doe", body
|
132
|
+
end
|
133
|
+
|
134
|
+
it "exposes nested params with indifferent hash" do
|
135
|
+
mock_app {
|
136
|
+
get '/testme' do
|
137
|
+
assert_equal 'baz', params['bar']['foo']
|
138
|
+
assert_equal 'baz', params['bar'][:foo]
|
139
|
+
'well, alright'
|
140
|
+
end
|
141
|
+
}
|
142
|
+
get '/testme?bar[foo]=baz'
|
143
|
+
assert_equal 'well, alright', body
|
144
|
+
end
|
145
|
+
|
146
|
+
it "supports deeply nested params" do
|
147
|
+
input = {
|
148
|
+
'browser[chrome][engine][name]' => 'V8',
|
149
|
+
'browser[chrome][engine][version]' => '1.0',
|
150
|
+
'browser[firefox][engine][name]' => 'spidermonkey',
|
151
|
+
'browser[firefox][engine][version]' => '1.7.0',
|
152
|
+
'emacs[map][goto-line]' => 'M-g g',
|
153
|
+
'emacs[version]' => '22.3.1',
|
154
|
+
'paste[name]' => 'hello world',
|
155
|
+
'paste[syntax]' => 'ruby'
|
156
|
+
}
|
157
|
+
expected = {
|
158
|
+
"emacs" => {
|
159
|
+
"map" => { "goto-line" => "M-g g" },
|
160
|
+
"version" => "22.3.1"
|
161
|
+
},
|
162
|
+
"browser" => {
|
163
|
+
"firefox" => {"engine" => {"name"=>"spidermonkey", "version"=>"1.7.0"}},
|
164
|
+
"chrome" => {"engine" => {"name"=>"V8", "version"=>"1.0"}}
|
165
|
+
},
|
166
|
+
"paste" => {"name"=>"hello world", "syntax"=>"ruby"}
|
167
|
+
}
|
168
|
+
mock_app {
|
169
|
+
get '/foo' do
|
170
|
+
assert_equal expected, params
|
171
|
+
'looks good'
|
172
|
+
end
|
173
|
+
}
|
174
|
+
get "/foo?#{build_query(input)}"
|
175
|
+
assert ok?
|
176
|
+
assert_equal 'looks good', body
|
124
177
|
end
|
125
178
|
|
126
179
|
it "supports paths that include spaces" do
|
@@ -131,21 +184,21 @@ describe "Routing" do
|
|
131
184
|
}
|
132
185
|
|
133
186
|
get '/path%20with%20spaces'
|
134
|
-
|
135
|
-
|
187
|
+
assert ok?
|
188
|
+
assert_equal 'looks good', body
|
136
189
|
end
|
137
190
|
|
138
191
|
it "URL decodes named parameters and splats" do
|
139
192
|
mock_app {
|
140
193
|
get '/:foo/*' do
|
141
|
-
params['foo']
|
142
|
-
|
194
|
+
assert_equal 'hello world', params['foo']
|
195
|
+
assert_equal ['how are you'], params['splat']
|
143
196
|
nil
|
144
197
|
end
|
145
198
|
}
|
146
199
|
|
147
200
|
get '/hello%20world/how%20are%20you'
|
148
|
-
|
201
|
+
assert ok?
|
149
202
|
end
|
150
203
|
|
151
204
|
it 'supports regular expressions' do
|
@@ -156,21 +209,21 @@ describe "Routing" do
|
|
156
209
|
}
|
157
210
|
|
158
211
|
get '/foooom/bar'
|
159
|
-
|
160
|
-
|
212
|
+
assert ok?
|
213
|
+
assert_equal 'Hello World', body
|
161
214
|
end
|
162
215
|
|
163
216
|
it 'makes regular expression captures available in params[:captures]' do
|
164
217
|
mock_app {
|
165
218
|
get(/^\/fo(.*)\/ba(.*)/) do
|
166
|
-
|
219
|
+
assert_equal ['orooomma', 'f'], params[:captures]
|
167
220
|
'right on'
|
168
221
|
end
|
169
222
|
}
|
170
223
|
|
171
224
|
get '/foorooomma/baf'
|
172
|
-
|
173
|
-
|
225
|
+
assert ok?
|
226
|
+
assert_equal 'right on', body
|
174
227
|
end
|
175
228
|
|
176
229
|
it "returns response immediately on halt" do
|
@@ -182,8 +235,8 @@ describe "Routing" do
|
|
182
235
|
}
|
183
236
|
|
184
237
|
get '/'
|
185
|
-
|
186
|
-
|
238
|
+
assert ok?
|
239
|
+
assert_equal 'Hello World', body
|
187
240
|
end
|
188
241
|
|
189
242
|
it "transitions to the next matching route on pass" do
|
@@ -194,14 +247,14 @@ describe "Routing" do
|
|
194
247
|
end
|
195
248
|
|
196
249
|
get '/*' do
|
197
|
-
params.
|
250
|
+
assert !params.include?('foo')
|
198
251
|
'Hello World'
|
199
252
|
end
|
200
253
|
}
|
201
254
|
|
202
255
|
get '/bar'
|
203
|
-
|
204
|
-
|
256
|
+
assert ok?
|
257
|
+
assert_equal 'Hello World', body
|
205
258
|
end
|
206
259
|
|
207
260
|
it "transitions to 404 when passed and no subsequent route matches" do
|
@@ -213,7 +266,7 @@ describe "Routing" do
|
|
213
266
|
}
|
214
267
|
|
215
268
|
get '/bar'
|
216
|
-
|
269
|
+
assert not_found?
|
217
270
|
end
|
218
271
|
|
219
272
|
it "passes when matching condition returns false" do
|
@@ -225,11 +278,11 @@ describe "Routing" do
|
|
225
278
|
}
|
226
279
|
|
227
280
|
get '/bar'
|
228
|
-
|
229
|
-
|
281
|
+
assert ok?
|
282
|
+
assert_equal 'Hello World', body
|
230
283
|
|
231
284
|
get '/foo'
|
232
|
-
|
285
|
+
assert not_found?
|
233
286
|
end
|
234
287
|
|
235
288
|
it "does not pass when matching condition returns nil" do
|
@@ -241,8 +294,8 @@ describe "Routing" do
|
|
241
294
|
}
|
242
295
|
|
243
296
|
get '/bar'
|
244
|
-
|
245
|
-
|
297
|
+
assert ok?
|
298
|
+
assert_equal 'Hello World', body
|
246
299
|
end
|
247
300
|
|
248
301
|
it "passes to next route when condition calls pass explicitly" do
|
@@ -254,11 +307,11 @@ describe "Routing" do
|
|
254
307
|
}
|
255
308
|
|
256
309
|
get '/bar'
|
257
|
-
|
258
|
-
|
310
|
+
assert ok?
|
311
|
+
assert_equal 'Hello World', body
|
259
312
|
|
260
313
|
get '/foo'
|
261
|
-
|
314
|
+
assert not_found?
|
262
315
|
end
|
263
316
|
|
264
317
|
it "passes to the next route when host_name does not match" do
|
@@ -269,37 +322,70 @@ describe "Routing" do
|
|
269
322
|
end
|
270
323
|
}
|
271
324
|
get '/foo'
|
272
|
-
|
325
|
+
assert not_found?
|
273
326
|
|
274
327
|
get '/foo', :env => { 'HTTP_HOST' => 'example.com' }
|
275
|
-
|
276
|
-
|
328
|
+
assert_equal 200, status
|
329
|
+
assert_equal 'Hello World', body
|
277
330
|
end
|
278
331
|
|
279
332
|
it "passes to the next route when user_agent does not match" do
|
280
333
|
mock_app {
|
281
|
-
user_agent
|
334
|
+
user_agent(/Foo/)
|
282
335
|
get '/foo' do
|
283
336
|
'Hello World'
|
284
337
|
end
|
285
338
|
}
|
286
339
|
get '/foo'
|
287
|
-
|
340
|
+
assert not_found?
|
288
341
|
|
289
342
|
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
290
|
-
|
291
|
-
|
343
|
+
assert_equal 200, status
|
344
|
+
assert_equal 'Hello World', body
|
292
345
|
end
|
293
346
|
|
294
347
|
it "makes captures in user agent pattern available in params[:agent]" do
|
295
348
|
mock_app {
|
296
|
-
user_agent
|
349
|
+
user_agent(/Foo (.*)/)
|
297
350
|
get '/foo' do
|
298
351
|
'Hello ' + params[:agent].first
|
299
352
|
end
|
300
353
|
}
|
301
354
|
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
302
|
-
|
303
|
-
|
355
|
+
assert_equal 200, status
|
356
|
+
assert_equal 'Hello Bar', body
|
357
|
+
end
|
358
|
+
|
359
|
+
it "filters by accept header" do
|
360
|
+
mock_app {
|
361
|
+
get '/', :provides => :xml do
|
362
|
+
request.env['HTTP_ACCEPT']
|
363
|
+
end
|
364
|
+
}
|
365
|
+
|
366
|
+
get '/', :env => { :accept => 'application/xml' }
|
367
|
+
assert ok?
|
368
|
+
assert_equal 'application/xml', body
|
369
|
+
assert_equal 'application/xml', response.headers['Content-Type']
|
370
|
+
|
371
|
+
get '/', :env => { :accept => 'text/html' }
|
372
|
+
assert !ok?
|
373
|
+
end
|
374
|
+
|
375
|
+
it "allows multiple mime types for accept header" do
|
376
|
+
types = ['image/jpeg', 'image/pjpeg']
|
377
|
+
|
378
|
+
mock_app {
|
379
|
+
get '/', :provides => types do
|
380
|
+
request.env['HTTP_ACCEPT']
|
381
|
+
end
|
382
|
+
}
|
383
|
+
|
384
|
+
types.each do |type|
|
385
|
+
get '/', :env => { :accept => type }
|
386
|
+
assert ok?
|
387
|
+
assert_equal type, body
|
388
|
+
assert_equal type, response.headers['Content-Type']
|
389
|
+
end
|
304
390
|
end
|
305
391
|
end
|