proxy-server 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/proxy-server.gemspec CHANGED
@@ -1,36 +1,36 @@
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.6'
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
- s.add_dependency 'goliath', '0.9.2'
19
- s.add_dependency 'em-synchrony'
20
- s.add_dependency 'em-http-request'
21
-
22
- s.add_development_dependency 'rake', '>= 0.9.2'
23
- s.add_development_dependency 'rspec', '>= 2.7.0'
24
- s.add_development_dependency 'simplecov', '>= 0.4.2'
25
- s.add_development_dependency 'webmock'
26
-
27
-
28
- s.add_development_dependency 'rack-test', '>= 0.5.7'
29
-
30
- s.rubygems_version = ">= 1.6.1"
31
- s.files = `git ls-files`.split("\n")
32
- s.test_files = `git ls-files -- {spec}/*`.split("\n")
33
- s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
- s.rdoc_options = ["--charset=UTF-8"]
35
- 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.7'
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
+ s.add_dependency 'goliath', '0.9.2'
19
+ s.add_dependency 'em-synchrony'
20
+ s.add_dependency 'em-http-request'
21
+
22
+ s.add_development_dependency 'rake', '>= 0.9.2'
23
+ s.add_development_dependency 'rspec', '>= 2.7.0'
24
+ s.add_development_dependency 'simplecov', '>= 0.4.2'
25
+ s.add_development_dependency 'webmock'
26
+
27
+
28
+ s.add_development_dependency 'rack-test', '>= 0.5.7'
29
+
30
+ s.rubygems_version = ">= 1.6.1"
31
+ s.files = `git ls-files`.split("\n")
32
+ s.test_files = `git ls-files -- {spec}/*`.split("\n")
33
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_path = "lib"
36
36
  end
@@ -0,0 +1,99 @@
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/proxy/proxy_app'
3
+ require_relative '../../lib/proxy/proxy_manager'
4
+ require 'rack/test'
5
+ require 'webmock/rspec'
6
+
7
+ describe ProxyApp do
8
+ include Rack::Test::Methods
9
+
10
+ before do
11
+ @proxy_server = mock('ProxyServer')
12
+ @proxy_manager = ProxyManager.new
13
+ @proxy_manager.stub(:start_proxy).with(anything()).and_return(@proxy_server)
14
+ @proxy_manager.stub(:get_proxy).with(anything()).and_return(@proxy_server)
15
+ end
16
+
17
+ let(:app) { ProxyApp.new(@proxy_manager) }
18
+
19
+ it "should create a new proxy" do
20
+ @proxy_manager.should_receive(:new_proxy).and_return(URI('http://localhost:4000'))
21
+ response = post '/proxies'
22
+ proxy = JSON.parse(response.body)
23
+ proxy['port'].should == 4000
24
+ proxy['url'].should == "http://localhost:4000"
25
+ end
26
+
27
+ it "should create a new proxy with an upstream proxy when asked to" do
28
+ @proxy_manager.should_receive(:start_proxy).with(hash_including(:proxy => 'http://my_proxy:80'))
29
+ response = post '/proxies', {:proxy => 'http://my_proxy:80'}
30
+ end
31
+
32
+ it "should tell me about all the proxies that have been created" do
33
+ @proxy_manager.should_receive(:find_proxy_port).and_return(4000)
34
+
35
+ post '/proxies'
36
+ response = get '/proxies'
37
+ JSON.parse(response.body).should == [4000]
38
+ end
39
+
40
+ it "should remove all proxies when asked" do
41
+ post '/proxies'
42
+ delete '/proxies'
43
+
44
+ response = get '/proxies'
45
+ JSON.parse(response.body).should == []
46
+ end
47
+
48
+ it "should remove a proxy when asked to" do
49
+ running_proxy_port = JSON.parse(post('/proxies').body)['port']
50
+ ProxyManager.any_instance.should_receive(:stop_proxy).with(running_proxy_port)
51
+
52
+ response = delete "/proxies/#{running_proxy_port}"
53
+
54
+ response.status.should == 200
55
+ end
56
+
57
+ it "should assign new proxy ports when more than one is asked for" do
58
+ first_expected_proxy = 100
59
+ second_expected_proxy = 101
60
+ available_proxy_ports = [first_expected_proxy, second_expected_proxy]
61
+ ProxyManager.any_instance.stub(:find_proxy_port).and_return { available_proxy_ports.shift }
62
+
63
+ first_response = post '/proxies'
64
+ second_response = post '/proxies'
65
+
66
+ JSON.parse(first_response.body)['port'].should == first_expected_proxy
67
+ JSON.parse(second_response.body)['port'].should == second_expected_proxy
68
+ end
69
+
70
+ it "ads a given url to a list of them to track" do
71
+ track_url = 'public/.*.js'
72
+
73
+ @proxy_server.should_receive(:track_request).with(track_url)
74
+ post "/proxies/1111/requests", {:track => track_url}
75
+ end
76
+
77
+ it "ads a given url to a list of them to track" do
78
+ @proxy_server.stub!(:requests).and_return(['request 1', 'request 2'])
79
+ response = get "/proxies/1111/requests"
80
+ requests = JSON.parse(response.body)
81
+ requests.should include('request 1')
82
+ requests.should include('request 2')
83
+ end
84
+
85
+ it "can substitute a request with another body" do
86
+ @proxy_server.should_receive(:substitute_request).with('*.js', :body => 'alert(1);')
87
+ post "/proxies/1111/requests/substitute", {:pattern => '*.js', :body => 'alert(1);'}
88
+ end
89
+
90
+ it "can substitute a request with another url" do
91
+ @proxy_server.should_receive(:substitute_request).with('*.js', :url => 'http://example.com/test.js')
92
+ post "/proxies/1111/requests/substitute", {:pattern => '*.js', :url => 'http://example.com/test.js'}
93
+ end
94
+
95
+ it "can reset the configuration of a proxy" do
96
+ @proxy_server.should_receive(:reset)
97
+ post "/proxies/1111/reset"
98
+ end
99
+ end
@@ -1,96 +1,20 @@
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
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/proxy/proxy_manager'
3
+ require 'rack/test'
4
+
5
+ describe ProxyManager do
6
+ it "creates a new proxy" do
7
+ pm = ProxyManager.new
8
+
9
+ pm.should_receive(:find_proxy_port).and_return(4000)
10
+ pm.should_receive(:proxy_host_address).and_return('127.0.0.1')
11
+
12
+ pm.new_proxy.should == URI.parse("http://127.0.0.1:4000")
13
+ end
14
+
15
+ it "finds the address of this machine" do
16
+ expected_address = `ifconfig`.scan(/inet addr:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) Bcast:/)[0][0]
17
+ pm = ProxyManager.new
18
+ pm.proxy_host_address.should == expected_address
19
+ end
96
20
  end
@@ -1,119 +1,119 @@
1
- require_relative '../spec_helper'
2
- require_relative '../../lib/proxy/proxy_server'
3
-
4
- describe ProxyServer do
5
- it "can be configured with an upstream proxy" do
6
- proxy_uri = 'http://test-proxy:80'
7
- proxy = ProxyServer.new :proxy => proxy_uri
8
- proxy.upstream_proxy.should == URI.parse(proxy_uri)
9
- end
10
-
11
- it "should get the proxy settings" do
12
- proxy_uri = 'http://test-proxy:80'
13
-
14
- proxy = ProxyServer.new :proxy => proxy_uri
15
- options = {}
16
- proxy.get_proxy(options)
17
- options[:proxy].should == {:type => 'http', :host => 'test-proxy', :port => 80}
18
- end
19
-
20
- it "should ignore proxy settings if none specified" do
21
- proxy = ProxyServer.new
22
- options = {}
23
-
24
- proxy.get_proxy(options)
25
-
26
- options.should_not have_key :proxy
27
- end
28
-
29
- it "can be configured to run on a specific port" do
30
- proxy_port = 8080
31
- proxy = ProxyServer.new :port => proxy_port
32
- proxy.port.should == proxy_port
33
- end
34
-
35
- it "default the port when none is specified" do
36
- proxy = ProxyServer.new
37
- proxy.port.should == ProxyServer::DEFAULT_PORT
38
- end
39
-
40
- it "should track a request" do
41
- uri = "http://google.com"
42
- proxy = ProxyServer.new
43
-
44
- proxy.track uri
45
-
46
- proxy.requests.length.should == 1
47
- proxy.requests.should include uri
48
- end
49
-
50
- it "should determine whether tracking is enabled for a uri" do
51
- uri = "http://google.com"
52
- proxy = ProxyServer.new
53
-
54
- proxy.track_request('.*com')
55
-
56
- proxy.tracking_enabled?('http://www.google.com').should be_true
57
- proxy.tracking_enabled?('http://www.google.co.uk').should be_false
58
- end
59
-
60
- it "should determine whether there is a substitute for a uri" do
61
- proxy = ProxyServer.new
62
-
63
- proxy.substitute_request('.*com', :body => 'substitute_response')
64
-
65
- proxy.has_substitute?('http://www.google.com').should be_true
66
- proxy.has_substitute?('http://www.google.co.uk').should be_false
67
- end
68
-
69
- it "should substitute a request body" do
70
- proxy = ProxyServer.new
71
- substitute_response = 'substitute this'
72
- proxy.substitute_request('.*com', :body => substitute_response)
73
-
74
- proxy.substitute('http://www.google.com').should == [200, {}, [substitute_response]]
75
- end
76
-
77
- it "should substitute a request url" do
78
- proxy = ProxyServer.new
79
- substitute_url = 'http://www.google.co.uk'
80
- proxy.substitute_request('.*com', :url => substitute_url)
81
- proxy.should_receive(:request_uri).with(substitute_url)
82
-
83
- proxy.substitute('http://www.google.com')
84
- end
85
-
86
- it "reset causes request tracking to be removed" do
87
- proxy = ProxyServer.new
88
- proxy.requests << 'request 1' << 'request 2'
89
- proxy.track_requests << 'request 1' << 'request 2'
90
- proxy.reset
91
- proxy.requests.length.should == 0
92
- proxy.track_requests.length.should == 0
93
- end
94
-
95
- it "reset causes request substituting to be removed" do
96
- proxy = ProxyServer.new
97
- proxy.substitute_requests['test'] = 'item'
98
- proxy.reset
99
- proxy.substitute_requests.length.should == 0
100
- end
101
-
102
- context "using response" do
103
- it "tracks requests" do
104
- proxy = ProxyServer.new
105
- proxy.should_receive(:request_uri).with('http://www.google.com')
106
-
107
- proxy.track_request '.*google\.com'
108
- proxy.response('REQUEST_URI' => 'http://www.google.com')
109
- end
110
-
111
- it "substitutes a request" do
112
- proxy = ProxyServer.new
113
- substitute_body = 'substitute'
114
-
115
- proxy.substitute_request '.*google\.com', :body => substitute_body
116
- proxy.response('REQUEST_URI' => 'http://www.google.com')[2].should == [substitute_body]
117
- end
118
- end
119
- end
1
+ require_relative '../spec_helper'
2
+ require_relative '../../lib/proxy/proxy_server'
3
+
4
+ describe ProxyServer do
5
+ it "can be configured with an upstream proxy" do
6
+ proxy_uri = 'http://test-proxy:80'
7
+ proxy = ProxyServer.new :proxy => proxy_uri
8
+ proxy.upstream_proxy.should == URI.parse(proxy_uri)
9
+ end
10
+
11
+ it "should get the proxy settings" do
12
+ proxy_uri = 'http://test-proxy:80'
13
+
14
+ proxy = ProxyServer.new :proxy => proxy_uri
15
+ options = {}
16
+ proxy.get_proxy(options)
17
+ options[:proxy].should == {:type => 'http', :host => 'test-proxy', :port => 80}
18
+ end
19
+
20
+ it "should ignore proxy settings if none specified" do
21
+ proxy = ProxyServer.new
22
+ options = {}
23
+
24
+ proxy.get_proxy(options)
25
+
26
+ options.should_not have_key :proxy
27
+ end
28
+
29
+ it "can be configured to run on a specific port" do
30
+ proxy_port = 8080
31
+ proxy = ProxyServer.new :port => proxy_port
32
+ proxy.port.should == proxy_port
33
+ end
34
+
35
+ it "default the port when none is specified" do
36
+ proxy = ProxyServer.new
37
+ proxy.port.should == ProxyServer::DEFAULT_PORT
38
+ end
39
+
40
+ it "should track a request" do
41
+ uri = "http://google.com"
42
+ proxy = ProxyServer.new
43
+
44
+ proxy.track uri
45
+
46
+ proxy.requests.length.should == 1
47
+ proxy.requests.should include uri
48
+ end
49
+
50
+ it "should determine whether tracking is enabled for a uri" do
51
+ uri = "http://google.com"
52
+ proxy = ProxyServer.new
53
+
54
+ proxy.track_request('.*com')
55
+
56
+ proxy.tracking_enabled?('http://www.google.com').should be_true
57
+ proxy.tracking_enabled?('http://www.google.co.uk').should be_false
58
+ end
59
+
60
+ it "should determine whether there is a substitute for a uri" do
61
+ proxy = ProxyServer.new
62
+
63
+ proxy.substitute_request('.*com', :body => 'substitute_response')
64
+
65
+ proxy.has_substitute?('http://www.google.com').should be_true
66
+ proxy.has_substitute?('http://www.google.co.uk').should be_false
67
+ end
68
+
69
+ it "should substitute a request body" do
70
+ proxy = ProxyServer.new
71
+ substitute_response = 'substitute this'
72
+ proxy.substitute_request('.*com', :body => substitute_response)
73
+
74
+ proxy.substitute('http://www.google.com').should == [200, {}, [substitute_response]]
75
+ end
76
+
77
+ it "should substitute a request url" do
78
+ proxy = ProxyServer.new
79
+ substitute_url = 'http://www.google.co.uk'
80
+ proxy.substitute_request('.*com', :url => substitute_url)
81
+ proxy.should_receive(:request_uri).with(substitute_url)
82
+
83
+ proxy.substitute('http://www.google.com')
84
+ end
85
+
86
+ it "reset causes request tracking to be removed" do
87
+ proxy = ProxyServer.new
88
+ proxy.requests << 'request 1' << 'request 2'
89
+ proxy.track_requests << 'request 1' << 'request 2'
90
+ proxy.reset
91
+ proxy.requests.length.should == 0
92
+ proxy.track_requests.length.should == 0
93
+ end
94
+
95
+ it "reset causes request substituting to be removed" do
96
+ proxy = ProxyServer.new
97
+ proxy.substitute_requests['test'] = 'item'
98
+ proxy.reset
99
+ proxy.substitute_requests.length.should == 0
100
+ end
101
+
102
+ context "using response" do
103
+ it "tracks requests" do
104
+ proxy = ProxyServer.new
105
+ proxy.should_receive(:request_uri).with('http://www.google.com')
106
+
107
+ proxy.track_request '.*google\.com'
108
+ proxy.response('REQUEST_URI' => 'http://www.google.com')
109
+ end
110
+
111
+ it "substitutes a request" do
112
+ proxy = ProxyServer.new
113
+ substitute_body = 'substitute'
114
+
115
+ proxy.substitute_request '.*google\.com', :body => substitute_body
116
+ proxy.response('REQUEST_URI' => 'http://www.google.com')[2].should == [substitute_body]
117
+ end
118
+ end
119
+ end