Skip to content

Commit

Permalink
Merge pull request #150 from thunderboltlabs/master
Browse files Browse the repository at this point in the history
Fix #to_rack to handle non-array response bodies.
  • Loading branch information
bblimke committed Jan 22, 2012
2 parents beb8c65 + 146acef commit d0c58e9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/webmock/rack_response.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ def evaluate(request)
status, headers, response = @app.call(env) status, headers, response = @app.call(env)


Response.new( Response.new(
:body => response.join, :body => body_from_rack_response(response),
:headers => headers, :headers => headers,
:status => status :status => status
) )
end end


def body_from_rack_response(response)
body = ""
response.each { |line| body << line }
return body
end

def build_rack_env(request) def build_rack_env(request)
uri = request.uri uri = request.uri
headers = request.headers || {} headers = request.headers || {}
Expand Down
15 changes: 14 additions & 1 deletion spec/support/my_rack_app.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,18 +1,31 @@
require 'rack' require 'rack'


class MyRackApp class MyRackApp
class NonArrayResponse
# The rack response body need not implement #join,
# but it must implement #each. It need not be an Array.
# ActionDispatch::Response, for example, exercises that fact.
# See: http://rack.rubyforge.org/doc/SPEC.html

def each(*args, &blk)
["This is not in an array!"].each(*args, &blk)
end
end

def self.call(env) def self.call(env)
case env.values_at('REQUEST_METHOD', 'PATH_INFO') case env.values_at('REQUEST_METHOD', 'PATH_INFO')
when ['GET', '/'] when ['GET', '/']
[200, {}, ["This is my root!"]] [200, {}, ["This is my root!"]]
when ['GET', '/greet'] when ['GET', '/greet']
name = env['QUERY_STRING'][/name=([^&]*)/, 1] || "World" name = env['QUERY_STRING'][/name=([^&]*)/, 1] || "World"
[200, {}, ["Hello, #{name}"]] [200, {}, ["Hello, #{name}"]]
when ['GET', '/non_array_response']
[200, {}, NonArrayResponse.new]
when ['POST', '/greet'] when ['POST', '/greet']
name = env["rack.input"].read[/name=([^&]*)/, 1] || "World" name = env["rack.input"].read[/name=([^&]*)/, 1] || "World"
[200, {}, ["Good to meet you, #{name}!"]] [200, {}, ["Good to meet you, #{name}!"]]
else else
[404, {}, ['']] [404, {}, ['']]
end end
end end
end end
8 changes: 8 additions & 0 deletions spec/unit/rack_response_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@
response.body.should include('This is my root!') response.body.should include('This is my root!')
end end


it "should behave correctly when the rack response is not a simple array of strings" do
request = WebMock::RequestSignature.new(:get, 'www.example.com/non_array_response')
response = @rack_response.evaluate(request)

response.status.first.should == 200
response.body.should include('This is not in an array!')
end

it "should send along params" do it "should send along params" do
request = WebMock::RequestSignature.new(:get, 'www.example.com/greet?name=Johnny') request = WebMock::RequestSignature.new(:get, 'www.example.com/greet?name=Johnny')


Expand Down

0 comments on commit d0c58e9

Please sign in to comment.