forked from igrigorik/em-http-request
/
redirect_spec.rb
203 lines (176 loc) · 6.45 KB
/
redirect_spec.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
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
require 'helper'
class RedirectMiddleware
class << self
attr_accessor :call_count
end
RedirectMiddleware.call_count = 0
def request(c, h, r)
RedirectMiddleware.call_count += 1
h['EM-Middleware'] = RedirectMiddleware.call_count.to_s
[h, r]
end
def response(r)
RedirectMiddleware.call_count = (r.response_header['EM_MIDDLEWARE'].to_i + 1)
end
end
class PickyRedirectMiddleware < RedirectMiddleware
def response(r)
raise EventMachine::InvalidRedirectError if r.state == :redirecting && r.response_header['LOCATION'][-1] == '3'
super
end
end
describe EventMachine::HttpRequest do
it "should follow location redirects" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect').get :redirects => 1
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header["CONTENT_ENCODING"].should == "gzip"
http.response.should == "compressed"
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
http.redirects.should == 1
EM.stop
}
}
end
it "should redirect with missing content-length" do
EventMachine.run {
@s = StubServer.new("HTTP/1.0 301 MOVED PERMANENTLY\r\nlocation: http://127.0.0.1:8090/redirect\r\n\r\n")
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/').get :redirects => 3
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header["CONTENT_ENCODING"].should == "gzip"
http.response.should == "compressed"
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
http.redirects.should == 3
@s.stop
EM.stop
}
}
end
it "should follow redirects on HEAD method" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/head').head :redirects => 1
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/'
EM.stop
}
}
end
it "should report last_effective_url" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/').get
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/'
EM.stop
}
}
end
it "should default to 0 redirects" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect').get
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 301
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/redirect'
http.redirects.should == 0
EM.stop
}
}
end
it "should not invoke redirect logic on failed(http) connections" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8081/', :connect_timeout => 0.1).get :redirects => 5
http.callback { failed(http) }
http.errback {
http.redirects.should == 0
EM.stop
}
}
end
it "should normalize redirect urls" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/bad').get :redirects => 1
http.errback { failed(http) }
http.callback {
http.last_effective_url.to_s.should match('http://127.0.0.1:8090/')
http.response.should match('Hello, World!')
EM.stop
}
}
end
it "should fail gracefully on a missing host in absolute Location header" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/nohost').get :redirects => 1
http.callback { failed(http) }
http.errback {
http.error.should == 'Location header format error'
EM.stop
}
}
end
it "should capture and pass cookies on redirect and pass_cookies by default" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/multiple-with-cookie').get :redirects => 2, :head => {'cookie' => 'id=2;'}
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header["CONTENT_ENCODING"].should == "gzip"
http.response.should == "compressed"
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
http.redirects.should == 2
http.cookies.should include("id=2;")
http.cookies.should include("another_id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;")
EM.stop
}
}
end
it "should capture and not pass cookies on redirect if passing is disabled via pass_cookies" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/multiple-with-cookie').get :redirects => 2, :pass_cookies => false, :head => {'cookie' => 'id=2;'}
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
http.response_header["CONTENT_ENCODING"].should == "gzip"
http.response.should == "compressed"
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
http.redirects.should == 2
http.cookies.should include("id=2;")
http.cookies.should_not include("another_id=1; expires=Tue, 09-Aug-2011 17:53:39 GMT; path=/;")
EM.stop
}
}
end
it "should call middleware each time it redirects" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/middleware_redirects_1')
conn.use RedirectMiddleware
http = conn.get :redirects => 3
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 200
RedirectMiddleware.call_count.should == 6
EM.stop
}
}
end
it "should call middleware which may reject a redirection" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/middleware_redirects_1')
conn.use PickyRedirectMiddleware
http = conn.get :redirects => 3
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 301
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/redirect/middleware_redirects_2'
EM.stop
}
}
end
end