Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Cookie value set in functional test, key present in controller but unreadable

#6272

Initially I posted a message on rspec-users, but was told[0] that this sounds like a Rails bug.

I set a value in controller spec using @request.cookies:
https://gist.github.com/371356ba0a19666fd3b5

but when the controller reads it, it's nil somehow, even though the key is present, as this screenshot from the debugger shows:
http://dl.dropbox.com/u/830772/p/Selection_033.jpeg

This happens in test environment only. Eventually I figured out that I can use request.cookies in controller to make it work. But it's strange that I need to.

I'm using RSpec 2.4 and Rails 3.0.3.

[0] http://rubyforge.org/pipermail/rspec-users/2011-January/019143.html

Reported by Marko Anastasov · January 10th, 2011 @ 02:19 PM

State: resolved
Milestone: 3.0.6
Assigned to: Andrew White Andrew White
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • State changed from new to open
    • Milestone set to 3.0.6
    • Assigned user set to Andrew White
    • Importance changed from to Low

    It looks as though this has been addressed in master somewhat, but you still need to use string keys when assigning the cookie in the request. The cookies hash is cached in the request and the symbol key is assigned. However the access in the controller is done using a string key and the nil value is cached in the hash so you end up with both the string and symbol keys in the cookies hash.

    The fix is to clear the cookies instance variable when the request object is recycled, after first writing out the cookies to HTTP_COOKIE in the environment hash. AD::TestRequest.write_cookies! also needs fixing so that it escapes the cookie values as well.

    I'll come up with a patch this weekend, in the meantime just assign cookie value using a string key.

    March 5th, 2011 @ 12:19 PM

  2. Repository
    Repository
    • State changed from open to resolved

    (from [e2523ff68309f444cd052031c1fe8a3030d2865a]) Improve testing of cookies in functional tests: - cookies can be set using string or symbol keys - cookies are preserved across calls to get, post, etc. - cookie names and values are escaped - cookies can be cleared using @request.cookies.clear

    [#6272 state:resolved] https://github.com/rails/rails/commit/e2523ff68309f444cd052031c1fe8...

    March 6th, 2011 @ 03:03 PM

  3. Repository
    Repository

    (from [31f09f9dbc1b8e598fc82d86b622167bfc01d18a]) Improve testing of cookies in functional tests: - cookies can be set using string or symbol keys - cookies are preserved across calls to get, post, etc. - cookie names and values are escaped - cookies can be cleared using @request.cookies.clear

    [#6272 state:resolved] https://github.com/rails/rails/commit/31f09f9dbc1b8e598fc82d86b6221...

    March 6th, 2011 @ 03:03 PM