spec/request_spec.rb in em-http-request-0.2.11 vs spec/request_spec.rb in em-http-request-0.2.12

- old
+ new

@@ -66,21 +66,41 @@ EventMachine.stop } } end - it "should accept optional host override" do - EventMachine.run { - http = EventMachine::HttpRequest.new('http://google.com:8080/').get :host => '127.0.0.1' + context "host override" do - http.errback { failed } - http.callback { - http.response_header.status.should == 200 - http.response.should match(/Hello/) - EventMachine.stop + it "should accept optional host" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://google.com:8080/').get :host => '127.0.0.1' + + http.errback { failed } + http.callback { + http.response_header.status.should == 200 + http.response.should match(/Hello/) + EventMachine.stop + } } - } + end + + it "should reset host on redirect" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect').get :redirects => 1, :host => '127.0.0.1' + + http.errback { failed } + 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:8080/gzip' + http.redirects.should == 1 + + EM.stop + } + } + end end it "should perform successfull GET with a URI passed as argument" do EventMachine.run { uri = URI.parse('http://127.0.0.1:8080/') @@ -244,11 +264,11 @@ it "should perform a streaming GET" do EventMachine.run { # digg.com uses chunked encoding - http = EventMachine::HttpRequest.new('http://digg.com/').get + http = EventMachine::HttpRequest.new('http://digg.com/news').get http.errback { failed } http.callback { http.response_header.status.should == 200 EventMachine.stop @@ -433,16 +453,15 @@ } } end it "should fail gracefully on an invalid host in Location header" do - pending "validate tld's?" EventMachine.run { http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/redirect/badhost').get :redirects => 1 http.callback { failed } http.errback { - http.error.should == 'Location header format error' + http.error.should == 'unable to resolve server address' EM.stop } } end end @@ -462,10 +481,53 @@ EventMachine.stop } } end + context "optional header callback" do + it "should optionally pass the response headers" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get + + http.errback { failed } + http.headers { |hash| + hash.should be_an_kind_of Hash + hash.should include 'CONNECTION' + hash.should include 'CONTENT_LENGTH' + } + + http.callback { + http.response_header.status.should == 200 + http.response.should match(/Hello/) + EventMachine.stop + } + } + end + + it "should allow to terminate current connection from header callback" do + EventMachine.run { + http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/').get + + http.callback { failed } + http.headers { |hash| + hash.should be_an_kind_of Hash + hash.should include 'CONNECTION' + hash.should include 'CONTENT_LENGTH' + + http.close('header callback terminated connection') + } + + http.errback { |e| + http.response_header.status.should == 200 + http.error.should == 'header callback terminated connection' + http.response.should == '' + EventMachine.stop + } + } + end + end + it "should optionally pass the deflate-encoded response body progressively" do EventMachine.run { body = '' http = EventMachine::HttpRequest.new('http://127.0.0.1:8080/deflate').get :head => {"accept-encoding" => "deflate, compressed"} @@ -655,11 +717,11 @@ EventMachine.run { http = EventMachine::MockHttpRequest.new('http://www.google.ca/').get http.errback { fail } http.callback { - c1 = "PREF=ID=9454187d21c4a6a6:TM=1258403955:LM=1258403955:S=2-mf1n5oV5yAeT9-; expires=Wed, 16-Nov-2011 20:39:15 GMT; path=/; domain=.google.ca" - c2 = "NID=28=lvxxVdiBQkCetu_WFaUxLyB7qPlHXS5OdAGYTqge_laVlCKVN8VYYeVBh4bNZiK_Oan2gm8oP9GA-FrZfMPC3ZMHeNq37MG2JH8AIW9LYucU8brOeuggMEbLNNXuiWg4; expires=Tue, 18-May-2010 20:39:15 GMT; path=/; domain=.google.ca; HttpOnly" + c1 = "PREF=ID=11955ae9690fd292:TM=1281823106:LM=1281823106:S=wHdloFqGQ_OLSE92; expires=Mon, 13-Aug-2012 21:58:26 GMT; path=/; domain=.google.ca" + c2 = "NID=37=USTdOsxOSMbLjphkJ3S5Ueua3Yc23COXuK_pbztcHx7JoyhomwQySrvebCf3_u8eyrBiLWssVzaZcEOiKGEJbNdy8lRhnq_mfrdz693LaMjNPh__ccW4sgn1ZO6nQltE; expires=Sun, 13-Feb-2011 21:58:26 GMT; path=/; domain=.google.ca; HttpOnly" http.response_header.cookie.should == [c1, c2] EventMachine.stop } }