Skip to content

Commit

Permalink
DRY'ing compression spec
Browse files Browse the repository at this point in the history
Minor refactoring of HTTPResponse extension
  • Loading branch information
John Pignata committed Nov 1, 2009
1 parent 715c7f1 commit d9c7053
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
3 changes: 1 addition & 2 deletions lib/extensions/http_success.rb
Expand Up @@ -5,8 +5,7 @@ module Net
class HTTPSuccess

def body
case self['content-encoding']
when 'gzip'
if self['content-encoding'] == 'gzip'
body = StringIO.new(super)
uncompressed_body = Zlib::GzipReader.new(body)
uncompressed_body.read
Expand Down
31 changes: 19 additions & 12 deletions spec/compression_spec.rb
Expand Up @@ -2,22 +2,29 @@

describe "HTTPSuccess" do
describe ".body" do
it "returns a plain text body if no compression is used in the response" do
uri = URI.parse("http://www.example.com")
FakeWeb.register_uri(:any, uri, :body => "#{File.dirname(__FILE__)}/support/fakeweb/www.example.com")

request = Net::HTTP::Get.new(uri.request_uri)
response = Net::HTTP.new(uri.host).request(request)
def do_request
request = Net::HTTP::Get.new(@uri.request_uri)
response = Net::HTTP.new(@uri.host).request(request)
response.body.should match(/These domain names are reserved for use in documentation/)
end

it "returns a plain text body if gzip is used in the response" do
uri = URI.parse("http://www.example.com")
FakeWeb.register_uri(:any, uri, :body => "#{File.dirname(__FILE__)}/support/fakeweb/www.example.com.gz", 'content-encoding' => 'gzip')
before do
@uri = URI.parse("http://www.example.com")
end

request = Net::HTTP::Get.new(uri.request_uri)
response = Net::HTTP.new(uri.host).request(request)
response.body.should match(/These domain names are reserved for use in documentation/)
it "returns a plain text body if no compression is used in the response" do
options = { :body => "#{File.dirname(__FILE__)}/support/fakeweb/www.example.com" }
FakeWeb.register_uri(:any, @uri, options)
do_request
end

it "returns a plain text body if gzip is used in the response" do
options = {
:body => "#{File.dirname(__FILE__)}/support/fakeweb/www.example.com.gz",
'Content-Encoding' => :gzip
}
FakeWeb.register_uri(:any, @uri, options)
do_request
end
end
end

0 comments on commit d9c7053

Please sign in to comment.