Permalink
Browse files

Improved handling of User-Agent: Should be able to omit.

  • Loading branch information...
1 parent 99da0db commit 37ba2e6268e035aacd0096558e410123b445fd14 Mikael Wikman committed Apr 11, 2013
Showing with 49 additions and 1 deletion.
  1. +4 −0 Changelog.md
  2. +5 −1 lib/em-http/client.rb
  3. +38 −0 spec/client_spec.rb
  4. +2 −0 spec/stallion.rb
View
4 Changelog.md
@@ -1,5 +1,9 @@
# Changelog
+## master
+
+- User-Agent header is now removed if set to nil.
+
## 1.0.0.beta.1 / 2011-02-20 - The big rewrite
- Switched parser from Ragel to http_parser.rb
View
6 lib/em-http/client.rb
@@ -154,7 +154,11 @@ def build_request
head['host'] ||= encode_host
# Set the User-Agent if it hasn't been specified
- head['user-agent'] ||= "EventMachine HttpClient"
+ if not head.key?('user-agent')
+ head['user-agent'] = "EventMachine HttpClient"
+ elsif head['user-agent'].nil?
+ head.delete('user-agent')
+ end
# Set the auth from the URI if given
head['Authorization'] = @req.uri.userinfo.split(/:/, 2) if @req.uri.userinfo
View
38 spec/client_spec.rb
@@ -831,4 +831,42 @@ def failed(http=nil)
}
}
end
+
+ context "User-Agent" do
+ it 'should default to "EventMachine HttpClient"' do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get
+
+ http.errback { failed(http) }
+ http.callback {
+ http.response.should == '"EventMachine HttpClient"'
+ EventMachine.stop
+ }
+ }
+ end
+
+ it 'should keep header if given empty string' do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get(:head => { 'user-agent'=>'' })
+
+ http.errback { failed(http) }
+ http.callback {
+ http.response.should == '""'
+ EventMachine.stop
+ }
+ }
+ end
+
+ it 'should ommit header if given nil' do
+ EventMachine.run {
+ http = EventMachine::HttpRequest.new('http://127.0.0.1:8090/echo-user-agent').get(:head => { 'user-agent'=>nil })
+
+ http.errback { failed(http) }
+ http.callback {
+ http.response.should == 'nil'
+ EventMachine.stop
+ }
+ }
+ end
+ end
end
View
2 spec/stallion.rb
@@ -228,6 +228,8 @@ def self.call(env)
elsif stable.request.path_info == '/relative-location'
stable.response.status = 301
stable.response["Location"] = '/forwarded'
+ elsif stable.request.path_info == '/echo-user-agent'
+ stable.response.write stable.request.env["HTTP_USER_AGENT"].inspect
elsif
stable.response.write 'Hello, World!'

0 comments on commit 37ba2e6

Please sign in to comment.