Class: MiniProxy::Remote

Inherits:
Object
  • Object
show all
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

Class Method Details

.drb_process_alive?Boolean

Returns:

  • (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

.pidObject



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

#clearObject



105
106
107
# File 'lib/miniproxy/remote.rb', line 105

def clear
  @stubs.clear
end

#drain_messagesObject



109
110
111
# File 'lib/miniproxy/remote.rb', line 109

def drain_messages
  @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 = ""
    queue_message "WARN: external request to #{req.host}#{req.path} not mocked"
    queue_message %Q{Stub with: MiniProxy.stub_request(method: "#{req.request_method}", url: "#{req.host}#{req.path}")}
  end
end

#portObject



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

Returns:

  • (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

#stopObject



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