Skip to content

Commit cab77aa

Browse files
committed
Add HttpStreamingResponse patch to support webmock and solve #1
1 parent 67ed824 commit cab77aa

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

spec/spec_helper.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
require 'rspec'
44
require 'rack/test'
55
require 'webmock/rspec'
6+
# Patch HttpStreamingResponse to make rack-proxy compatible with webmocks
7+
require 'support/http_streaming_response_patch'
68

79
$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
810
$LOAD_PATH << File.join(File.dirname(__FILE__))
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
##
2+
# Patch HttpStreamingResponse
3+
# in order to support webmocks and still use rack-proxy
4+
#
5+
# Inspired by @ehlertij commits on sportngin/rack-proxy:
6+
# 616574e452fa731f5427d2ff2aff6823fcf28bde
7+
# d8c377f7485997b229ced23c33cfef87d3fb8693
8+
# 75b446a26ceb519ddc28f38b33309e9a2799074c
9+
#
10+
module Rack
11+
class HttpStreamingResponse
12+
def each(&block)
13+
response.read_body(&block)
14+
ensure
15+
session.end_request_hacked unless mocking?
16+
end
17+
18+
protected
19+
20+
def response
21+
if mocking?
22+
@response ||= session.request(@request)
23+
else
24+
super
25+
end
26+
end
27+
28+
def mocking?
29+
defined?(WebMock) || defined?(FakeWeb)
30+
end
31+
end
32+
end

0 commit comments

Comments
 (0)