sinatra-acd 1.4.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +5 -0
- data/AUTHORS +61 -0
- data/CHANGES +1293 -0
- data/Gemfile +76 -0
- data/LICENSE +23 -0
- data/README.de.md +2864 -0
- data/README.es.md +2786 -0
- data/README.fr.md +2924 -0
- data/README.hu.md +694 -0
- data/README.ja.md +2726 -0
- data/README.ko.md +2832 -0
- data/README.md +2980 -0
- data/README.pt-br.md +965 -0
- data/README.pt-pt.md +791 -0
- data/README.ru.md +2799 -0
- data/README.zh.md +2158 -0
- data/Rakefile +199 -0
- data/examples/chat.rb +61 -0
- data/examples/simple.rb +3 -0
- data/examples/stream.ru +26 -0
- data/lib/sinatra.rb +4 -0
- data/lib/sinatra/base.rb +2044 -0
- data/lib/sinatra/images/404.png +0 -0
- data/lib/sinatra/images/500.png +0 -0
- data/lib/sinatra/main.rb +34 -0
- data/lib/sinatra/show_exceptions.rb +345 -0
- data/lib/sinatra/version.rb +3 -0
- data/sinatra.gemspec +19 -0
- data/test/asciidoctor_test.rb +72 -0
- data/test/base_test.rb +171 -0
- data/test/builder_test.rb +91 -0
- data/test/coffee_test.rb +90 -0
- data/test/compile_test.rb +183 -0
- data/test/contest.rb +100 -0
- data/test/creole_test.rb +65 -0
- data/test/delegator_test.rb +160 -0
- data/test/encoding_test.rb +20 -0
- data/test/erb_test.rb +116 -0
- data/test/extensions_test.rb +98 -0
- data/test/filter_test.rb +487 -0
- data/test/haml_test.rb +109 -0
- data/test/helper.rb +131 -0
- data/test/helpers_test.rb +1917 -0
- data/test/integration/app.rb +79 -0
- data/test/integration_helper.rb +236 -0
- data/test/integration_test.rb +104 -0
- data/test/less_test.rb +69 -0
- data/test/liquid_test.rb +77 -0
- data/test/mapped_error_test.rb +285 -0
- data/test/markaby_test.rb +80 -0
- data/test/markdown_test.rb +82 -0
- data/test/mediawiki_test.rb +68 -0
- data/test/middleware_test.rb +68 -0
- data/test/nokogiri_test.rb +67 -0
- data/test/public/favicon.ico +0 -0
- data/test/rabl_test.rb +89 -0
- data/test/rack_test.rb +45 -0
- data/test/radius_test.rb +59 -0
- data/test/rdoc_test.rb +66 -0
- data/test/readme_test.rb +130 -0
- data/test/request_test.rb +97 -0
- data/test/response_test.rb +63 -0
- data/test/result_test.rb +76 -0
- data/test/route_added_hook_test.rb +59 -0
- data/test/routing_test.rb +1412 -0
- data/test/sass_test.rb +115 -0
- data/test/scss_test.rb +88 -0
- data/test/server_test.rb +48 -0
- data/test/settings_test.rb +582 -0
- data/test/sinatra_test.rb +12 -0
- data/test/slim_test.rb +102 -0
- data/test/static_test.rb +236 -0
- data/test/streaming_test.rb +149 -0
- data/test/stylus_test.rb +90 -0
- data/test/templates_test.rb +382 -0
- data/test/textile_test.rb +65 -0
- data/test/views/a/in_a.str +1 -0
- data/test/views/ascii.erb +2 -0
- data/test/views/b/in_b.str +1 -0
- data/test/views/calc.html.erb +1 -0
- data/test/views/error.builder +3 -0
- data/test/views/error.erb +3 -0
- data/test/views/error.haml +3 -0
- data/test/views/error.sass +2 -0
- data/test/views/explicitly_nested.str +1 -0
- data/test/views/foo/hello.test +1 -0
- data/test/views/hello.asciidoc +1 -0
- data/test/views/hello.builder +1 -0
- data/test/views/hello.coffee +1 -0
- data/test/views/hello.creole +1 -0
- data/test/views/hello.erb +1 -0
- data/test/views/hello.haml +1 -0
- data/test/views/hello.less +5 -0
- data/test/views/hello.liquid +1 -0
- data/test/views/hello.mab +1 -0
- data/test/views/hello.md +1 -0
- data/test/views/hello.mediawiki +1 -0
- data/test/views/hello.nokogiri +1 -0
- data/test/views/hello.rabl +2 -0
- data/test/views/hello.radius +1 -0
- data/test/views/hello.rdoc +1 -0
- data/test/views/hello.sass +2 -0
- data/test/views/hello.scss +3 -0
- data/test/views/hello.slim +1 -0
- data/test/views/hello.str +1 -0
- data/test/views/hello.styl +2 -0
- data/test/views/hello.test +1 -0
- data/test/views/hello.textile +1 -0
- data/test/views/hello.wlang +1 -0
- data/test/views/hello.yajl +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.liquid +2 -0
- data/test/views/layout2.mab +2 -0
- data/test/views/layout2.nokogiri +3 -0
- data/test/views/layout2.rabl +3 -0
- data/test/views/layout2.radius +2 -0
- data/test/views/layout2.slim +3 -0
- data/test/views/layout2.str +2 -0
- data/test/views/layout2.test +1 -0
- data/test/views/layout2.wlang +2 -0
- data/test/views/nested.str +1 -0
- data/test/views/utf8.erb +2 -0
- data/test/wlang_test.rb +87 -0
- data/test/yajl_test.rb +86 -0
- metadata +280 -0
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class SinatraTest < Test::Unit::TestCase
|
4
|
+
it 'creates a new Sinatra::Base subclass on new' do
|
5
|
+
app = Sinatra.new { get('/') { 'Hello World' } }
|
6
|
+
assert_same Sinatra::Base, app.superclass
|
7
|
+
end
|
8
|
+
|
9
|
+
it "responds to #template_cache" do
|
10
|
+
assert_kind_of Tilt::Cache, Sinatra::Base.new!.template_cache
|
11
|
+
end
|
12
|
+
end
|
data/test/slim_test.rb
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'slim'
|
5
|
+
|
6
|
+
class SlimTest < Test::Unit::TestCase
|
7
|
+
def slim_app(&block)
|
8
|
+
mock_app do
|
9
|
+
set :views, File.dirname(__FILE__) + '/views'
|
10
|
+
get('/', &block)
|
11
|
+
end
|
12
|
+
get '/'
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'renders inline slim strings' do
|
16
|
+
slim_app { slim "h1 Hiya\n" }
|
17
|
+
assert ok?
|
18
|
+
assert_equal "<h1>Hiya</h1>", body
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'renders .slim files in views path' do
|
22
|
+
slim_app { slim :hello }
|
23
|
+
assert ok?
|
24
|
+
assert_equal "<h1>Hello From Slim</h1>", body
|
25
|
+
end
|
26
|
+
|
27
|
+
it "renders with inline layouts" do
|
28
|
+
mock_app do
|
29
|
+
layout { %(h1\n | THIS. IS. \n == yield.upcase ) }
|
30
|
+
get('/') { slim 'em Sparta' }
|
31
|
+
end
|
32
|
+
get '/'
|
33
|
+
assert ok?
|
34
|
+
assert_equal "<h1>THIS. IS. <EM>SPARTA</EM></h1>", body
|
35
|
+
end
|
36
|
+
|
37
|
+
it "renders with file layouts" do
|
38
|
+
slim_app { slim('| Hello World', :layout => :layout2) }
|
39
|
+
assert ok?
|
40
|
+
assert_equal "<h1>Slim Layout!</h1><p>Hello World</p>", body
|
41
|
+
end
|
42
|
+
|
43
|
+
it "raises error if template not found" do
|
44
|
+
mock_app { get('/') { slim(:no_such_template) } }
|
45
|
+
assert_raise(Errno::ENOENT) { get('/') }
|
46
|
+
end
|
47
|
+
|
48
|
+
HTML4_DOCTYPE = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">"
|
49
|
+
|
50
|
+
it "passes slim options to the slim engine" do
|
51
|
+
mock_app { get('/') { slim("x foo='bar'", :attr_quote => "'") }}
|
52
|
+
get '/'
|
53
|
+
assert ok?
|
54
|
+
assert_body "<x foo='bar'></x>"
|
55
|
+
end
|
56
|
+
|
57
|
+
it "passes default slim options to the slim engine" do
|
58
|
+
mock_app do
|
59
|
+
set :slim, :attr_quote => "'"
|
60
|
+
get('/') { slim("x foo='bar'") }
|
61
|
+
end
|
62
|
+
get '/'
|
63
|
+
assert ok?
|
64
|
+
assert_body "<x foo='bar'></x>"
|
65
|
+
end
|
66
|
+
|
67
|
+
it "merges the default slim options with the overrides and passes them to the slim engine" do
|
68
|
+
mock_app do
|
69
|
+
set :slim, :attr_quote => "'"
|
70
|
+
get('/') { slim("x foo='bar'") }
|
71
|
+
get('/other') { slim("x foo='bar'", :attr_quote => '"') }
|
72
|
+
end
|
73
|
+
get '/'
|
74
|
+
assert ok?
|
75
|
+
assert_body "<x foo='bar'></x>"
|
76
|
+
get '/other'
|
77
|
+
assert ok?
|
78
|
+
assert_body '<x foo="bar"></x>'
|
79
|
+
end
|
80
|
+
|
81
|
+
it "can render truly nested layouts by accepting a layout and a block with the contents" do
|
82
|
+
mock_app do
|
83
|
+
template(:main_outer_layout) { "h1 Title\n== yield" }
|
84
|
+
template(:an_inner_layout) { "h2 Subtitle\n== yield" }
|
85
|
+
template(:a_page) { "p Contents." }
|
86
|
+
get('/') do
|
87
|
+
slim :main_outer_layout, :layout => false do
|
88
|
+
slim :an_inner_layout do
|
89
|
+
slim :a_page
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
get '/'
|
95
|
+
assert ok?
|
96
|
+
assert_body "<h1>Title</h1>\n<h2>Subtitle</h2>\n<p>Contents.</p>\n"
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
rescue LoadError
|
101
|
+
warn "#{$!.to_s}: skipping slim tests"
|
102
|
+
end
|
data/test/static_test.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class StaticTest < Test::Unit::TestCase
|
4
|
+
setup do
|
5
|
+
mock_app do
|
6
|
+
set :static, true
|
7
|
+
set :public_folder, File.dirname(__FILE__)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'serves GET requests for files in the public directory' do
|
12
|
+
get "/#{File.basename(__FILE__)}"
|
13
|
+
assert ok?
|
14
|
+
assert_equal File.read(__FILE__), body
|
15
|
+
assert_equal File.size(__FILE__).to_s, response['Content-Length']
|
16
|
+
assert response.headers.include?('Last-Modified')
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'produces a body that can be iterated over multiple times' do
|
20
|
+
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
21
|
+
_, _, body = @app.call(env)
|
22
|
+
buf1, buf2 = [], []
|
23
|
+
body.each { |part| buf1 << part }
|
24
|
+
body.each { |part| buf2 << part }
|
25
|
+
assert_equal buf1.join, buf2.join
|
26
|
+
assert_equal File.read(__FILE__), buf1.join
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets the sinatra.static_file env variable if served' do
|
30
|
+
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
31
|
+
@app.call(env)
|
32
|
+
assert_equal File.expand_path(__FILE__), env['sinatra.static_file']
|
33
|
+
end
|
34
|
+
|
35
|
+
it 'serves HEAD requests for files in the public directory' do
|
36
|
+
head "/#{File.basename(__FILE__)}"
|
37
|
+
assert ok?
|
38
|
+
assert_equal '', body
|
39
|
+
assert response.headers.include?('Last-Modified')
|
40
|
+
assert_equal File.size(__FILE__).to_s, response['Content-Length']
|
41
|
+
end
|
42
|
+
|
43
|
+
%w[POST PUT DELETE].each do |verb|
|
44
|
+
it "does not serve #{verb} requests" do
|
45
|
+
send verb.downcase, "/#{File.basename(__FILE__)}"
|
46
|
+
assert_equal 404, status
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'serves files in preference to custom routes' do
|
51
|
+
@app.get("/#{File.basename(__FILE__)}") { 'Hello World' }
|
52
|
+
get "/#{File.basename(__FILE__)}"
|
53
|
+
assert ok?
|
54
|
+
assert body != 'Hello World'
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'does not serve directories' do
|
58
|
+
get "/"
|
59
|
+
assert not_found?
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'passes to the next handler when the static option is disabled' do
|
63
|
+
@app.set :static, false
|
64
|
+
get "/#{File.basename(__FILE__)}"
|
65
|
+
assert not_found?
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'passes to the next handler when the public option is nil' do
|
69
|
+
@app.set :public_folder, nil
|
70
|
+
get "/#{File.basename(__FILE__)}"
|
71
|
+
assert not_found?
|
72
|
+
end
|
73
|
+
|
74
|
+
it '404s when a file is not found' do
|
75
|
+
get "/foobarbaz.txt"
|
76
|
+
assert not_found?
|
77
|
+
end
|
78
|
+
|
79
|
+
it 'serves files when .. path traverses within public directory' do
|
80
|
+
get "/data/../#{File.basename(__FILE__)}"
|
81
|
+
assert ok?
|
82
|
+
assert_equal File.read(__FILE__), body
|
83
|
+
end
|
84
|
+
|
85
|
+
it '404s when .. path traverses outside of public directory' do
|
86
|
+
mock_app do
|
87
|
+
set :static, true
|
88
|
+
set :public_folder, File.dirname(__FILE__) + '/data'
|
89
|
+
end
|
90
|
+
get "/../#{File.basename(__FILE__)}"
|
91
|
+
assert not_found?
|
92
|
+
end
|
93
|
+
|
94
|
+
def assert_valid_range(http_range, range, path, file)
|
95
|
+
request = Rack::MockRequest.new(@app)
|
96
|
+
response = request.get("/#{File.basename(path)}", 'HTTP_RANGE' => http_range)
|
97
|
+
|
98
|
+
should_be = file[range]
|
99
|
+
expected_range = "bytes #{range.begin}-#{range.end}/#{file.length}"
|
100
|
+
|
101
|
+
assert_equal(
|
102
|
+
206,response.status,
|
103
|
+
"Should be HTTP/1.1 206 Partial content"
|
104
|
+
)
|
105
|
+
assert_equal(
|
106
|
+
should_be.length,
|
107
|
+
response.body.length,
|
108
|
+
"Unexpected response length for #{http_range}"
|
109
|
+
)
|
110
|
+
assert_equal(
|
111
|
+
should_be,
|
112
|
+
response.body,
|
113
|
+
"Unexpected response data for #{http_range}"
|
114
|
+
)
|
115
|
+
assert_equal(
|
116
|
+
should_be.length.to_s,
|
117
|
+
response['Content-Length'],
|
118
|
+
"Incorrect Content-Length for #{http_range}"
|
119
|
+
)
|
120
|
+
assert_equal(
|
121
|
+
expected_range,
|
122
|
+
response['Content-Range'],
|
123
|
+
"Incorrect Content-Range for #{http_range}"
|
124
|
+
)
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'handles valid byte ranges correctly' do
|
128
|
+
# Use the biggest file in this dir so we can test ranges > 8k bytes. (StaticFile sends in 8k chunks.)
|
129
|
+
path = File.dirname(__FILE__) + '/helpers_test.rb' # currently 16k bytes
|
130
|
+
file = File.read(path)
|
131
|
+
length = file.length
|
132
|
+
assert length > 9000, "The test file #{path} is too short (#{length} bytes) to run these tests"
|
133
|
+
|
134
|
+
[0..0, 42..88, 1234..1234, 100..9000, 0..(length-1), (length-1)..(length-1)].each do |range|
|
135
|
+
assert_valid_range("bytes=#{range.begin}-#{range.end}", range, path, file)
|
136
|
+
end
|
137
|
+
|
138
|
+
[0, 100, length-100, length-1].each do |start|
|
139
|
+
assert_valid_range("bytes=#{start}-", (start..length-1), path, file)
|
140
|
+
end
|
141
|
+
|
142
|
+
[1, 100, length-100, length-1, length].each do |range_length|
|
143
|
+
assert_valid_range("bytes=-#{range_length}", (length-range_length..length-1), path, file)
|
144
|
+
end
|
145
|
+
|
146
|
+
# Some valid ranges that exceed the length of the file:
|
147
|
+
assert_valid_range("bytes=100-999999", (100..length-1), path, file)
|
148
|
+
assert_valid_range("bytes=100-#{length}", (100..length-1), path, file)
|
149
|
+
assert_valid_range("bytes=-#{length}", (0..length-1), path, file)
|
150
|
+
assert_valid_range("bytes=-#{length+1}", (0..length-1), path, file)
|
151
|
+
assert_valid_range("bytes=-999999", (0..length-1), path, file)
|
152
|
+
end
|
153
|
+
|
154
|
+
it 'correctly ignores syntactically invalid range requests' do
|
155
|
+
# ...and also ignores multi-range requests, which aren't supported yet
|
156
|
+
["bytes=45-40", "bytes=IV-LXVI", "octets=10-20", "bytes=-", "bytes=1-2,3-4"].each do |http_range|
|
157
|
+
request = Rack::MockRequest.new(@app)
|
158
|
+
response = request.get("/#{File.basename(__FILE__)}", 'HTTP_RANGE' => http_range)
|
159
|
+
|
160
|
+
assert_equal(
|
161
|
+
200,
|
162
|
+
response.status,
|
163
|
+
"Invalid range '#{http_range}' should be ignored"
|
164
|
+
)
|
165
|
+
assert_equal(
|
166
|
+
nil,
|
167
|
+
response['Content-Range'],
|
168
|
+
"Invalid range '#{http_range}' should be ignored"
|
169
|
+
)
|
170
|
+
end
|
171
|
+
end
|
172
|
+
|
173
|
+
it 'returns error 416 for unsatisfiable range requests' do
|
174
|
+
# An unsatisfiable request is one that specifies a start that's at or past the end of the file.
|
175
|
+
length = File.read(__FILE__).length
|
176
|
+
["bytes=888888-", "bytes=888888-999999", "bytes=#{length}-#{length}"].each do |http_range|
|
177
|
+
request = Rack::MockRequest.new(@app)
|
178
|
+
response = request.get("/#{File.basename(__FILE__)}", 'HTTP_RANGE' => http_range)
|
179
|
+
|
180
|
+
assert_equal(
|
181
|
+
416,
|
182
|
+
response.status,
|
183
|
+
"Unsatisfiable range '#{http_range}' should return 416"
|
184
|
+
)
|
185
|
+
assert_equal(
|
186
|
+
"bytes */#{length}",
|
187
|
+
response['Content-Range'],
|
188
|
+
"416 response should include actual length"
|
189
|
+
)
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
it 'does not include static cache control headers by default' do
|
194
|
+
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
195
|
+
_, headers, _ = @app.call(env)
|
196
|
+
assert !headers.has_key?('Cache-Control')
|
197
|
+
end
|
198
|
+
|
199
|
+
it 'sets cache control headers on static files if set' do
|
200
|
+
@app.set :static_cache_control, :public
|
201
|
+
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
202
|
+
status, headers, body = @app.call(env)
|
203
|
+
assert headers.has_key?('Cache-Control')
|
204
|
+
assert_equal headers['Cache-Control'], 'public'
|
205
|
+
|
206
|
+
@app.set(
|
207
|
+
:static_cache_control,
|
208
|
+
[:public, :must_revalidate, {:max_age => 300}]
|
209
|
+
)
|
210
|
+
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
211
|
+
status, headers, body = @app.call(env)
|
212
|
+
assert headers.has_key?('Cache-Control')
|
213
|
+
assert_equal(
|
214
|
+
headers['Cache-Control'],
|
215
|
+
'public, must-revalidate, max-age=300'
|
216
|
+
)
|
217
|
+
end
|
218
|
+
|
219
|
+
it 'renders static assets with custom status via options' do
|
220
|
+
mock_app do
|
221
|
+
set :static, true
|
222
|
+
set :public_folder, File.dirname(__FILE__)
|
223
|
+
|
224
|
+
post '/*' do
|
225
|
+
static!(:status => params[:status])
|
226
|
+
end
|
227
|
+
end
|
228
|
+
|
229
|
+
post "/#{File.basename(__FILE__)}?status=422"
|
230
|
+
assert_equal response.status, 422
|
231
|
+
assert_equal File.read(__FILE__), body
|
232
|
+
assert_equal File.size(__FILE__).to_s, response['Content-Length']
|
233
|
+
assert response.headers.include?('Last-Modified')
|
234
|
+
end
|
235
|
+
|
236
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require File.expand_path('../helper', __FILE__)
|
2
|
+
|
3
|
+
class StreamingTest < Test::Unit::TestCase
|
4
|
+
Stream = Sinatra::Helpers::Stream
|
5
|
+
|
6
|
+
it 'returns the concatenated body' do
|
7
|
+
mock_app do
|
8
|
+
get('/') do
|
9
|
+
stream do |out|
|
10
|
+
out << "Hello" << " "
|
11
|
+
out << "World!"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
get('/')
|
17
|
+
assert_body "Hello World!"
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'always yields strings' do
|
21
|
+
stream = Stream.new { |out| out << :foo }
|
22
|
+
stream.each { |str| assert_equal 'foo', str }
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'postpones body generation' do
|
26
|
+
step = 0
|
27
|
+
|
28
|
+
stream = Stream.new do |out|
|
29
|
+
10.times do
|
30
|
+
out << step
|
31
|
+
step += 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
stream.each do |s|
|
36
|
+
assert_equal s, step.to_s
|
37
|
+
step += 1
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'calls the callback after it is done' do
|
42
|
+
step = 0
|
43
|
+
final = 0
|
44
|
+
stream = Stream.new { |_| 10.times { step += 1 }}
|
45
|
+
stream.callback { final = step }
|
46
|
+
stream.each {|_|}
|
47
|
+
assert_equal 10, final
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'does not trigger the callback if close is set to :keep_open' do
|
51
|
+
step = 0
|
52
|
+
final = 0
|
53
|
+
stream = Stream.new(Stream, :keep_open) { |_| 10.times { step += 1 } }
|
54
|
+
stream.callback { final = step }
|
55
|
+
stream.each {|_|}
|
56
|
+
assert_equal 0, final
|
57
|
+
end
|
58
|
+
|
59
|
+
it 'allows adding more than one callback' do
|
60
|
+
a = b = false
|
61
|
+
stream = Stream.new { }
|
62
|
+
stream.callback { a = true }
|
63
|
+
stream.callback { b = true }
|
64
|
+
stream.each {|_| }
|
65
|
+
assert a, 'should trigger first callback'
|
66
|
+
assert b, 'should trigger second callback'
|
67
|
+
end
|
68
|
+
|
69
|
+
class MockScheduler
|
70
|
+
def initialize(*) @schedule, @defer = [], [] end
|
71
|
+
def schedule(&block) @schedule << block end
|
72
|
+
def defer(&block) @defer << block end
|
73
|
+
def schedule!(*) @schedule.pop.call until @schedule.empty? end
|
74
|
+
def defer!(*) @defer.pop.call until @defer.empty? end
|
75
|
+
end
|
76
|
+
|
77
|
+
it 'allows dropping in another scheduler' do
|
78
|
+
scheduler = MockScheduler.new
|
79
|
+
processing = sending = done = false
|
80
|
+
|
81
|
+
stream = Stream.new(scheduler) do |out|
|
82
|
+
processing = true
|
83
|
+
out << :foo
|
84
|
+
end
|
85
|
+
|
86
|
+
stream.each { sending = true}
|
87
|
+
stream.callback { done = true }
|
88
|
+
|
89
|
+
scheduler.schedule!
|
90
|
+
assert !processing
|
91
|
+
assert !sending
|
92
|
+
assert !done
|
93
|
+
|
94
|
+
scheduler.defer!
|
95
|
+
assert processing
|
96
|
+
assert !sending
|
97
|
+
assert !done
|
98
|
+
|
99
|
+
scheduler.schedule!
|
100
|
+
assert sending
|
101
|
+
assert done
|
102
|
+
end
|
103
|
+
|
104
|
+
it 'schedules exceptions to be raised on the main thread/event loop/...' do
|
105
|
+
scheduler = MockScheduler.new
|
106
|
+
Stream.new(scheduler) { fail 'should be caught' }.each { }
|
107
|
+
scheduler.defer!
|
108
|
+
assert_raise(RuntimeError) { scheduler.schedule! }
|
109
|
+
end
|
110
|
+
|
111
|
+
it 'does not trigger an infinite loop if you call close in a callback' do
|
112
|
+
stream = Stream.new { |out| out.callback { out.close }}
|
113
|
+
stream.each { |_| }
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'gives access to route specific params' do
|
117
|
+
mock_app do
|
118
|
+
get('/:name') do
|
119
|
+
stream { |o| o << params[:name] }
|
120
|
+
end
|
121
|
+
end
|
122
|
+
get '/foo'
|
123
|
+
assert_body 'foo'
|
124
|
+
end
|
125
|
+
|
126
|
+
it 'sets up async.close if available' do
|
127
|
+
ran = false
|
128
|
+
mock_app do
|
129
|
+
get('/') do
|
130
|
+
close = Object.new
|
131
|
+
def close.callback; yield end
|
132
|
+
def close.errback; end
|
133
|
+
env['async.close'] = close
|
134
|
+
stream(:keep_open) do |out|
|
135
|
+
out.callback { ran = true }
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
get '/'
|
140
|
+
assert ran
|
141
|
+
end
|
142
|
+
|
143
|
+
it 'has a public interface to inspect its open/closed state' do
|
144
|
+
stream = Stream.new(Stream) { |out| out << :foo }
|
145
|
+
assert !stream.closed?
|
146
|
+
stream.close
|
147
|
+
assert stream.closed?
|
148
|
+
end
|
149
|
+
end
|