forked from sinatra/sinatra
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbase_test.rb
160 lines (133 loc) · 4.43 KB
/
base_test.rb
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
require File.expand_path('../helper', __FILE__)
class BaseTest < Test::Unit::TestCase
def test_default
assert true
end
describe 'Sinatra::Base subclasses' do
class TestApp < Sinatra::Base
get '/' do
'Hello World'
end
end
it 'include Rack::Utils' do
assert TestApp.included_modules.include?(Rack::Utils)
end
it 'processes requests with #call' do
assert TestApp.respond_to?(:call)
request = Rack::MockRequest.new(TestApp)
response = request.get('/')
assert response.ok?
assert_equal 'Hello World', response.body
end
class TestApp < Sinatra::Base
get '/state' do
@foo ||= "new"
body = "Foo: #{@foo}"
@foo = 'discard'
body
end
end
it 'does not maintain state between requests' do
request = Rack::MockRequest.new(TestApp)
2.times do
response = request.get('/state')
assert response.ok?
assert_equal 'Foo: new', response.body
end
end
it "passes the subclass to configure blocks" do
ref = nil
TestApp.configure { |app| ref = app }
assert_equal TestApp, ref
end
it "allows the configure block arg to be omitted and does not change context" do
context = nil
TestApp.configure { context = self }
assert_equal self, context
end
end
describe "Sinatra::Base as Rack middleware" do
app = lambda { |env|
headers = {'X-Downstream' => 'true'}
headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
[210, headers, ['Hello from downstream']] }
class TestMiddleware < Sinatra::Base
end
it 'creates a middleware that responds to #call with .new' do
middleware = TestMiddleware.new(app)
assert middleware.respond_to?(:call)
end
it 'exposes the downstream app' do
middleware = TestMiddleware.new!(app)
assert_same app, middleware.app
end
class TestMiddleware < Sinatra::Base
def route_missing
env['sinatra.route-missing'] = '1'
super
end
get '/' do
'Hello from middleware'
end
end
middleware = TestMiddleware.new(app)
request = Rack::MockRequest.new(middleware)
it 'intercepts requests' do
response = request.get('/')
assert response.ok?
assert_equal 'Hello from middleware', response.body
end
it 'automatically forwards requests downstream when no matching route found' do
response = request.get('/missing')
assert_equal 210, response.status
assert_equal 'Hello from downstream', response.body
end
it 'calls #route_missing before forwarding downstream' do
response = request.get('/missing')
assert_equal '1', response['X-Route-Missing']
end
class TestMiddleware < Sinatra::Base
get '/low-level-forward' do
app.call(env)
end
end
it 'can call the downstream app directly and return result' do
response = request.get('/low-level-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello from downstream', response.body
end
class TestMiddleware < Sinatra::Base
get '/explicit-forward' do
response['X-Middleware'] = 'true'
res = forward
assert_nil res
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal ['Hello from downstream'], response.body
'Hello after explicit forward'
end
end
it 'forwards the request downstream and integrates the response into the current context' do
response = request.get('/explicit-forward')
assert_equal 210, response.status
assert_equal 'true', response['X-Downstream']
assert_equal 'Hello after explicit forward', response.body
assert_equal '28', response['Content-Length']
end
app_content_length = lambda {|env|
[200, {'Content-Length' => '16'}, 'From downstream!']}
class TestMiddlewareContentLength < Sinatra::Base
get '/forward' do
res = forward
'From after explicit forward!'
end
end
middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
request_content_length = Rack::MockRequest.new(middleware_content_length)
it "sets content length for last response" do
response = request_content_length.get('/forward')
assert_equal '28', response['Content-Length']
end
end
end