Skip to content

Commit 6972f10

Browse files
committed
Add logging of http exchange contents
1 parent 38f393a commit 6972f10

File tree

4 files changed

+157
-3
lines changed

4 files changed

+157
-3
lines changed

java/client/src/org/openqa/selenium/remote/RemoteWebDriverBuilder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
import org.openqa.selenium.ImmutableCapabilities;
2525
import org.openqa.selenium.SessionNotCreatedException;
2626
import org.openqa.selenium.WebDriver;
27-
import org.openqa.selenium.WebDriverException;
2827
import org.openqa.selenium.WebDriverInfo;
2928
import org.openqa.selenium.internal.Either;
3029
import org.openqa.selenium.internal.Require;
3130
import org.openqa.selenium.remote.http.ClientConfig;
31+
import org.openqa.selenium.remote.http.DumpHttpExchangeFilter;
3232
import org.openqa.selenium.remote.http.Filter;
3333
import org.openqa.selenium.remote.http.HttpClient;
3434
import org.openqa.selenium.remote.http.HttpHandler;
@@ -355,7 +355,8 @@ private WebDriver getRemoteDriver() {
355355
HttpHandler handler = Require.nonNull("Http handler", client)
356356
.with(new CloseHttpClientFilter(client)
357357
.andThen(new AddWebDriverSpecHeaders())
358-
.andThen(new ErrorFilter()));
358+
.andThen(new ErrorFilter())
359+
.andThen(new DumpHttpExchangeFilter()));
359360

360361
Either<SessionNotCreatedException, ProtocolHandshake.Result> result = null;
361362
try {
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Licensed to the Software Freedom Conservancy (SFC) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The SFC licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package org.openqa.selenium.remote.http;
19+
20+
import com.google.common.annotations.VisibleForTesting;
21+
import org.openqa.selenium.internal.Debug;
22+
import org.openqa.selenium.internal.Require;
23+
24+
import java.io.InputStream;
25+
import java.util.function.Supplier;
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
import java.util.stream.StreamSupport;
29+
30+
import static java.util.stream.Collectors.joining;
31+
32+
public class DumpHttpExchangeFilter implements Filter {
33+
34+
public static final Logger LOG = Logger.getLogger(DumpHttpExchangeFilter.class.getName());
35+
private final Level logLevel;
36+
37+
public DumpHttpExchangeFilter() {
38+
this(Debug.getDebugLogLevel());
39+
}
40+
41+
public DumpHttpExchangeFilter(Level logLevel) {
42+
this.logLevel = Require.nonNull("Log level", logLevel);
43+
}
44+
45+
@Override
46+
public HttpHandler apply(HttpHandler next) {
47+
return req -> {
48+
// Use the supplier to avoid messing with the request unless we're logging
49+
LOG.log(logLevel, () -> requestLogMessage(req));
50+
51+
HttpResponse res = next.execute(req);
52+
53+
LOG.log(logLevel, () -> responseLogMessage(res));
54+
55+
return res;
56+
};
57+
}
58+
59+
private void expandHeadersAndContent(StringBuilder builder, HttpMessage<?> message) {
60+
message.getHeaderNames().forEach(name -> {
61+
builder.append(" ").append(name).append(": ");
62+
builder.append(StreamSupport.stream(message.getHeaders(name).spliterator(), false).collect(joining(", ")));
63+
builder.append("\n");
64+
});
65+
builder.append("\n");
66+
builder.append(Contents.string(message));
67+
}
68+
69+
@VisibleForTesting
70+
String requestLogMessage(HttpRequest req) {
71+
// There's no requirement that requests or responses can be read more than once. Protect ourselves.
72+
Supplier<InputStream> memoized = Contents.memoize(req.getContent());
73+
req.setContent(memoized);
74+
75+
StringBuilder reqInfo = new StringBuilder();
76+
reqInfo.append("HTTP Request: ").append(req).append("\n");
77+
expandHeadersAndContent(reqInfo, req);
78+
return reqInfo.toString();
79+
}
80+
81+
@VisibleForTesting
82+
String responseLogMessage(HttpResponse res) {
83+
Supplier<InputStream> resContents = Contents.memoize(res.getContent());
84+
res.setContent(resContents);
85+
86+
StringBuilder resInfo = new StringBuilder("HTTP Response: ");
87+
resInfo.append("Status code: ").append(res.getStatus()).append("\n");
88+
expandHeadersAndContent(resInfo, res);
89+
return resInfo.toString();
90+
}
91+
}

java/client/test/org/openqa/selenium/remote/http/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ load("@rules_jvm_external//:defs.bzl", "artifact")
22
load("//java:defs.bzl", "java_test_suite")
33

44
java_test_suite(
5-
name = "SmallTests",
5+
name = "small-tests",
66
size = "small",
77
srcs = glob(["*.java"]),
88
javacopts = [
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package org.openqa.selenium.remote.http;
2+
3+
import org.junit.Test;
4+
5+
import java.nio.charset.StandardCharsets;
6+
import java.util.logging.Level;
7+
8+
import static java.nio.charset.StandardCharsets.UTF_8;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
import static org.openqa.selenium.remote.http.Contents.string;
11+
import static org.openqa.selenium.remote.http.HttpMethod.GET;
12+
13+
public class DumpHttpExchangeFilterTest {
14+
15+
@Test
16+
public void shouldIncludeRequestAndResponseHeaders() {
17+
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter();
18+
19+
String reqLog = dumpFilter.requestLogMessage(
20+
new HttpRequest(GET, "/foo").addHeader("Peas", "and Sausages"));
21+
22+
assertThat(reqLog).contains("Peas");
23+
assertThat(reqLog).contains("and Sausages");
24+
25+
String resLog = dumpFilter.responseLogMessage(new HttpResponse()
26+
.addHeader("Cheese", "Brie")
27+
.setContent(string("Hello, World!", UTF_8)));
28+
29+
assertThat(resLog).contains("Cheese");
30+
assertThat(resLog).contains("Brie");
31+
}
32+
33+
@Test
34+
public void shouldIncludeRequestContentInLogMessage() {
35+
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter();
36+
37+
String reqLog = dumpFilter.requestLogMessage(
38+
new HttpRequest(GET, "/foo").setContent(Contents.string("Cheese is lovely", UTF_8)));
39+
40+
assertThat(reqLog).contains("Cheese is lovely");
41+
}
42+
43+
@Test
44+
public void shouldIncludeResponseCodeInLogMessage() {
45+
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter();
46+
47+
String resLog = dumpFilter.responseLogMessage(
48+
new HttpResponse().setStatus(505));
49+
50+
assertThat(resLog).contains("505");
51+
}
52+
53+
@Test
54+
public void shouldIncludeBodyOfResponseInLogMessage() {
55+
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter();
56+
57+
String resLog = dumpFilter.responseLogMessage(
58+
new HttpResponse().setContent(Contents.string("Peas", UTF_8)));
59+
60+
assertThat(resLog).contains("Peas");
61+
}
62+
}

0 commit comments

Comments
 (0)