Sinatra,Rack :: Test和条件GET请求 [英] Sinatra, Rack::Test, and Conditional GET requests

查看:74
本文介绍了Sinatra,Rack :: Test和条件GET请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Sinatra 1.2.0应用程序,该应用程序使用Rack :: Cache进行Last-Modified验证缓存.一切都很好-我在路由正文中调用了last_modified,如果缓存具有最新副本,其余的执行将暂停,我的应用将对缓存的响应为304 Not Modified,并且缓存将缓存页面,而无需生成新页面.

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.

我的问题是尝试为此过程编写测试.使用Rack :: Test和Minitest :: Spec,我正在模拟缓存的条件Get请求,如下所示:

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

但是,最后一行的断言失败.该应用程序仍在发送200状态消息.我可以将请求设置错误吗? Rack :: Test是否可以正确执行条件GET?任何建议将不胜感激.

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.

推荐答案

我在If-None-Match标头和ETag上遇到了类似的问题.我也无法通过Rack :: Test的header方法来工作.但这是可行的:

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

信用:这是受到Rack :: Test如何实现follow_redirect!

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

这篇关于Sinatra,Rack :: Test和条件GET请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆