我正在使用以下代码从 rake 任务中对服务器执行请求:
app = ActionDispatch::Integration::Session.new(Rails.application)
app.host!('localhost:3000')
app.get(path)
这很好用。
但是,如果我app.get(path)
用相同的路径再次调用,则不会重复请求并返回先前的结果。
有没有办法强制 app.get 重复通话?
我正在使用以下代码从 rake 任务中对服务器执行请求:
app = ActionDispatch::Integration::Session.new(Rails.application)
app.host!('localhost:3000')
app.get(path)
这很好用。
但是,如果我app.get(path)
用相同的路径再次调用,则不会重复请求并返回先前的结果。
有没有办法强制 app.get 重复通话?
尝试重置会话:
app.reset!
这是重置时的工作方式,
def reset!
@https = false
@controller = @request = @response = nil
@_mock_session = nil
@request_count = 0
@url_options = nil
self.host = DEFAULT_HOST
self.remote_addr = "127.0.0.1"
self.accept = "text/xml,application/xml,application/xhtml+xml," +
"text/html;q=0.9,text/plain;q=0.8,image/png," +
"*/*;q=0.5"
unless defined? @named_routes_configured
# the helpers are made protected by default--we make them public for
# easier access during testing and troubleshooting.
@named_routes_configured = true
end
end
否则它只会重用最后一个响应:
# this is a private method in Session, it will called every time you call `get/post, etc`
def process
......
@request_count += 1
@request = ActionDispatch::Request.new(session.last_request.env)
response = _mock_session.last_response
@response = ActionDispatch::TestResponse.new(response.status, response.headers, response.body)
@html_document = nil
....
end
祝你好运!
我已经弄清楚发生了什么。
基本上,“请求不重复”的观察是 Rails 自己的缓存在起作用。这是有道理的,app.get
被视为任何其他请求,如果启用缓存,则返回缓存,如果不是,它将重复(如@henrikhodne 声称的那样)。这就解释了为什么puts
缓存控制器中的 a 不会第二次输出。
为了验证,puts
在 2 个控制器方法中添加一个,但只expires_in
在第二个中设置。第一个将重复输出,第二个不会。
强制请求重复的方法是通过修改 URL 来破坏缓存,例如
app.get("/")
,app.get("/?r=123456")
就像使用 HTTP 一样。事后看来,这一切似乎都很明显,基本上app.get
完全被视为客户请求,并且所有相同的规则都适用。