|
| 1 | +/** |
| 2 | + * Copyright (C) 2015 Red Hat, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.fabric8.kubernetes.client.http; |
| 17 | + |
| 18 | +import io.fabric8.mockwebserver.DefaultMockServer; |
| 19 | +import io.fabric8.mockwebserver.utils.ResponseProviders; |
| 20 | +import org.junit.jupiter.api.AfterAll; |
| 21 | +import org.junit.jupiter.api.BeforeAll; |
| 22 | +import org.junit.jupiter.api.BeforeEach; |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.mockito.InOrder; |
| 26 | +import org.mockito.Mockito; |
| 27 | +import org.slf4j.Logger; |
| 28 | + |
| 29 | +import java.net.URI; |
| 30 | +import java.util.Collections; |
| 31 | +import java.util.concurrent.TimeUnit; |
| 32 | + |
| 33 | +import static org.mockito.ArgumentMatchers.anyInt; |
| 34 | +import static org.mockito.ArgumentMatchers.anyString; |
| 35 | +import static org.mockito.ArgumentMatchers.argThat; |
| 36 | +import static org.mockito.ArgumentMatchers.eq; |
| 37 | +import static org.mockito.Mockito.mock; |
| 38 | +import static org.mockito.Mockito.timeout; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +public abstract class AbstractHttpLoggingInterceptorTest { |
| 42 | + |
| 43 | + private static DefaultMockServer server; |
| 44 | + private Logger logger; |
| 45 | + private InOrder inOrder; |
| 46 | + private HttpClient httpClient; |
| 47 | + |
| 48 | + @BeforeAll |
| 49 | + static void beforeAll() { |
| 50 | + server = new DefaultMockServer(false); |
| 51 | + server.start(); |
| 52 | + } |
| 53 | + |
| 54 | + @AfterAll |
| 55 | + static void afterAll() { |
| 56 | + server.shutdown(); |
| 57 | + } |
| 58 | + |
| 59 | + @BeforeEach |
| 60 | + void setUp() { |
| 61 | + logger = mock(Logger.class); |
| 62 | + inOrder = Mockito.inOrder(logger); |
| 63 | + when(logger.isTraceEnabled()).thenReturn(true); |
| 64 | + httpClient = getHttpClientFactory().newBuilder() |
| 65 | + .addOrReplaceInterceptor("loggingInterceptor", new HttpLoggingInterceptor(logger)) |
| 66 | + .build(); |
| 67 | + } |
| 68 | + |
| 69 | + protected abstract HttpClient.Factory getHttpClientFactory(); |
| 70 | + |
| 71 | + @Test |
| 72 | + @DisplayName("HTTP request URI is logged") |
| 73 | + public void httpRequestUriLogged() throws Exception { |
| 74 | + server.expect().withPath("/request-uri") |
| 75 | + .andReturn(200, "This is the response body") |
| 76 | + .always(); |
| 77 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 78 | + .uri(server.url("/request-uri")) |
| 79 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 80 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 81 | + inOrder.verify(logger) |
| 82 | + .trace(eq("> {} {}"), eq("GET"), argThat((URI uri) -> uri.toString().endsWith("/request-uri"))); |
| 83 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @DisplayName("HTTP request headers are logged") |
| 88 | + public void httpRequestHeadersLogged() throws Exception { |
| 89 | + server.expect().withPath("/request-headers") |
| 90 | + .andReturn(200, "This is the response body") |
| 91 | + .always(); |
| 92 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 93 | + .header("test-type", "header-test") |
| 94 | + .uri(server.url("/request-headers")) |
| 95 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 96 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 97 | + inOrder.verify(logger).trace("> {}: {}", "test-type", "header-test"); |
| 98 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + @DisplayName("HTTP request body is logged") |
| 103 | + public void httpRequestBodyLogged() throws Exception { |
| 104 | + server.expect().withPath("/request-body") |
| 105 | + .andReturn(200, "This is the response body") |
| 106 | + .always(); |
| 107 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 108 | + .uri(server.url("/request-body")) |
| 109 | + .method("POST", "test/plain", "This is the request body") |
| 110 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 111 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 112 | + inOrder.verify(logger).trace("This is the request body"); |
| 113 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + @DisplayName("HTTP response status is logged") |
| 118 | + public void httpResponseStatusLogged() throws Exception { |
| 119 | + server.expect().withPath("/response-status") |
| 120 | + .andReturn(200, "This is the response body") |
| 121 | + .always(); |
| 122 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 123 | + .uri(server.url("/response-status")) |
| 124 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 125 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 126 | + inOrder.verify(logger).trace("< {} {}", 200, "OK"); |
| 127 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + @DisplayName("HTTP response headers are logged") |
| 132 | + public void httpResponseHeadersLogged() throws Exception { |
| 133 | + server.expect().withPath("/response-headers") |
| 134 | + .andReply(ResponseProviders.of(204, "", Collections.singletonMap("test-type", "response-header-test"))) |
| 135 | + .always(); |
| 136 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 137 | + .uri(server.url("/response-headers")) |
| 138 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 139 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 140 | + inOrder.verify(logger).trace(eq("< {} {}"), anyInt(), anyString()); |
| 141 | + inOrder.verify(logger).trace("< {}: {}", "test-type", "response-header-test"); |
| 142 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + @DisplayName("HTTP response body is logged") |
| 147 | + public void httpResponseBodyLogged() throws Exception { |
| 148 | + server.expect().withPath("/response-body") |
| 149 | + .andReturn(200, "This is the response body") |
| 150 | + .always(); |
| 151 | + httpClient.sendAsync(httpClient.newHttpRequestBuilder() |
| 152 | + .uri(server.url("/response-body")) |
| 153 | + .build(), String.class).get(10, TimeUnit.SECONDS); |
| 154 | + inOrder.verify(logger, timeout(1000L)).trace("-HTTP START-"); |
| 155 | + inOrder.verify(logger).trace(eq("< {} {}"), anyInt(), anyString()); |
| 156 | + inOrder.verify(logger).trace("This is the response body"); |
| 157 | + inOrder.verify(logger).trace("-HTTP END-"); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + @DisplayName("WS request URI is logged") |
| 162 | + public void wsRequestUriLogged() throws Exception { |
| 163 | + server.expect().withPath("/ws-request-uri") |
| 164 | + .andUpgradeToWebSocket() |
| 165 | + .open().done().always(); |
| 166 | + httpClient.newWebSocketBuilder() |
| 167 | + .uri(URI.create(server.url("/ws-request-uri"))) |
| 168 | + .buildAsync(new WebSocket.Listener() { |
| 169 | + }) |
| 170 | + .get(10, TimeUnit.SECONDS); |
| 171 | + inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); |
| 172 | + inOrder.verify(logger) |
| 173 | + .trace(eq("> {} {}"), eq("GET"), argThat((URI uri) -> uri.toString().endsWith("/ws-request-uri"))); |
| 174 | + inOrder.verify(logger).trace("-WS END-"); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + @DisplayName("WS request headers are logged") |
| 179 | + public void wsRequestHeadersLogged() throws Exception { |
| 180 | + server.expect().withPath("/ws-request-headers") |
| 181 | + .andUpgradeToWebSocket() |
| 182 | + .open().done().always(); |
| 183 | + httpClient.newWebSocketBuilder() |
| 184 | + .header("test-type", "ws-header-test") |
| 185 | + .uri(URI.create(server.url("/ws-request-headers"))) |
| 186 | + .buildAsync(new WebSocket.Listener() { |
| 187 | + }) |
| 188 | + .get(10, TimeUnit.SECONDS); |
| 189 | + inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); |
| 190 | + inOrder.verify(logger).trace("> {}: {}", "test-type", "ws-header-test"); |
| 191 | + inOrder.verify(logger).trace("-WS END-"); |
| 192 | + } |
| 193 | + |
| 194 | + @Test |
| 195 | + @DisplayName("WS response status is logged") |
| 196 | + public void wsResponseStatusLogged() throws Exception { |
| 197 | + server.expect().withPath("/ws-response-status") |
| 198 | + .andUpgradeToWebSocket() |
| 199 | + .open().done().always(); |
| 200 | + httpClient.newWebSocketBuilder() |
| 201 | + .uri(URI.create(server.url("/ws-response-status"))) |
| 202 | + .buildAsync(new WebSocket.Listener() { |
| 203 | + }) |
| 204 | + .get(10, TimeUnit.SECONDS); |
| 205 | + inOrder.verify(logger, timeout(1000L)).trace("-WS START-"); |
| 206 | + inOrder.verify(logger).trace("< {} {}", 101, "Switching Protocols"); |
| 207 | + inOrder.verify(logger).trace("-WS END-"); |
| 208 | + } |
| 209 | +} |
0 commit comments