Class: AgileProxy::RequestHandler

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Handler
Defined in:
lib/agile_proxy/handlers/request_handler.rb

Overview

The Central Request Handler

As a request is made from the client to the server via the proxy server, it comes through an instance of this class.

This class will then pass the request on to the StubHandler, then finally the ProxyHandler

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Array

A rack endpoint

This method is called as a rack endpoint and returns a rack response.

Parameters:

  • env (Hash)

    The ‘rack’ environment

Returns:

  • (Array)

    The rack response (status, headers, content)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/agile_proxy/handlers/request_handler.rb', line 24

def call(env)
  request = ActionDispatch::Request.new(env)
  username, password = username_password env
  application = Application.where(username: username, password: password).first
  body = request.body.read
  request.body.rewind
  rack_response = rack_app.call(env)
  if rack_response[0] == 404
    rack_response = [
      500,
      {},
      "Connection to #{request.url}#{body} not stubbed and new http connections are disabled"
    ]
  end
  request_spec = env['agile_proxy.request_spec']
  exclude_headers = ['@env', 'rack.errors', 'rack.logger']
  if application.record_requests || (request_spec && request_spec.record_requests)
    application.recordings.create request_headers: request.headers.reject {|key, value| exclude_headers.include?(key)},
                                  request_body: body,
                                  request_url: request.url,
                                  request_method: request.request_method,
                                  response_headers: rack_response[1],
                                  response_body: rack_response[2],
                                  response_status: rack_response[0],
                                  request_spec_id: request_spec ? request_spec.id : nil
  end
  rack_response
end