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

.serverObject



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

#clearObject



103
104
105
# File 'lib/miniproxy/remote.rb', line 103

def clear
  @stubs.clear
end

#drain_messagesObject



107
108
109
# File 'lib/miniproxy/remote.rb', line 107

def drain_messages
  @messages.slice!(0, @messages.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 = ""
    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



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

Returns:

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

#stopObject



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