rack-proxy 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f9520dd879490c3e4e00d0617a74994fd178488878cf149f44acc4795975609e
4
- data.tar.gz: 99c9ea6a98cbae6713c03f4ac4a6b87cf682a9189c073f29ecd5845d3ddc5c7e
3
+ metadata.gz: e5728c44041f0dddc056d3ebc1b35e5429e3319115c07ade4e98278d02f5ffc2
4
+ data.tar.gz: 2d0a518195f43bb38557ab68c00c8bce11f234675fa1a4e1f242b3ce177d83f6
5
5
  SHA512:
6
- metadata.gz: f5b53e01fef046f99405006fcb5b60f053ede10fc23bb231ad27deebbe52593d1ad1fcd1ba77471dcb76405c65a9d7a3a685ed13caea6bdb1dbf927ad19ffffc
7
- data.tar.gz: 940b3f2ba7a1248475799497e757a9a9be8875e933ef47b0fdc583181d0368804c56d0bd5b356860c169e4bdd9e68f2cfd6b7c6f273b9d542fd5bdd93b6e7f46
6
+ metadata.gz: 353fc92d5a941f141f18f17d885506ff51098756687fcab816aa7e61fa5ace9e62a2658530c23d917f059d36db6585988f80d829a2818036f4f232d02436cf4e
7
+ data.tar.gz: 8874cf1e8ee86ad08db5b894ed9eb5e9975f1b43cf1c46e3b2422e720f57356b683a3c0782cd682435567506afeee690f90030e540b532a4c992b5724e9d820b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rack-proxy (0.8.0)
4
+ rack-proxy (0.8.1)
5
5
  rack
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -43,6 +43,7 @@ Options can be set when initializing the middleware or overriding a method.
43
43
  * `:ssl_version` - tell `Net::HTTP` to set a specific `ssl_version`
44
44
  * `:backend` - the URI parseable format of host and port of the target proxy backend. If not set it will assume the backend target is the same as the source.
45
45
  * `:read_timeout` - set proxy timeout it defaults to 60 seconds
46
+ * `:logger` - any object responding to `#<<` (e.g. `$stdout`, a `StringIO`, or a Ruby `Logger`). Wired through to `Net::HTTP#set_debug_output` so the full HTTP wire-level conversation is written to the sink. Useful for debugging.
46
47
 
47
48
  To pass in options, when you configure your middleware you can pass them in as an optional hash.
48
49
 
@@ -10,7 +10,7 @@ module Rack
10
10
  304 => true
11
11
  }.freeze
12
12
 
13
- attr_accessor :use_ssl, :verify_mode, :read_timeout, :ssl_version, :cert, :key
13
+ attr_accessor :use_ssl, :verify_mode, :read_timeout, :ssl_version, :cert, :key, :logger
14
14
 
15
15
  def initialize(request, host, port = nil)
16
16
  @request, @host, @port = request, host, port
@@ -61,6 +61,7 @@ module Rack
61
61
  http.ssl_version = ssl_version if ssl_version
62
62
  http.cert = cert if cert
63
63
  http.key = key if key
64
+ http.set_debug_output(logger) if logger
64
65
  http.start
65
66
  end
66
67
  end
data/lib/rack/proxy.rb CHANGED
@@ -5,7 +5,7 @@ module Rack
5
5
 
6
6
  # Subclass and bring your own #rewrite_request and #rewrite_response
7
7
  class Proxy
8
- VERSION = "0.8.0".freeze
8
+ VERSION = "0.8.1".freeze
9
9
 
10
10
  HOP_BY_HOP_HEADERS = {
11
11
  'connection' => true,
@@ -83,6 +83,10 @@ module Rack
83
83
  @username = opts[:username]
84
84
  @password = opts[:password]
85
85
 
86
+ # Optional logger for Net::HTTP debug output. Accepts anything with a #<< method
87
+ # (e.g. $stdout, a StringIO, or a Ruby Logger instance).
88
+ @logger = opts[:logger]
89
+
86
90
  @opts = opts
87
91
  end
88
92
 
@@ -143,6 +147,7 @@ module Rack
143
147
  target_response.verify_mode = (@verify_mode || OpenSSL::SSL::VERIFY_PEER) if use_ssl
144
148
  target_response.cert = @cert if @cert
145
149
  target_response.key = @key if @key
150
+ target_response.logger = @logger if @logger
146
151
  else
147
152
  http = Net::HTTP.new(backend.host, backend.port)
148
153
  http.use_ssl = use_ssl if use_ssl
@@ -151,6 +156,7 @@ module Rack
151
156
  http.verify_mode = @verify_mode || OpenSSL::SSL::VERIFY_PEER if use_ssl
152
157
  http.cert = @cert if @cert
153
158
  http.key = @key if @key
159
+ http.set_debug_output(@logger) if @logger
154
160
 
155
161
  target_response = http.start do
156
162
  http.request(target_request)
@@ -226,6 +226,41 @@ class RackProxyTest < Test::Unit::TestCase
226
226
  assert last_response.ok?
227
227
  end
228
228
 
229
+ # Issue #80: a :logger option should pipe Net::HTTP debug output to the
230
+ # given sink (anything responding to #<<). We use a StringIO to capture it.
231
+ def test_logger_captures_request_in_non_streaming
232
+ sink = StringIO.new
233
+ with_webrick_proxy(streaming: false, logger: sink) do |port, proxy|
234
+ proxy.host = "127.0.0.1:#{port}"
235
+ get '/empty'
236
+ assert last_response.ok?
237
+ end
238
+ assert_match(/GET \/empty/, sink.string,
239
+ "expected debug output to include request line, got: #{sink.string.inspect}")
240
+ end
241
+
242
+ def test_logger_captures_request_in_streaming
243
+ sink = StringIO.new
244
+ with_webrick_proxy(streaming: true, logger: sink) do |port, proxy|
245
+ proxy.host = "127.0.0.1:#{port}"
246
+ get '/empty'
247
+ assert last_response.ok?
248
+ end
249
+ assert_match(/GET \/empty/, sink.string,
250
+ "expected debug output to include request line, got: #{sink.string.inspect}")
251
+ end
252
+
253
+ def test_no_logger_means_no_debug_output
254
+ # Without a :logger option, Net::HTTP's set_debug_output should never be
255
+ # called. We can't directly assert that, but we can confirm requests still
256
+ # work when no logger is configured (covered by every other test).
257
+ with_webrick_proxy(streaming: false) do |port, proxy|
258
+ proxy.host = "127.0.0.1:#{port}"
259
+ get '/empty'
260
+ assert last_response.ok?
261
+ end
262
+ end
263
+
229
264
  private
230
265
 
231
266
  def assert_no_array_header_values(streaming:)
@@ -242,7 +277,7 @@ class RackProxyTest < Test::Unit::TestCase
242
277
 
243
278
  # Spin up a tiny WEBrick server with fixed routes so we can exercise the
244
279
  # proxy against real Net::HTTP requests without depending on a remote host.
245
- def with_webrick_proxy(streaming:)
280
+ def with_webrick_proxy(**proxy_opts)
246
281
  require 'webrick'
247
282
  server = WEBrick::HTTPServer.new(
248
283
  Port: 0,
@@ -260,7 +295,7 @@ class RackProxyTest < Test::Unit::TestCase
260
295
  Thread.new { server.start }
261
296
  port = server.config[:Port]
262
297
 
263
- proxy = HostProxy.new(streaming: streaming)
298
+ proxy = HostProxy.new(**proxy_opts)
264
299
  @app = proxy
265
300
  yield port, proxy
266
301
  ensure
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-proxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jacek Becela