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(host = "127.0.0.1") ⇒ 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 70 71 |
# File 'lib/miniproxy/remote.rb', line 23 def self.server(host = "127.0.0.1") @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( MiniProxyHost: host, 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( MiniProxyHost: host, 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
105 106 107 |
# File 'lib/miniproxy/remote.rb', line 105 def clear @stubs.clear end |
#drain_messages ⇒ Object
109 110 111 |
# File 'lib/miniproxy/remote.rb', line 109 def @messages.slice!(0, @messages.length) end |
#handler(req, res) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/miniproxy/remote.rb', line 73 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
93 94 95 |
# File 'lib/miniproxy/remote.rb', line 93 def port @port end |
#port=(value) ⇒ Object
97 98 99 |
# File 'lib/miniproxy/remote.rb', line 97 def port=(value) @port = value end |
#started? ⇒ Boolean
113 114 115 116 |
# File 'lib/miniproxy/remote.rb', line 113 def started? current_server = DRb.current_server() current_server && current_server.alive? end |
#stop ⇒ Object
101 102 103 |
# File 'lib/miniproxy/remote.rb', line 101 def stop DRb.stop_service end |
#stub_request(method:, url:, response:) ⇒ Object
87 88 89 90 91 |
# File 'lib/miniproxy/remote.rb', line 87 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 |