Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 161 lines (133 sloc) 4.53 kb
c00a25e @rtomayko Test framework refactoring
rtomayko authored
1 require File.dirname(__FILE__) + '/helper'
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
2
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
3 class BaseTest < Test::Unit::TestCase
da37a88 @halorgium Less warnings to sate zenspider's needs
halorgium authored
4 def test_default
5 assert true
6 end
7
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
8 describe 'Sinatra::Base subclasses' do
9 class TestApp < Sinatra::Base
10 get '/' do
11 'Hello World'
12 end
13 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
14
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
15 it 'include Rack::Utils' do
16 assert TestApp.included_modules.include?(Rack::Utils)
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
17 end
18
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
19 it 'processes requests with #call' do
20 assert TestApp.respond_to?(:call)
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
21
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
22 request = Rack::MockRequest.new(TestApp)
23 response = request.get('/')
24 assert response.ok?
25 assert_equal 'Hello World', response.body
26 end
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
27
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
28 class TestApp < Sinatra::Base
29 get '/state' do
da37a88 @halorgium Less warnings to sate zenspider's needs
halorgium authored
30 @foo ||= "new"
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
31 body = "Foo: #{@foo}"
32 @foo = 'discard'
33 body
34 end
35 end
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
36
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
37 it 'does not maintain state between requests' do
38 request = Rack::MockRequest.new(TestApp)
39 2.times do
40 response = request.get('/state')
41 assert response.ok?
da37a88 @halorgium Less warnings to sate zenspider's needs
halorgium authored
42 assert_equal 'Foo: new', response.body
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
43 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
44 end
54794fc @tj Pass the class to configure blocks
tj authored
45
46 it "passes the subclass to configure blocks" do
47 ref = nil
48 TestApp.configure { |app| ref = app }
49 assert_equal TestApp, ref
50 end
51
52 it "allows the configure block arg to be omitted and does not change context" do
53 context = nil
54 TestApp.configure { context = self }
55 assert_equal self, context
56 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
57 end
58
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
59 describe "Sinatra::Base as Rack middleware" do
60 app = lambda { |env|
61 headers = {'X-Downstream' => 'true'}
62 headers['X-Route-Missing'] = env['sinatra.route-missing'] || ''
63 [210, headers, ['Hello from downstream']] }
64
65 class TestMiddleware < Sinatra::Base
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
66 end
67
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
68 it 'creates a middleware that responds to #call with .new' do
69 middleware = TestMiddleware.new(app)
70 assert middleware.respond_to?(:call)
71 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
72
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
73 it 'exposes the downstream app' do
1d9034f @rkh use new! rather than new for tests testing instance method
rkh authored
74 middleware = TestMiddleware.new!(app)
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
75 assert_same app, middleware.app
76 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
77
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
78 class TestMiddleware < Sinatra::Base
79 def route_missing
80 env['sinatra.route-missing'] = '1'
81 super
82 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
83
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
84 get '/' do
85 'Hello from middleware'
86 end
87 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
88
89 middleware = TestMiddleware.new(app)
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
90 request = Rack::MockRequest.new(middleware)
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
91
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
92 it 'intercepts requests' do
93 response = request.get('/')
94 assert response.ok?
95 assert_equal 'Hello from middleware', response.body
0e6becf @jcrosby Add #bypassed callback before calling forward
jcrosby authored
96 end
97
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
98 it 'automatically forwards requests downstream when no matching route found' do
99 response = request.get('/missing')
100 assert_equal 210, response.status
101 assert_equal 'Hello from downstream', response.body
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
102 end
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
103
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
104 it 'calls #route_missing before forwarding downstream' do
105 response = request.get('/missing')
106 assert_equal '1', response['X-Route-Missing']
107 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
108
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
109 class TestMiddleware < Sinatra::Base
110 get '/low-level-forward' do
111 app.call(env)
112 end
113 end
0e6becf @jcrosby Add #bypassed callback before calling forward
jcrosby authored
114
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
115 it 'can call the downstream app directly and return result' do
116 response = request.get('/low-level-forward')
117 assert_equal 210, response.status
118 assert_equal 'true', response['X-Downstream']
119 assert_equal 'Hello from downstream', response.body
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
120 end
121
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
122 class TestMiddleware < Sinatra::Base
123 get '/explicit-forward' do
124 response['X-Middleware'] = 'true'
125 res = forward
126 assert_nil res
127 assert_equal 210, response.status
128 assert_equal 'true', response['X-Downstream']
129 assert_equal ['Hello from downstream'], response.body
130 'Hello after explicit forward'
131 end
132 end
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
133
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
134 it 'forwards the request downstream and integrates the response into the current context' do
135 response = request.get('/explicit-forward')
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
136 assert_equal 210, response.status
137 assert_equal 'true', response['X-Downstream']
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
138 assert_equal 'Hello after explicit forward', response.body
139 assert_equal '28', response['Content-Length']
cf32f2e @rtomayko Middleware enhancements: automatic and explicit forward to downstream [#...
rtomayko authored
140 end
141
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
142 app_content_length = lambda {|env|
143 [200, {'Content-Length' => '16'}, 'From downstream!']}
7196ea4 @vangberg Fix issues with Content-Length after explicit forward [#159]
vangberg authored
144
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
145 class TestMiddlewareContentLength < Sinatra::Base
146 get '/forward' do
147 res = forward
148 'From after explicit forward!'
149 end
7196ea4 @vangberg Fix issues with Content-Length after explicit forward [#159]
vangberg authored
150 end
151
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
152 middleware_content_length = TestMiddlewareContentLength.new(app_content_length)
153 request_content_length = Rack::MockRequest.new(middleware_content_length)
7196ea4 @vangberg Fix issues with Content-Length after explicit forward [#159]
vangberg authored
154
ff0d068 @sr Use contest instead of test/spec/mini
sr authored
155 it "sets content length for last response" do
156 response = request_content_length.get('/forward')
157 assert_equal '28', response['Content-Length']
158 end
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
159 end
160 end
Something went wrong with that request. Please try again.