Setting cookie value in test


#1

In the initial version of my application I am just going to require
cookies - down the road considering changing that and providing
something that works without.

Anyway, this is somewhat of a two part question

  1. Does this approach make sense or is there a better way typically
    used?
    Just have a before_filter in my application.rb to verify that the
    :_session_id cookie is present. If it isn’t: kick them out to some
    page that gives them the “you need cookies” message. If it’s there:
    good, go ahead with whatever their request was.

  2. Given that I am going to use the approach above: my functional
    tests will need to add the :_session_id cookie before making their
    requests. I am having a bit of trouble doing this. The simple

cookies[:_session_id] = “faked out”

doesn’t seem to work.

@controller.cookies[] is protected
Seems like I’m missing something pretty obvious this morning.

Thanks in advance

  • jim

#2

Is anyone wants to pick off just one of these and leave the other
unanswered - that’s fine too :slight_smile:


#3

Hello Jim !

2006/5/11, Jim H. removed_email_address@domain.invalid:

good, go ahead with whatever their request was.
That probably won’t work. The first time a person comes in, they sure
won’t have a session going, so they’ll get kicked out saying “You need
cookies”. They’ll check, notice they accept cookies, and dump the
site since it doesn’t work.

Of course, if you have a special entry page that doesn’t require the
cookies, you’ll be fine.

  1. Given that I am going to use the approach above: my functional
    tests will need to add the :_session_id cookie before making their
    requests. I am having a bit of trouble doing this. The simple

See my blog post:
http://blog.teksol.info/articles/2006/04/18/using-cookies-in-functional-tests

Hope that helps !


#4

Thanks for the help François. It’s much appreciated!

It didn’t perfectly fit what I was struggling with (my fault, I don’t
think I was clear enough)… detail below.

On 5/11/06, Francois B. removed_email_address@domain.invalid wrote:

Just have a before_filter in my application.rb to verify that the
cookies, you’ll be fine.

These were my initial thoughts too but I figured: what the heck, I’ll
try it out. I am seeing the results that I desire here: haven’t
looked too much deeper but it seems that the session_id cookie is
being added (when enabled) prior to the before_filter executing so
that my check for it’s presence is working.

  1. Given that I am going to use the approach above: my functional
    tests will need to add the :_session_id cookie before making their
    requests. I am having a bit of trouble doing this. The simple

See my blog post:
http://blog.teksol.info/articles/2006/04/18/using-cookies-in-functional-tests

What I was actually struggling with was that in my web application
everything was already working fine when accessing the cookie like:
cookies[:_session_id]

and my test was already working when using (note: here “@request” but
not above)
@request.cookies["_session_id"]

Since my before_filter is used both by test and application… I
needed to find a way to access the cookie in a uniform way in my
before_filter.

Anyway, I seem to have come up with something that works and have
posted on it (at length :slight_smile: ) here:
http://jameshalberg.wordpress.com/2006/05/12/requiring-and-testing-cookies/

Thanks again