-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: test: HttpLoggingInterceptor tests
Signed-off-by: Marc Nuri <[email protected]>
- Loading branch information
Showing
6 changed files
with
323 additions
and
0 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
27 changes: 27 additions & 0 deletions
27
...jdk/src/test/java/io/fabric8/kubernetes/client/jdkhttp/JdkHttpLoggingInterceptorTest.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,27 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed 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 io.fabric8.kubernetes.client.jdkhttp; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpLoggingInterceptorTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class JdkHttpLoggingInterceptorTest extends AbstractHttpLoggingInterceptorTest { | ||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new JdkHttpClientFactory(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...tty/src/test/java/io/fabric8/kubernetes/client/jetty/JettyHttpLoggingInterceptorTest.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,28 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed 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 io.fabric8.kubernetes.client.jetty; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpLoggingInterceptorTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class JettyHttpLoggingInterceptorTest extends AbstractHttpLoggingInterceptorTest { | ||
|
||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new JettyHttpClientFactory(); | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...p/src/test/java/io/fabric8/kubernetes/client/okhttp/OkHttpHttpLoggingInterceptorTest.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,27 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed 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 io.fabric8.kubernetes.client.okhttp; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpLoggingInterceptorTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class OkHttpHttpLoggingInterceptorTest extends AbstractHttpLoggingInterceptorTest { | ||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new OkHttpClientFactory(); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
...rtx/src/test/java/io/fabric8/kubernetes/client/vertx/VertxHttpLoggingInterceptorTest.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,28 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed 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 io.fabric8.kubernetes.client.vertx; | ||
|
||
import io.fabric8.kubernetes.client.http.AbstractHttpLoggingInterceptorTest; | ||
import io.fabric8.kubernetes.client.http.HttpClient; | ||
|
||
@SuppressWarnings("java:S2187") | ||
public class VertxHttpLoggingInterceptorTest extends AbstractHttpLoggingInterceptorTest { | ||
|
||
@Override | ||
protected HttpClient.Factory getHttpClientFactory() { | ||
return new VertxHttpClientFactory(); | ||
} | ||
} |
209 changes: 209 additions & 0 deletions
209
...i/src/test/java/io/fabric8/kubernetes/client/http/AbstractHttpLoggingInterceptorTest.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,209 @@ | ||
/** | ||
* Copyright (C) 2015 Red Hat, Inc. | ||
* | ||
* Licensed 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 io.fabric8.kubernetes.client.http; | ||
|
||
import io.fabric8.mockwebserver.DefaultMockServer; | ||
import io.fabric8.mockwebserver.utils.ResponseProviders; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.mockito.InOrder; | ||
import org.mockito.Mockito; | ||
import org.slf4j.Logger; | ||
|
||
import java.net.URI; | ||
import java.util.Collections; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.ArgumentMatchers.argThat; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.timeout; | ||
import static org.mockito.Mockito.when; | ||
|
||
public abstract class AbstractHttpLoggingInterceptorTest { | ||
|
||
private static DefaultMockServer server; | ||
private Logger logger; | ||
private InOrder inOrder; | ||
private HttpClient httpClient; | ||
|
||
@BeforeAll | ||
static void beforeAll() { | ||
server = new DefaultMockServer(false); | ||
server.start(); | ||
} | ||
|
||
@AfterAll | ||
static void afterAll() { | ||
server.shutdown(); | ||
} | ||
|
||
@BeforeEach | ||
void setUp() { | ||
logger = mock(Logger.class); | ||
inOrder = Mockito.inOrder(logger); | ||
when(logger.isTraceEnabled()).thenReturn(true); | ||
httpClient = getHttpClientFactory().newBuilder() | ||
.addOrReplaceInterceptor("loggingInterceptor", new HttpLoggingInterceptor(logger)) | ||
.build(); | ||
} | ||
|
||
protected abstract HttpClient.Factory getHttpClientFactory(); | ||
|
||
@Test | ||
@DisplayName("HTTP request URI is logged") | ||
public void httpRequestUriLogged() throws Exception { | ||
server.expect().withPath("/request-uri") | ||
.andReturn(200, "This is the response body") | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.uri(server.url("/request-uri")) | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger) | ||
.trace(eq("> {} {}"), eq("GET"), argThat((URI uri) -> uri.toString().endsWith("/request-uri"))); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("HTTP request headers are logged") | ||
public void httpRequestHeadersLogged() throws Exception { | ||
server.expect().withPath("/request-headers") | ||
.andReturn(200, "This is the response body") | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.header("test-type", "header-test") | ||
.uri(server.url("/request-headers")) | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger).trace("> {}: {}", "test-type", "header-test"); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("HTTP request body is logged") | ||
public void httpRequestBodyLogged() throws Exception { | ||
server.expect().withPath("/request-body") | ||
.andReturn(200, "This is the response body") | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.uri(server.url("/request-body")) | ||
.method("POST", "test/plain", "This is the request body") | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger).trace("This is the request body"); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("HTTP response status is logged") | ||
public void httpResponseStatusLogged() throws Exception { | ||
server.expect().withPath("/response-status") | ||
.andReturn(200, "This is the response body") | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.uri(server.url("/response-status")) | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger).trace("< {} {}", 200, "OK"); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("HTTP response headers are logged") | ||
public void httpResponseHeadersLogged() throws Exception { | ||
server.expect().withPath("/response-headers") | ||
.andReply(ResponseProviders.of(204, "", Collections.singletonMap("test-type", "response-header-test"))) | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.uri(server.url("/response-headers")) | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger).trace(eq("< {} {}"), anyInt(), anyString()); | ||
inOrder.verify(logger).trace("< {}: {}", "test-type", "response-header-test"); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("HTTP response body is logged") | ||
public void httpResponseBodyLogged() throws Exception { | ||
server.expect().withPath("/response-body") | ||
.andReturn(200, "This is the response body") | ||
.always(); | ||
httpClient.sendAsync(httpClient.newHttpRequestBuilder() | ||
.uri(server.url("/response-body")) | ||
.build(), String.class).get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); | ||
inOrder.verify(logger).trace(eq("< {} {}"), anyInt(), anyString()); | ||
inOrder.verify(logger).trace("This is the response body"); | ||
inOrder.verify(logger).trace("-HTTP END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("WS request URI is logged") | ||
public void wsRequestUriLogged() throws Exception { | ||
server.expect().withPath("/ws-request-uri") | ||
.andUpgradeToWebSocket() | ||
.open().done().always(); | ||
httpClient.newWebSocketBuilder() | ||
.uri(URI.create(server.url("/ws-request-uri"))) | ||
.buildAsync(new WebSocket.Listener() { | ||
}) | ||
.get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); | ||
inOrder.verify(logger) | ||
.trace(eq("> {} {}"), eq("GET"), argThat((URI uri) -> uri.toString().endsWith("/ws-request-uri"))); | ||
inOrder.verify(logger).trace("-WS END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("WS request headers are logged") | ||
public void wsRequestHeadersLogged() throws Exception { | ||
server.expect().withPath("/ws-request-headers") | ||
.andUpgradeToWebSocket() | ||
.open().done().always(); | ||
httpClient.newWebSocketBuilder() | ||
.header("test-type", "ws-header-test") | ||
.uri(URI.create(server.url("/ws-request-headers"))) | ||
.buildAsync(new WebSocket.Listener() { | ||
}) | ||
.get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); | ||
inOrder.verify(logger).trace("> {}: {}", "test-type", "ws-header-test"); | ||
inOrder.verify(logger).trace("-WS END-"); | ||
} | ||
|
||
@Test | ||
@DisplayName("WS response status is logged") | ||
public void wsResponseStatusLogged() throws Exception { | ||
server.expect().withPath("/ws-response-status") | ||
.andUpgradeToWebSocket() | ||
.open().done().always(); | ||
httpClient.newWebSocketBuilder() | ||
.uri(URI.create(server.url("/ws-response-status"))) | ||
.buildAsync(new WebSocket.Listener() { | ||
}) | ||
.get(10, TimeUnit.SECONDS); | ||
inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); | ||
inOrder.verify(logger).trace("< {} {}", 101, "Switching Protocols"); | ||
inOrder.verify(logger).trace("-WS END-"); | ||
} | ||
} |