sinatra-sinatra 0.8.9
Sign up to get free protection for your applications and to get access to all the features.
- data/AUTHORS +40 -0
- data/CHANGES +174 -0
- data/LICENSE +22 -0
- data/README.rdoc +545 -0
- data/Rakefile +180 -0
- data/compat/app_test.rb +300 -0
- data/compat/application_test.rb +334 -0
- data/compat/builder_test.rb +101 -0
- data/compat/custom_error_test.rb +62 -0
- data/compat/erb_test.rb +136 -0
- data/compat/events_test.rb +78 -0
- data/compat/filter_test.rb +30 -0
- data/compat/haml_test.rb +233 -0
- data/compat/helper.rb +30 -0
- data/compat/mapped_error_test.rb +72 -0
- data/compat/pipeline_test.rb +71 -0
- data/compat/public/foo.xml +1 -0
- data/compat/sass_test.rb +57 -0
- data/compat/sessions_test.rb +39 -0
- data/compat/streaming_test.rb +121 -0
- data/compat/sym_params_test.rb +19 -0
- data/compat/template_test.rb +30 -0
- data/compat/use_in_file_templates_test.rb +47 -0
- data/compat/views/foo.builder +1 -0
- data/compat/views/foo.erb +1 -0
- data/compat/views/foo.haml +1 -0
- data/compat/views/foo.sass +2 -0
- data/compat/views/foo_layout.erb +2 -0
- data/compat/views/foo_layout.haml +2 -0
- data/compat/views/layout_test/foo.builder +1 -0
- data/compat/views/layout_test/foo.erb +1 -0
- data/compat/views/layout_test/foo.haml +1 -0
- data/compat/views/layout_test/foo.sass +2 -0
- data/compat/views/layout_test/layout.builder +3 -0
- data/compat/views/layout_test/layout.erb +1 -0
- data/compat/views/layout_test/layout.haml +1 -0
- data/compat/views/layout_test/layout.sass +2 -0
- data/compat/views/no_layout/no_layout.builder +1 -0
- data/compat/views/no_layout/no_layout.haml +1 -0
- data/lib/sinatra/base.rb +818 -0
- data/lib/sinatra/compat.rb +239 -0
- data/lib/sinatra/images/404.png +0 -0
- data/lib/sinatra/images/500.png +0 -0
- data/lib/sinatra/main.rb +48 -0
- data/lib/sinatra/test/bacon.rb +17 -0
- data/lib/sinatra/test/rspec.rb +9 -0
- data/lib/sinatra/test/spec.rb +9 -0
- data/lib/sinatra/test/unit.rb +11 -0
- data/lib/sinatra/test.rb +109 -0
- data/lib/sinatra.rb +4 -0
- data/sinatra.gemspec +109 -0
- data/test/base_test.rb +68 -0
- data/test/builder_test.rb +64 -0
- data/test/data/reload_app_file.rb +3 -0
- data/test/erb_test.rb +50 -0
- data/test/filter_test.rb +35 -0
- data/test/haml_test.rb +68 -0
- data/test/helper.rb +20 -0
- data/test/helpers_test.rb +361 -0
- data/test/mapped_error_test.rb +160 -0
- data/test/middleware_test.rb +58 -0
- data/test/options_test.rb +97 -0
- data/test/reload_test.rb +61 -0
- data/test/request_test.rb +9 -0
- data/test/result_test.rb +88 -0
- data/test/routing_test.rb +334 -0
- data/test/sass_test.rb +36 -0
- data/test/sinatra_test.rb +13 -0
- data/test/static_test.rb +57 -0
- data/test/templates_test.rb +88 -0
- data/test/views/hello.builder +1 -0
- data/test/views/hello.erb +1 -0
- data/test/views/hello.haml +1 -0
- data/test/views/hello.sass +2 -0
- data/test/views/hello.test +1 -0
- data/test/views/layout2.builder +3 -0
- data/test/views/layout2.erb +2 -0
- data/test/views/layout2.haml +2 -0
- data/test/views/layout2.test +1 -0
- metadata +161 -0
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe "Middleware" do
|
4
|
+
before do
|
5
|
+
@app = mock_app(Sinatra::Default) {
|
6
|
+
get '/*' do
|
7
|
+
response.headers['X-Tests'] = env['test.ran'].join(', ')
|
8
|
+
env['PATH_INFO']
|
9
|
+
end
|
10
|
+
}
|
11
|
+
end
|
12
|
+
|
13
|
+
class MockMiddleware < Struct.new(:app)
|
14
|
+
def call(env)
|
15
|
+
(env['test.ran'] ||= []) << self.class.to_s
|
16
|
+
app.call(env)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
class UpcaseMiddleware < MockMiddleware
|
21
|
+
def call(env)
|
22
|
+
env['PATH_INFO'] = env['PATH_INFO'].upcase
|
23
|
+
super
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "is added with Sinatra::Application.use" do
|
28
|
+
@app.use UpcaseMiddleware
|
29
|
+
get '/hello-world'
|
30
|
+
assert ok?
|
31
|
+
assert_equal '/HELLO-WORLD', body
|
32
|
+
end
|
33
|
+
|
34
|
+
class DowncaseMiddleware < MockMiddleware
|
35
|
+
def call(env)
|
36
|
+
env['PATH_INFO'] = env['PATH_INFO'].downcase
|
37
|
+
super
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
specify "runs in the order defined" do
|
42
|
+
@app.use UpcaseMiddleware
|
43
|
+
@app.use DowncaseMiddleware
|
44
|
+
get '/Foo'
|
45
|
+
assert_equal "/foo", body
|
46
|
+
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
47
|
+
end
|
48
|
+
|
49
|
+
specify "resets the prebuilt pipeline when new middleware is added" do
|
50
|
+
@app.use UpcaseMiddleware
|
51
|
+
get '/Foo'
|
52
|
+
assert_equal "/FOO", body
|
53
|
+
@app.use DowncaseMiddleware
|
54
|
+
get '/Foo'
|
55
|
+
assert_equal '/foo', body
|
56
|
+
assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
|
57
|
+
end
|
58
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe 'Options' do
|
4
|
+
before { @app = Class.new(Sinatra::Base) }
|
5
|
+
|
6
|
+
it 'sets options to literal values' do
|
7
|
+
@app.set(:foo, 'bar')
|
8
|
+
assert @app.respond_to?(:foo)
|
9
|
+
assert_equal 'bar', @app.foo
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'sets options to Procs' do
|
13
|
+
@app.set(:foo, Proc.new { 'baz' })
|
14
|
+
assert @app.respond_to?(:foo)
|
15
|
+
assert_equal 'baz', @app.foo
|
16
|
+
end
|
17
|
+
|
18
|
+
it "sets multiple options with a Hash" do
|
19
|
+
@app.set :foo => 1234,
|
20
|
+
:bar => 'Hello World',
|
21
|
+
:baz => Proc.new { 'bizzle' }
|
22
|
+
assert_equal 1234, @app.foo
|
23
|
+
assert_equal 'Hello World', @app.bar
|
24
|
+
assert_equal 'bizzle', @app.baz
|
25
|
+
end
|
26
|
+
|
27
|
+
it 'inherits option methods when subclassed' do
|
28
|
+
@app.set :foo, 'bar'
|
29
|
+
@app.set :biz, Proc.new { 'baz' }
|
30
|
+
|
31
|
+
sub = Class.new(@app)
|
32
|
+
assert sub.respond_to?(:foo)
|
33
|
+
assert_equal 'bar', sub.foo
|
34
|
+
assert sub.respond_to?(:biz)
|
35
|
+
assert_equal 'baz', sub.biz
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'overrides options in subclass' do
|
39
|
+
@app.set :foo, 'bar'
|
40
|
+
@app.set :biz, Proc.new { 'baz' }
|
41
|
+
sub = Class.new(@app)
|
42
|
+
sub.set :foo, 'bling'
|
43
|
+
assert_equal 'bling', sub.foo
|
44
|
+
assert_equal 'bar', @app.foo
|
45
|
+
end
|
46
|
+
|
47
|
+
it 'creates setter methods when first defined' do
|
48
|
+
@app.set :foo, 'bar'
|
49
|
+
assert @app.respond_to?('foo=')
|
50
|
+
@app.foo = 'biz'
|
51
|
+
assert_equal 'biz', @app.foo
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'creates predicate methods when first defined' do
|
55
|
+
@app.set :foo, 'hello world'
|
56
|
+
assert @app.respond_to?(:foo?)
|
57
|
+
assert @app.foo?
|
58
|
+
@app.set :foo, nil
|
59
|
+
assert !@app.foo?
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'uses existing setter methods if detected' do
|
63
|
+
class << @app
|
64
|
+
def foo
|
65
|
+
@foo
|
66
|
+
end
|
67
|
+
def foo=(value)
|
68
|
+
@foo = 'oops'
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
@app.set :foo, 'bam'
|
73
|
+
assert_equal 'oops', @app.foo
|
74
|
+
end
|
75
|
+
|
76
|
+
it "sets multiple options to true with #enable" do
|
77
|
+
@app.enable :sessions, :foo, :bar
|
78
|
+
assert @app.sessions
|
79
|
+
assert @app.foo
|
80
|
+
assert @app.bar
|
81
|
+
end
|
82
|
+
|
83
|
+
it "sets multiple options to false with #disable" do
|
84
|
+
@app.disable :sessions, :foo, :bar
|
85
|
+
assert !@app.sessions
|
86
|
+
assert !@app.foo
|
87
|
+
assert !@app.bar
|
88
|
+
end
|
89
|
+
|
90
|
+
it 'enables MethodOverride middleware when :methodoverride is enabled' do
|
91
|
+
@app.set :methodoverride, true
|
92
|
+
@app.put('/') { 'okay' }
|
93
|
+
post '/', {'_method'=>'PUT'}, {}
|
94
|
+
assert_equal 200, status
|
95
|
+
assert_equal 'okay', body
|
96
|
+
end
|
97
|
+
end
|
data/test/reload_test.rb
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
$reload_count = 0
|
4
|
+
$reload_app = nil
|
5
|
+
|
6
|
+
describe "Reloading" do
|
7
|
+
before {
|
8
|
+
@app = mock_app(Sinatra::Default)
|
9
|
+
$reload_app = @app
|
10
|
+
}
|
11
|
+
|
12
|
+
after {
|
13
|
+
$reload_app = nil
|
14
|
+
}
|
15
|
+
|
16
|
+
it 'is enabled by default when in development and the app_file is set' do
|
17
|
+
@app.set :app_file, __FILE__
|
18
|
+
@app.set :environment, :development
|
19
|
+
assert_same true, @app.reload
|
20
|
+
assert_same true, @app.reload?
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'is disabled by default when running in non-development environment' do
|
24
|
+
@app.set :app_file, __FILE__
|
25
|
+
@app.set :environment, :test
|
26
|
+
assert !@app.reload
|
27
|
+
assert_same false, @app.reload?
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'is disabled by default when no app_file is available' do
|
31
|
+
@app.set :app_file, nil
|
32
|
+
@app.set :environment, :development
|
33
|
+
assert !@app.reload
|
34
|
+
assert_same false, @app.reload?
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'can be turned off explicitly' do
|
38
|
+
@app.set :app_file, __FILE__
|
39
|
+
@app.set :environment, :development
|
40
|
+
assert_same true, @app.reload
|
41
|
+
@app.set :reload, false
|
42
|
+
assert_same false, @app.reload
|
43
|
+
assert_same false, @app.reload?
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'reloads the app_file each time a request is made' do
|
47
|
+
@app.set :app_file, File.dirname(__FILE__) + '/data/reload_app_file.rb'
|
48
|
+
@app.set :reload, true
|
49
|
+
@app.get('/') { 'Hello World' }
|
50
|
+
|
51
|
+
get '/'
|
52
|
+
assert_equal 200, status
|
53
|
+
assert_equal 'Hello from reload file', body
|
54
|
+
assert_equal 1, $reload_count
|
55
|
+
|
56
|
+
get '/'
|
57
|
+
assert_equal 200, status
|
58
|
+
assert_equal 'Hello from reload file', body
|
59
|
+
assert_equal 2, $reload_count
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe 'Sinatra::Request' do
|
4
|
+
it 'responds to #user_agent' do
|
5
|
+
request = Sinatra::Request.new({'HTTP_USER_AGENT' => 'Test'})
|
6
|
+
assert request.respond_to?(:user_agent)
|
7
|
+
assert_equal 'Test', request.user_agent
|
8
|
+
end
|
9
|
+
end
|
data/test/result_test.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe 'Result Handling' do
|
4
|
+
it "sets response.body when result is a String" do
|
5
|
+
mock_app {
|
6
|
+
get '/' do
|
7
|
+
'Hello World'
|
8
|
+
end
|
9
|
+
}
|
10
|
+
|
11
|
+
get '/'
|
12
|
+
assert ok?
|
13
|
+
assert_equal 'Hello World', body
|
14
|
+
end
|
15
|
+
|
16
|
+
it "sets response.body when result is an Array of Strings" do
|
17
|
+
mock_app {
|
18
|
+
get '/' do
|
19
|
+
['Hello', 'World']
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
get '/'
|
24
|
+
assert ok?
|
25
|
+
assert_equal 'HelloWorld', body
|
26
|
+
end
|
27
|
+
|
28
|
+
it "sets response.body when result responds to #each" do
|
29
|
+
mock_app {
|
30
|
+
get '/' do
|
31
|
+
res = lambda { 'Hello World' }
|
32
|
+
def res.each ; yield call ; end
|
33
|
+
res
|
34
|
+
end
|
35
|
+
}
|
36
|
+
|
37
|
+
get '/'
|
38
|
+
assert ok?
|
39
|
+
assert_equal 'Hello World', body
|
40
|
+
end
|
41
|
+
|
42
|
+
it "sets response.body to [] when result is nil" do
|
43
|
+
mock_app {
|
44
|
+
get '/' do
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
}
|
48
|
+
|
49
|
+
get '/'
|
50
|
+
assert ok?
|
51
|
+
assert_equal '', body
|
52
|
+
end
|
53
|
+
|
54
|
+
it "sets status, headers, and body when result is a Rack response tuple" do
|
55
|
+
mock_app {
|
56
|
+
get '/' do
|
57
|
+
[205, {'Content-Type' => 'foo/bar'}, 'Hello World']
|
58
|
+
end
|
59
|
+
}
|
60
|
+
|
61
|
+
get '/'
|
62
|
+
assert_equal 205, status
|
63
|
+
assert_equal 'foo/bar', response['Content-Type']
|
64
|
+
assert_equal 'Hello World', body
|
65
|
+
end
|
66
|
+
|
67
|
+
it "sets status and body when result is a two-tuple" do
|
68
|
+
mock_app {
|
69
|
+
get '/' do
|
70
|
+
[409, 'formula of']
|
71
|
+
end
|
72
|
+
}
|
73
|
+
|
74
|
+
get '/'
|
75
|
+
assert_equal 409, status
|
76
|
+
assert_equal 'formula of', body
|
77
|
+
end
|
78
|
+
|
79
|
+
it "sets status when result is a Fixnum status code" do
|
80
|
+
mock_app {
|
81
|
+
get('/') { 205 }
|
82
|
+
}
|
83
|
+
|
84
|
+
get '/'
|
85
|
+
assert_equal 205, status
|
86
|
+
assert_equal '', body
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,334 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe "Routing" do
|
4
|
+
%w[get put post delete head].each do |verb|
|
5
|
+
it "defines #{verb.upcase} request handlers with #{verb}" do
|
6
|
+
mock_app {
|
7
|
+
send verb, '/hello' do
|
8
|
+
'Hello World'
|
9
|
+
end
|
10
|
+
}
|
11
|
+
|
12
|
+
request = Rack::MockRequest.new(@app)
|
13
|
+
response = request.request(verb.upcase, '/hello', {})
|
14
|
+
assert response.ok?
|
15
|
+
assert_equal 'Hello World', response.body
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "404s when no route satisfies the request" do
|
20
|
+
mock_app {
|
21
|
+
get('/foo') { }
|
22
|
+
}
|
23
|
+
get '/bar'
|
24
|
+
assert_equal 404, status
|
25
|
+
end
|
26
|
+
|
27
|
+
it "exposes params with indifferent hash" do
|
28
|
+
mock_app {
|
29
|
+
get '/:foo' do
|
30
|
+
fail unless params['foo'] == 'bar'
|
31
|
+
fail unless params[:foo] == 'bar'
|
32
|
+
'well, alright'
|
33
|
+
end
|
34
|
+
}
|
35
|
+
get '/bar'
|
36
|
+
assert_equal 'well, alright', body
|
37
|
+
end
|
38
|
+
|
39
|
+
it "merges named params and query string params in params" do
|
40
|
+
mock_app {
|
41
|
+
get '/:foo' do
|
42
|
+
fail unless params['foo'] == 'bar'
|
43
|
+
fail unless params['baz'] == 'biz'
|
44
|
+
end
|
45
|
+
}
|
46
|
+
get '/bar?baz=biz'
|
47
|
+
assert ok?
|
48
|
+
end
|
49
|
+
|
50
|
+
it "supports named params like /hello/:person" do
|
51
|
+
mock_app {
|
52
|
+
get '/hello/:person' do
|
53
|
+
"Hello #{params['person']}"
|
54
|
+
end
|
55
|
+
}
|
56
|
+
get '/hello/Frank'
|
57
|
+
assert_equal 'Hello Frank', body
|
58
|
+
end
|
59
|
+
|
60
|
+
it "supports optional named params like /?:foo?/?:bar?" do
|
61
|
+
mock_app {
|
62
|
+
get '/?:foo?/?:bar?' do
|
63
|
+
"foo=#{params[:foo]};bar=#{params[:bar]}"
|
64
|
+
end
|
65
|
+
}
|
66
|
+
|
67
|
+
get '/hello/world'
|
68
|
+
assert ok?
|
69
|
+
assert_equal "foo=hello;bar=world", body
|
70
|
+
|
71
|
+
get '/hello'
|
72
|
+
assert ok?
|
73
|
+
assert_equal "foo=hello;bar=", body
|
74
|
+
|
75
|
+
get '/'
|
76
|
+
assert ok?
|
77
|
+
assert_equal "foo=;bar=", body
|
78
|
+
end
|
79
|
+
|
80
|
+
it "supports single splat params like /*" do
|
81
|
+
mock_app {
|
82
|
+
get '/*' do
|
83
|
+
fail unless params['splat'].kind_of?(Array)
|
84
|
+
params['splat'].join "\n"
|
85
|
+
end
|
86
|
+
}
|
87
|
+
|
88
|
+
get '/foo'
|
89
|
+
assert_equal "foo", body
|
90
|
+
|
91
|
+
get '/foo/bar/baz'
|
92
|
+
assert_equal "foo/bar/baz", body
|
93
|
+
end
|
94
|
+
|
95
|
+
it "supports mixing multiple splat params like /*/foo/*/*" do
|
96
|
+
mock_app {
|
97
|
+
get '/*/foo/*/*' do
|
98
|
+
fail unless params['splat'].kind_of?(Array)
|
99
|
+
params['splat'].join "\n"
|
100
|
+
end
|
101
|
+
}
|
102
|
+
|
103
|
+
get '/bar/foo/bling/baz/boom'
|
104
|
+
assert_equal "bar\nbling\nbaz/boom", body
|
105
|
+
|
106
|
+
get '/bar/foo/baz'
|
107
|
+
assert not_found?
|
108
|
+
end
|
109
|
+
|
110
|
+
it "supports mixing named and splat params like /:foo/*" do
|
111
|
+
mock_app {
|
112
|
+
get '/:foo/*' do
|
113
|
+
fail unless params['foo'] == 'foo'
|
114
|
+
fail unless params['splat'] == ['bar/baz']
|
115
|
+
end
|
116
|
+
}
|
117
|
+
|
118
|
+
get '/foo/bar/baz'
|
119
|
+
assert ok?
|
120
|
+
end
|
121
|
+
|
122
|
+
it "supports paths that include spaces" do
|
123
|
+
mock_app {
|
124
|
+
get '/path with spaces' do
|
125
|
+
'looks good'
|
126
|
+
end
|
127
|
+
}
|
128
|
+
|
129
|
+
get '/path%20with%20spaces'
|
130
|
+
assert ok?
|
131
|
+
assert_equal 'looks good', body
|
132
|
+
end
|
133
|
+
|
134
|
+
it "URL decodes named parameters and splats" do
|
135
|
+
mock_app {
|
136
|
+
get '/:foo/*' do
|
137
|
+
fail unless params['foo'] == 'hello world'
|
138
|
+
fail unless params['splat'] == ['how are you']
|
139
|
+
nil
|
140
|
+
end
|
141
|
+
}
|
142
|
+
|
143
|
+
get '/hello%20world/how%20are%20you'
|
144
|
+
assert ok?
|
145
|
+
end
|
146
|
+
|
147
|
+
it 'supports regular expressions' do
|
148
|
+
mock_app {
|
149
|
+
get(/^\/foo...\/bar$/) do
|
150
|
+
'Hello World'
|
151
|
+
end
|
152
|
+
}
|
153
|
+
|
154
|
+
get '/foooom/bar'
|
155
|
+
assert ok?
|
156
|
+
assert_equal 'Hello World', body
|
157
|
+
end
|
158
|
+
|
159
|
+
it 'makes regular expression captures available in params[:captures]' do
|
160
|
+
mock_app {
|
161
|
+
get(/^\/fo(.*)\/ba(.*)/) do
|
162
|
+
fail unless params[:captures] == ['orooomma', 'f']
|
163
|
+
'right on'
|
164
|
+
end
|
165
|
+
}
|
166
|
+
|
167
|
+
get '/foorooomma/baf'
|
168
|
+
assert ok?
|
169
|
+
assert_equal 'right on', body
|
170
|
+
end
|
171
|
+
|
172
|
+
it "returns response immediately on halt" do
|
173
|
+
mock_app {
|
174
|
+
get '/' do
|
175
|
+
halt 'Hello World'
|
176
|
+
'Boo-hoo World'
|
177
|
+
end
|
178
|
+
}
|
179
|
+
|
180
|
+
get '/'
|
181
|
+
assert ok?
|
182
|
+
assert_equal 'Hello World', body
|
183
|
+
end
|
184
|
+
|
185
|
+
it "transitions to the next matching route on pass" do
|
186
|
+
mock_app {
|
187
|
+
get '/:foo' do
|
188
|
+
pass
|
189
|
+
'Hello Foo'
|
190
|
+
end
|
191
|
+
|
192
|
+
get '/*' do
|
193
|
+
fail if params.include?('foo')
|
194
|
+
'Hello World'
|
195
|
+
end
|
196
|
+
}
|
197
|
+
|
198
|
+
get '/bar'
|
199
|
+
assert ok?
|
200
|
+
assert_equal 'Hello World', body
|
201
|
+
end
|
202
|
+
|
203
|
+
it "transitions to 404 when passed and no subsequent route matches" do
|
204
|
+
mock_app {
|
205
|
+
get '/:foo' do
|
206
|
+
pass
|
207
|
+
'Hello Foo'
|
208
|
+
end
|
209
|
+
}
|
210
|
+
|
211
|
+
get '/bar'
|
212
|
+
assert not_found?
|
213
|
+
end
|
214
|
+
|
215
|
+
it "passes when matching condition returns false" do
|
216
|
+
mock_app {
|
217
|
+
condition { params[:foo] == 'bar' }
|
218
|
+
get '/:foo' do
|
219
|
+
'Hello World'
|
220
|
+
end
|
221
|
+
}
|
222
|
+
|
223
|
+
get '/bar'
|
224
|
+
assert ok?
|
225
|
+
assert_equal 'Hello World', body
|
226
|
+
|
227
|
+
get '/foo'
|
228
|
+
assert not_found?
|
229
|
+
end
|
230
|
+
|
231
|
+
it "does not pass when matching condition returns nil" do
|
232
|
+
mock_app {
|
233
|
+
condition { nil }
|
234
|
+
get '/:foo' do
|
235
|
+
'Hello World'
|
236
|
+
end
|
237
|
+
}
|
238
|
+
|
239
|
+
get '/bar'
|
240
|
+
assert ok?
|
241
|
+
assert_equal 'Hello World', body
|
242
|
+
end
|
243
|
+
|
244
|
+
it "passes to next route when condition calls pass explicitly" do
|
245
|
+
mock_app {
|
246
|
+
condition { pass unless params[:foo] == 'bar' }
|
247
|
+
get '/:foo' do
|
248
|
+
'Hello World'
|
249
|
+
end
|
250
|
+
}
|
251
|
+
|
252
|
+
get '/bar'
|
253
|
+
assert ok?
|
254
|
+
assert_equal 'Hello World', body
|
255
|
+
|
256
|
+
get '/foo'
|
257
|
+
assert not_found?
|
258
|
+
end
|
259
|
+
|
260
|
+
it "passes to the next route when host_name does not match" do
|
261
|
+
mock_app {
|
262
|
+
host_name 'example.com'
|
263
|
+
get '/foo' do
|
264
|
+
'Hello World'
|
265
|
+
end
|
266
|
+
}
|
267
|
+
get '/foo'
|
268
|
+
assert not_found?
|
269
|
+
|
270
|
+
get '/foo', :env => { 'HTTP_HOST' => 'example.com' }
|
271
|
+
assert_equal 200, status
|
272
|
+
assert_equal 'Hello World', body
|
273
|
+
end
|
274
|
+
|
275
|
+
it "passes to the next route when user_agent does not match" do
|
276
|
+
mock_app {
|
277
|
+
user_agent(/Foo/)
|
278
|
+
get '/foo' do
|
279
|
+
'Hello World'
|
280
|
+
end
|
281
|
+
}
|
282
|
+
get '/foo'
|
283
|
+
assert not_found?
|
284
|
+
|
285
|
+
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
286
|
+
assert_equal 200, status
|
287
|
+
assert_equal 'Hello World', body
|
288
|
+
end
|
289
|
+
|
290
|
+
it "makes captures in user agent pattern available in params[:agent]" do
|
291
|
+
mock_app {
|
292
|
+
user_agent(/Foo (.*)/)
|
293
|
+
get '/foo' do
|
294
|
+
'Hello ' + params[:agent].first
|
295
|
+
end
|
296
|
+
}
|
297
|
+
get '/foo', :env => { 'HTTP_USER_AGENT' => 'Foo Bar' }
|
298
|
+
assert_equal 200, status
|
299
|
+
assert_equal 'Hello Bar', body
|
300
|
+
end
|
301
|
+
|
302
|
+
it "filters by accept header" do
|
303
|
+
mock_app {
|
304
|
+
get '/', :provides => :xml do
|
305
|
+
request.env['HTTP_ACCEPT']
|
306
|
+
end
|
307
|
+
}
|
308
|
+
|
309
|
+
get '/', :env => { :accept => 'application/xml' }
|
310
|
+
assert ok?
|
311
|
+
assert_equal 'application/xml', body
|
312
|
+
assert_equal 'application/xml', response.headers['Content-Type']
|
313
|
+
|
314
|
+
get '/', :env => { :accept => 'text/html' }
|
315
|
+
assert !ok?
|
316
|
+
end
|
317
|
+
|
318
|
+
it "allows multiple mime types for accept header" do
|
319
|
+
types = ['image/jpeg', 'image/pjpeg']
|
320
|
+
|
321
|
+
mock_app {
|
322
|
+
get '/', :provides => types do
|
323
|
+
request.env['HTTP_ACCEPT']
|
324
|
+
end
|
325
|
+
}
|
326
|
+
|
327
|
+
types.each do |type|
|
328
|
+
get '/', :env => { :accept => type }
|
329
|
+
assert ok?
|
330
|
+
assert_equal type, body
|
331
|
+
assert_equal type, response.headers['Content-Type']
|
332
|
+
end
|
333
|
+
end
|
334
|
+
end
|
data/test/sass_test.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/helper'
|
2
|
+
|
3
|
+
describe "Sass Templates" do
|
4
|
+
def sass_app(&block)
|
5
|
+
mock_app {
|
6
|
+
set :views, File.dirname(__FILE__) + '/views'
|
7
|
+
get '/', &block
|
8
|
+
}
|
9
|
+
get '/'
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'renders inline Sass strings' do
|
13
|
+
sass_app { sass "#sass\n :background-color #FFF\n" }
|
14
|
+
assert ok?
|
15
|
+
assert_equal "#sass {\n background-color: #FFF; }\n", body
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'renders .sass files in views path' do
|
19
|
+
sass_app { sass :hello }
|
20
|
+
assert ok?
|
21
|
+
assert_equal "#sass {\n background-color: #FFF; }\n", body
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'ignores the layout option' do
|
25
|
+
sass_app { sass :hello, :layout => :layout2 }
|
26
|
+
assert ok?
|
27
|
+
assert_equal "#sass {\n background-color: #FFF; }\n", body
|
28
|
+
end
|
29
|
+
|
30
|
+
it "raises error if template not found" do
|
31
|
+
mock_app {
|
32
|
+
get('/') { sass :no_such_template }
|
33
|
+
}
|
34
|
+
assert_raise(Errno::ENOENT) { get('/') }
|
35
|
+
end
|
36
|
+
end
|