diff --git a/.gitignore b/.gitignore index 2b236dbd..2d7f9a47 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ Gemfile.lock misc gems/ vendor/ +.idea diff --git a/spec/client_spec.rb b/spec/client_spec.rb index f4daf778..f510b6b1 100644 --- a/spec/client_spec.rb +++ b/spec/client_spec.rb @@ -214,7 +214,7 @@ def failed(http=nil) } } end - + it "should set content-length to 0 on posts with empty bodies" do EventMachine.run { http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length_from_header').post @@ -362,18 +362,34 @@ def failed(http=nil) it "should detect gzip encoding" do EventMachine.run { - http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => { - "accept-encoding" => "gzip, compressed" - } + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => {"accept-encoding" => "gzip, compressed"} - http.errback { failed(http) } - http.callback { - http.response_header.status.should == 200 - http.response_header["CONTENT_ENCODING"].should == "gzip" - http.response.should == "compressed" + http.errback { failed(http) } + http.callback { + http.response_header.status.should == 200 + http.response_header["CONTENT_ENCODING"].should == "gzip" + http.response.should == "compressed" - EventMachine.stop + EventMachine.stop + } } + end + + it "should handle gzip responses larger than 2048 bytes" do + expected_response = Zlib::GzipReader.open(File.dirname(__FILE__) + "/fixtures/gzip-sample.gz") { |f| f.read } + + EventMachine.run { + + http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip-large').get :head => {"accept-encoding" => "gzip, compressed"} + + http.errback { failed(http) } + http.callback { + http.response_header.status.should == 200 + http.response_header["CONTENT_ENCODING"].should == "gzip" + http.response.should == expected_response + + EventMachine.stop + } } end diff --git a/spec/external_spec.rb b/spec/external_spec.rb index 156709d1..53cda1ac 100644 --- a/spec/external_spec.rb +++ b/spec/external_spec.rb @@ -96,6 +96,22 @@ } end + it "should detect gzip encoding" do + pending "need an endpoint which supports gzip" + EventMachine.run { + options = {:head => {"accept-encoding" => "gzip"}} + http = EventMachine::HttpRequest.new('https://stream.twitter.com/1/statuses/sample.json').get options + + http.errback { failed(http) } + http.callback { + http.response_header.status.should == 200 + http.response_header["CONTENT_ENCODING"].should == "gzip" + + EventMachine.stop + } + } + end + context "keepalive" do it "should default to non-keepalive" do EventMachine.run { diff --git a/spec/fixtures/gzip-sample.gz b/spec/fixtures/gzip-sample.gz new file mode 100644 index 00000000..a59aa595 Binary files /dev/null and b/spec/fixtures/gzip-sample.gz differ diff --git a/spec/stallion.rb b/spec/stallion.rb index 3fc25672..92fbdc4a 100644 --- a/spec/stallion.rb +++ b/spec/stallion.rb @@ -90,7 +90,7 @@ def self.call(env) elsif stable.request.path_info == '/echo_content_length_from_header' stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}" - + elsif stable.request.head? && stable.request.path_info == '/' stable.response.status = 200 @@ -180,6 +180,12 @@ def self.call(env) stable.response.write io.string stable.response["Content-Encoding"] = "gzip" + elsif stable.request.path_info == '/gzip-large' + contents = File.open(File.dirname(__FILE__) + "/fixtures/gzip-sample.gz", 'r') { |f| f.read } + + stable.response.write contents + stable.response["Content-Encoding"] = "gzip" + elsif stable.request.path_info == '/deflate' deflater = Zlib::Deflate.new( Zlib::DEFAULT_COMPRESSION,