Can't set cookies in functional tests

Howdy,

I am having difficulty setting cookies in functional
tests. My application contains a user authentication
system based on cookies, and so to test areas of the
application dependent on whether the user is
authorized, I need to be able to set cookies in my
functional tests. The odd thing is that it seems
possible to set cookies only under certain
circumstances. Allow me to explain: my UserController
handles the task of receiving user authentication
credentials and setting a cookie to indicate the user
is logged in, as well as other mundane activities such
as allowing the user to edit the information stored in
his profile. To test the “edit profile” action
as an authenticated user, I first send a POST request
containing the appropriate username and password to
the “login” action. After the request completes, a
cookie containing the user ID and an authentication
token unique to that user will be set, so I simply
call “@request.cookies = cookies” in my test and make
my subsequent request to the “edit profile” action.
ApplicationController “sees” the necessary cookies,
the user is recognized as having logged in, and
everything works just as one would expect.

The trouble comes when attempting to test other
controllers as an authenticated user, for
ApplicationController is unable to see anything I
store in cookies prior to making a request from my
functional test. I will do something such as this in
my test:

@request.cookies = { ‘user_id’ => ‘1’ }; get
:something

And then insert the following into my
ApplicationController to see exactly what it is
receiving via cookies:

require ‘pp’; pp cookies; puts cookies.has_key?
‘user_id’; puts cookies[‘user_id’].nil?; puts
cookies[‘user_id’]

Calling “pp cookies” will print out the same hash I
set in my functional test, and
cookies.has_key?(‘user_id’) returns true, so
everything looks as if it is working as it ought to.
But “cookies[‘user_id’].nil?” is true, and printing
cookies[‘user_id’] prints nil! Although calling pp (or
puts) on cookies shows everything is fine, I’m not
able to actually reference the individual values
stored in the hash by key. The
same diagnostic code in ApplicationController run when
testing UserController works fine.

The one peculiarity I ran into is that in testing
UserController, which works properly, calling
cookies() in the functional test after making a
request returns a hash in an odd format:

{“user_id”=>[“1”]}

The value, rather than being only a string, is a
string inside a single-element array. Using this exact
format in setting cookies from the problematic
functional tests does not result in different
behaviour in A
pplicationController, however.

Whatever assistance (or sympathy ;)) you can provide
will be much appreciated.

Thanks,

Jeff


Yahoo! DSL ? Something to write home about.
Just $16.99/mo. or less.
dsl.yahoo.com