Skip to content

Commit

Permalink
Improved handling of User-Agent: Should be able to omit.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikael Wikman committed Apr 15, 2013
1 parent 99da0db commit 37ba2e6
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 5 additions & 1 deletion lib/em-http/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 38 additions & 0 deletions spec/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 2 additions & 0 deletions spec/stallion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!'
Expand Down

0 comments on commit 37ba2e6

Please sign in to comment.