From 1f232c0872228315cac9768be0502a4cecb4d360 Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Tue, 29 Mar 2022 17:23:17 +1100 Subject: [PATCH] fix: only log API requests/responses, not web assets, when http_debug_logging_enabled is true --- .../api/middleware/http_debug_logs.rb | 20 ++++++++++++------- .../api/middleware/http_debug_logs_spec.rb | 13 ++++++++++-- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/lib/pact_broker/api/middleware/http_debug_logs.rb b/lib/pact_broker/api/middleware/http_debug_logs.rb index 08ee6623b..a1ada76a1 100644 --- a/lib/pact_broker/api/middleware/http_debug_logs.rb +++ b/lib/pact_broker/api/middleware/http_debug_logs.rb @@ -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" @@ -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) diff --git a/spec/lib/pact_broker/api/middleware/http_debug_logs_spec.rb b/spec/lib/pact_broker/api/middleware/http_debug_logs_spec.rb index 4b36707a4..fd300bc0b 100644 --- a/spec/lib/pact_broker/api/middleware/http_debug_logs_spec.rb +++ b/spec/lib/pact_broker/api/middleware/http_debug_logs_spec.rb @@ -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 @@ -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 @@ -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