Class: MiniProxy::Remote
- Inherits:
-
Object
- Object
- MiniProxy::Remote
- Defined in:
- lib/miniproxy/remote.rb
Overview
Controls the remote DRb service, which provides a communcation mechanism
Constant Summary collapse
- SERVER_DYNAMIC_PORT_RANGE =
(12345..32768).to_a.freeze
- SERVER_START_TIMEOUT =
10
Class Method Summary collapse
Instance Method Summary collapse
- #clear ⇒ Object
- #drain_messages ⇒ Object
- #handler(req, res) ⇒ Object
- #port ⇒ Object
- #port=(value) ⇒ Object
- #started? ⇒ Boolean
- #stop ⇒ Object
- #stub_request(method:, url:, response:) ⇒ Object
Class Method Details
.drb_process_alive? ⇒ Boolean
17 18 19 20 21 |
# File 'lib/miniproxy/remote.rb', line 17 def self.drb_process_alive? pid && Process.kill(0, pid) == 1 rescue Errno::ESRCH false end |
.pid ⇒ Object
13 14 15 |
# File 'lib/miniproxy/remote.rb', line 13 def self.pid @pid end |
.server ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/miniproxy/remote.rb', line 23 def self.server @unix_socket_uri ||= begin tempfile = Tempfile.new("mini_proxy") socket_path = tempfile.path tempfile.close! "drbunix:///#{socket_path}" end return @unix_socket_uri if drb_process_alive? @pid = fork do remote = Remote.new Timeout.timeout(SERVER_START_TIMEOUT) do begin fake_server_port = SERVER_DYNAMIC_PORT_RANGE.sample fake_server = FakeSSLServer.new( Port: fake_server_port, MockHandlerCallback: remote.method(:handler), ) Thread.new { fake_server.start } rescue Errno::EADDRINUSE retry end begin remote.port = ENV["MINI_PROXY_PORT"] || SERVER_DYNAMIC_PORT_RANGE.sample proxy = MiniProxy::ProxyServer.new( Port: remote.port, FakeServerPort: fake_server_port, MockHandlerCallback: remote.method(:handler), ) Thread.new { proxy.start } rescue Errno::EADDRINUSE retry end end DRb.start_service(@unix_socket_uri, remote) DRb.thread.join Process.exit! end Process.detach(@pid) @unix_socket_uri end |
Instance Method Details
#clear ⇒ Object
103 104 105 |
# File 'lib/miniproxy/remote.rb', line 103 def clear @stubs.clear end |
#drain_messages ⇒ Object
107 108 109 |
# File 'lib/miniproxy/remote.rb', line 107 def .slice!(0, .length) end |
#handler(req, res) ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/miniproxy/remote.rb', line 71 def handler(req, res) if (request = @stubs.detect { |mock_request| mock_request.match?(req) }) response = request.response res.status = response.code response.headers.each { |key, value| res[key] = value } res.body = response.body else res.status = 200 res.body = "" "WARN: external request to #{req.host}#{req.path} not mocked" %Q{Stub with: MiniProxy.stub_request(method: "#{req.request_method}", url: "#{req.host}#{req.path}")} end end |
#port ⇒ Object
91 92 93 |
# File 'lib/miniproxy/remote.rb', line 91 def port @port end |
#port=(value) ⇒ Object
95 96 97 |
# File 'lib/miniproxy/remote.rb', line 95 def port=(value) @port = value end |
#started? ⇒ Boolean
111 112 113 114 |
# File 'lib/miniproxy/remote.rb', line 111 def started? current_server = DRb.current_server() current_server && current_server.alive? end |
#stop ⇒ Object
99 100 101 |
# File 'lib/miniproxy/remote.rb', line 99 def stop DRb.stop_service end |
#stub_request(method:, url:, response:) ⇒ Object
85 86 87 88 89 |
# File 'lib/miniproxy/remote.rb', line 85 def stub_request(method:, url:, response:) response = MiniProxy::Stub::Response.new(headers: response[:headers], body: response[:body]) request = MiniProxy::Stub::Request.new(method: method, url: url, response: response) @stubs.push(request) end |