Skip to content

Commit

Permalink
Add HttpStreamingResponse patch to support webmock and solve #1
Browse files Browse the repository at this point in the history
  • Loading branch information
pex committed Sep 15, 2014
1 parent 67ed824 commit cab77aa
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require 'rspec'
require 'rack/test'
require 'webmock/rspec'
# Patch HttpStreamingResponse to make rack-proxy compatible with webmocks
require 'support/http_streaming_response_patch'

$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH << File.join(File.dirname(__FILE__))
Expand Down
32 changes: 32 additions & 0 deletions spec/support/http_streaming_response_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
##
# Patch HttpStreamingResponse
# in order to support webmocks and still use rack-proxy
#
# Inspired by @ehlertij commits on sportngin/rack-proxy:
# 616574e452fa731f5427d2ff2aff6823fcf28bde
# d8c377f7485997b229ced23c33cfef87d3fb8693
# 75b446a26ceb519ddc28f38b33309e9a2799074c
#
module Rack
class HttpStreamingResponse
def each(&block)
response.read_body(&block)
ensure
session.end_request_hacked unless mocking?
end

protected

def response
if mocking?
@response ||= session.request(@request)
else
super
end
end

def mocking?
defined?(WebMock) || defined?(FakeWeb)
end
end
end

0 comments on commit cab77aa

Please sign in to comment.