Permalink
Browse files

Add User-Agent header by default. Closes #144.

You can remove User-Agent header by setting nil to
HTTPClient#agent_name.
  • Loading branch information...
1 parent b6ec07f commit fd5851f3eb148c42d3ff08178eae8e1352f8b469 @nahi committed Feb 24, 2013
Showing with 21 additions and 18 deletions.
  1. +5 −2 lib/httpclient.rb
  2. +2 −2 lib/httpclient/session.rb
  3. +4 −4 test/test_http-access2.rb
  4. +10 −10 test/test_httpclient.rb
View
7 lib/httpclient.rb
@@ -229,7 +229,7 @@
# ruby -rhttpclient -e 'p HTTPClient.head(ARGV.shift).header["last-modified"]' http://dev.ctor.org/
#
class HTTPClient
- RUBY_VERSION_STRING = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]"
+ RUBY_VERSION_STRING = "ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE})"
LIB_NAME = "(#{VERSION}, #{RUBY_VERSION_STRING})"
include Util
@@ -357,6 +357,9 @@ def attr_proxy(symbol, assignable = false)
# Default header for PROPFIND request.
PROPFIND_DEFAULT_EXTHEADER = { 'Depth' => '0' }
+ # Default User-Agent header
+ DEFAULT_AGENT_NAME = 'HTTPClient/1.0'
+
# Creates a HTTPClient instance which manages sessions, cookies, etc.
#
# HTTPClient.new takes 3 optional arguments for proxy url string,
@@ -385,7 +388,7 @@ def initialize(*args)
@redirect_uri_callback = method(:default_redirect_uri_callback)
@test_loopback_response = []
@session_manager = SessionManager.new(self)
- @session_manager.agent_name = agent_name
+ @session_manager.agent_name = agent_name || DEFAULT_AGENT_NAME
@session_manager.from = from
@session_manager.ssl_config = @ssl_config = SSLConfig.new(self)
@cookie_manager = WebAgent::CookieManager.new
View
4 lib/httpclient/session.rb
@@ -726,10 +726,10 @@ def set_header(req)
req.http_version = $1
end
end
- if @agent_name
+ if @agent_name && req.header.get('User-Agent').empty?
req.header.set('User-Agent', "#{@agent_name} #{LIB_NAME}")
end
- if @from
+ if @from && req.header.get('From').empty?
req.header.set('From', @from)
end
if req.header.get('Accept').empty?
View
8 test/test_http-access2.rb
@@ -47,7 +47,7 @@ def test_from
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
- assert_match(/^From: from_bar/, lines[4])
+ assert_match(/^From: from_bar/, lines[5])
end
def test_debug_dev
@@ -81,8 +81,8 @@ def test_protocol_version_http10
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET /hello HTTP/1.0", lines[3])
- assert_equal("Connection: close", lines[6])
- assert_equal("= Response", lines[7])
+ assert_equal("Connection: close", lines[7])
+ assert_equal("= Response", lines[8])
end
def test_protocol_version_http11
@@ -93,7 +93,7 @@ def test_protocol_version_http11
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
- assert_equal("Host: localhost:#{serverport}", lines[6])
+ assert_equal("Host: localhost:#{serverport}", lines[7])
@client.protocol_version = 'HTTP/1.1'
str = ""
@client.debug_dev = str
View
20 test/test_httpclient.rb
@@ -44,7 +44,7 @@ def test_from
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
assert_equal("= Request", lines[0])
- assert_match(/^From: from_bar/, lines[4])
+ assert_match(/^From: from_bar/, lines[5])
end
def test_debug_dev
@@ -77,10 +77,10 @@ def test_protocol_version_http09
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET /hello HTTP/0.9", lines[3])
- assert_equal("Connection: close", lines[6])
- assert_equal("= Response", lines[7])
- assert_match(/^hello$/, lines[8])
- assert_match(/^world$/, lines[9])
+ assert_equal("Connection: close", lines[7])
+ assert_equal("= Response", lines[8])
+ assert_match(/^hello$/, lines[9])
+ assert_match(/^world$/, lines[10])
end
def test_protocol_version_http10
@@ -94,16 +94,16 @@ def test_protocol_version_http10
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET /hello HTTP/1.0", lines[3])
- assert_equal("Connection: close", lines[6])
- assert_equal("= Response", lines[7])
+ assert_equal("Connection: close", lines[7])
+ assert_equal("= Response", lines[8])
end
def test_header_accept_by_default
str = ""
@client.debug_dev = str
@client.get(serverurl)
lines = str.split(/(?:\r?\n)+/)
- assert_equal("Accept: */*", lines[4])
+ assert_equal("Accept: */*", lines[5])
end
def test_header_accept
@@ -122,7 +122,7 @@ def test_host_given
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
- assert_equal("Host: localhost:#{serverport}", lines[6])
+ assert_equal("Host: localhost:#{serverport}", lines[7])
#
@client.reset_all
str = ""
@@ -152,7 +152,7 @@ def test_protocol_version_http11
assert_equal("= Request", lines[0])
assert_equal("! CONNECTION ESTABLISHED", lines[2])
assert_equal("GET / HTTP/1.1", lines[3])
- assert_equal("Host: localhost:#{serverport}", lines[6])
+ assert_equal("Host: localhost:#{serverport}", lines[7])
@client.protocol_version = 'HTTP/1.1'
assert_equal('HTTP/1.1', @client.protocol_version)
str = ""

1 comment on commit fd5851f

@buildhive

Hiroshi Nakamura » httpclient #61 FAILURE
Looks like this commit caused a build failure
(what's this?)

Please sign in to comment.