Sinatra、Rack::Test 和条件 GET 请求
我有一个 Sinatra 1.2.0 应用程序,它使用 Rack::Cache 进行 Last-Modified 验证缓存。事情进展顺利——我在路由主体中调用了last_modified,如果缓存有最新的副本,则其余的执行将停止,我的应用程序会以 304 Not Modified 响应缓存,并且缓存会为缓存提供服务页面,而无需生成新页面。
我的问题是尝试为此过程编写测试。使用 Rack::Test 和 Minitest::Spec,我模拟缓存的条件 Get 请求,如下所示:
header "If-Modified-Since", (Time.now.midnight + 1.hour).httpdate
get "/test-url"
last_response.status.must_equal 304
但是,最后一行的断言失败。该应用程序仍在发送 200 状态消息。我是否设置了错误的请求? Rack::Test 是否正确执行条件 GET?任何建议将不胜感激。
I've got a Sinatra 1.2.0 app that is doing Last-Modified validation caching with Rack::Cache. Things are working great-- I call last_modified in my route body and if the cache has an up-to-date copy, the rest of the execution halts, my app responds to the cache with 304 Not Modified, and the cache serves the cached page without having to generate a new one.
My issue is in trying to write tests for this process. Using Rack::Test and Minitest::Spec, I'm simulating the cache's conditional Get request like so:
header "If-Modified-Since", (Time.now.midnight + 1.hour).httpdate
get "/test-url"
last_response.status.must_equal 304
However, that assertion on the last line fails. The app is still sending a 200 status message. Could I be setting up the request wrong? Does Rack::Test do conditional GET's correctly? Any advice would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我在
If-None-Match
标头和 ETag 方面遇到了类似的问题。我无法使用 Rack::Test 的header
方法来实现此功能。但有效的是:所以,在你的情况下,尝试:
学分:这是受到 Rack::Test 如何实现
follow_redirect!
的启发。I was having a similar problem with the
If-None-Match
header and ETags. I couldn't get this working either with Rack::Test'sheader
method. But what worked was this:So, in your case, try:
Credits: This was inspired by how Rack::Test implements
follow_redirect!
我将使用从响应发送的标头,这就是实际的 HTTP 客户端应该用来生成下一个请求的标头:
I would use the header that is sent from the response, that is what the actual HTTP client should be using to generate the next request: