Skip to content

Commit

Permalink
Adding tests for igrigorik/em-http-request issue #173
Browse files Browse the repository at this point in the history
  • Loading branch information
eriwen committed Apr 1, 2012
1 parent dd1ead8 commit a7eb84e
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -6,3 +6,4 @@ Gemfile.lock
misc misc
gems/ gems/
vendor/ vendor/
.idea
36 changes: 26 additions & 10 deletions spec/client_spec.rb
Expand Up @@ -214,7 +214,7 @@ def failed(http=nil)
} }
} }
end end

it "should set content-length to 0 on posts with empty bodies" do it "should set content-length to 0 on posts with empty bodies" do
EventMachine.run { EventMachine.run {
http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length_from_header').post http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo_content_length_from_header').post
Expand Down Expand Up @@ -362,18 +362,34 @@ def failed(http=nil)
it "should detect gzip encoding" do it "should detect gzip encoding" do
EventMachine.run { EventMachine.run {


http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => { http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/gzip').get :head => {"accept-encoding" => "gzip, compressed"}
"accept-encoding" => "gzip, compressed"
}


http.errback { failed(http) } http.errback { failed(http) }
http.callback { http.callback {
http.response_header.status.should == 200 http.response_header.status.should == 200
http.response_header["CONTENT_ENCODING"].should == "gzip" http.response_header["CONTENT_ENCODING"].should == "gzip"
http.response.should == "compressed" 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 end


Expand Down
16 changes: 16 additions & 0 deletions spec/external_spec.rb
Expand Up @@ -96,6 +96,22 @@
} }
end 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 context "keepalive" do
it "should default to non-keepalive" do it "should default to non-keepalive" do
EventMachine.run { EventMachine.run {
Expand Down
Binary file added spec/fixtures/gzip-sample.gz
Binary file not shown.
8 changes: 7 additions & 1 deletion spec/stallion.rb
Expand Up @@ -90,7 +90,7 @@ def self.call(env)


elsif stable.request.path_info == '/echo_content_length_from_header' elsif stable.request.path_info == '/echo_content_length_from_header'
stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}" stable.response.write "content-length:#{stable.request.env["CONTENT_LENGTH"]}"

elsif stable.request.head? && stable.request.path_info == '/' elsif stable.request.head? && stable.request.path_info == '/'
stable.response.status = 200 stable.response.status = 200


Expand Down Expand Up @@ -180,6 +180,12 @@ def self.call(env)
stable.response.write io.string stable.response.write io.string
stable.response["Content-Encoding"] = "gzip" 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' elsif stable.request.path_info == '/deflate'
deflater = Zlib::Deflate.new( deflater = Zlib::Deflate.new(
Zlib::DEFAULT_COMPRESSION, Zlib::DEFAULT_COMPRESSION,
Expand Down

0 comments on commit a7eb84e

Please sign in to comment.