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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
|
require 'helper'
class RedirectMiddleware
attr_reader :call_count
def initialize
@call_count = 0
end
def request(c, h, r)
@call_count += 1
[h.merge({'EM-Middleware' => @call_count.to_s}), r]
end
end
class PickyRedirectMiddleware < RedirectMiddleware
def response(r)
if r.redirect? && r.response_header['LOCATION'][-1].chr == '3'
# set redirects to 0 to avoid further processing
r.req.redirects = 0
end
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 not follow redirects on created" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/created').get :redirects => 1
http.errback { failed(http) }
http.callback {
http.response_header.status.should == 201
http.response.should match(/Hello/)
EM.stop
}
}
end
it "should not forward cookies across domains with http redirect" do
expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
response =<<-HTTP.gsub(/^ +/, '')
HTTP/1.1 301 MOVED PERMANENTLY
Location: http://localhost:8071/
Set-Cookie: foo=bar; expires=#{expires}; path=/; HttpOnly
HTTP
EventMachine.run do
@stub = StubServer.new(:host => '127.0.0.1', :port => 8070, :response => response)
@echo = StubServer.new(:host => 'localhost', :port => 8071, :echo => true)
http = EventMachine::HttpRequest.new('http://127.0.0.1:8070/').get :redirects => 1
http.errback { failed(http) }
http.callback do
http.response.should_not match(/Cookie/)
@stub.stop
@echo.stop
EM.stop
end
end
end
it "should forward valid cookies across domains with http redirect" do
expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
response =<<-HTTP.gsub(/^ +/, '')
HTTP/1.1 301 MOVED PERMANENTLY
Location: http://127.0.0.1:8071/
Set-Cookie: foo=bar; expires=#{expires}; path=/; HttpOnly
HTTP
EventMachine.run do
@stub = StubServer.new(:host => '127.0.0.1', :port => 8070, :response => response)
@echo = StubServer.new(:host => '127.0.0.1', :port => 8071, :echo => true)
http = EventMachine::HttpRequest.new('http://127.0.0.1:8070/').get :redirects => 1
http.errback { failed(http) }
http.callback do
http.response.should match(/Cookie/)
@stub.stop
@echo.stop
EM.stop
end
end
end
it "should normalize path and forward valid cookies across domains" do
expires = (Date.today + 2).strftime('%a, %d %b %Y %T GMT')
response =<<-HTTP.gsub(/^ +/, '')
HTTP/1.1 301 MOVED PERMANENTLY
Location: http://127.0.0.1:8071?omg=ponies
Set-Cookie: foo=bar; expires=#{expires}; path=/; HttpOnly
HTTP
EventMachine.run do
@stub = StubServer.new(:host => '127.0.0.1', :port => 8070, :response => response)
@echo = StubServer.new(:host => '127.0.0.1', :port => 8071, :echo => true)
http = EventMachine::HttpRequest.new('http://127.0.0.1:8070/').get :redirects => 1
http.errback { failed(http) }
http.callback do
http.response.should match(/Cookie/)
@stub.stop
@echo.stop
EM.stop
end
end
end
it "should redirect with missing content-length" do
EventMachine.run {
response = "HTTP/1.0 301 MOVED PERMANENTLY\r\nlocation: http://127.0.0.1:8090/redirect\r\n\r\n"
@stub = StubServer.new(:host => '127.0.0.1', :port => 8070, :response => response)
http = EventMachine::HttpRequest.new('http://127.0.0.1:8070/').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
@stub.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:8070/', :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 apply timeout settings on redirects" do
EventMachine.run {
t = Time.now.to_i
EventMachine.heartbeat_interval = 0.1
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/timeout', :inactivity_timeout => 0.1)
http = conn.get :redirects => 1
http.callback { failed(http) }
http.errback {
(Time.now.to_i - t).should <= 1
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")
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=Sat, 09 Aug 2031 17:53:39 GMT; path=/;")
EM.stop
}
}
end
it "should follow location redirects with path" do
EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect').get :path => '/redirect', :redirects => 1
http.errback { failed(http) }
http.callback {
http.last_effective_url.to_s.should == 'http://127.0.0.1:8090/gzip'
http.response_header.status.should == 200
http.redirects.should == 1
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
http.response_header['EM_MIDDLEWARE'].to_i.should == 3
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
it "should not add default http port to redirect url that don't include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_no_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'http://host/'
EM.stop
}
}
end
it "should not add default https port to redirect url that don't include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_no_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'https://host/'
EM.stop
}
}
end
it "should keep default http port in redirect url that include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_with_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'http://host/'
EM.stop
}
}
end
it "should keep default https port in redirect url that include it" do
EventMachine.run {
conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_with_port')
http = conn.get :redirects => 1
http.errback {
http.last_effective_url.to_s.should == 'https://host/'
EM.stop
}
}
end
end
|