sinatra-contrib 1.4.7 → 2.0.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -16
- data/Rakefile +0 -1
- data/lib/sinatra/config_file.rb +8 -2
- data/lib/sinatra/content_for.rb +63 -1
- data/lib/sinatra/contrib.rb +1 -1
- data/lib/sinatra/contrib/version.rb +1 -12
- data/lib/sinatra/cookies.rb +1 -1
- data/lib/sinatra/link_header.rb +2 -2
- data/lib/sinatra/namespace.rb +90 -18
- data/lib/sinatra/reloader.rb +15 -1
- data/lib/sinatra/required_params.rb +71 -0
- data/lib/sinatra/respond_with.rb +7 -5
- data/sinatra-contrib.gemspec +14 -10
- data/spec/capture_spec.rb +5 -5
- data/spec/config_file/key_value.yml +1 -0
- data/spec/config_file_spec.rb +27 -13
- data/spec/content_for/footer.haml +1 -1
- data/spec/content_for/footer.slim +1 -1
- data/spec/content_for_spec.rb +65 -36
- data/spec/cookies_spec.rb +167 -173
- data/spec/extension_spec.rb +4 -4
- data/spec/json_spec.rb +11 -11
- data/spec/link_header_spec.rb +13 -13
- data/spec/multi_route_spec.rb +13 -13
- data/spec/namespace_spec.rb +190 -140
- data/spec/reloader_spec.rb +56 -31
- data/spec/required_params_spec.rb +68 -0
- data/spec/respond_with_spec.rb +58 -56
- data/spec/streaming_spec.rb +57 -57
- metadata +37 -22
- data/lib/sinatra/decompile.rb +0 -127
- data/spec/decompile_spec.rb +0 -43
data/spec/extension_spec.rb
CHANGED
@@ -22,11 +22,11 @@ describe Sinatra::Extension do
|
|
22
22
|
|
23
23
|
before { mock_app { register ExampleExtension }}
|
24
24
|
|
25
|
-
it('allows using set') { settings.foo.
|
26
|
-
it('implements configure') { settings.reload_stuff.
|
25
|
+
it('allows using set') { expect(settings.foo).to eq(:bar) }
|
26
|
+
it('implements configure') { expect(settings.reload_stuff).to be false }
|
27
27
|
|
28
28
|
it 'allows defing routes' do
|
29
|
-
get('/').
|
30
|
-
body.
|
29
|
+
expect(get('/')).to be_ok
|
30
|
+
expect(body).to eq("from extension, yay")
|
31
31
|
end
|
32
32
|
end
|
data/spec/json_spec.rb
CHANGED
@@ -9,7 +9,7 @@ shared_examples_for "a json encoder" do |lib, const|
|
|
9
9
|
require lib if lib
|
10
10
|
@encoder = eval(const)
|
11
11
|
rescue LoadError
|
12
|
-
|
12
|
+
skip "unable to load #{lib}"
|
13
13
|
end
|
14
14
|
end
|
15
15
|
|
@@ -37,7 +37,7 @@ describe Sinatra::JSON do
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def results_in(obj)
|
40
|
-
OkJson.decode(get('/').body).
|
40
|
+
expect(OkJson.decode(get('/').body)).to eq(obj)
|
41
41
|
end
|
42
42
|
|
43
43
|
it "encodes objects to json out of the box" do
|
@@ -47,36 +47,36 @@ describe Sinatra::JSON do
|
|
47
47
|
|
48
48
|
it "sets the content type to 'application/json'" do
|
49
49
|
mock_app { get('/') { json({}) } }
|
50
|
-
get('/')["Content-Type"].
|
50
|
+
expect(get('/')["Content-Type"]).to include("application/json")
|
51
51
|
end
|
52
52
|
|
53
53
|
it "allows overriding content type with :content_type" do
|
54
54
|
mock_app { get('/') { json({}, :content_type => "foo/bar") } }
|
55
|
-
get('/')["Content-Type"].
|
55
|
+
expect(get('/')["Content-Type"]).to eq("foo/bar")
|
56
56
|
end
|
57
57
|
|
58
58
|
it "accepts shorthands for :content_type" do
|
59
59
|
mock_app { get('/') { json({}, :content_type => :js) } }
|
60
|
-
get('/')["Content-Type"].
|
60
|
+
expect(get('/')["Content-Type"]).to eq("application/javascript;charset=utf-8")
|
61
61
|
end
|
62
62
|
|
63
63
|
it 'calls generate on :encoder if available' do
|
64
64
|
enc = Object.new
|
65
65
|
def enc.generate(obj) obj.inspect end
|
66
66
|
mock_app { get('/') { json(42, :encoder => enc) }}
|
67
|
-
get('/').body.
|
67
|
+
expect(get('/').body).to eq('42')
|
68
68
|
end
|
69
69
|
|
70
70
|
it 'calls encode on :encoder if available' do
|
71
71
|
enc = Object.new
|
72
72
|
def enc.encode(obj) obj.inspect end
|
73
73
|
mock_app { get('/') { json(42, :encoder => enc) }}
|
74
|
-
get('/').body.
|
74
|
+
expect(get('/').body).to eq('42')
|
75
75
|
end
|
76
76
|
|
77
77
|
it 'sends :encoder as method call if it is a Symbol' do
|
78
78
|
mock_app { get('/') { json(42, :encoder => :inspect) }}
|
79
|
-
get('/').body.
|
79
|
+
expect(get('/').body).to eq('42')
|
80
80
|
end
|
81
81
|
|
82
82
|
it 'calls generate on settings.json_encoder if available' do
|
@@ -86,7 +86,7 @@ describe Sinatra::JSON do
|
|
86
86
|
set :json_encoder, enc
|
87
87
|
get('/') { json 42 }
|
88
88
|
end
|
89
|
-
get('/').body.
|
89
|
+
expect(get('/').body).to eq('42')
|
90
90
|
end
|
91
91
|
|
92
92
|
it 'calls encode on settings.json_encode if available' do
|
@@ -96,7 +96,7 @@ describe Sinatra::JSON do
|
|
96
96
|
set :json_encoder, enc
|
97
97
|
get('/') { json 42 }
|
98
98
|
end
|
99
|
-
get('/').body.
|
99
|
+
expect(get('/').body).to eq('42')
|
100
100
|
end
|
101
101
|
|
102
102
|
it 'sends settings.json_encode as method call if it is a Symbol' do
|
@@ -104,7 +104,7 @@ describe Sinatra::JSON do
|
|
104
104
|
set :json_encoder, :inspect
|
105
105
|
get('/') { json 42 }
|
106
106
|
end
|
107
|
-
get('/').body.
|
107
|
+
expect(get('/').body).to eq('42')
|
108
108
|
end
|
109
109
|
|
110
110
|
describe('Yajl') { it_should_behave_like "a json encoder", "yajl", "Yajl::Encoder" } unless defined? JRUBY_VERSION
|
data/spec/link_header_spec.rb
CHANGED
@@ -30,70 +30,70 @@ describe Sinatra::LinkHeader do
|
|
30
30
|
describe :link do
|
31
31
|
it "sets link headers" do
|
32
32
|
get '/'
|
33
|
-
headers['Link'].lines.
|
33
|
+
expect(headers['Link'].lines).to include('<booyah>; rel="something"')
|
34
34
|
end
|
35
35
|
|
36
36
|
it "returns link html tags" do
|
37
37
|
get '/'
|
38
|
-
body.
|
38
|
+
expect(body).to eq('<link href="booyah" rel="something" />')
|
39
39
|
end
|
40
40
|
|
41
41
|
it "takes an options hash" do
|
42
42
|
get '/'
|
43
43
|
elements = ["<something>", "foo=\"bar\"", "rel=\"from-filter\""]
|
44
|
-
headers['Link'].
|
44
|
+
expect(headers['Link'].split(",\n").first.strip.split('; ').sort).to eq(elements)
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
48
48
|
describe :stylesheet do
|
49
49
|
it 'sets link headers' do
|
50
50
|
get '/style'
|
51
|
-
headers['Link'].
|
51
|
+
expect(headers['Link']).to match(%r{^</style\.css>;})
|
52
52
|
end
|
53
53
|
|
54
54
|
it 'sets type to text/css' do
|
55
55
|
get '/style'
|
56
|
-
headers['Link'].
|
56
|
+
expect(headers['Link']).to include('type="text/css"')
|
57
57
|
end
|
58
58
|
|
59
59
|
it 'sets rel to stylesheet' do
|
60
60
|
get '/style'
|
61
|
-
headers['Link'].
|
61
|
+
expect(headers['Link']).to include('rel="stylesheet"')
|
62
62
|
end
|
63
63
|
|
64
64
|
it 'returns html tag' do
|
65
65
|
get '/style'
|
66
|
-
body.
|
66
|
+
expect(body).to match(%r{^<link href="/style\.css"})
|
67
67
|
end
|
68
68
|
end
|
69
69
|
|
70
70
|
describe :prefetch do
|
71
71
|
it 'sets link headers' do
|
72
72
|
get '/prefetch'
|
73
|
-
headers['Link'].
|
73
|
+
expect(headers['Link']).to match(%r{^</foo>;})
|
74
74
|
end
|
75
75
|
|
76
76
|
it 'sets rel to prefetch' do
|
77
77
|
get '/prefetch'
|
78
|
-
headers['Link'].
|
78
|
+
expect(headers['Link']).to include('rel="prefetch"')
|
79
79
|
end
|
80
80
|
|
81
81
|
it 'returns html tag' do
|
82
82
|
get '/prefetch'
|
83
|
-
body.
|
83
|
+
expect(body).to eq('<link href="/foo" rel="prefetch" />')
|
84
84
|
end
|
85
85
|
end
|
86
86
|
|
87
87
|
describe :link_headers do
|
88
88
|
it 'generates html for all link headers' do
|
89
89
|
get '/link_headers'
|
90
|
-
body.
|
91
|
-
body.
|
90
|
+
expect(body).to include('<link href="/foo" rel="prefetch" />')
|
91
|
+
expect(body).to include('<link href="/style.css" ')
|
92
92
|
end
|
93
93
|
|
94
94
|
it "respects Link headers not generated on its own" do
|
95
95
|
get '/link_headers'
|
96
|
-
body.
|
96
|
+
expect(body).to include('<link href="foo" bar="baz" />')
|
97
97
|
end
|
98
98
|
end
|
99
99
|
end
|
data/spec/multi_route_spec.rb
CHANGED
@@ -8,8 +8,8 @@ describe Sinatra::MultiRoute do
|
|
8
8
|
get('/') { 'normal' }
|
9
9
|
end
|
10
10
|
|
11
|
-
get('/').
|
12
|
-
body.
|
11
|
+
expect(get('/')).to be_ok
|
12
|
+
expect(body).to eq('normal')
|
13
13
|
end
|
14
14
|
|
15
15
|
it 'supports multiple routes' do
|
@@ -18,10 +18,10 @@ describe Sinatra::MultiRoute do
|
|
18
18
|
get('/foo', '/bar') { 'paths' }
|
19
19
|
end
|
20
20
|
|
21
|
-
get('/foo').
|
22
|
-
body.
|
23
|
-
get('/bar').
|
24
|
-
body.
|
21
|
+
expect(get('/foo')).to be_ok
|
22
|
+
expect(body).to eq('paths')
|
23
|
+
expect(get('/bar')).to be_ok
|
24
|
+
expect(body).to eq('paths')
|
25
25
|
end
|
26
26
|
|
27
27
|
it 'triggers conditions' do
|
@@ -32,7 +32,7 @@ describe Sinatra::MultiRoute do
|
|
32
32
|
get('/foo', '/bar', :some_condition => true) { 'paths' }
|
33
33
|
end
|
34
34
|
|
35
|
-
count.
|
35
|
+
expect(count).to eq(4)
|
36
36
|
end
|
37
37
|
|
38
38
|
it 'supports multiple verbs' do
|
@@ -41,10 +41,10 @@ describe Sinatra::MultiRoute do
|
|
41
41
|
route('PUT', 'POST', '/') { 'verb' }
|
42
42
|
end
|
43
43
|
|
44
|
-
post('/').
|
45
|
-
body.
|
46
|
-
put('/').
|
47
|
-
body.
|
44
|
+
expect(post('/')).to be_ok
|
45
|
+
expect(body).to eq('verb')
|
46
|
+
expect(put('/')).to be_ok
|
47
|
+
expect(body).to eq('verb')
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'takes symbols as verbs' do
|
@@ -53,7 +53,7 @@ describe Sinatra::MultiRoute do
|
|
53
53
|
route(:get, '/baz') { 'symbol as verb' }
|
54
54
|
end
|
55
55
|
|
56
|
-
get('/baz').
|
57
|
-
body.
|
56
|
+
expect(get('/baz')).to be_ok
|
57
|
+
expect(body).to eq('symbol as verb')
|
58
58
|
end
|
59
59
|
end
|
data/spec/namespace_spec.rb
CHANGED
@@ -1,8 +1,7 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Sinatra::Namespace do
|
4
|
-
verbs = [:get, :head, :post, :put, :delete, :options]
|
5
|
-
verbs << :patch if Sinatra::VERSION >= '1.3'
|
4
|
+
verbs = [:get, :head, :post, :put, :delete, :options, :patch]
|
6
5
|
|
7
6
|
def mock_app(&block)
|
8
7
|
super do
|
@@ -20,96 +19,147 @@ describe Sinatra::Namespace do
|
|
20
19
|
|
21
20
|
it 'prefixes the path with the namespace' do
|
22
21
|
namespace('/foo') { send(verb, '/bar') { 'baz' }}
|
23
|
-
send(verb, '/foo/bar').
|
24
|
-
body.
|
25
|
-
send(verb, '/foo/baz').
|
22
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
23
|
+
expect(body).to eq('baz') unless verb == :head
|
24
|
+
expect(send(verb, '/foo/baz')).not_to be_ok
|
25
|
+
end
|
26
|
+
|
27
|
+
describe 'redirect_to' do
|
28
|
+
it 'redirect within namespace' do
|
29
|
+
namespace('/foo') { send(verb, '/bar') { redirect_to '/foo_bar' }}
|
30
|
+
expect(send(verb, '/foo/bar')).to be_redirect
|
31
|
+
expect(send(verb, '/foo/bar').location).to include("/foo/foo_bar")
|
32
|
+
end
|
26
33
|
end
|
27
34
|
|
28
35
|
context 'when namespace is a string' do
|
29
36
|
it 'accepts routes with no path' do
|
30
37
|
namespace('/foo') { send(verb) { 'bar' } }
|
31
|
-
send(verb, '/foo').
|
32
|
-
body.
|
38
|
+
expect(send(verb, '/foo')).to be_ok
|
39
|
+
expect(body).to eq('bar') unless verb == :head
|
33
40
|
end
|
34
41
|
|
35
42
|
it 'accepts the path as a named parameter' do
|
36
43
|
namespace('/foo') { send(verb, '/:bar') { params[:bar] }}
|
37
|
-
send(verb, '/foo/bar').
|
38
|
-
body.
|
39
|
-
send(verb, '/foo/baz').
|
40
|
-
body.
|
44
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
45
|
+
expect(body).to eq('bar') unless verb == :head
|
46
|
+
expect(send(verb, '/foo/baz')).to be_ok
|
47
|
+
expect(body).to eq('baz') unless verb == :head
|
41
48
|
end
|
42
49
|
|
43
50
|
it 'accepts the path as a regular expression' do
|
44
51
|
namespace('/foo') { send(verb, /\/\d\d/) { 'bar' }}
|
45
|
-
send(verb, '/foo/12').
|
46
|
-
body.
|
47
|
-
send(verb, '/foo/123').
|
52
|
+
expect(send(verb, '/foo/12')).to be_ok
|
53
|
+
expect(body).to eq 'bar' unless verb == :head
|
54
|
+
expect(send(verb, '/foo/123')).not_to be_ok
|
48
55
|
end
|
49
56
|
end
|
50
57
|
|
51
58
|
context 'when namespace is a named parameter' do
|
52
59
|
it 'accepts routes with no path' do
|
53
60
|
namespace('/:foo') { send(verb) { 'bar' } }
|
54
|
-
send(verb, '/foo').
|
55
|
-
body.
|
61
|
+
expect(send(verb, '/foo')).to be_ok
|
62
|
+
expect(body).to eq('bar') unless verb == :head
|
56
63
|
end
|
57
64
|
|
58
65
|
it 'sets the parameter correctly' do
|
59
66
|
namespace('/:foo') { send(verb, '/bar') { params[:foo] }}
|
60
|
-
send(verb, '/foo/bar').
|
61
|
-
body.
|
62
|
-
send(verb, '/fox/bar').
|
63
|
-
body.
|
64
|
-
send(verb, '/foo/baz').
|
67
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
68
|
+
expect(body).to eq('foo') unless verb == :head
|
69
|
+
expect(send(verb, '/fox/bar')).to be_ok
|
70
|
+
expect(body).to eq('fox') unless verb == :head
|
71
|
+
expect(send(verb, '/foo/baz')).not_to be_ok
|
65
72
|
end
|
66
73
|
|
67
74
|
it 'accepts the path as a named parameter' do
|
68
75
|
namespace('/:foo') { send(verb, '/:bar') { params[:bar] }}
|
69
|
-
send(verb, '/foo/bar').
|
70
|
-
body.
|
71
|
-
send(verb, '/foo/baz').
|
72
|
-
body.
|
76
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
77
|
+
expect(body).to eq('bar') unless verb == :head
|
78
|
+
expect(send(verb, '/foo/baz')).to be_ok
|
79
|
+
expect(body).to eq('baz') unless verb == :head
|
73
80
|
end
|
74
81
|
|
75
82
|
it 'accepts the path as regular expression' do
|
76
83
|
namespace('/:foo') { send(verb, %r{/bar}) { params[:foo] }}
|
77
|
-
send(verb, '/foo/bar').
|
78
|
-
body.
|
79
|
-
send(verb, '/fox/bar').
|
80
|
-
body.
|
81
|
-
send(verb, '/foo/baz').
|
84
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
85
|
+
expect(body).to eq('foo') unless verb == :head
|
86
|
+
expect(send(verb, '/fox/bar')).to be_ok
|
87
|
+
expect(body).to eq('fox') unless verb == :head
|
88
|
+
expect(send(verb, '/foo/baz')).not_to be_ok
|
82
89
|
end
|
83
90
|
end
|
84
91
|
|
85
92
|
context 'when namespace is a regular expression' do
|
86
93
|
it 'accepts routes with no path' do
|
87
94
|
namespace(%r{/foo}) { send(verb) { 'bar' } }
|
88
|
-
send(verb, '/foo').
|
89
|
-
body.
|
95
|
+
expect(send(verb, '/foo')).to be_ok
|
96
|
+
expect(body).to eq('bar') unless verb == :head
|
90
97
|
end
|
91
98
|
|
92
99
|
it 'accepts the path as a named parameter' do
|
93
100
|
namespace(%r{/foo}) { send(verb, '/:bar') { params[:bar] }}
|
94
|
-
send(verb, '/foo/bar').
|
95
|
-
body.
|
96
|
-
send(verb, '/foo/baz').
|
97
|
-
body.
|
101
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
102
|
+
expect(body).to eq('bar') unless verb == :head
|
103
|
+
expect(send(verb, '/foo/baz')).to be_ok
|
104
|
+
expect(body).to eq('baz') unless verb == :head
|
98
105
|
end
|
99
106
|
|
100
107
|
it 'accepts the path as a regular expression' do
|
101
108
|
namespace(/\/\d\d/) { send(verb, /\/\d\d/) { 'foo' }}
|
102
|
-
send(verb, '/23/12').
|
103
|
-
body.
|
104
|
-
send(verb, '/123/12').
|
109
|
+
expect(send(verb, '/23/12')).to be_ok
|
110
|
+
expect(body).to eq('foo') unless verb == :head
|
111
|
+
expect(send(verb, '/123/12')).not_to be_ok
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "before/after filters" do
|
115
|
+
it 'trigger before filter' do
|
116
|
+
ran = false
|
117
|
+
namespace(/\/foo\/([^\/&?]+)\/bar\/([^\/&?]+)\//) { before { ran = true };}
|
118
|
+
|
119
|
+
send(verb, '/bar/')
|
120
|
+
expect(ran).to eq(false)
|
121
|
+
|
122
|
+
send(verb, '/foo/1/bar/1/')
|
123
|
+
expect(ran).to eq(true)
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'trigger after filter' do
|
127
|
+
ran = false
|
128
|
+
namespace(/\/foo\/([^\/&?]+)\/bar\/([^\/&?]+)\//) { after { ran = true };}
|
129
|
+
|
130
|
+
send(verb, '/bar/')
|
131
|
+
expect(ran).to eq(false)
|
132
|
+
|
133
|
+
send(verb, '/foo/1/bar/1/')
|
134
|
+
expect(ran).to eq(true)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
describe 'helpers' do
|
139
|
+
it 'are defined using the helpers method' do
|
140
|
+
namespace /\/foo\/([^\/&?]+)\/bar\/([^\/&?]+)\// do
|
141
|
+
helpers do
|
142
|
+
def foo
|
143
|
+
'foo'
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
send verb, '' do
|
148
|
+
foo.to_s
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
152
|
+
expect(send(verb, '/foo/1/bar/1/')).to be_ok
|
153
|
+
expect(body).to eq('foo') unless verb == :head
|
154
|
+
end
|
105
155
|
end
|
106
156
|
end
|
107
157
|
|
108
158
|
context 'when namespace is a splat' do
|
109
159
|
it 'accepts the path as a splat' do
|
110
160
|
namespace('/*') { send(verb, '/*') { params[:splat].join ' - ' }}
|
111
|
-
send(verb, '/foo/bar').
|
112
|
-
body.
|
161
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
162
|
+
expect(body).to eq('foo - bar') unless verb == :head
|
113
163
|
end
|
114
164
|
end
|
115
165
|
|
@@ -118,14 +168,14 @@ describe Sinatra::Namespace do
|
|
118
168
|
ran = false
|
119
169
|
namespace('/foo') { before { ran = true }}
|
120
170
|
send(verb, '/foo')
|
121
|
-
ran.
|
171
|
+
expect(ran).to be true
|
122
172
|
end
|
123
173
|
|
124
174
|
specify 'are not triggered for a different namespace' do
|
125
175
|
ran = false
|
126
176
|
namespace('/foo') { before { ran = true }}
|
127
177
|
send(verb, '/fox')
|
128
|
-
ran.
|
178
|
+
expect(ran).to be false
|
129
179
|
end
|
130
180
|
end
|
131
181
|
|
@@ -134,14 +184,14 @@ describe Sinatra::Namespace do
|
|
134
184
|
ran = false
|
135
185
|
namespace('/foo') { after { ran = true }}
|
136
186
|
send(verb, '/foo')
|
137
|
-
ran.
|
187
|
+
expect(ran).to be true
|
138
188
|
end
|
139
189
|
|
140
190
|
specify 'are not triggered for a different namespace' do
|
141
191
|
ran = false
|
142
192
|
namespace('/foo') { after { ran = true }}
|
143
193
|
send(verb, '/fox')
|
144
|
-
ran.
|
194
|
+
expect(ran).to be false
|
145
195
|
end
|
146
196
|
end
|
147
197
|
|
@@ -153,20 +203,20 @@ describe Sinatra::Namespace do
|
|
153
203
|
send(verb, '/') { 'no' }
|
154
204
|
end
|
155
205
|
send(verb, '/', {}, 'HTTP_HOST' => 'example.com')
|
156
|
-
last_response.
|
157
|
-
body.
|
206
|
+
expect(last_response).to be_ok
|
207
|
+
expect(body).to eq('yes') unless verb == :head
|
158
208
|
send(verb, '/', {}, 'HTTP_HOST' => 'example.org')
|
159
|
-
last_response.
|
160
|
-
body.
|
209
|
+
expect(last_response).to be_ok
|
210
|
+
expect(body).to eq('no') unless verb == :head
|
161
211
|
end
|
162
212
|
|
163
213
|
specify 'are accepted in the route definition' do
|
164
214
|
namespace :host_name => 'example.com' do
|
165
215
|
send(verb, '/foo', :provides => :txt) { 'ok' }
|
166
216
|
end
|
167
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain').
|
168
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html').
|
169
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain').
|
217
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')).to be_ok
|
218
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html')).not_to be_ok
|
219
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain')).not_to be_ok
|
170
220
|
end
|
171
221
|
|
172
222
|
specify 'are accepted in the before-filter' do
|
@@ -176,13 +226,13 @@ describe Sinatra::Namespace do
|
|
176
226
|
send(verb, '/*') { 'ok' }
|
177
227
|
end
|
178
228
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain')
|
179
|
-
ran.
|
229
|
+
expect(ran).to be false
|
180
230
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html')
|
181
|
-
ran.
|
231
|
+
expect(ran).to be false
|
182
232
|
send(verb, '/bar', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
183
|
-
ran.
|
233
|
+
expect(ran).to be false
|
184
234
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
185
|
-
ran.
|
235
|
+
expect(ran).to be true
|
186
236
|
end
|
187
237
|
|
188
238
|
specify 'are accepted in the after-filter' do
|
@@ -192,13 +242,13 @@ describe Sinatra::Namespace do
|
|
192
242
|
send(verb, '/*') { 'ok' }
|
193
243
|
end
|
194
244
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain')
|
195
|
-
ran.
|
245
|
+
expect(ran).to be false
|
196
246
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html')
|
197
|
-
ran.
|
247
|
+
expect(ran).to be false
|
198
248
|
send(verb, '/bar', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
199
|
-
ran.
|
249
|
+
expect(ran).to be false
|
200
250
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
201
|
-
ran.
|
251
|
+
expect(ran).to be true
|
202
252
|
end
|
203
253
|
end
|
204
254
|
|
@@ -207,8 +257,8 @@ describe Sinatra::Namespace do
|
|
207
257
|
namespace '/foo', :host_name => 'example.com' do
|
208
258
|
send(verb) { 'ok' }
|
209
259
|
end
|
210
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com').
|
211
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org').
|
260
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com')).to be_ok
|
261
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org')).not_to be_ok
|
212
262
|
end
|
213
263
|
|
214
264
|
specify 'are accepted in the before-filter' do
|
@@ -217,11 +267,11 @@ describe Sinatra::Namespace do
|
|
217
267
|
send(verb) { @yes || 'no' }
|
218
268
|
end
|
219
269
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com')
|
220
|
-
last_response.
|
221
|
-
body.
|
270
|
+
expect(last_response).to be_ok
|
271
|
+
expect(body).to eq('yes') unless verb == :head
|
222
272
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org')
|
223
|
-
last_response.
|
224
|
-
body.
|
273
|
+
expect(last_response).to be_ok
|
274
|
+
expect(body).to eq('no') unless verb == :head
|
225
275
|
end
|
226
276
|
|
227
277
|
specify 'are accepted in the after-filter' do
|
@@ -231,17 +281,17 @@ describe Sinatra::Namespace do
|
|
231
281
|
send(verb) { 'ok' }
|
232
282
|
end
|
233
283
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org')
|
234
|
-
ran.
|
284
|
+
expect(ran).to be false
|
235
285
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com')
|
236
|
-
ran.
|
286
|
+
expect(ran).to be true
|
237
287
|
end
|
238
288
|
|
239
289
|
specify 'are accepted in the route definition' do
|
240
290
|
namespace '/foo' do
|
241
291
|
send(verb, :host_name => 'example.com') { 'ok' }
|
242
292
|
end
|
243
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com').
|
244
|
-
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org').
|
293
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com')).to be_ok
|
294
|
+
expect(send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org')).not_to be_ok
|
245
295
|
end
|
246
296
|
|
247
297
|
context 'when the namespace has a condition' do
|
@@ -252,11 +302,11 @@ describe Sinatra::Namespace do
|
|
252
302
|
send(verb) { 'ok' }
|
253
303
|
end
|
254
304
|
send(verb, '/', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain')
|
255
|
-
ran.
|
305
|
+
expect(ran).to be false
|
256
306
|
send(verb, '/', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html')
|
257
|
-
ran.
|
307
|
+
expect(ran).to be false
|
258
308
|
send(verb, '/', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
259
|
-
ran.
|
309
|
+
expect(ran).to be true
|
260
310
|
end
|
261
311
|
|
262
312
|
specify 'are accepted in the filters' do
|
@@ -266,13 +316,13 @@ describe Sinatra::Namespace do
|
|
266
316
|
send(verb, '/*') { 'ok' }
|
267
317
|
end
|
268
318
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.org', 'HTTP_ACCEPT' => 'text/plain')
|
269
|
-
ran.
|
319
|
+
expect(ran).to be false
|
270
320
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/html')
|
271
|
-
ran.
|
321
|
+
expect(ran).to be false
|
272
322
|
send(verb, '/far', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
273
|
-
ran.
|
323
|
+
expect(ran).to be false
|
274
324
|
send(verb, '/foo', {}, 'HTTP_HOST' => 'example.com', 'HTTP_ACCEPT' => 'text/plain')
|
275
|
-
ran.
|
325
|
+
expect(ran).to be true
|
276
326
|
end
|
277
327
|
end
|
278
328
|
end
|
@@ -292,8 +342,8 @@ describe Sinatra::Namespace do
|
|
292
342
|
end
|
293
343
|
end
|
294
344
|
|
295
|
-
send(verb, '/foo/bar').
|
296
|
-
body.
|
345
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
346
|
+
expect(body).to eq('42') unless verb == :head
|
297
347
|
end
|
298
348
|
|
299
349
|
it 'can be defined as normal methods' do
|
@@ -307,8 +357,8 @@ describe Sinatra::Namespace do
|
|
307
357
|
end
|
308
358
|
end
|
309
359
|
|
310
|
-
send(verb, '/foo/bar').
|
311
|
-
body.
|
360
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
361
|
+
expect(body).to eq('42') unless verb == :head
|
312
362
|
end
|
313
363
|
|
314
364
|
it 'can be defined using module mixins' do
|
@@ -325,8 +375,8 @@ describe Sinatra::Namespace do
|
|
325
375
|
end
|
326
376
|
end
|
327
377
|
|
328
|
-
send(verb, '/foo/bar').
|
329
|
-
body.
|
378
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
379
|
+
expect(body).to eq('42') unless verb == :head
|
330
380
|
end
|
331
381
|
|
332
382
|
specify 'are unavailable outside the namespace where they are defined' do
|
@@ -346,7 +396,7 @@ describe Sinatra::Namespace do
|
|
346
396
|
end
|
347
397
|
end
|
348
398
|
|
349
|
-
|
399
|
+
expect { send verb, '/' }.to raise_error(NameError)
|
350
400
|
end
|
351
401
|
|
352
402
|
specify 'are unavailable outside the namespace that they are mixed into' do
|
@@ -369,7 +419,7 @@ describe Sinatra::Namespace do
|
|
369
419
|
end
|
370
420
|
end
|
371
421
|
|
372
|
-
|
422
|
+
expect { send verb, '/' }.to raise_error(NameError)
|
373
423
|
end
|
374
424
|
|
375
425
|
specify 'are available to nested namespaces' do
|
@@ -387,8 +437,8 @@ describe Sinatra::Namespace do
|
|
387
437
|
end
|
388
438
|
end
|
389
439
|
|
390
|
-
send(verb, '/foo/bar').
|
391
|
-
body.
|
440
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
441
|
+
expect(body).to eq('42') unless verb == :head
|
392
442
|
end
|
393
443
|
|
394
444
|
specify 'can call super from nested definitions' do
|
@@ -410,8 +460,8 @@ describe Sinatra::Namespace do
|
|
410
460
|
end
|
411
461
|
end
|
412
462
|
|
413
|
-
send(verb, '/foo/bar').
|
414
|
-
body.
|
463
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
464
|
+
expect(body).to eq('23') unless verb == :head
|
415
465
|
end
|
416
466
|
end
|
417
467
|
|
@@ -423,8 +473,8 @@ describe Sinatra::Namespace do
|
|
423
473
|
end
|
424
474
|
end
|
425
475
|
|
426
|
-
send(verb, '/foo/bar/baz').
|
427
|
-
body.
|
476
|
+
expect(send(verb, '/foo/bar/baz')).to be_ok
|
477
|
+
expect(body).to eq('OKAY!!11!') unless verb == :head
|
428
478
|
end
|
429
479
|
|
430
480
|
it 'exposes helpers to nested namespaces' do
|
@@ -442,8 +492,8 @@ describe Sinatra::Namespace do
|
|
442
492
|
end
|
443
493
|
end
|
444
494
|
|
445
|
-
send(verb, '/foo/bar/baz').
|
446
|
-
body.
|
495
|
+
expect(send(verb, '/foo/bar/baz')).to be_ok
|
496
|
+
expect(body).to eq('42') unless verb == :head
|
447
497
|
end
|
448
498
|
|
449
499
|
specify 'does not provide access to nested helper methods' do
|
@@ -463,13 +513,13 @@ describe Sinatra::Namespace do
|
|
463
513
|
end
|
464
514
|
end
|
465
515
|
|
466
|
-
|
516
|
+
expect { send verb, '/foo' }.to raise_error(NameError)
|
467
517
|
end
|
468
518
|
|
469
519
|
it 'accepts a nested namespace as a named parameter' do
|
470
520
|
namespace('/:a') { namespace('/:b') { send(verb) { params[:a] }}}
|
471
|
-
send(verb, '/foo/bar').
|
472
|
-
body.
|
521
|
+
expect(send(verb, '/foo/bar')).to be_ok
|
522
|
+
expect(body).to eq('foo') unless verb == :head
|
473
523
|
end
|
474
524
|
end
|
475
525
|
|
@@ -478,24 +528,24 @@ describe Sinatra::Namespace do
|
|
478
528
|
namespace('/de') do
|
479
529
|
not_found { 'nicht gefunden' }
|
480
530
|
end
|
481
|
-
send(verb, '/foo').status.
|
482
|
-
last_response.body.
|
483
|
-
get('/en/foo').status.
|
484
|
-
last_response.body.
|
485
|
-
get('/de/foo').status.
|
486
|
-
last_response.body.
|
531
|
+
expect(send(verb, '/foo').status).to eq 404
|
532
|
+
expect(last_response.body).not_to eq 'nicht gefunden' unless verb == :head
|
533
|
+
expect(get('/en/foo').status).to eq 404
|
534
|
+
expect(last_response.body).not_to eq 'nicht gefunden' unless verb == :head
|
535
|
+
expect(get('/de/foo').status).to eq 404
|
536
|
+
expect(last_response.body).to eq 'nicht gefunden' unless verb == :head
|
487
537
|
end
|
488
538
|
|
489
539
|
it 'can be customized for specific error codes' do
|
490
540
|
namespace('/de') do
|
491
541
|
error(404) { 'nicht gefunden' }
|
492
542
|
end
|
493
|
-
send(verb, '/foo').status.
|
494
|
-
last_response.body.
|
495
|
-
get('/en/foo').status.
|
496
|
-
last_response.body.
|
497
|
-
get('/de/foo').status.
|
498
|
-
last_response.body.
|
543
|
+
expect(send(verb, '/foo').status).to eq 404
|
544
|
+
expect(last_response.body).not_to eq 'nicht gefunden' unless verb == :head
|
545
|
+
expect(get('/en/foo').status).to eq 404
|
546
|
+
expect(last_response.body).not_to eq 'nicht gefunden' unless verb == :head
|
547
|
+
expect(get('/de/foo').status).to eq 404
|
548
|
+
expect(last_response.body).to eq 'nicht gefunden' unless verb == :head
|
499
549
|
end
|
500
550
|
|
501
551
|
it 'falls back to the handler defined in the base app' do
|
@@ -507,12 +557,12 @@ describe Sinatra::Namespace do
|
|
507
557
|
error(404) { 'nicht gefunden' }
|
508
558
|
end
|
509
559
|
end
|
510
|
-
send(verb, '/foo').status.
|
511
|
-
last_response.body.
|
512
|
-
get('/en/foo').status.
|
513
|
-
last_response.body.
|
514
|
-
get('/de/foo').status.
|
515
|
-
last_response.body.
|
560
|
+
expect(send(verb, '/foo').status).to eq 404
|
561
|
+
expect(last_response.body).to eq 'not found...' unless verb == :head
|
562
|
+
expect(get('/en/foo').status).to eq 404
|
563
|
+
expect(last_response.body).to eq 'not found...' unless verb == :head
|
564
|
+
expect(get('/de/foo').status).to eq 404
|
565
|
+
expect(last_response.body).to eq 'nicht gefunden' unless verb == :head
|
516
566
|
end
|
517
567
|
|
518
568
|
it 'can be customized for specific Exception classes' do
|
@@ -542,10 +592,10 @@ describe Sinatra::Namespace do
|
|
542
592
|
end
|
543
593
|
end
|
544
594
|
end
|
545
|
-
get('/en/foo').status.
|
546
|
-
last_response.body.
|
547
|
-
get('/de/foo').status.
|
548
|
-
last_response.body.
|
595
|
+
expect(get('/en/foo').status).to eq 401
|
596
|
+
expect(last_response.body).to eq 'auth failed' unless verb == :head
|
597
|
+
expect(get('/de/foo').status).to eq 406
|
598
|
+
expect(last_response.body).to eq 'methode nicht erlaubt' unless verb == :head
|
549
599
|
end
|
550
600
|
|
551
601
|
it "allows custom error handlers when namespace is declared as /en/:id. Issue #119" do
|
@@ -563,7 +613,7 @@ describe Sinatra::Namespace do
|
|
563
613
|
end
|
564
614
|
}
|
565
615
|
|
566
|
-
get('/en/1').status.
|
616
|
+
expect(get('/en/1').status).to eq(201)
|
567
617
|
end
|
568
618
|
end
|
569
619
|
|
@@ -578,8 +628,8 @@ describe Sinatra::Namespace do
|
|
578
628
|
end
|
579
629
|
end
|
580
630
|
|
581
|
-
send(verb, '/').body.
|
582
|
-
send(verb, '/foo').body.
|
631
|
+
expect(send(verb, '/').body).to eq 'hi'
|
632
|
+
expect(send(verb, '/foo').body).to eq 'hi'
|
583
633
|
end
|
584
634
|
|
585
635
|
specify 'can be nested' do
|
@@ -592,8 +642,8 @@ describe Sinatra::Namespace do
|
|
592
642
|
end
|
593
643
|
end
|
594
644
|
|
595
|
-
send(verb, '/').body.
|
596
|
-
send(verb, '/foo').body.
|
645
|
+
expect(send(verb, '/').body).to eq 'hi'
|
646
|
+
expect(send(verb, '/foo').body).to eq 'ho'
|
597
647
|
end
|
598
648
|
|
599
649
|
specify 'can use a custom views directory' do
|
@@ -606,8 +656,8 @@ describe Sinatra::Namespace do
|
|
606
656
|
end
|
607
657
|
end
|
608
658
|
|
609
|
-
send(verb, '/').body.
|
610
|
-
send(verb, '/foo').body.
|
659
|
+
expect(send(verb, '/').body).to eq "hi\n"
|
660
|
+
expect(send(verb, '/foo').body).to eq "ho\n"
|
611
661
|
end
|
612
662
|
|
613
663
|
specify 'default to the base app\'s layout' do
|
@@ -621,8 +671,8 @@ describe Sinatra::Namespace do
|
|
621
671
|
end
|
622
672
|
end
|
623
673
|
|
624
|
-
send(verb, '/').body.
|
625
|
-
send(verb, '/foo').body.
|
674
|
+
expect(send(verb, '/').body).to eq 'he said: hi'
|
675
|
+
expect(send(verb, '/foo').body).to eq 'he said: ho'
|
626
676
|
end
|
627
677
|
|
628
678
|
specify 'can define nested layouts' do
|
@@ -636,8 +686,8 @@ describe Sinatra::Namespace do
|
|
636
686
|
end
|
637
687
|
end
|
638
688
|
|
639
|
-
send(verb, '/').body.
|
640
|
-
send(verb, '/foo').body.
|
689
|
+
expect(send(verb, '/').body).to eq 'Hello World!'
|
690
|
+
expect(send(verb, '/foo').body).to eq 'Hi World!'
|
641
691
|
end
|
642
692
|
end
|
643
693
|
end
|
@@ -651,7 +701,7 @@ describe Sinatra::Namespace do
|
|
651
701
|
value = foo
|
652
702
|
end
|
653
703
|
end
|
654
|
-
value.
|
704
|
+
expect(value).to eq 42
|
655
705
|
end
|
656
706
|
|
657
707
|
specify 'can be registered within a namespace' do
|
@@ -664,8 +714,8 @@ describe Sinatra::Namespace do
|
|
664
714
|
end
|
665
715
|
b = views
|
666
716
|
end
|
667
|
-
a.
|
668
|
-
b.
|
717
|
+
expect(a).to eq 'CUSTOM!!!'
|
718
|
+
expect(b).not_to eq 'CUSTOM!!!'
|
669
719
|
end
|
670
720
|
|
671
721
|
specify 'trigger the route_added hook' do
|
@@ -681,11 +731,11 @@ describe Sinatra::Namespace do
|
|
681
731
|
end
|
682
732
|
get('/bar') { }
|
683
733
|
end
|
684
|
-
route[1].
|
734
|
+
expect(route[1]).to eq(Mustermann.new '/foo')
|
685
735
|
end
|
686
736
|
|
687
737
|
specify 'prevent app-global settings from being changed' do
|
688
|
-
|
738
|
+
expect { namespace('/') { set :foo, :bar }}.to raise_error(ArgumentError)
|
689
739
|
end
|
690
740
|
end
|
691
741
|
end
|
@@ -703,8 +753,8 @@ describe Sinatra::Namespace do
|
|
703
753
|
end
|
704
754
|
end
|
705
755
|
|
706
|
-
get('/foo/bar').status.
|
707
|
-
last_response.body.
|
756
|
+
expect(get('/foo/bar').status).to eq(200)
|
757
|
+
expect(last_response.body).to eq('ok')
|
708
758
|
end
|
709
759
|
|
710
760
|
it 'uses some repro' do
|
@@ -719,8 +769,8 @@ describe Sinatra::Namespace do
|
|
719
769
|
end
|
720
770
|
end
|
721
771
|
|
722
|
-
get('/foo/bar').status.
|
723
|
-
last_response.body.
|
772
|
+
expect(get('/foo/bar').status).to eq(200)
|
773
|
+
expect(last_response.body).to eq('42')
|
724
774
|
end
|
725
775
|
|
726
776
|
it 'allows checking setting existence with respond_to?' do
|
@@ -734,8 +784,8 @@ describe Sinatra::Namespace do
|
|
734
784
|
end
|
735
785
|
end
|
736
786
|
|
737
|
-
get('/foo/bar').status.
|
738
|
-
last_response.body.
|
787
|
+
expect(get('/foo/bar').status).to eq(200)
|
788
|
+
expect(last_response.body).to eq('true')
|
739
789
|
end
|
740
790
|
end
|
741
791
|
end
|