Permalink
Browse files

Add Request#url

darcs-hash:20070221170342-4fc50-415bd697c2f0dcb8c261b79a211ec6ab6034bb62.gz
  • Loading branch information...
1 parent 442ed2d commit fd51d36318ccfd43944c184fa4ae8366a2331752 @chneukirchen chneukirchen committed Feb 21, 2007
Showing with 35 additions and 0 deletions.
  1. +19 −0 lib/rack/request.rb
  2. +16 −0 test/spec_rack_request.rb
View
@@ -65,5 +65,24 @@ def cookies
def xhr?
@env["HTTP_X_REQUESTED_WITH"] == "XMLHttpRequest"
end
+
+ def url
+ url = scheme + "://"
+ url << host
+
+ if scheme == "https" && port != 443 ||
+ scheme == "http" && port != 80
+ url << ":#{port}"
+ end
+
+ url << script_name
+ url << path_info
+
+ if @env["QUERY_STRING"] && !@env["QUERY_STRING"].empty?
+ url << "?" << @env["QUERY_STRING"]
+ end
+
+ url
+ end
end
end
View
@@ -93,4 +93,20 @@
req = Rack::Request.new(e=TestRequest.env({}))
req.env.should.be e
end
+
+ specify "can restore the URL" do
+ Rack::Request.new(TestRequest.env({})).url.
+ should.equal "http://example.org:8080/"
+ Rack::Request.new(TestRequest.env({"SCRIPT_NAME" => "/foo"})).url.
+ should.equal "http://example.org:8080/foo/"
+ Rack::Request.new(TestRequest.env({"PATH_INFO" => "/foo"})).url.
+ should.equal "http://example.org:8080/foo"
+ Rack::Request.new(TestRequest.env({"QUERY_STRING" => "foo"})).url.
+ should.equal "http://example.org:8080/?foo"
+ Rack::Request.new(TestRequest.env({"SERVER_PORT" => "80"})).url.
+ should.equal "http://example.org/"
+ Rack::Request.new(TestRequest.env({"SERVER_PORT" => "443",
+ "rack.url_scheme" => "https"})).url.
+ should.equal "https://example.org/"
+ end
end

0 comments on commit fd51d36

Please sign in to comment.