diff --git a/lib/rack/ssl.rb b/lib/rack/ssl.rb index 1d02629..3373fe9 100644 --- a/lib/rack/ssl.rb +++ b/lib/rack/ssl.rb @@ -18,6 +18,7 @@ def initialize(app, options = {}) @exclude = options[:exclude] @host = options[:host] + @port = options[:port] end def call(env) @@ -50,6 +51,7 @@ def redirect_to_https(env) url = URI(req.url) url.scheme = "https" url.host = @host if @host + url.port = @port if @port headers = hsts_headers.merge('Content-Type' => 'text/html', 'Location' => url.to_s) diff --git a/test/test_ssl.rb b/test/test_ssl.rb index e8b3cc0..f27c685 100644 --- a/test/test_ssl.rb +++ b/test/test_ssl.rb @@ -124,6 +124,20 @@ def test_redirect_to_host last_response.headers['Location'] end + def test_redirect_to_port + self.app = Rack::SSL.new(default_app, :port => 8443) + get "http://example.org/path?key=value" + assert_equal "https://example.org:8443/path?key=value", + last_response.headers['Location'] + end + + def test_redirect_to_host_and_port + self.app = Rack::SSL.new(default_app, :host => "ssl.example.org", :port => 8443) + get "http://example.org/path?key=value" + assert_equal "https://ssl.example.org:8443/path?key=value", + last_response.headers['Location'] + end + def test_redirect_to_secure_host_when_on_subdomain self.app = Rack::SSL.new(default_app, :host => "ssl.example.org") get "http://ssl.example.org/path?key=value"