miniproxy 0.2.1 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea9741957bdc4bef7bb92c6578011c272b3d0008b6e709a6f28a353fe6e895cb
4
- data.tar.gz: 233289bb7192cde0f0a103309f29161ff49fcce40aa3c7e1b8acda6a98cb5d0a
3
+ metadata.gz: 3b7f9d3ebcc5ca86f33c600718e26b87fe70dd833c0b569784b85e9d511f1a19
4
+ data.tar.gz: b90490d44b05ab40035d4d908c308ed3dbd5dd161556ff796a94474f1de737c7
5
5
  SHA512:
6
- metadata.gz: e0319daa2cd385c9a714b287e3dceff33a27a32feabe905e9661a968d75dc301b31ed1ab3107e5215edcee06ec9ed13ce37f5dbc57feaa71989dc4379c12dc9c
7
- data.tar.gz: b810589825cc2a36ce302cfe115c04b78f242239c58cafc4d62a600bb9297f9c04b5e92537d7192bf9a23d8f8f2227c93c0f3f62e21b3e538de02fde4e15749a
6
+ metadata.gz: f79ee6a1dc87e492ac2bcbd1799d339c3cbe809f13e78c011ecc4db78948cd93e0871449999c944756c6ba9ff2835892c4646780e81aa9c916ac0e89361a98cb
7
+ data.tar.gz: 31af7de678cc5cc577c93d847e08773e4c440fd143190f13d421c2590a3ec43fe47fd6a5d016ee635ba12ac57f4cf5bd77ad91e0f55aeba28a313fa3c398d198
data/lib/miniproxy.rb CHANGED
@@ -18,7 +18,11 @@ module MiniProxy
18
18
  end
19
19
 
20
20
  def self.host
21
- "127.0.0.1"
21
+ @host || "127.0.0.1"
22
+ end
23
+
24
+ def self.host=(host)
25
+ @host = host
22
26
  end
23
27
 
24
28
  def self.ignore_all_requests
@@ -33,10 +37,14 @@ module MiniProxy
33
37
  remote.stub_request(method: method, url: url, response: response)
34
38
  end
35
39
 
40
+ def self.allow_request(method:, url:)
41
+ remote.allow_request(method: method, url: url)
42
+ end
43
+
36
44
  private_class_method def self.remote
37
45
  Timeout.timeout(DRB_SERVICE_TIMEOUT) do
38
46
  begin
39
- remote = DRbObject.new(nil, Remote.server)
47
+ remote = DRbObject.new(nil, Remote.server(self.host))
40
48
 
41
49
  until remote.started?
42
50
  sleep 0.01
@@ -5,8 +5,6 @@ module MiniProxy
5
5
  # MiniProxy fake SSL enabled server, which receives relayed requests from the ProxyServer
6
6
  #
7
7
  class FakeSSLServer < WEBrick::HTTPServer
8
- ALLOWED_HOSTS = ["127.0.0.1", "localhost"].freeze
9
-
10
8
  def initialize(config = {}, default = WEBrick::Config::HTTP)
11
9
  config = config.merge({
12
10
  Logger: WEBrick::Log.new(nil, 0), # silence logging
@@ -21,7 +19,7 @@ module MiniProxy
21
19
  end
22
20
 
23
21
  def service(req, res)
24
- if ALLOWED_HOSTS.include?(req.host)
22
+ if self.config[:AllowedRequestCheck].call(req)
25
23
  super(req, res)
26
24
  else
27
25
  self.config[:MockHandlerCallback].call(req, res)
@@ -4,8 +4,6 @@ module MiniProxy
4
4
  # MiniProxy server, which boots a WEBrick proxy server
5
5
  #
6
6
  class ProxyServer < WEBrick::HTTPProxyServer
7
- ALLOWED_HOSTS = ["127.0.0.1", "localhost"].freeze
8
-
9
7
  attr_accessor :requests
10
8
 
11
9
  def initialize(config = {}, default = WEBrick::Config::HTTP)
@@ -17,32 +15,55 @@ module MiniProxy
17
15
  super(config, default)
18
16
  end
19
17
 
20
- def do_PUT(req, res)
21
- perform_proxy_request(req, res) do |http, path, header|
22
- http.put(path, req.body || "", header)
18
+ if RUBY_VERSION < "2.6.0"
19
+ def do_PUT(req, res)
20
+ perform_proxy_request(req, res) do |http, path, header|
21
+ http.put(path, req.body || "", header)
22
+ end
23
23
  end
24
- end
25
24
 
26
- def do_DELETE(req, res)
27
- perform_proxy_request(req, res) do |http, path, header|
28
- http.delete(path, header)
25
+ def do_DELETE(req, res)
26
+ perform_proxy_request(req, res) do |http, path, header|
27
+ http.delete(path, header)
28
+ end
29
29
  end
30
- end
31
30
 
32
- def do_PATCH(req, res)
33
- perform_proxy_request(req, res) do |http, path, header|
34
- http.patch(path, req.body || "", header)
31
+ def do_PATCH(req, res)
32
+ perform_proxy_request(req, res) do |http, path, header|
33
+ http.patch(path, req.body || "", header)
34
+ end
35
+ end
36
+ else
37
+ def do_POST(req, res)
38
+ perform_proxy_request(req, res, Net::HTTP::Post, StringIO.new(req.body))
39
+ end
40
+
41
+ def do_PUT(req, res)
42
+ perform_proxy_request(req, res, Net::HTTP::Put, req.body_reader)
43
+ end
44
+
45
+ def do_DELETE(req, res)
46
+ perform_proxy_request(req, res, Net::HTTP::Delete)
47
+ end
48
+
49
+ def do_PATCH(req, res)
50
+ perform_proxy_request(req, res, Net::HTTP::Patch, req.body_reader)
35
51
  end
36
52
  end
37
53
 
38
54
  def service(req, res)
39
- if ALLOWED_HOSTS.include?(req.host)
55
+ if self.config[:AllowedRequestCheck].call(req)
40
56
  super(req, res)
41
57
  else
42
58
  if req.request_method == "CONNECT"
59
+ is_ssl = req.unparsed_uri.include?(":443")
60
+
43
61
  # If something is trying to initiate an SSL connection, rewrite
44
- # the URI to point to our fake server.
45
- req.instance_variable_set(:@unparsed_uri, "localhost:#{self.config[:FakeServerPort]}")
62
+ # the URI to point to our fake server so we can stub SSL requests.
63
+ if is_ssl
64
+ req.instance_variable_set(:@unparsed_uri, "localhost:#{self.config[:FakeServerPort]}")
65
+ end
66
+
46
67
  super(req, res)
47
68
  else
48
69
  # Otherwise, call our handler to respond with an appropriate
@@ -20,7 +20,7 @@ module MiniProxy
20
20
  false
21
21
  end
22
22
 
23
- def self.server
23
+ def self.server(host = "127.0.0.1")
24
24
  @unix_socket_uri ||= begin
25
25
  tempfile = Tempfile.new("mini_proxy")
26
26
  socket_path = tempfile.path
@@ -31,12 +31,13 @@ module MiniProxy
31
31
  return @unix_socket_uri if drb_process_alive?
32
32
 
33
33
  @pid = fork do
34
- remote = Remote.new
34
+ remote = Remote.new(host)
35
35
 
36
36
  Timeout.timeout(SERVER_START_TIMEOUT) do
37
37
  begin
38
38
  fake_server_port = SERVER_DYNAMIC_PORT_RANGE.sample
39
39
  fake_server = FakeSSLServer.new(
40
+ AllowedRequestCheck: remote.method(:allowed_request?),
40
41
  Port: fake_server_port,
41
42
  MockHandlerCallback: remote.method(:handler),
42
43
  )
@@ -48,6 +49,7 @@ module MiniProxy
48
49
  begin
49
50
  remote.port = ENV["MINI_PROXY_PORT"] || SERVER_DYNAMIC_PORT_RANGE.sample
50
51
  proxy = MiniProxy::ProxyServer.new(
52
+ AllowedRequestCheck: remote.method(:allowed_request?),
51
53
  Port: remote.port,
52
54
  FakeServerPort: fake_server_port,
53
55
  MockHandlerCallback: remote.method(:handler),
@@ -88,6 +90,16 @@ module MiniProxy
88
90
  @stubs.push(request)
89
91
  end
90
92
 
93
+ def allow_request(method:, url:)
94
+ @allowed_requests.push(MiniProxy::Stub::Request.new(method: method, url: url, response: nil))
95
+ end
96
+
97
+ def allowed_request?(req)
98
+ return true if ["127.0.0.1", "localhost", @host].include?(req.host)
99
+
100
+ @allowed_requests.any? { |allowed_request| allowed_request.match?(req) }
101
+ end
102
+
91
103
  def port
92
104
  @port
93
105
  end
@@ -102,6 +114,7 @@ module MiniProxy
102
114
 
103
115
  def clear
104
116
  @stubs.clear
117
+ @allowed_requests.clear
105
118
  end
106
119
 
107
120
  def drain_messages
@@ -119,9 +132,11 @@ module MiniProxy
119
132
  @messages.push msg
120
133
  end
121
134
 
122
- def initialize
135
+ def initialize(host)
136
+ @host = host
123
137
  @stubs = []
124
138
  @messages = []
139
+ @allowed_requests = []
125
140
  end
126
141
  end
127
142
  end
@@ -16,8 +16,13 @@ module MiniProxy
16
16
 
17
17
  # @param [WEBrick::HTTPRequest] http_request
18
18
  def match?(http_request)
19
- request_uri = http_request.host + http_request.path
20
- http_request.request_method == @method && request_uri.match?(@url)
19
+ if http_request.request_method == "CONNECT"
20
+ host = http_request.unparsed_uri.split(":").first
21
+ @url.match?(host)
22
+ else
23
+ request_uri = http_request.host + http_request.path
24
+ http_request.request_method == @method && request_uri.match?(@url)
25
+ end
21
26
  end
22
27
  end
23
28
  end
@@ -1,3 +1,3 @@
1
1
  module MiniProxy
2
- VERSION = "0.2.1".freeze
2
+ VERSION = "0.7.0".freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miniproxy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - The Conversation Dev Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-02 00:00:00.000000000 Z
11
+ date: 2021-05-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -87,8 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  - !ruby/object:Gem::Version
88
88
  version: '0'
89
89
  requirements: []
90
- rubyforge_project:
91
- rubygems_version: 2.7.3
90
+ rubygems_version: 3.1.2
92
91
  signing_key:
93
92
  specification_version: 4
94
93
  summary: Easily stub external requests for your browser tests.