Class: PactBroker::Api::Middleware::HttpDebugLogs

Inherits:
Object
  • Object
show all
Includes:
Logging, Rack::PactBroker::RequestTarget
Defined in:
lib/pact_broker/api/middleware/http_debug_logs.rb

Constant Summary collapse

EXCLUDE_HEADERS =
["puma.", "rack.", "pactbroker."]
RACK_SESSION =
"rack.session"

Constants included from Rack::PactBroker::RequestTarget

Rack::PactBroker::RequestTarget::API_CONTENT_TYPES, Rack::PactBroker::RequestTarget::WEB_ASSET_EXTENSIONS

Instance Method Summary collapse

Methods included from Rack::PactBroker::RequestTarget

#request_for_api?, #request_for_ui?

Methods included from Logging

included, #log_error, #log_with_tag

Constructor Details

#initialize(app) ⇒ HttpDebugLogs

Returns a new instance of HttpDebugLogs.



14
15
16
17
# File 'lib/pact_broker/api/middleware/http_debug_logs.rb', line 14

def initialize(app)
  @app = app
  @logger = logger
end

Instance Method Details

#call(env) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/pact_broker/api/middleware/http_debug_logs.rb', line 19

def call(env)
  if request_for_api?(env)
    env_to_log = env.reject { | header, _ | header.start_with?(*EXCLUDE_HEADERS) }
    env_to_log["rack.session"] = env["rack.session"].to_hash if env["rack.session"]
    env_to_log["rack.input"] = request_body(env) if env["rack.input"]
    logger.debug("env", payload: env_to_log)
    status, headers, body = @app.call(env)
    logger.debug("response", payload: { "status" => status, "headers" => headers, "body" => body })
    [status, headers, body]
  else
    @app.call(env)
  end
end

#request_body(env) ⇒ Object



33
34
35
36
37
38
# File 'lib/pact_broker/api/middleware/http_debug_logs.rb', line 33

def request_body(env)
  buffer = env["rack.input"]
  request_body = buffer.read
  buffer.respond_to?(:rewind) && buffer.rewind
  JSON.parse(request_body) rescue request_body
end