1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
| | # frozen_string_literal: true
require_relative 'helper'
separate_testing do
require_relative '../lib/rack/builder'
require_relative '../lib/rack/lint'
require_relative '../lib/rack/mock_request'
require_relative '../lib/rack/content_length'
require_relative '../lib/rack/show_exceptions'
require_relative '../lib/rack/auth/basic'
end
class NothingMiddleware
def initialize(app, **)
@app = app
end
def call(env)
@@env = env
response = @app.call(env)
response
end
def self.env
@@env
end
end
describe Rack::Builder do
def builder(&block)
Rack::Lint.new Rack::Builder.new(&block)
end
def builder_to_app(&block)
Rack::Lint.new Rack::Builder.new(&block).to_app
end
it "supports run with block" do
app = builder_to_app do
run {|env| [200, { "content-type" => "text/plain" }, ["OK"]]}
end
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'OK'
end
it "supports mapping" do
app = builder_to_app do
map '/' do |outer_env|
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['root']] }
end
map '/sub' do
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['sub']] }
end
end
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'root'
Rack::MockRequest.new(app).get("/sub").body.to_s.must_equal 'sub'
end
it "supports use when mapping" do
app = builder_to_app do
map '/sub' do
use Rack::ContentLength
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['sub']] }
end
use Rack::ContentLength
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['root']] }
end
Rack::MockRequest.new(app).get("/").headers['content-length'].must_equal '4'
Rack::MockRequest.new(app).get("/sub").headers['content-length'].must_equal '3'
end
it "doesn't dupe env even when mapping" do
app = builder_to_app do
use NothingMiddleware, noop: :noop
map '/' do |outer_env|
run lambda { |inner_env|
inner_env['new_key'] = 'new_value'
[200, { "content-type" => "text/plain" }, ['root']]
}
end
end
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'root'
NothingMiddleware.env['new_key'].must_equal 'new_value'
end
it "dupe #to_app when mapping so Rack::Reloader can reload the application on each request" do
app = builder do
map '/' do |outer_env|
run lambda { |env| [200, { "content-type" => "text/plain" }, [object_id.to_s]] }
end
end
builder_app1_id = Rack::MockRequest.new(app).get("/").body.to_s
builder_app2_id = Rack::MockRequest.new(app).get("/").body.to_s
builder_app2_id.wont_equal builder_app1_id
end
it "chains apps by default" do
app = builder_to_app do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
end
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
end
it "has implicit #to_app" do
app = builder do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
end
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
end
it "supports blocks on use" do
app = builder do
use Rack::ShowExceptions
use Rack::Auth::Basic do |username, password|
'secret' == password
end
run lambda { |env| [200, { "content-type" => "text/plain" }, ['Hi Boss']] }
end
response = Rack::MockRequest.new(app).get("/")
response.must_be :client_error?
response.status.must_equal 401
# with auth...
response = Rack::MockRequest.new(app).get("/",
'HTTP_AUTHORIZATION' => 'Basic ' + ["joe:secret"].pack("m*"))
response.status.must_equal 200
response.body.to_s.must_equal 'Hi Boss'
end
it "has explicit #to_app" do
app = builder do
use Rack::ShowExceptions
run lambda { |env| raise "bzzzt" }
end
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
end
it "can mix map and run for endpoints" do
app = builder do
map '/sub' do
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['sub']] }
end
run lambda { |inner_env| [200, { "content-type" => "text/plain" }, ['root']] }
end
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'root'
Rack::MockRequest.new(app).get("/sub").body.to_s.must_equal 'sub'
end
it "accepts middleware-only map blocks" do
app = builder do
map('/foo') { use Rack::ShowExceptions }
run lambda { |env| raise "bzzzt" }
end
proc { Rack::MockRequest.new(app).get("/") }.must_raise(RuntimeError)
Rack::MockRequest.new(app).get("/foo").must_be :server_error?
end
it "yields the generated app to a block for warmup" do
warmed_up_app = nil
app = Rack::Builder.new do
warmup { |a| warmed_up_app = a }
run lambda { |env| [200, {}, []] }
end.to_app
warmed_up_app.must_equal app
end
it "initialize apps once" do
app = builder do
class AppClass
def initialize
@called = 0
end
def call(env)
raise "bzzzt" if @called > 0
@called += 1
[200, { 'content-type' => 'text/plain' }, ['OK']]
end
end
use Rack::ShowExceptions
run AppClass.new
end
Rack::MockRequest.new(app).get("/").status.must_equal 200
Rack::MockRequest.new(app).get("/").must_be :server_error?
end
it "allows use after run" do
app = builder do
run lambda { |env| raise "bzzzt" }
use Rack::ShowExceptions
end
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
Rack::MockRequest.new(app).get("/").must_be :server_error?
end
it "supports #freeze_app for freezing app and middleware" do
app = builder do
freeze_app
use Rack::ShowExceptions
use(Class.new do
def initialize(app) @app = app end
def call(env) @a = 1 if env['PATH_INFO'] == '/a'; @app.call(env) end
end)
o = Object.new
def o.call(env)
@a = 1 if env['PATH_INFO'] == '/b';
[200, {}, []]
end
run o
end
Rack::MockRequest.new(app).get("/a").must_be :server_error?
Rack::MockRequest.new(app).get("/b").must_be :server_error?
Rack::MockRequest.new(app).get("/c").status.must_equal 200
end
it 'complains about a missing run' do
proc do
Rack::Lint.new Rack::Builder.app { use Rack::ShowExceptions }
end.must_raise(RuntimeError)
end
describe "parse_file" do
def config_file(name)
File.join(File.dirname(__FILE__), 'builder', name)
end
it "raises if parses commented options" do
proc do
Rack::Builder.parse_file config_file('options.ru')
end.must_raise(RuntimeError).
message.must_include('Parsing options from the first comment line is no longer supported')
end
it "removes __END__ before evaluating app" do
app, _ = Rack::Builder.parse_file config_file('end.ru')
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'OK'
end
it "supports multi-line comments" do
app = Rack::Builder.parse_file(config_file('comment.ru'))
app.must_be_kind_of(Proc)
end
it 'requires an_underscore_app not ending in .ru' do
$: << File.dirname(__FILE__)
app, * = Rack::Builder.parse_file 'builder/an_underscore_app'
Rack::MockRequest.new(app).get('/').body.to_s.must_equal 'OK'
$:.pop
end
it "sets __LINE__ correctly" do
app, _ = Rack::Builder.parse_file config_file('line.ru')
Rack::MockRequest.new(app).get("/").body.to_s.must_equal '3'
end
it "strips leading unicode byte order mark when present" do
enc = Encoding.default_external
begin
verbose, $VERBOSE = $VERBOSE, nil
Encoding.default_external = 'UTF-8'
app, _ = Rack::Builder.parse_file config_file('bom.ru')
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'OK'
ensure
Encoding.default_external = enc
$VERBOSE = verbose
end
end
it "respects the frozen_string_literal magic comment" do
app, _ = Rack::Builder.parse_file(config_file('frozen.ru'))
response = Rack::MockRequest.new(app).get('/')
response.body.must_equal 'frozen'
body = response.instance_variable_get(:@body)
body.must_equal(['frozen'])
body[0].frozen?.must_equal true
end
end
describe 'new_from_string' do
it "builds a rack app from string" do
app, = Rack::Builder.new_from_string "run lambda{|env| [200, {'content-type' => 'text/plane'}, ['OK']] }"
Rack::MockRequest.new(app).get("/").body.to_s.must_equal 'OK'
end
end
end
|