proxy-server 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -1,4 +1,4 @@
1
- .idea
2
- .bundle
3
- coverage
4
- pkg
1
+ .idea
2
+ .bundle
3
+ coverage
4
+ pkg
data/Gemfile CHANGED
@@ -1,2 +1,2 @@
1
- source :rubygems
1
+ source :rubygems
2
2
  gemspec
data/Gemfile.lock CHANGED
@@ -1,54 +1,66 @@
1
- PATH
2
- remote: .
3
- specs:
4
- proxy-server (0.0.2)
5
- httpclient
6
- json (>= 1.4.6)
7
- sinatra (>= 1.2.6)
8
-
9
- GEM
10
- remote: http://rubygems.org/
11
- specs:
12
- addressable (2.2.6)
13
- crack (0.3.1)
14
- diff-lcs (1.1.3)
15
- httpclient (2.2.1)
16
- json (1.6.1)
17
- multi_json (1.0.3)
18
- rack (1.3.4)
19
- rack-protection (1.1.4)
20
- rack
21
- rack-test (0.6.1)
22
- rack (>= 1.0)
23
- rake (0.9.2)
24
- rspec (2.6.0)
25
- rspec-core (~> 2.6.0)
26
- rspec-expectations (~> 2.6.0)
27
- rspec-mocks (~> 2.6.0)
28
- rspec-core (2.6.4)
29
- rspec-expectations (2.6.0)
30
- diff-lcs (~> 1.1.2)
31
- rspec-mocks (2.6.0)
32
- simplecov (0.5.4)
33
- multi_json (~> 1.0.3)
34
- simplecov-html (~> 0.5.3)
35
- simplecov-html (0.5.3)
36
- sinatra (1.3.1)
37
- rack (>= 1.3.4, ~> 1.3)
38
- rack-protection (>= 1.1.2, ~> 1.1)
39
- tilt (>= 1.3.3, ~> 1.3)
40
- tilt (1.3.3)
41
- webmock (1.7.6)
42
- addressable (~> 2.2, > 2.2.5)
43
- crack (>= 0.1.7)
44
-
45
- PLATFORMS
46
- ruby
47
-
48
- DEPENDENCIES
49
- proxy-server!
50
- rack-test (>= 0.5.7)
51
- rake (>= 0.9.2)
52
- rspec (>= 2.6.0)
53
- simplecov (>= 0.4.2)
54
- webmock
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proxy-server (0.0.3)
5
+ httpclient
6
+ json (>= 1.4.6)
7
+ sinatra (>= 1.2.6)
8
+ thin
9
+
10
+ GEM
11
+ remote: http://rubygems.org/
12
+ specs:
13
+ addressable (2.2.6)
14
+ crack (0.3.1)
15
+ daemons (1.1.4)
16
+ diff-lcs (1.1.3)
17
+ eventmachine (1.0.0.beta.4.1-x86-mingw32)
18
+ httpclient (2.2.2)
19
+ json (1.6.1)
20
+ multi_json (1.0.3)
21
+ rack (1.3.4)
22
+ rack-protection (1.1.4)
23
+ rack
24
+ rack-test (0.6.1)
25
+ rack (>= 1.0)
26
+ rake (0.9.2)
27
+ rspec (2.7.0)
28
+ rspec-core (~> 2.7.0)
29
+ rspec-expectations (~> 2.7.0)
30
+ rspec-mocks (~> 2.7.0)
31
+ rspec-core (2.7.0)
32
+ rspec-expectations (2.7.0)
33
+ diff-lcs (~> 1.1.2)
34
+ rspec-mocks (2.7.0)
35
+ simplecov (0.5.4)
36
+ multi_json (~> 1.0.3)
37
+ simplecov-html (~> 0.5.3)
38
+ simplecov-html (0.5.3)
39
+ sinatra (1.3.1)
40
+ rack (~> 1.3, >= 1.3.4)
41
+ rack-protection (~> 1.1, >= 1.1.2)
42
+ tilt (~> 1.3, >= 1.3.3)
43
+ thin (1.2.11)
44
+ daemons (>= 1.0.9)
45
+ eventmachine (>= 0.12.6)
46
+ rack (>= 1.0.0)
47
+ thin (1.2.11-x86-mingw32)
48
+ daemons (>= 1.0.9)
49
+ eventmachine (>= 0.12.6)
50
+ rack (>= 1.0.0)
51
+ tilt (1.3.3)
52
+ webmock (1.7.6)
53
+ addressable (~> 2.2, > 2.2.5)
54
+ crack (>= 0.1.7)
55
+
56
+ PLATFORMS
57
+ ruby
58
+ x86-mingw32
59
+
60
+ DEPENDENCIES
61
+ proxy-server!
62
+ rack-test (>= 0.5.7)
63
+ rake (>= 0.9.2)
64
+ rspec (>= 2.7.0)
65
+ simplecov (>= 0.4.2)
66
+ webmock
data/Rakefile CHANGED
@@ -1,19 +1,19 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- require 'rspec/core/rake_task'
4
- Bundler::GemHelper.install_tasks
5
-
6
- task :coverage do
7
- require 'simplecov'
8
- require 'rspec/core'
9
-
10
- SimpleCov.start do
11
- add_group "lib", "lib"
12
- end
13
- SimpleCov.start
14
- RSpec::Core::Runner.run %w[spec]
15
- end
16
-
17
-
18
- require 'rake/clean'
19
- CLEAN.include %w(**/*.{log,pyc,rbc,tgz} doc)
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ require 'rspec/core/rake_task'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ task :coverage do
7
+ require 'simplecov'
8
+ require 'rspec/core'
9
+
10
+ SimpleCov.start do
11
+ add_group "lib", "lib"
12
+ end
13
+ SimpleCov.start
14
+ RSpec::Core::Runner.run %w[spec]
15
+ end
16
+
17
+
18
+ require 'rake/clean'
19
+ CLEAN.include %w(**/*.{log,pyc,rbc,tgz} doc)
@@ -1,28 +1,23 @@
1
- require 'rubygems'
2
- require 'proxy-server'
3
- require 'rest_client' # you will need to install this via gem install rest-client / bundler etc
4
- require 'json'
5
-
6
- ENV.clear
7
-
8
- Thread.new do
9
- ProxyManager.run! :port => 4983
10
- end
11
-
12
- until_proxy_is_running = 2
13
- sleep until_proxy_is_running # need to implement the ability to wait for the proxy to be running
14
-
15
- proxy = JSON.parse(RestClient.post 'http://localhost:4983/proxies')
16
-
17
- sleep until_proxy_is_running
18
-
19
- RestClient.post "http://localhost:4983/proxies/#{proxy['port']}/requests", {:track => 'google.com'}
20
-
21
- client = HTTPClient.new :proxy => "http://localhost:#{proxy['port']}"
22
- client.get 'http://www.google.com'
23
- client.get 'http://github.com'
24
-
25
- p JSON.parse(HTTPClient.get("http://localhost:4983/proxies/#{proxy['port']}/requests").body)
26
-
27
-
28
-
1
+ require 'rubygems'
2
+ require 'proxy-server'
3
+ require 'rest_client' # you will need to install this via gem install rest-client / bundler etc
4
+ require 'json'
5
+
6
+ Thread.abort_on_exception=true
7
+ Thread.new do
8
+ ProxyManager.settings.port = 4983
9
+ ProxyManager.run!
10
+ end
11
+
12
+ until_proxy_is_running = 3
13
+ sleep until_proxy_is_running # need to implement the ability to wait for the proxy to be running
14
+
15
+ proxy = JSON.parse(RestClient.post 'http://localhost:4983/proxies', {})
16
+
17
+ RestClient.post "http://localhost:4983/proxies/#{proxy['port']}/requests", {:track => 'google.com'}
18
+
19
+ client = HTTPClient.new :proxy => "http://localhost:#{proxy['port']}"
20
+ client.get 'http://www.google.com'
21
+ client.get 'http://github.com'
22
+
23
+ p JSON.parse(HTTPClient.get("http://localhost:4983/proxies/#{proxy['port']}/requests").body)
@@ -0,0 +1,15 @@
1
+ # lovingly reused from selenium-webdriver
2
+
3
+ class PortProber
4
+ def self.above(port)
5
+ port += 1 until free? port
6
+ port
7
+ end
8
+
9
+ def self.free?(port)
10
+ TCPServer.new('localhost', port).close
11
+ true
12
+ rescue SocketError, Errno::EADDRINUSE
13
+ false
14
+ end
15
+ end
@@ -1,70 +1,87 @@
1
- require_relative './proxy_server'
2
- require 'sinatra/base'
3
- require 'json'
4
-
5
- class ProxyManager < Sinatra::Base
6
- START_PORT = 5000
7
-
8
- attr_reader :running_proxy_servers, :assigned_proxy_ports
9
- disable :show_exceptions
10
-
11
- def initialize()
12
- @running_proxy_servers = {}
13
- @assigned_proxy_ports = []
14
- super
15
- end
16
-
17
- get '/proxies' do
18
- assigned_proxy_ports.to_json
19
- end
20
-
21
- post '/proxies' do
22
- new_proxy_port = assigned_proxy_ports.max
23
- new_proxy_port += 1 unless new_proxy_port.nil?
24
- new_proxy_port ||= START_PORT
25
-
26
- assigned_proxy_ports << new_proxy_port
27
-
28
- options = {
29
- :port => new_proxy_port
30
- }
31
-
32
- options[:proxy] = params[:proxy] if params[:proxy]
33
- start_proxy(options)
34
-
35
- {:port => new_proxy_port}.to_json
36
- end
37
-
38
- delete '/proxies' do
39
- assigned_proxy_ports.clear
40
- end
41
-
42
- post '/proxies/:port/requests' do |port|
43
- proxy_server = get_proxy(port.to_i)
44
- proxy_server.track_request(params[:track])
45
- end
46
-
47
- get '/proxies/:port/requests' do |port|
48
- proxy_server = get_proxy(port.to_i)
49
- proxy_server.requests.to_json
50
- end
51
-
52
- post '/proxies/:port/requests/substitute' do |port|
53
- proxy_server = get_proxy(port.to_i)
54
- options = {}
55
- options[:body] = params[:body] if params[:body]
56
- options[:url] = params[:url] if params[:url]
57
- proxy_server.substitute_request(params[:pattern], options)
58
- end
59
-
60
- def get_proxy(port)
61
- running_proxy_servers[port]
62
- end
63
-
64
- def start_proxy(options)
65
- proxy_server = ProxyServer.new(options)
66
- proxy_server.run
67
- running_proxy_servers[options[:port]] = proxy_server
68
- end
69
- end
70
-
1
+ require_relative './proxy_server'
2
+ require_relative './port_prober'
3
+ require 'sinatra/base'
4
+ require 'json'
5
+
6
+ class ProxyManager < Sinatra::Base
7
+ START_PORT = 5000
8
+
9
+ attr_reader :running_proxy_servers, :assigned_proxy_ports
10
+ disable :show_exceptions
11
+
12
+ def initialize()
13
+ @running_proxy_servers = {}
14
+ @assigned_proxy_ports = []
15
+ super
16
+ end
17
+
18
+ get '/proxies' do
19
+ assigned_proxy_ports.to_json
20
+ end
21
+
22
+ post '/proxies' do
23
+ new_proxy_port = find_proxy_port
24
+ assigned_proxy_ports << new_proxy_port
25
+
26
+ options = {
27
+ :port => new_proxy_port
28
+ }
29
+
30
+ options[:proxy] = params[:proxy] if params[:proxy]
31
+ start_proxy(options)
32
+
33
+ {:port => new_proxy_port}.to_json
34
+ end
35
+
36
+ delete '/proxies' do
37
+ assigned_proxy_ports.clear
38
+ end
39
+
40
+ delete '/proxies/:port' do |port|
41
+ stop_proxy port.to_i
42
+ end
43
+
44
+ post '/proxies/:port/reset' do |port|
45
+ proxy_server = get_proxy(port.to_i)
46
+ proxy_server.reset
47
+ end
48
+
49
+ post '/proxies/:port/requests' do |port|
50
+ proxy_server = get_proxy(port.to_i)
51
+ proxy_server.track_request(params[:track])
52
+ end
53
+
54
+ get '/proxies/:port/requests' do |port|
55
+ proxy_server = get_proxy(port.to_i)
56
+ proxy_server.requests.to_json
57
+ end
58
+
59
+ post '/proxies/:port/requests/substitute' do |port|
60
+ proxy_server = get_proxy(port.to_i)
61
+ options = {}
62
+ options[:body] = params[:body] if params[:body]
63
+ options[:url] = params[:url] if params[:url]
64
+ proxy_server.substitute_request(params[:pattern], options)
65
+ end
66
+
67
+ def get_proxy(port)
68
+ running_proxy_servers[port]
69
+ end
70
+
71
+ def stop_proxy(port)
72
+ proxy_server = running_proxy_servers[port]
73
+ proxy_server.stop
74
+ running_proxy_servers.delete proxy_server
75
+ end
76
+
77
+ def start_proxy(options)
78
+ proxy_server = ProxyServer.new(options)
79
+ proxy_server.run
80
+ running_proxy_servers[options[:port]] = proxy_server
81
+ end
82
+
83
+ def find_proxy_port
84
+ new_proxy_port = (assigned_proxy_ports.max || ProxyManager::START_PORT) + 1
85
+ PortProber.above(new_proxy_port)
86
+ end
87
+ end
@@ -1,104 +1,117 @@
1
- require 'rack'
2
- require 'httpclient'
3
-
4
- class ProxyServer
5
-
6
- attr_reader :upstream_proxy, :port
7
- attr_reader :requests
8
-
9
- DEFAULT_PORT = 8080
10
-
11
- def initialize(options = {})
12
- @upstream_proxy = options[:proxy]
13
- @client = create_http_client
14
-
15
- @port = options.fetch(:port, DEFAULT_PORT)
16
-
17
- @substitute_requests = {}
18
- @requests = []
19
- @track_requests = []
20
- end
21
-
22
- def create_http_client
23
- HTTPClient.new :proxy => @upstream_proxy
24
- end
25
-
26
- def run
27
- Thread.new do
28
- @handler = Rack::Handler::WEBrick
29
-
30
- @handler.run self, :Port => self.port
31
- end
32
- end
33
-
34
- def call(env)
35
- log_request env
36
-
37
- response = get_substitution(env)
38
- return response unless response.nil?
39
-
40
- method = env['REQUEST_METHOD']
41
- uri = "http://#{env['HTTP_HOST']}#{env['PATH_INFO']}"
42
- params = get_params(env['QUERY_STRING'])
43
- body = get_request_body(env)
44
- headers = get_request_headers(env)
45
- response = @client.request(method, uri, params, body, headers)
46
-
47
- [ response.status, response.headers, [response.body] ]
48
- end
49
-
50
- def get_substitution(env)
51
- uri = env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'] ? '?'+env['QUERY_STRING'] : ''}"
52
- @substitute_requests.each do |pattern, options|
53
- if Regexp.new(pattern) =~ uri
54
- return get_substituted_response(options)
55
- end
56
- end
57
- nil
58
- end
59
-
60
- def get_substituted_response(options)
61
- if options[:body]
62
- [ 200, {}, [options[:body]] ]
63
- elsif options[:url]
64
- response = @client.get(options[:url])
65
- [ response.status, response.headers, [response.body] ]
66
- end
67
- end
68
-
69
- def substitute_request(pattern, options)
70
- @substitute_requests[pattern] = options
71
- end
72
-
73
- def track_request(pattern)
74
- @track_requests << pattern
75
- end
76
-
77
- def get_params(query_string)
78
- query_string.split('&').inject({}) do |hsh, i|
79
- kv = i.split('='); hsh[kv[0]] = kv[1]; hsh
80
- end unless query_string.nil? or query_string.length == 0
81
- end
82
-
83
- def log_request(env)
84
-
85
- url = env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'].length>0 ? '?'+env['QUERY_STRING'] : ''}"
86
- @track_requests.each do |pattern|
87
- @requests << url if Regexp.new(pattern) =~ url
88
- end
89
- end
90
-
91
- private
92
- def get_request_body(env)
93
- body = ''
94
- env['rack.input'].each_line {|string| body << string }
95
- body
96
- end
97
-
98
- def get_request_headers(env)
99
- headers = {}
100
- env.each {|k,v| if k =~ /HTTP_(\w+)/ then headers[$1] = v end }
101
- headers
102
- end
103
- end
104
-
1
+ require 'rack'
2
+ require 'httpclient'
3
+
4
+ class ProxyServer
5
+
6
+ attr_reader :upstream_proxy, :port
7
+ attr_reader :requests, :track_requests, :substitute_requests
8
+
9
+ DEFAULT_PORT = 8080
10
+
11
+ def initialize(options = {})
12
+ @upstream_proxy = options[:proxy]
13
+ @client = create_http_client
14
+
15
+ @port = options.fetch(:port, DEFAULT_PORT)
16
+
17
+ @substitute_requests = {}
18
+ @requests = []
19
+ @track_requests = []
20
+ end
21
+
22
+ def create_http_client
23
+ HTTPClient.new :proxy => @upstream_proxy
24
+ end
25
+
26
+ def run
27
+ @proxy_thread = Thread.new do
28
+ Rack::Handler::WEBrick.run self, :Port => self.port, :AccessLog => []
29
+ end
30
+ end
31
+
32
+ def stop
33
+ Thread.kill @proxy_thread if @proxy_thread
34
+ end
35
+
36
+ def reset
37
+ @requests.clear
38
+ @track_requests.clear
39
+ @substitute_requests.clear
40
+ end
41
+
42
+ def call(env)
43
+ log_request env
44
+
45
+ response = get_substitution(env)
46
+ return response unless response.nil?
47
+
48
+ method = env['REQUEST_METHOD']
49
+ uri = "http://#{env['HTTP_HOST']}#{env['PATH_INFO']}"
50
+ params = get_params(env['QUERY_STRING'])
51
+ body = get_request_body(env)
52
+ headers = get_request_headers(env)
53
+ #
54
+ #p "YES" if uri.include? 'unicast.com'
55
+ #p uri if uri.include? 'unicast.com'
56
+ #[ 200, {}, ['']] if uri.include? 'unicast.com'
57
+
58
+ response = @client.request(method, uri, params, body, headers)
59
+
60
+ [ response.status, response.headers, [response.body] ]
61
+ end
62
+
63
+ def get_substitution(env)
64
+ uri = env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'] ? '?'+env['QUERY_STRING'] : ''}"
65
+ @substitute_requests.each do |pattern, options|
66
+ if Regexp.new(pattern) =~ uri
67
+ return get_substituted_response(options)
68
+ end
69
+ end
70
+ nil
71
+ end
72
+
73
+ def get_substituted_response(options)
74
+ if options[:body]
75
+ [ 200, {}, [options[:body]] ]
76
+ elsif options[:url]
77
+ response = @client.get(options[:url])
78
+ [ response.status, response.headers, [response.body] ]
79
+ end
80
+ end
81
+
82
+ def substitute_request(pattern, options)
83
+ @substitute_requests[pattern] = options
84
+ end
85
+
86
+ def track_request(pattern)
87
+ @track_requests << pattern
88
+ end
89
+
90
+ def get_params(query_string)
91
+ query_string.split('&').inject({}) do |hsh, i|
92
+ kv = i.split('='); hsh[kv[0]] = kv[1]; hsh
93
+ end unless query_string.nil? or query_string.length == 0
94
+ end
95
+
96
+ def log_request(env)
97
+
98
+ url = env['REQUEST_URI'] || "http://#{env['SERVER_NAME']}#{env['PATH_INFO']}#{env['QUERY_STRING'].length>0 ? '?'+env['QUERY_STRING'] : ''}"
99
+ @track_requests.each do |pattern|
100
+ @requests << url if Regexp.new(pattern) =~ url
101
+ end
102
+ end
103
+
104
+ private
105
+ def get_request_body(env)
106
+ body = ''
107
+ env['rack.input'].each_line {|string| body << string }
108
+ body
109
+ end
110
+
111
+ def get_request_headers(env)
112
+ headers = {}
113
+ env.each {|k,v| if k =~ /HTTP_(\w+)/ then headers[$1] = v end }
114
+ headers
115
+ end
116
+ end
117
+
data/lib/proxy-server.rb CHANGED
@@ -1,2 +1,2 @@
1
- require_relative './proxy/proxy_server'
1
+ require_relative './proxy/proxy_server'
2
2
  require_relative './proxy/proxy_manager'
data/proxy-server.gemspec CHANGED
@@ -1,32 +1,33 @@
1
- $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
-
3
- Gem::Specification.new do |s|
4
- s.name = 'proxy-server'
5
- s.version = '0.0.2'
6
- s.authors = ["Derek Ekins"]
7
- s.description = 'Proxy server'
8
- s.summary = "proxy-server-#{s.version}"
9
- s.email = 'derek@spathi.com'
10
- s.homepage = "http://github.com/dereke/proxy-server"
11
-
12
- s.platform = Gem::Platform::RUBY
13
-
14
- s.add_dependency 'json', '>= 1.4.6'
15
- s.add_dependency 'sinatra', '>= 1.2.6'
16
- s.add_dependency 'httpclient'
17
-
18
- s.add_development_dependency 'rake', '>= 0.9.2'
19
- s.add_development_dependency 'rspec', '>= 2.6.0'
20
- s.add_development_dependency 'simplecov', '>= 0.4.2'
21
- s.add_development_dependency 'webmock'
22
-
23
-
24
- s.add_development_dependency 'rack-test', '>= 0.5.7'
25
-
26
- s.rubygems_version = ">= 1.6.1"
27
- s.files = `git ls-files`.split("\n")
28
- s.test_files = `git ls-files -- {spec}/*`.split("\n")
29
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
30
- s.rdoc_options = ["--charset=UTF-8"]
31
- s.require_path = "lib"
1
+ $LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'proxy-server'
5
+ s.version = '0.0.3'
6
+ s.authors = ["Derek Ekins"]
7
+ s.description = 'Proxy server'
8
+ s.summary = "proxy-server-#{s.version}"
9
+ s.email = 'derek@spathi.com'
10
+ s.homepage = "http://github.com/dereke/proxy-server"
11
+
12
+ s.platform = Gem::Platform::RUBY
13
+
14
+ s.add_dependency 'json', '>= 1.4.6'
15
+ s.add_dependency 'sinatra', '>= 1.2.6'
16
+ s.add_dependency 'httpclient'
17
+ s.add_dependency 'thin'
18
+
19
+ s.add_development_dependency 'rake', '>= 0.9.2'
20
+ s.add_development_dependency 'rspec', '>= 2.7.0'
21
+ s.add_development_dependency 'simplecov', '>= 0.4.2'
22
+ s.add_development_dependency 'webmock'
23
+
24
+
25
+ s.add_development_dependency 'rack-test', '>= 0.5.7'
26
+
27
+ s.rubygems_version = ">= 1.6.1"
28
+ s.files = `git ls-files`.split("\n")
29
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
30
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
31
+ s.rdoc_options = ["--charset=UTF-8"]
32
+ s.require_path = "lib"
32
33
  end
@@ -1,77 +1,96 @@
1
- require_relative '../spec_helper'
2
- require_relative '../../lib/proxy/proxy_manager'
3
- require 'rack/test'
4
- require 'webmock/rspec'
5
-
6
- describe ProxyManager do
7
- include Rack::Test::Methods
8
-
9
- before do
10
- @proxy_server = Object.new
11
- ProxyManager.any_instance.stub(:start_proxy).with(anything()).and_return(@proxy_server)
12
- ProxyManager.any_instance.stub(:get_proxy).with(anything()).and_return(@proxy_server)
13
- end
14
-
15
- let(:app) { ProxyManager.new }
16
-
17
- it "should create a new proxy when I ask for one" do
18
- response = post '/proxies'
19
- proxy = JSON.parse(response.body)
20
- proxy.should == {'port' => ProxyManager::START_PORT}
21
- end
22
-
23
- it "should create a new proxy with an upstream proxy when asked to" do
24
- ProxyManager.any_instance.should_receive(:start_proxy).with(hash_including(:proxy => 'http://my_proxy:80'))
25
- response = post '/proxies', {:proxy => 'http://my_proxy:80'}
26
- end
27
-
28
- it "should tell me about all the proxies that have been created" do
29
- post '/proxies'
30
- response = get '/proxies'
31
- JSON.parse(response.body).should == [ProxyManager::START_PORT]
32
- end
33
-
34
- it "should remove all proxies when asked" do
35
- post '/proxies'
36
- delete '/proxies'
37
-
38
- response = get '/proxies'
39
- JSON.parse(response.body).should == []
40
- end
41
-
42
- it "should assign new proxy ports when more than one is asked for" do
43
- first_expected_proxy = ProxyManager::START_PORT
44
- second_expected_proxy = ProxyManager::START_PORT + 1
45
-
46
- first_response = post '/proxies'
47
- second_response = post '/proxies'
48
-
49
- JSON.parse(first_response.body)['port'].should == first_expected_proxy
50
- JSON.parse(second_response.body)['port'].should == second_expected_proxy
51
- end
52
-
53
- it "ads a given url to a list of them to track" do
54
- track_url = 'public/.*.js'
55
-
56
- @proxy_server.should_receive(:track_request).with(track_url)
57
- post "/proxies/1111/requests", {:track => track_url}
58
- end
59
-
60
- it "ads a given url to a list of them to track" do
61
- @proxy_server.stub!(:requests).and_return(['request 1', 'request 2'])
62
- response = get "/proxies/1111/requests"
63
- requests = JSON.parse(response.body)
64
- requests.should include('request 1')
65
- requests.should include('request 2')
66
- end
67
-
68
- it "can substitute a request with another body" do
69
- @proxy_server.should_receive(:substitute_request).with('*.js', :body => 'alert(1);')
70
- post "/proxies/1111/requests/substitute", {:pattern => '*.js', :body => 'alert(1);'}
71
- end
72
-
73
- it "can substitute a request with another url" do
74
- @proxy_server.should_receive(:substitute_request).with('*.js', :url => 'http://example.com/test.js')
75
- post "/proxies/1111/requests/substitute", {:pattern => '*.js', :url => 'http://example.com/test.js'}
76
- end
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/proxy/proxy_manager'
3
+ require 'rack/test'
4
+ require 'webmock/rspec'
5
+
6
+ describe ProxyManager do
7
+ include Rack::Test::Methods
8
+
9
+ before do
10
+ @proxy_server = mock('ProxyServer')
11
+ ProxyManager.any_instance.stub(:start_proxy).with(anything()).and_return(@proxy_server)
12
+ ProxyManager.any_instance.stub(:get_proxy).with(anything()).and_return(@proxy_server)
13
+ end
14
+
15
+ let(:app) { ProxyManager.new }
16
+
17
+ it "should create a new proxy when I ask for one" do
18
+ ProxyManager.any_instance.should_receive(:find_proxy_port).and_return(4000)
19
+ response = post '/proxies'
20
+ proxy = JSON.parse(response.body)
21
+ proxy.should == {'port' => 4000}
22
+ end
23
+
24
+ it "should create a new proxy with an upstream proxy when asked to" do
25
+ ProxyManager.any_instance.should_receive(:start_proxy).with(hash_including(:proxy => 'http://my_proxy:80'))
26
+ response = post '/proxies', {:proxy => 'http://my_proxy:80'}
27
+ end
28
+
29
+ it "should tell me about all the proxies that have been created" do
30
+ ProxyManager.any_instance.should_receive(:find_proxy_port).and_return(4000)
31
+
32
+ post '/proxies'
33
+ response = get '/proxies'
34
+ JSON.parse(response.body).should == [4000]
35
+ end
36
+
37
+ it "should remove all proxies when asked" do
38
+ post '/proxies'
39
+ delete '/proxies'
40
+
41
+ response = get '/proxies'
42
+ JSON.parse(response.body).should == []
43
+ end
44
+
45
+ it "should remove a proxy when asked to" do
46
+ running_proxy_port = JSON.parse(post('/proxies').body)['port']
47
+ ProxyManager.any_instance.should_receive(:stop_proxy).with(running_proxy_port)
48
+
49
+ response = delete "/proxies/#{running_proxy_port}"
50
+
51
+ response.status.should == 200
52
+ end
53
+
54
+ it "should assign new proxy ports when more than one is asked for" do
55
+ first_expected_proxy = 100
56
+ second_expected_proxy = 101
57
+ available_proxy_ports = [first_expected_proxy, second_expected_proxy]
58
+ ProxyManager.any_instance.stub(:find_proxy_port).and_return { available_proxy_ports.shift }
59
+
60
+ first_response = post '/proxies'
61
+ second_response = post '/proxies'
62
+
63
+ JSON.parse(first_response.body)['port'].should == first_expected_proxy
64
+ JSON.parse(second_response.body)['port'].should == second_expected_proxy
65
+ end
66
+
67
+ it "ads a given url to a list of them to track" do
68
+ track_url = 'public/.*.js'
69
+
70
+ @proxy_server.should_receive(:track_request).with(track_url)
71
+ post "/proxies/1111/requests", {:track => track_url}
72
+ end
73
+
74
+ it "ads a given url to a list of them to track" do
75
+ @proxy_server.stub!(:requests).and_return(['request 1', 'request 2'])
76
+ response = get "/proxies/1111/requests"
77
+ requests = JSON.parse(response.body)
78
+ requests.should include('request 1')
79
+ requests.should include('request 2')
80
+ end
81
+
82
+ it "can substitute a request with another body" do
83
+ @proxy_server.should_receive(:substitute_request).with('*.js', :body => 'alert(1);')
84
+ post "/proxies/1111/requests/substitute", {:pattern => '*.js', :body => 'alert(1);'}
85
+ end
86
+
87
+ it "can substitute a request with another url" do
88
+ @proxy_server.should_receive(:substitute_request).with('*.js', :url => 'http://example.com/test.js')
89
+ post "/proxies/1111/requests/substitute", {:pattern => '*.js', :url => 'http://example.com/test.js'}
90
+ end
91
+
92
+ it "can reset the configuration of a proxy" do
93
+ @proxy_server.should_receive(:reset)
94
+ post "/proxies/1111/reset"
95
+ end
77
96
  end
@@ -1,81 +1,94 @@
1
- require_relative '../spec_helper'
2
- require_relative '../../lib/proxy/proxy_server'
3
- require 'rack/test'
4
- require 'webmock/rspec'
5
-
6
- describe ProxyServer do
7
- include Rack::Test::Methods
8
- before do
9
- ENV.clear
10
- end
11
-
12
- let(:app) { ProxyServer.new }
13
-
14
- it "can be configured with an upstream proxy" do
15
- proxy_uri = 'http://test-proxy:80'
16
- proxy = ProxyServer.new :proxy => proxy_uri
17
- proxy.upstream_proxy.should == proxy_uri
18
- end
19
-
20
- it "can be configured to run on a specific port" do
21
- proxy_port = 8080
22
- proxy = ProxyServer.new :port => proxy_port
23
- proxy.port.should == proxy_port
24
- end
25
-
26
- it "default the port when none is specified" do
27
- proxy = ProxyServer.new
28
- proxy.port.should == ProxyServer::DEFAULT_PORT
29
- end
30
-
31
- it "should allow requests" do
32
- stub_request(:get, "http://www.example.com")
33
-
34
- response = get 'http://www.example.com'
35
- response.status.should == 200
36
- end
37
-
38
- it "should allow requests with query strings" do
39
- stub_request(:get, "http://www.example.com?para=value")
40
-
41
- response = get "http://www.example.com?para=value"
42
- response.status.should == 200
43
- end
44
-
45
- it "can substitute the response of a request with a body of text" do
46
- proxy = ProxyServer.new
47
- expected_response = "this is not what you are looking for"
48
- proxy.substitute_request '.*com', :body => expected_response
49
-
50
- browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
51
- response = browser.get 'http://www.google.com'
52
- response.body.should == expected_response
53
- end
54
-
55
- it "can substitute the response of a request with another url to call" do
56
- proxy = ProxyServer.new
57
- substitute_url = "http://example.com"
58
- expected_response_body = "substitute body"
59
- stub_request(:get, substitute_url).to_return(:body => expected_response_body)
60
- proxy.substitute_request '.*com', :url => substitute_url
61
-
62
- browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
63
- response = browser.get 'http://www.google.com'
64
- response.body.should == expected_response_body
65
- end
66
-
67
- it "tracks a url that matches the pattern" do
68
- proxy = ProxyServer.new
69
- expected_response = "this is not what you are looking for"
70
- proxy.track_request '.*com'
71
-
72
- stub_request(:get, 'http://www.google.com/')
73
- stub_request(:get, 'http://www.google.co.uk/')
74
-
75
- browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
76
- browser.get 'http://www.google.com/'
77
- browser.get 'http://www.google.co.uk/'
78
-
79
- proxy.requests.should include('http://www.google.com/')
80
- end
81
- end
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/proxy/proxy_server'
3
+ require 'rack/test'
4
+ require 'webmock/rspec'
5
+
6
+ describe ProxyServer do
7
+ include Rack::Test::Methods
8
+
9
+ let(:app) { ProxyServer.new }
10
+
11
+ it "can be configured with an upstream proxy" do
12
+ proxy_uri = 'http://test-proxy:80'
13
+ proxy = ProxyServer.new :proxy => proxy_uri
14
+ proxy.upstream_proxy.should == proxy_uri
15
+ end
16
+
17
+ it "can be configured to run on a specific port" do
18
+ proxy_port = 8080
19
+ proxy = ProxyServer.new :port => proxy_port
20
+ proxy.port.should == proxy_port
21
+ end
22
+
23
+ it "default the port when none is specified" do
24
+ proxy = ProxyServer.new
25
+ proxy.port.should == ProxyServer::DEFAULT_PORT
26
+ end
27
+
28
+ it "should allow requests" do
29
+ stub_request(:get, "http://www.example.com")
30
+
31
+ response = get 'http://www.example.com'
32
+ response.status.should == 200
33
+ end
34
+
35
+ it "should allow requests with query strings" do
36
+ stub_request(:get, "http://www.example.com?para=value")
37
+
38
+ response = get "http://www.example.com?para=value"
39
+ response.status.should == 200
40
+ end
41
+
42
+ it "can substitute the response of a request with a body of text" do
43
+ proxy = ProxyServer.new
44
+ expected_response = "this is not what you are looking for"
45
+ proxy.substitute_request '.*com', :body => expected_response
46
+
47
+ browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
48
+ response = browser.get 'http://www.google.com'
49
+ response.body.should == expected_response
50
+ end
51
+
52
+ it "can substitute the response of a request with another url to call" do
53
+ proxy = ProxyServer.new
54
+ substitute_url = "http://example.com"
55
+ expected_response_body = "substitute body"
56
+ stub_request(:get, substitute_url).to_return(:body => expected_response_body)
57
+ proxy.substitute_request '.*com', :url => substitute_url
58
+
59
+ browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
60
+ response = browser.get 'http://www.google.com'
61
+ response.body.should == expected_response_body
62
+ end
63
+
64
+ it "tracks a url that matches the pattern" do
65
+ proxy = ProxyServer.new
66
+ expected_response = "this is not what you are looking for"
67
+ proxy.track_request '.*com'
68
+
69
+ stub_request(:get, 'http://www.google.com/')
70
+ stub_request(:get, 'http://www.google.co.uk/')
71
+
72
+ browser = Rack::Test::Session.new(Rack::MockSession.new(proxy))
73
+ browser.get 'http://www.google.com/'
74
+ browser.get 'http://www.google.co.uk/'
75
+
76
+ proxy.requests.should include('http://www.google.com/')
77
+ end
78
+
79
+ it "reset causes request tracking to be removed" do
80
+ proxy = ProxyServer.new
81
+ proxy.requests << 'request 1' << 'request 2'
82
+ proxy.track_requests << 'request 1' << 'request 2'
83
+ proxy.reset
84
+ proxy.requests.length.should == 0
85
+ proxy.track_requests.length.should == 0
86
+ end
87
+
88
+ it "reset causes request substituting to be removed" do
89
+ proxy = ProxyServer.new
90
+ proxy.substitute_requests['test'] = 'item'
91
+ proxy.reset
92
+ proxy.substitute_requests.length.should == 0
93
+ end
94
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,9 @@
1
- require 'rubygems'
2
- require 'bundler'
3
- Bundler.setup
4
- require 'rspec'
5
- ENV['RACK_ENV'] = 'test'
6
-
7
- class String
8
- alias each each_line
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ Bundler.setup
4
+ require 'rspec'
5
+ ENV['RACK_ENV'] = 'test'
6
+
7
+ class String
8
+ alias each each_line
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proxy-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-13 00:00:00.000000000Z
12
+ date: 2011-11-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
16
- requirement: &22574760 !ruby/object:Gem::Requirement
16
+ requirement: &11366952 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 1.4.6
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *22574760
24
+ version_requirements: *11366952
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sinatra
27
- requirement: &22574160 !ruby/object:Gem::Requirement
27
+ requirement: &11366664 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 1.2.6
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *22574160
35
+ version_requirements: *11366664
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: httpclient
38
- requirement: &22573640 !ruby/object:Gem::Requirement
38
+ requirement: &11366436 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,21 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *22573640
46
+ version_requirements: *11366436
47
+ - !ruby/object:Gem::Dependency
48
+ name: thin
49
+ requirement: &11366160 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :runtime
56
+ prerelease: false
57
+ version_requirements: *11366160
47
58
  - !ruby/object:Gem::Dependency
48
59
  name: rake
49
- requirement: &22572960 !ruby/object:Gem::Requirement
60
+ requirement: &11365860 !ruby/object:Gem::Requirement
50
61
  none: false
51
62
  requirements:
52
63
  - - ! '>='
@@ -54,21 +65,21 @@ dependencies:
54
65
  version: 0.9.2
55
66
  type: :development
56
67
  prerelease: false
57
- version_requirements: *22572960
68
+ version_requirements: *11365860
58
69
  - !ruby/object:Gem::Dependency
59
70
  name: rspec
60
- requirement: &22572340 !ruby/object:Gem::Requirement
71
+ requirement: &11365560 !ruby/object:Gem::Requirement
61
72
  none: false
62
73
  requirements:
63
74
  - - ! '>='
64
75
  - !ruby/object:Gem::Version
65
- version: 2.6.0
76
+ version: 2.7.0
66
77
  type: :development
67
78
  prerelease: false
68
- version_requirements: *22572340
79
+ version_requirements: *11365560
69
80
  - !ruby/object:Gem::Dependency
70
81
  name: simplecov
71
- requirement: &22571760 !ruby/object:Gem::Requirement
82
+ requirement: &11365284 !ruby/object:Gem::Requirement
72
83
  none: false
73
84
  requirements:
74
85
  - - ! '>='
@@ -76,10 +87,10 @@ dependencies:
76
87
  version: 0.4.2
77
88
  type: :development
78
89
  prerelease: false
79
- version_requirements: *22571760
90
+ version_requirements: *11365284
80
91
  - !ruby/object:Gem::Dependency
81
92
  name: webmock
82
- requirement: &22571260 !ruby/object:Gem::Requirement
93
+ requirement: &11365056 !ruby/object:Gem::Requirement
83
94
  none: false
84
95
  requirements:
85
96
  - - ! '>='
@@ -87,10 +98,10 @@ dependencies:
87
98
  version: '0'
88
99
  type: :development
89
100
  prerelease: false
90
- version_requirements: *22571260
101
+ version_requirements: *11365056
91
102
  - !ruby/object:Gem::Dependency
92
103
  name: rack-test
93
- requirement: &22521520 !ruby/object:Gem::Requirement
104
+ requirement: &11364732 !ruby/object:Gem::Requirement
94
105
  none: false
95
106
  requirements:
96
107
  - - ! '>='
@@ -98,7 +109,7 @@ dependencies:
98
109
  version: 0.5.7
99
110
  type: :development
100
111
  prerelease: false
101
- version_requirements: *22521520
112
+ version_requirements: *11364732
102
113
  description: Proxy server
103
114
  email: derek@spathi.com
104
115
  executables: []
@@ -111,6 +122,7 @@ files:
111
122
  - Rakefile
112
123
  - examples/proxy_in_process.rb
113
124
  - lib/proxy-server.rb
125
+ - lib/proxy/port_prober.rb
114
126
  - lib/proxy/proxy_manager.rb
115
127
  - lib/proxy/proxy_server.rb
116
128
  - proxy-server.gemspec
@@ -130,16 +142,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
130
142
  - - ! '>='
131
143
  - !ruby/object:Gem::Version
132
144
  version: '0'
145
+ segments:
146
+ - 0
147
+ hash: -732842897
133
148
  required_rubygems_version: !ruby/object:Gem::Requirement
134
149
  none: false
135
150
  requirements:
136
151
  - - ! '>='
137
152
  - !ruby/object:Gem::Version
138
153
  version: '0'
154
+ segments:
155
+ - 0
156
+ hash: -732842897
139
157
  requirements: []
140
158
  rubyforge_project:
141
- rubygems_version: 1.8.10
159
+ rubygems_version: 1.8.11
142
160
  signing_key:
143
161
  specification_version: 3
144
- summary: proxy-server-0.0.2
162
+ summary: proxy-server-0.0.3
145
163
  test_files: []