I think the following link should give you a good start. How to get the displayed result of the controller’s action without visiting a web page?
I am trying to do the same and, as far as I can see, your fake request should have the correct host, because the cache key contains host information.
I performed caching with ActionController :: Integration :: Session
ais = ActionController::Integration::Session.new ais.host = host ais.xml_http_request(:post, url, params, headers)
I have one more:
class FakeRequest include ActionController::UrlWriter def initialize(url, params, session, host) @url = url @params = params @session = session default_url_options[:host] = URI.parse(host).host end def post process(:post) end def get process(:get) end def xhr process(:post, true) end def process(method, ajax = false) uri = URI.parse(url_for(@url)) request = ActionController::TestRequest.new({'HTTP_HOST' => uri.host,'rack.input' => '','rack.url_scheme' => 'http'}) request.query_parameters = @params request.path = uri.path request.host = uri.host request.env['REQUEST_METHOD'] = method.to_s.upcase if ajax request.headers['X-Requested-With'] = 'XMLHttpRequest' end @session.each_pair do |k,v| request.session[k] = v end response = ActionController::TestResponse.new controller = ActionController::Routing::Routes.recognize(request).new return controller.process(request, response) end end
This also returns a response object.
source share