ruby-on-rails - 强制 Rails 的 app.get 重做请求

标签 ruby-on-rails ruby-on-rails-4

我正在使用以下代码从 rake 任务中在服务器上执行请求:

app = ActionDispatch::Integration::Session.new(Rails.application)
app.host!('localhost:3000')
app.get(path) 

这很好用。

但是,如果我使用相同的路径再次调用 app.get(path),则不会重复请求并返回之前的结果。

有没有办法强制 app.get 重复调用?

最佳答案

尝试重置 session :

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

祝你好运!

关于ruby-on-rails - 强制 Rails 的 app.get 重做请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17659104/

相关文章:

ruby-on-rails - Rails 相当于 Django 的 "sites"框架?

ruby-on-rails - "name"是 Rails 中的保留字吗?

ruby-on-rails - 未定义的局部变量或方法 `confirmed_at' 用于设计

ruby-on-rails-4 - 翻译电子邮件

ruby-on-rails - 轮胎/Elasticsearch 搜索关联

ruby-on-rails - Ruby on Rails session = 无

ruby-on-rails - Rails Metal (& Rack) 是实现高流量 Web 服务 api 的好方法吗?

ruby - 如何检测客户端是否关闭 Rails 4.2 上的连接

jquery - Bootstrap 日期选择器的性能问题

ruby-on-rails - Ruby On Rails 中的小写字母大写