Skip to content
Browse files

Merge branch 'dont_include_default_ports' of github.com:wader/em-http…

…-request
  • Loading branch information...
2 parents 041c3e9 + a6ce53e commit eb24fc1ee44405d8b97c83b3bf7392df2b88864a @igrigorik igrigorik committed Oct 25, 2013
Showing with 65 additions and 8 deletions.
  1. +4 −7 lib/em-http/http_client_options.rb
  2. +1 −1 lib/em-http/http_encoding.rb
  3. +44 −0 spec/redirect_spec.rb
  4. +16 −0 spec/stallion.rb
View
11 lib/em-http/http_client_options.rb
@@ -35,17 +35,14 @@ def set_uri(uri, path = nil)
@uri = uri
@path = uri.path
+ @host = uri.host
+ @port = uri.port
# Make sure the ports are set as Addressable::URI doesn't
# set the port if it isn't there
- if @uri.scheme == "https"
- @uri.port ||= 443
- else
- @uri.port ||= 80
+ if @port.nil?
+ @port = @uri.scheme == "https" ? 443 : 80
end
- @host = @uri.host
- @port = @uri.port
-
end
end
View
2 lib/em-http/http_encoding.rb
@@ -39,7 +39,7 @@ def munge_header_keys(head)
end
def encode_host
- if @req.uri.port == 80 || @req.uri.port == 443
+ if @req.uri.port.nil? || @req.uri.port == 80 || @req.uri.port == 443
return @req.uri.host
else
@req.uri.host + ":#{@req.uri.port}"
View
44 spec/redirect_spec.rb
@@ -318,4 +318,48 @@ def response(r)
}
end
+ it "should not add default http port to redirect url that don't include it" do
+ EventMachine.run {
+ conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_no_port')
+ http = conn.get :redirects => 1
+ http.errback {
+ http.last_effective_url.to_s.should == 'http://host/'
+ EM.stop
+ }
+ }
+ end
+
+ it "should not add default https port to redirect url that don't include it" do
+ EventMachine.run {
+ conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_no_port')
+ http = conn.get :redirects => 1
+ http.errback {
+ http.last_effective_url.to_s.should == 'https://host/'
+ EM.stop
+ }
+ }
+ end
+
+ it "should keep default http port in redirect url that include it" do
+ EventMachine.run {
+ conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/http_with_port')
+ http = conn.get :redirects => 1
+ http.errback {
+ http.last_effective_url.to_s.should == 'http://host:80/'
+ EM.stop
+ }
+ }
+ end
+
+ it "should keep default https port in redirect url that include it" do
+ EventMachine.run {
+ conn = EventMachine::HttpRequest.new('http://127.0.0.1:8090/redirect/https_with_port')
+ http = conn.get :redirects => 1
+ http.errback {
+ http.last_effective_url.to_s.should == 'https://host:443/'
+ EM.stop
+ }
+ }
+ end
+
end
View
16 spec/stallion.rb
@@ -185,6 +185,22 @@ def self.call(env)
stable.response.status = 301
stable.response["Location"] = "http://$$$@$!%&^"
+ elsif stable.request.path_info == '/redirect/http_no_port'
+ stable.response.status = 301
+ stable.response["Location"] = "http://host/"
+
+ elsif stable.request.path_info == '/redirect/https_no_port'
+ stable.response.status = 301
+ stable.response["Location"] = "https://host/"
+
+ elsif stable.request.path_info == '/redirect/http_with_port'
+ stable.response.status = 301
+ stable.response["Location"] = "http://host:80/"
+
+ elsif stable.request.path_info == '/redirect/https_with_port'
+ stable.response.status = 301
+ stable.response["Location"] = "https://host:443/"
+
elsif stable.request.path_info == '/gzip'
io = StringIO.new
gzip = Zlib::GzipWriter.new(io)

0 comments on commit eb24fc1

Please sign in to comment.
Something went wrong with that request. Please try again.