1
0
Fork 0
mirror of https://github.com/jnunemaker/httparty synced 2023-03-27 23:23:07 -04:00

Decode gzip'd responses like Ruby 1.9

Thanks to carsonmcdonald for the inspiration
Closes gh-40
This commit is contained in:
Sandro Turriate 2010-06-12 00:41:11 -04:00
parent d950d38d3a
commit ad6b06d605
3 changed files with 31 additions and 0 deletions

View file

@ -10,3 +10,10 @@ Feature: Handles Compressed Responses
And that service is accessed at the path '/service.html'
When I call HTTParty#get with '/service.html'
Then the return value should match '<h1>Some HTML</h1>'
Scenario: Supports gzip encoding
Given a remote gzip service
And the response from the service has a body of '<h1>Some HTML</h1>'
And that service is accessed at the path '/service.html'
When I call HTTParty#get with '/service.html'
Then the return value should match '<h1>Some HTML</h1>'

View file

@ -31,6 +31,26 @@ class DeflateHandler < BasicMongrelHandler
end
end
class GzipHandler < BasicMongrelHandler
def process(request, response)
response.start do |head, body|
head['Content-Encoding'] = 'gzip'
body.write gzip(response_body)
end
end
protected
def gzip(string)
sio = StringIO.new('', 'r+')
gz = Zlib::GzipWriter.new sio
gz.write string
gz.finish
sio.rewind
sio.read
end
end
module BasicAuthentication
def self.extended(base)
base.custom_headers["WWW-Authenticate"] = 'Basic Realm="Super Secret Page"'

View file

@ -21,6 +21,10 @@ Given /^a remote deflate service$/ do
@handler = DeflateHandler.new
end
Given /^a remote gzip service$/ do
@handler = GzipHandler.new
end
Given /the response from the service has a Content-Type of '(.*)'/ do |content_type|
@handler.content_type = content_type
end