-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logging of http exchange contents
- Loading branch information
Showing
4 changed files
with
157 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
java/client/src/org/openqa/selenium/remote/http/DumpHttpExchangeFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// Licensed to the Software Freedom Conservancy (SFC) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The SFC licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
package org.openqa.selenium.remote.http; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import org.openqa.selenium.internal.Debug; | ||
import org.openqa.selenium.internal.Require; | ||
|
||
import java.io.InputStream; | ||
import java.util.function.Supplier; | ||
import java.util.logging.Level; | ||
import java.util.logging.Logger; | ||
import java.util.stream.StreamSupport; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
|
||
public class DumpHttpExchangeFilter implements Filter { | ||
|
||
public static final Logger LOG = Logger.getLogger(DumpHttpExchangeFilter.class.getName()); | ||
private final Level logLevel; | ||
|
||
public DumpHttpExchangeFilter() { | ||
this(Debug.getDebugLogLevel()); | ||
} | ||
|
||
public DumpHttpExchangeFilter(Level logLevel) { | ||
this.logLevel = Require.nonNull("Log level", logLevel); | ||
} | ||
|
||
@Override | ||
public HttpHandler apply(HttpHandler next) { | ||
return req -> { | ||
// Use the supplier to avoid messing with the request unless we're logging | ||
LOG.log(logLevel, () -> requestLogMessage(req)); | ||
|
||
HttpResponse res = next.execute(req); | ||
|
||
LOG.log(logLevel, () -> responseLogMessage(res)); | ||
|
||
return res; | ||
}; | ||
} | ||
|
||
private void expandHeadersAndContent(StringBuilder builder, HttpMessage<?> message) { | ||
message.getHeaderNames().forEach(name -> { | ||
builder.append(" ").append(name).append(": "); | ||
builder.append(StreamSupport.stream(message.getHeaders(name).spliterator(), false).collect(joining(", "))); | ||
builder.append("\n"); | ||
}); | ||
builder.append("\n"); | ||
builder.append(Contents.string(message)); | ||
} | ||
|
||
@VisibleForTesting | ||
String requestLogMessage(HttpRequest req) { | ||
// There's no requirement that requests or responses can be read more than once. Protect ourselves. | ||
Supplier<InputStream> memoized = Contents.memoize(req.getContent()); | ||
req.setContent(memoized); | ||
|
||
StringBuilder reqInfo = new StringBuilder(); | ||
reqInfo.append("HTTP Request: ").append(req).append("\n"); | ||
expandHeadersAndContent(reqInfo, req); | ||
return reqInfo.toString(); | ||
} | ||
|
||
@VisibleForTesting | ||
String responseLogMessage(HttpResponse res) { | ||
Supplier<InputStream> resContents = Contents.memoize(res.getContent()); | ||
res.setContent(resContents); | ||
|
||
StringBuilder resInfo = new StringBuilder("HTTP Response: "); | ||
resInfo.append("Status code: ").append(res.getStatus()).append("\n"); | ||
expandHeadersAndContent(resInfo, res); | ||
return resInfo.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
java/client/test/org/openqa/selenium/remote/http/DumpHttpExchangeFilterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.openqa.selenium.remote.http; | ||
|
||
import org.junit.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.logging.Level; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.openqa.selenium.remote.http.Contents.string; | ||
import static org.openqa.selenium.remote.http.HttpMethod.GET; | ||
|
||
public class DumpHttpExchangeFilterTest { | ||
|
||
@Test | ||
public void shouldIncludeRequestAndResponseHeaders() { | ||
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter(); | ||
|
||
String reqLog = dumpFilter.requestLogMessage( | ||
new HttpRequest(GET, "/foo").addHeader("Peas", "and Sausages")); | ||
|
||
assertThat(reqLog).contains("Peas"); | ||
assertThat(reqLog).contains("and Sausages"); | ||
|
||
String resLog = dumpFilter.responseLogMessage(new HttpResponse() | ||
.addHeader("Cheese", "Brie") | ||
.setContent(string("Hello, World!", UTF_8))); | ||
|
||
assertThat(resLog).contains("Cheese"); | ||
assertThat(resLog).contains("Brie"); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeRequestContentInLogMessage() { | ||
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter(); | ||
|
||
String reqLog = dumpFilter.requestLogMessage( | ||
new HttpRequest(GET, "/foo").setContent(Contents.string("Cheese is lovely", UTF_8))); | ||
|
||
assertThat(reqLog).contains("Cheese is lovely"); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeResponseCodeInLogMessage() { | ||
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter(); | ||
|
||
String resLog = dumpFilter.responseLogMessage( | ||
new HttpResponse().setStatus(505)); | ||
|
||
assertThat(resLog).contains("505"); | ||
} | ||
|
||
@Test | ||
public void shouldIncludeBodyOfResponseInLogMessage() { | ||
DumpHttpExchangeFilter dumpFilter = new DumpHttpExchangeFilter(); | ||
|
||
String resLog = dumpFilter.responseLogMessage( | ||
new HttpResponse().setContent(Contents.string("Peas", UTF_8))); | ||
|
||
assertThat(resLog).contains("Peas"); | ||
} | ||
} |