Skip to content

Commit e3f9690

Browse files
author
Yaniv Inbar
committed
http: fix curl logging bug
https://codereview.appspot.com/8839043/
1 parent 745d743 commit e3f9690

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

google-http-client/src/main/java/com/google/api/client/http/HttpRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -872,8 +872,9 @@ public HttpResponse execute() throws IOException {
872872
if (loggable) {
873873
logger.config(logbuf.toString());
874874
if (curlbuf != null) {
875-
curlbuf.append(" -- ");
876-
curlbuf.append(urlString);
875+
curlbuf.append(" -- '");
876+
curlbuf.append(urlString.replaceAll("\'", "'\"'\"'"));
877+
curlbuf.append("'");
877878
if (streamingContent != null) {
878879
curlbuf.append(" << $$$");
879880
}

google-http-client/src/test/java/com/google/api/client/http/HttpRequestTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.google.api.client.testing.http.MockHttpUnsuccessfulResponseHandler;
2020
import com.google.api.client.testing.http.MockLowLevelHttpRequest;
2121
import com.google.api.client.testing.http.MockLowLevelHttpResponse;
22+
import com.google.api.client.testing.util.LogRecordingHandler;
2223
import com.google.api.client.util.Key;
2324
import com.google.api.client.util.LoggingStreamingContent;
2425
import com.google.api.client.util.StringUtils;
@@ -41,6 +42,7 @@
4142
import java.util.concurrent.Future;
4243
import java.util.concurrent.TimeUnit;
4344
import java.util.concurrent.TimeoutException;
45+
import java.util.logging.Level;
4446

4547
/**
4648
* Tests {@link HttpRequest}.
@@ -951,4 +953,22 @@ public void testExecute_redirectWithIncorrectContentRetryableSetting() throws Ex
951953
assertEquals(200, resp.getStatusCode());
952954
assertEquals(2, fakeTransport.lowLevelExecCalls);
953955
}
956+
957+
public void testExecute_curlLogger() throws Exception {
958+
LogRecordingHandler recorder = new LogRecordingHandler();
959+
HttpTransport.LOGGER.setLevel(Level.CONFIG);
960+
HttpTransport.LOGGER.addHandler(recorder);
961+
new MockHttpTransport().createRequestFactory()
962+
.buildGetRequest(new GenericUrl("http://google.com/#q=a'b'c")).execute();
963+
boolean found = false;
964+
for (String message : recorder.messages()) {
965+
if (message.startsWith("curl")) {
966+
found = true;
967+
assertEquals("curl -v --compressed -H 'Accept-Encoding: gzip' -H 'User-Agent: "
968+
+ HttpRequest.USER_AGENT_SUFFIX + "' -- 'http://google.com/#q=a'\"'\"'b'\"'\"'c'",
969+
message);
970+
}
971+
}
972+
assertTrue(found);
973+
}
954974
}

0 commit comments

Comments
 (0)