Skip to content

Commit

Permalink
Decode gzip'd responses like Ruby 1.9
Browse files Browse the repository at this point in the history
Thanks to carsonmcdonald for the inspiration
Closes jnunemakergh-40
  • Loading branch information
sandro committed Jun 12, 2010
1 parent d950d38 commit ad6b06d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
7 changes: 7 additions & 0 deletions features/handles_compressed_responses.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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>'
20 changes: 20 additions & 0 deletions features/steps/mongrel_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,26 @@ def process(request, response)
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"'
Expand Down
4 changes: 4 additions & 0 deletions features/steps/remote_service_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
@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
Expand Down

0 comments on commit ad6b06d

Please sign in to comment.