Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 69 lines (60 sloc) 1.65 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 MiddlewareTest < Test::Unit::TestCase
4 setup do
af1ee6a @sr Get rid of Default
sr authored
5 @app = mock_app(Sinatra::Application) {
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
6 get '/*' do
d074e0c @rtomayko Fix constant resolution in specs under 1.9
rtomayko authored
7 response.headers['X-Tests'] = env['test.ran'].
8 map { |n| n.split('::').last }.
9 join(', ')
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
10 env['PATH_INFO']
11 end
12 }
13 end
14
15 class MockMiddleware < Struct.new(:app)
16 def call(env)
17 (env['test.ran'] ||= []) << self.class.to_s
18 app.call(env)
19 end
20 end
21
22 class UpcaseMiddleware < MockMiddleware
23 def call(env)
24 env['PATH_INFO'] = env['PATH_INFO'].upcase
25 super
26 end
27 end
28
29 it "is added with Sinatra::Application.use" do
30 @app.use UpcaseMiddleware
31 get '/hello-world'
72a6e01 @rtomayko use Test::Unit style assertions in Sinatra specs
rtomayko authored
32 assert ok?
33 assert_equal '/HELLO-WORLD', body
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
34 end
35
36 class DowncaseMiddleware < MockMiddleware
37 def call(env)
38 env['PATH_INFO'] = env['PATH_INFO'].downcase
39 super
40 end
41 end
42
045f93b @sr Convert tests to custom test/spec/mini [#87]
sr authored
43 it "runs in the order defined" do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
44 @app.use UpcaseMiddleware
45 @app.use DowncaseMiddleware
46 get '/Foo'
72a6e01 @rtomayko use Test::Unit style assertions in Sinatra specs
rtomayko authored
47 assert_equal "/foo", body
48 assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
49 end
50
045f93b @sr Convert tests to custom test/spec/mini [#87]
sr authored
51 it "resets the prebuilt pipeline when new middleware is added" do
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
52 @app.use UpcaseMiddleware
53 get '/Foo'
72a6e01 @rtomayko use Test::Unit style assertions in Sinatra specs
rtomayko authored
54 assert_equal "/FOO", body
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
55 @app.use DowncaseMiddleware
56 get '/Foo'
72a6e01 @rtomayko use Test::Unit style assertions in Sinatra specs
rtomayko authored
57 assert_equal '/foo', body
58 assert_equal "UpcaseMiddleware, DowncaseMiddleware", response['X-Tests']
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
59 end
d7eabb9 @rtomayko Fix middleware not running when app run as middleware [#161]
rtomayko authored
60
61 it "works when app is used as middleware" do
62 @app.use UpcaseMiddleware
63 @app = @app.new
64 get '/Foo'
65 assert_equal "/FOO", body
66 assert_equal "UpcaseMiddleware", response['X-Tests']
67 end
a734cf3 @rtomayko I knew I shoulda taken that left turn at Hoboken
rtomayko authored
68 end
Something went wrong with that request. Please try again.