Skip to content

Commit

Permalink
fix: only log API requests/responses, not web assets, when http_debug…
Browse files Browse the repository at this point in the history
…_logging_enabled is true
  • Loading branch information
bethesque committed Mar 29, 2022
1 parent 9f7aed4 commit 1f232c0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
20 changes: 13 additions & 7 deletions lib/pact_broker/api/middleware/http_debug_logs.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
require "pact_broker/logging"
require "rack/pact_broker/request_target"

module PactBroker
module Api
module Middleware
class HttpDebugLogs
include PactBroker::Logging
include Rack::PactBroker::RequestTarget

EXCLUDE_HEADERS = ["puma.", "rack.", "pactbroker."]
RACK_SESSION = "rack.session"
Expand All @@ -15,13 +17,17 @@ def initialize(app)
end

def call(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]
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

def request_body(env)
Expand Down
13 changes: 11 additions & 2 deletions spec/lib/pact_broker/api/middleware/http_debug_logs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module Middleware
let(:app) { HttpDebugLogs.new(target_app) }
let(:logger) { double("logger", debug: nil) }

subject { post("/", { foo: "bar" }.to_json, { "HTTP_ACCEPT" => "foo" }) }
subject { post("/", { foo: "bar" }.to_json, { "HTTP_ACCEPT" => "application/json" }) }

it "returns the response" do
expect(subject.status).to eq 200
Expand All @@ -23,7 +23,7 @@ module Middleware
end

it "logs the rack env" do
expect(logger).to receive(:debug).with("env", payload: hash_including({ "rack.input" => { "foo" => "bar" }, "HTTP_ACCEPT" => "foo" }))
expect(logger).to receive(:debug).with("env", payload: hash_including({ "rack.input" => { "foo" => "bar" }, "HTTP_ACCEPT" => "application/json" }))
subject
end

Expand All @@ -32,6 +32,15 @@ module Middleware
expect(logger).to receive(:debug).with("response", payload: hash_including(expected_payload))
subject
end

context "when the request is not for the API" do
subject { get("/", nil, { "HTTP_ACCEPT" => "text/html" }) }

it "is not logged" do
expect(logger).to_not receive(:debug)
subject
end
end
end
end
end
Expand Down

0 comments on commit 1f232c0

Please sign in to comment.