是否可以验证为使用 Rails 集成测试设置的 cookie 的域?

发布于 2024-07-29 19:22:39 字数 374 浏览 0 评论 0原文

我正在使用以下技术为我们的域设置通配符 cookie:

cookies['root_domain_flash_warning'] = {
  'value' => 'mistakes have been made!',
  'domain' => ".#{APP_DOMAIN}",
  'expires' => 2.minutes.from_now
}

这似乎很方便。 不幸的是,除了与传递给 CookieJar#[]= 的散列中的“value”键关联的值之外,我似乎无法查找任何内容,因为 CookieJar#[] 实际上并不返回传递给其的选项散列。互惠法。

有谁知道在功能测试中验证已设置 cookie 的域的方法吗?

I'm using the following technique to set a wild-card cookie for our domain:

cookies['root_domain_flash_warning'] = {
  'value' => 'mistakes have been made!',
  'domain' => ".#{APP_DOMAIN}",
  'expires' => 2.minutes.from_now
}

Which seems to work handily. Unfortunately I can't seem to look up anything but the value associated with the 'value' key in the hash passed to CookieJar#[]= because of the fact that CookieJar#[] doesn't actually return the options hash passed to its reciprocal method.

Does anyone know of a way of verifying the domain a cookie has been set for in a functional test?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不吻晚风 2024-08-05 19:22:39

您可以检查 Set-Cookie 标头的值,该标头可在 @response.headers['Set-Cookie'] 中访问。

请尝试以下操作:

def test_something
  get '/my_action'
  assert_equal ["root_domain_flash_warning=mistakes+have+been+made!; domain=.mydomain.com; path=/; expires=Fri, 07-Aug-2009 11:42:21 GMT"], @response.headers['Set-Cookie']
end

不幸的是,您无法从 cookies 方法中提取这些值。 在 ActionController::Integration::Session#process 源代码 (actionpack/lib/action_controller/integration.rb) 中,集成测试 cookie 的处理如下,仅提取输入哈希的 value 部分:

(@headers['Set-Cookie'] || "").split("\n").each do |cookie|
  name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
  @cookies[name] = value
end

You could check the value of the Set-Cookie header which can be accessed in @response.headers['Set-Cookie'].

Try something along these lines:

def test_something
  get '/my_action'
  assert_equal ["root_domain_flash_warning=mistakes+have+been+made!; domain=.mydomain.com; path=/; expires=Fri, 07-Aug-2009 11:42:21 GMT"], @response.headers['Set-Cookie']
end

You can't extract these values from the cookies method, unfortunately. In the ActionController::Integration::Session#process source (actionpack/lib/action_controller/integration.rb), the integration test cookies are processes as below, which only extracts the value part of the input hash:

(@headers['Set-Cookie'] || "").split("\n").each do |cookie|
  name, value = cookie.match(/^([^=]*)=([^;]*);/)[1,2]
  @cookies[name] = value
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文