Permalink
Browse files

Allow a service name or port number string for the proxy port value. …

…Issue #167
  • Loading branch information...
1 parent 1fc525c commit aa885ded9925824f4bf927c87f142bff94fbac6f @drbrain drbrain committed Nov 9, 2011
Showing with 50 additions and 6 deletions.
  1. +2 −0 CHANGELOG.rdoc
  2. +17 −1 lib/mechanize/http/agent.rb
  3. +31 −5 test/test_mechanize_http_agent.rb
View
@@ -48,6 +48,8 @@
* Mechanize::Cookie.new accepts attributes in a hash.
* Mechanize::CookieJar#<<(cookie) (alias: add!) is added. Issue #139
* Different mechanize instances may now have different loggers. Issue #122
+ * Mechanize now accepts a proxy port as a service name or number string.
+ Issue #167
* Bug fixes
* Mechanize takes more care to avoid saving files with certain unsafe names.
@@ -973,10 +973,26 @@ def set_http
end
end
+ ##
# Sets the proxy address, port, user, and password +addr+ should be a host,
- # with no "http://"
+ # with no "http://", +port+ may be a port number, service name or port
+ # number string.
+
def set_proxy(addr, port, user = nil, pass = nil)
return unless addr and port
+
+ unless Integer === port then
+ begin
+ port = Socket.getservbyname port
+ rescue SocketError
+ begin
+ port = Integer port
+ rescue ArgumentError
+ raise ArgumentError, "invalid value for port: #{port.inspect}"
+ end
+ end
+ end
+
@proxy_uri = URI "http://#{addr}"
@proxy_uri.port = port
@proxy_uri.user = user if user
@@ -1167,12 +1167,38 @@ def test_set_http_retry_change_request
end
def test_set_proxy
- @agent.set_proxy('www.example.com', 9001, 'joe', 'lol')
+ @agent.set_proxy 'www.example.com', 9001, 'joe', 'lol'
- assert_equal(@agent.proxy_uri.host, 'www.example.com')
- assert_equal(@agent.proxy_uri.port, 9001)
- assert_equal(@agent.proxy_uri.user, 'joe')
- assert_equal(@agent.proxy_uri.password, 'lol')
+ assert_equal @agent.proxy_uri.host, 'www.example.com'
+ assert_equal @agent.proxy_uri.port, 9001
+ assert_equal @agent.proxy_uri.user, 'joe'
+ assert_equal @agent.proxy_uri.password, 'lol'
+ end
+
+ def test_set_proxy_port_string
+ @agent.set_proxy 'www.example.com', '9001', 'joe', 'lol'
+
+ assert_equal @agent.proxy_uri.host, 'www.example.com'
+ assert_equal @agent.proxy_uri.port, 9001
+ assert_equal @agent.proxy_uri.user, 'joe'
+ assert_equal @agent.proxy_uri.password, 'lol'
+ end
+
+ def test_set_proxy_service_name
+ @agent.set_proxy 'www.example.com', 'http', 'joe', 'lol'
+
+ assert_equal @agent.proxy_uri.host, 'www.example.com'
+ assert_equal @agent.proxy_uri.port, 80
+ assert_equal @agent.proxy_uri.user, 'joe'
+ assert_equal @agent.proxy_uri.password, 'lol'
+ end
+
+ def test_set_proxy_service_name_bad
+ e = assert_raises ArgumentError do
+ @agent.set_proxy 'www.example.com', 'nonexistent service', 'joe', 'lol'
+ end
+
+ assert_equal 'invalid value for port: "nonexistent service"', e.message
end
end

0 comments on commit aa885de

Please sign in to comment.