Sinatra、Rack::Test 和条件 GET 请求

发布于 2024-12-09 09:53:19 字数 493 浏览 6 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

旧时浪漫 2024-12-16 09:53:19

我在 If-None-Match 标头和 ETag 方面遇到了类似的问题。我无法使用 Rack::Test 的 header 方法来实现此功能。但有效的是:

get '/test-url', {}, {"HTTP_IF_NONE_MATCH" => '"15-xyz"'}
last_response.status.should == 304

所以,在你的情况下,尝试:

get '/test-url', {}, {"HTTP_IF_MODIFIED_SINCE" => (Time.now.midnight + 1.hour).httpdate}
last_response.status.must_equal 304

学分:这是受到 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's header method. But what worked was this:

get '/test-url', {}, {"HTTP_IF_NONE_MATCH" => '"15-xyz"'}
last_response.status.should == 304

So, in your case, try:

get '/test-url', {}, {"HTTP_IF_MODIFIED_SINCE" => (Time.now.midnight + 1.hour).httpdate}
last_response.status.must_equal 304

Credits: This was inspired by how Rack::Test implements follow_redirect!

同展鸳鸯锦 2024-12-16 09:53:19

我将使用从响应发送的标头,这就是实际的 HTTP 客户端应该用来生成下一个请求的标头:

# first time responds with the content
get "/test-url" 
last_response.status.must_equal 200 

# but includes a header timestamp to be used by the client
last_modified = last_response.headers["Last-Modified"] 
last_modified.wont_be_nil

# next time, it just responds with a 304 Not Modified status
get "/test-url", {}, {"HTTP_IF_MODIFIED_SINCE" => last_modified}
last_response.status.must_equal 304

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:

# first time responds with the content
get "/test-url" 
last_response.status.must_equal 200 

# but includes a header timestamp to be used by the client
last_modified = last_response.headers["Last-Modified"] 
last_modified.wont_be_nil

# next time, it just responds with a 304 Not Modified status
get "/test-url", {}, {"HTTP_IF_MODIFIED_SINCE" => last_modified}
last_response.status.must_equal 304
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文