Tuesday, July 31, 2007

Testing RESTful route redirection in Ruby on Rails

If you're doing functional testing with rails, you'll undoubtedly use follow_redirect - it allows you to follow the redirection for further tests.

  def test_something
    get :index

    assert_response :redirect

    follow_redirect

    assert_response :success
  end
However, the above code doesn't work when redirecting to a RESTful resource path. You'll get an error:

Exception: can't convert Symbol to String

This is because the URL helpers for restful resources return a string, where follow_redirect expects a hash.

The following code in your test_helper.rb file resolves this. It checks the type first to see if the normal follow_redirect should be called.

If not, it parses the path and calls the appropriate action thus following the redirection.

  alias_method_chain :follow_redirect, :restful_routes



  def follow_redirect_with_restful_routes
    #use the normal one unless its a string
    return follow_redirect_without_restful_routes unless @response.redirected_to.is_a?(String)
    #okay we need to follow the redirect, but first parse the path
    url = URI.parse(@response.redirected_to)

    path = url.path

    extras = CGI.parse(url.query)

    #parse puts values into array so flatten
    extras.each do |key, value|
      extras[key] = value[0] if value.is_a?(Array) && value.length == 1
    end


    # Assume given controller
    request = ActionController::TestRequest.new({}, {}, nil)
    request.env["REQUEST_METHOD"] = "GET"
    equest.path = path

    redirected_controller = ActionController::Routing::Routes.recognize(request)

    if @controller.is_a?(redirected_controller)
      #then we can redirect, otherwise we can't'
      get request.path_parameters[:action], extras.symbolize_keys!
    else
      raise "Can't follow redirects outside of current controller (from #{@controller.controller_name} to #{redirected_controller})"
    end
  end

Friday, July 13, 2007

Dublin Codejam

One of the exciting things happening with the Ruby Ireland group is Code Jam.

The principle was first proposed by Colm McHugh.

Oliver has setup a wiki page to discuss possible projects.

Monday, July 09, 2007

My ex-employer Oracle looking for rails developers

If anymore evidence was needed that rails is going mainstream then here it is (via Ruby Inside).

Friday, July 06, 2007

Amazon EC2 - Paid AMIs

Amazon Machine Instances (AMI) can be created with a specific software stack, registered and then run on EC2. Up until now they've relied on the community for public AMIs and I've used some of them. They have just announced paid AMIs where developers/companies can charge for prepackaged AMIs.