Rack::Test not able to find web app cookie
While testing a Sinatra app with Cucumber, Rack::Test was not able to find the cooki开发者_Go百科e that my app created, even though I could clearly see that it was in the Rack::Test::CookieJar object by dumping it with "p".
I'm answering my own question in order to share the solution with others:
Rack::Test::CookieJar#[] will only return the value of a cookie if it also matches the domain and path. Unfortunately, unless your app's domain is "example.org" you're out of luck.
Fortunately, there's an easy fix: If you're testing with Sinatra, paste the following monkey patch anywhere in your env.rb file in the outermost (global) scope:
module Rack module Test DEFAULT_HOST='localhost' end end
That's it!
精彩评论