#67 ✓out-of-date
diabolo

HTTP_REFERER being corrupted when following redirects

Reported by diabolo | November 20th, 2008 @ 09:40 AM | in 0.4.1

This is going to be a bit long there is quite a bit of semi-irrelevant stuff to set things up ... apologies

Bug introduced between 0.2 and 0.3.4 using gems.

The following feature now fails

Scenario: I can add remove items from the cart
  Given there are 4 products
  And I am on the products page
  When I add 2 products to the cart
  And remove 1 item from the cart
  Then 1 product should be in the cart
  When I add 2 products to the cart
  And remove 1 item from the cart
  Then 2 product should be in the cart

The relevant step is

When /^I add (\d+) product[s]? to the cart$/ do |n|
    get_products_not_in_cart[0,n.to_i].each do |product|
    clicks_link_within("#product_#{product.id}", "Add to Basket")
  end
end

The clicks link code goes into this controller

class CartItemsController < ApplicationController
  include ApplicationHelper # needed for find_cart
  def index
  end

  def new
    begin
      product = Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}")
      flash[:notice] = "Invalid product"
    else
      @cart = find_cart
      @cart.add(product)
    end
    respond_to do |format|
      format.html { redirect_to referer}
    end
  end

  def destroy
    @cart = find_cart
    @cart.remove(params[:id])

    respond_to do |format|
      format.html { redirect_to referer}
    end
  end

  private

  def referer
    ref = request.env["HTTP_REFERER"]
    ref ||= '/products'
  end
end

Now what is happening in the test is that the HTTP_REFERER is getting corrupted. After the first click link the feature should come from /products into the controller using the url /cart/new?id=x with HTTP_REFERER /products. Then all redirects should be followed and we should end up back on /products. However subsequent clicks_links instead have the HTTP_REFERER /cart/show/x which blows up when we redirect to it. Somewhere in the webrat code the /cart/new?id=x url is being converted to /cart/show/x and this is mistakenly used as the HTTP_REFERER. I've followed this at some length in the debugger, and am pretty sure that I've accurately described the symptoms and narrowed down the cause, but I was struggling to understand all that was happening in webrat, so I'm afraid I can't narrow it down any further.

Finallly I've included the stack trace that the error produced

Scenario: I can add remove items from the cart  # features/cart/view.feature:26
    Given there are 4 products                    # features/steps/product_steps.rb:5
    And I am on the products page                 # features/steps/product_steps.rb:35
    When I add 2 products to the cart             # features/steps/cart_steps.rb:46
    And remove 1 item from the cart               # features/steps/cart_steps.rb:80
    Then 1 product should be in the cart          # features/steps/cart_steps.rb:15
    When I add 2 products to the cart             # features/steps/cart_steps.rb:46
      No action responded to show (ActionController::UnknownAction)
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:579:in `call_filters'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:572:in `perform_action_without_benchmark'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
      /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/benchmarking.rb:68:in `perform_action_without_rescue'
      /Users/andy/dev/gitr/cucumber/main/cucumber/bin/../lib/cucumber/rails/world.rb:15:in `perform_action'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:in `send'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:529:in `process_without_filters'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/filters.rb:568:in `process_without_session_management_support'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/session_management.rb:130:in `sass_old_process'
      /Users/andy/dev/rails/tc/vendor/plugins/haml/lib/sass/plugin/rails.rb:19:in `process_without_test'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:16:in `process'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/base.rb:389:in `process'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:149:in `handle_request'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:107:in `dispatch'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:in `synchronize'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:104:in `dispatch'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:120:in `dispatch_cgi'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/dispatcher.rb:35:in `dispatch'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:285:in `process'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:176:in `get'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:124:in `follow_redirect!'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:134:in `request_via_redirect'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:47:in `do_request'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:20:in `get'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/session.rb:84:in `send'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/session.rb:84:in `request_page'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/link.rb:18:in `click'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/scope.rb:134:in `click_link'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/session.rb:129:in `clicks_link_within'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/session.rb:137:in `within'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/core/session.rb:128:in `clicks_link_within'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:88:in `send'
      /Library/Ruby/Gems/1.8/gems/webrat-0.3.2/lib/webrat/rails.rb:88:in `method_missing'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:501:in `send!'
      /Library/Ruby/Gems/1.8/gems/actionpack-2.1.1/lib/action_controller/integration.rb:501:in `method_missing'
      ./features/steps//cart_steps.rb:65:in `add_product'
      ./features/steps//cart_steps.rb:48:in `When /^I add (\d+) product[s]? to the cart$/'
      ./features/steps//cart_steps.rb:47:in `each'
      ./features/steps//cart_steps.rb:47:in `When /^I add (\d+) product[s]? to the cart$/'
      features/cart/view.feature:32:in `When I add 2 products to the cart'

Comments and changes to this ticket

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