/
filter_test.rb
99 lines (85 loc) · 1.85 KB
/
filter_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
require File.dirname(__FILE__) + '/helper'
describe "Filters" do
it "executes filters in the order defined" do
count = 0
mock_app do
get('/') { 'Hello World' }
before {
assert_equal 0, count
count = 1
}
before {
assert_equal 1, count
count = 2
}
end
get '/'
assert ok?
assert_equal 2, count
assert_equal 'Hello World', body
end
it "allows filters to modify the request" do
mock_app {
get('/foo') { 'foo' }
get('/bar') { 'bar' }
before { request.path_info = '/bar' }
}
get '/foo'
assert ok?
assert_equal 'bar', body
end
it "can modify instance variables available to routes" do
mock_app {
before { @foo = 'bar' }
get('/foo') { @foo }
}
get '/foo'
assert ok?
assert_equal 'bar', body
end
it "allows redirects in filters" do
mock_app {
before { redirect '/bar' }
get('/foo') do
fail 'before block should have halted processing'
'ORLY?!'
end
}
get '/foo'
assert redirect?
assert_equal '/bar', response['Location']
assert_equal '', body
end
it "does not modify the response with its return value" do
mock_app {
before { 'Hello World!' }
get '/foo' do
assert_equal [], response.body
'cool'
end
}
get '/foo'
assert ok?
assert_equal 'cool', body
end
it "does modify the response with halt" do
mock_app {
before { halt 302, 'Hi' }
get '/foo' do
"should not happen"
end
}
get '/foo'
assert_equal 302, response.status
assert_equal 'Hi', body
end
it "gives you access to params" do
mock_app {
before { @foo = params['foo'] }
get('/foo') { @foo }
}
get '/foo?foo=cool'
assert ok?
assert_equal 'cool', body
end
end