#362 new
jarrett (at uchicago)

HTTP_REFERER should include query string

Reported by jarrett (at uchicago) | March 2nd, 2010 @ 04:06 PM

Webrat ignores the query string when setting the referrer header. In my experience, browsers include the query string, which seems consistent with the concept of URI opacity. The practical implication, for me at least, is that I cannot test a section of my app that takes into account the query string in the referrer header.

This can be fixed by modifying webrat/core/session.rb, line 114 (#request_page).

The original:

h['HTTP_REFERER'] = @current_url if @current_url

My version:

if @current_url
  if @data and !@data.empty?
    h['HTTP_REFERER'] = @current_url + '?' + @data.inject([]) { |pairs, (key, val)|
      pairs << "#{key}=#{val}"
    }.join('&')
  else
    h['HTTP_REFERER'] = @current_url
  end
end

For the sake of URI opacity, it would be even better if we didn't have to reconstruct the URI from @current_url and @data, but rather used the original URI string as-is. I don't know if this is possible or practical.

Speaking of opacity, my code still leaves out the scheme and host. The browsers I've tested include those. So it might be a good idea to add them as well.

Comments and changes to this ticket

  • jarrett (at uchicago)

    jarrett (at uchicago) March 2nd, 2010 @ 04:24 PM

    For anyone who's having the same problem, here's my workaround. Despite the code above, I actually prefer not to patch my gem source, because my changes will be lost on the next upgrade. So, here's a hotfix that you can use in your test suite. It basically does the same thing, just in a different place:

    module Webrat
      class Session
        # https://webrat.lighthouseapp.com/projects/10503/tickets/362-http_referer-should-include-query-string#ticket-362-1
    
        def process_request_with_query_string_in_referrer(http_method, url, data, headers)
          if @current_url
            if @data and !@data.empty?
              headers['HTTP_REFERER'] = @current_url + '?' + @data.inject([]) { |pairs, (key, val)|
                pairs << "#{key}=#{val}"
              }.join('&')
            else
              headers['HTTP_REFERER'] = @current_url
            end
          end
          process_request_without_query_string_in_referrer(http_method, url, data, headers)
        end
    
        alias_method :process_request_without_query_string_in_referrer, :process_request
        alias_method :process_request, :process_request_with_query_string_in_referrer
      end
    end
    

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Ruby Acceptance Testing for Web applications.

Shared Ticket Bins

People watching this ticket

Pages