Skip to content

Commit

Permalink
wip: test: HttpLoggingInterceptor tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Nuri <[email protected]>
  • Loading branch information
manusa committed Apr 11, 2023
1 parent 57685e0 commit 67e02d3
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 0 deletions.
4 changes: 4 additions & 0 deletions httpclient-jdk/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@
<artifactId>mockwebserver</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-inline</artifactId>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
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();
}
}
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();
}
}
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();
}
}
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();
}
}
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-");
}
}

0 comments on commit 67e02d3

Please sign in to comment.