# Setting cookie value in test

**URL:** <https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254>\
**Category:** Rails\
**Created:** [May 11, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254 "2006-05-11T17:17:10Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [May 11, 2006, 5:17pm UTC](https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254/1 "2006-05-11T17:17:10Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [May 11, 2006, 7:40pm UTC](https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254/2 "2006-05-11T19:40:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [May 12, 2006, 1:12am UTC](https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254/3 "2006-05-12T01:12:37Z")

</div>

Hello Jim !

2006/5/11, Jim H. [removed\_email\_address@domain.invalid](mailto: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](http://blog.teksol.info/articles/2006/04/18/using-cookies-in-functional-tests)

Hope that helps !

---

<div class="post-metadata">

**Author:** ![jim-harris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/278dde/32.png) [@jim-harris](https://www.ruby-forum.com/u/jim-harris)\
**Post date:** [May 12, 2006, 5:07pm UTC](https://www.ruby-forum.com/t/setting-cookie-value-in-test/60254/4 "2006-05-12T17:07:29Z")

</div>

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](mailto: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](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 🙂 ) here:

> **[Requiring and Testing Cookies](https://jameshalberg.wordpress.com/2006/05/12/requiring-and-testing-cookies/)**
>
> Requirement: Users must have cookies enabled to use the site. If they don't: give them some sort of graceful warning to let them know there's going to be trouble. Doesn't sound too b…

Thanks again
