Skip to content
This repository has been archived by the owner on Nov 19, 2021. It is now read-only.

Commit

Permalink
Fix following of absolute redirect URL in Sinatra
Browse files Browse the repository at this point in the history
What's going on when the app redirects to an absolute URL?

* Sinatra relies on Rack::MockSession which sets SERVER_NAME to
  "example.org" [1] on request it makes. However, Webrat expects
  it to be "www.example.com"

* In MyClassyApp, the redirect URL is made out of Rack::Request#url [2]
  which uses  SERVER_NAME, which is set to "example.org" by Rack::MockSession.
  As a result, Webrat see it as an external redirect and don't follow it.

NOTE:

* SERVER_NAME is stricly equivalent to HTTP_HOST [3]
* This could have been fixed in Webrat::Session too. I'am not sure
  that it won't affect other frameworks so I left it intact.

      def request_page(url, http_method, data) #:nodoc:
         h = headers
         h['HTTP_REFERER'] = @current_url if @current_url
  +      h['HTTP_HOST']    = 'www.example.com'

[1] http://github.com/rack/rack/blob/5c00dd698edb953b4bee432fa12a20ba69a067c1/lib/rack/mock.rb#L79
[2] http://github.com/rack/rack/blob/5c00dd698edb953b4bee432fa12a20ba69a067c1/lib/rack/request.rb#L201
[3] http://github.com/rack/rack/blob/5c00dd698edb953b4bee432fa12a20ba69a067c1/lib/rack/request.rb#L72
  • Loading branch information
sr committed Feb 28, 2009
1 parent 7d7c322 commit 4e07f5b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/webrat/sinatra.rb
Expand Up @@ -30,6 +30,7 @@ def #{verb}(path, data, headers = {})
data[key] = Rack::Utils.unescape(value)
data
end
headers["HTTP_HOST"] = "www.example.com"
@browser.#{verb}(path, params, headers)
end
RUBY
Expand Down
8 changes: 8 additions & 0 deletions spec/integration/sinatra/modular_app.rb
Expand Up @@ -5,4 +5,12 @@ class MyModularApp < Sinatra::Default
get "/" do
"Hello World"
end

get "/redirect_absolute_url" do
redirect URI.join(request.url, "foo").to_s
end

get "/foo" do
"spam"
end
end
5 changes: 5 additions & 0 deletions spec/integration/sinatra/test/modular_app_test.rb
Expand Up @@ -13,4 +13,9 @@ def test_it_works
visit "/"
assert_contain "Hello World"
end

def test_redirects
visit "/redirect_absolute_url"
assert_equal "spam", response_body
end
end

0 comments on commit 4e07f5b

Please sign in to comment.