Forcing Rails app.get retry request

I use the following code to execute a server request from a rake task:

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

It works well.

However, if I call app.get(path) again with the same path, the request is not repeated and the previous result is returned.

Is there any way to get app.get to repeat the call?

+4
source share
2 answers

I worked out what was going on.

Basically, the “request does not repeat” observation is its own caching of Rails in action. Which makes sense, app.get treated like any other request, if caching is enabled, the cache is returned, and if not, it will be repeated (as @henrikhodne claimed). This explains why a puts in the cached controller will not be displayed a second time.

To check, add puts to the 2 controller methods, but just set expires_in to the second. The first will repeat the conclusion, the second will not.

A way to force the request to repeat is to reload the cache by changing the URL, for example. app.get("/") becomes app.get("/?r=123456") just as if you were using HTTP. All this seems obvious in retrospect, basically app.get handled exactly like a client request, and the same rules apply.

+1
source

Try resetting the session:

 app.reset! 

This is how it works when 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 

Otherwise, it just reuses the last answer:

 # 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 

Good luck

+1
source

Source: https://habr.com/ru/post/1491480/


All Articles