Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,9 @@ public String getReqQueryString() {

@SuppressWarnings("rawtypes")
public void logRequest(String url, String body, String method, Map<String, String> headerInformation, List<String> cookies, boolean force) {
if (!force && (logUtil == null || !logUtil.isDebugMode())) {
return;
}
try {
StringBuilder sb = new StringBuilder();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,15 @@ public interface TankHttpLogger {
* @return
*/
public AgentConfig getAgentConfig();

/**
* Returns true if the debugger is running the client code.
* Used to gate expensive request/response logging that is only
* consumed by the debugger UI.
*
* @return true if running inside the Tank Debugger tool
*/
default boolean isDebugMode() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.intuit.tank.http;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -244,7 +245,7 @@ public void testLogRequest() {
Map <String, String> headerInformation = Map.of("testHeaderKey", "testHeaderValue");
List<String> cookies = List.of("testCookie");
fixture.logRequest("testUrl", "testBody", "testMethod", headerInformation,
cookies, false);
cookies, true);
assertEquals("REQUEST URL: testMethod testUrl\n" +
"CONTENT TYPE: application/x-www-form-urlencoded\n" +
"REQUEST HEADER: testHeaderKey = testHeaderValue\n" +
Expand All @@ -270,4 +271,61 @@ public void testAsync() {
fixture.setAsync(true);
assertTrue(fixture.getAsync());
}

@Test
@DisplayName("logRequest skips string building when debugger is not running")
public void logRequest_skipsWhenNotDebugMode() {
// MockBaseRequest uses MockLogUtil which has isDebugMode() = false (default)
BaseRequest fixture = new MockBaseRequest(null);
Map<String, String> headers = Map.of("key", "value");
List<String> cookies = List.of("cookie1");
fixture.logRequest("http://test.com", "body", "GET", headers, cookies, false);

assertNull(fixture.logMsg,
"logMsg should remain null when debugger is not running and force is false");
}

@Test
@DisplayName("logRequest populates logMsg when force=true even if debugger is not running")
public void logRequest_populatesWhenForced() {
BaseRequest fixture = new MockBaseRequest(null);
Map<String, String> headers = Map.of("key", "value");
List<String> cookies = List.of("cookie1");
fixture.logRequest("http://test.com", "body", "GET", headers, cookies, true);

assertNotNull(fixture.logMsg,
"logMsg should be populated when force=true regardless of debug mode");
assertTrue(fixture.logMsg.contains("REQUEST URL: GET http://test.com"));
}

@Test
@DisplayName("logRequest populates logMsg when debugger is running")
public void logRequest_populatesWhenDebugMode() {
// Create a request with a logUtil that reports debug mode = true
MockLogUtil debugLogUtil = new MockLogUtil() {
@Override
public boolean isDebugMode() {
return true;
}
};
BaseRequest fixture = new MockBaseRequest(null) {
{
// Re-initialize with debug-enabled logUtil via reflection or direct field
}
};
// Use a custom MockBaseRequest that injects the debug logUtil
BaseRequest debugFixture = new BaseRequest(null, debugLogUtil) {
@Override public void setKey(String key, String value) {}
@Override public String getKey(String key) { return null; }
@Override public void setNamespace(String name, String value) {}
};

Map<String, String> headers = Map.of("key", "value");
List<String> cookies = List.of("cookie1");
debugFixture.logRequest("http://test.com", "body", "GET", headers, cookies, false);

assertNotNull(debugFixture.logMsg,
"logMsg should be populated when debugger is running");
assertTrue(debugFixture.logMsg.contains("REQUEST URL: GET http://test.com"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,10 @@ public AgentConfig getAgentConfig() {
return APITestHarness.getInstance().getTankConfig().getAgentConfig();
}

@Override
public boolean isDebugMode() {
return APITestHarness.getInstance().isDebug();
}

}
}