|
13 | 13 | */ |
14 | 14 | package feign; |
15 | 15 |
|
| 16 | +import static feign.Util.enumForName; |
| 17 | +import static java.util.Objects.nonNull; |
| 18 | + |
16 | 19 | import feign.Logger.Level; |
17 | | -import java.util.ArrayList; |
18 | | -import java.util.Arrays; |
19 | | -import java.util.Collections; |
20 | | -import java.util.List; |
| 20 | +import feign.Request.ProtocolVersion; |
| 21 | +import java.io.IOException; |
| 22 | +import java.net.HttpURLConnection; |
| 23 | +import java.util.*; |
21 | 24 | import java.util.concurrent.TimeUnit; |
22 | 25 | import okhttp3.mockwebserver.MockResponse; |
23 | 26 | import okhttp3.mockwebserver.MockWebServer; |
@@ -159,6 +162,69 @@ public void reasonPhraseOptional() { |
159 | 162 | } |
160 | 163 | } |
161 | 164 |
|
| 165 | + @RunWith(Parameterized.class) |
| 166 | + public static class HttpProtocolVersionTest extends LoggerTest { |
| 167 | + |
| 168 | + private final Level logLevel; |
| 169 | + private final String protocolVersionName; |
| 170 | + |
| 171 | + public HttpProtocolVersionTest( |
| 172 | + Level logLevel, String protocolVersionName, List<String> expectedMessages) { |
| 173 | + this.logLevel = logLevel; |
| 174 | + this.protocolVersionName = protocolVersionName; |
| 175 | + logger.expectMessages(expectedMessages); |
| 176 | + } |
| 177 | + |
| 178 | + @Parameters |
| 179 | + public static Iterable<Object[]> data() { |
| 180 | + return Arrays.asList( |
| 181 | + new Object[][] { |
| 182 | + { |
| 183 | + Level.BASIC, |
| 184 | + null, |
| 185 | + Arrays.asList( |
| 186 | + "\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1", |
| 187 | + "\\[SendsStuff#login\\] <--- HTTP/1.1 200 \\([0-9]+ms\\)") |
| 188 | + }, |
| 189 | + { |
| 190 | + Level.BASIC, |
| 191 | + "HTTP/1.1", |
| 192 | + Arrays.asList( |
| 193 | + "\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1", |
| 194 | + "\\[SendsStuff#login\\] <--- HTTP/1.1 200 \\([0-9]+ms\\)") |
| 195 | + }, |
| 196 | + { |
| 197 | + Level.BASIC, |
| 198 | + "HTTP/2.0", |
| 199 | + Arrays.asList( |
| 200 | + "\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1", |
| 201 | + "\\[SendsStuff#login\\] <--- HTTP/2.0 200 \\([0-9]+ms\\)") |
| 202 | + }, |
| 203 | + { |
| 204 | + Level.BASIC, |
| 205 | + "HTTP-XYZ", |
| 206 | + Arrays.asList( |
| 207 | + "\\[SendsStuff#login\\] ---> POST http://localhost:[0-9]+/ HTTP/1.1", |
| 208 | + "\\[SendsStuff#login\\] <--- UNKNOWN 200 \\([0-9]+ms\\)") |
| 209 | + } |
| 210 | + }); |
| 211 | + } |
| 212 | + |
| 213 | + @Test |
| 214 | + public void testHttpProtocolVersion() { |
| 215 | + server.enqueue(new MockResponse().setStatus("HTTP/1.1 " + 200)); |
| 216 | + |
| 217 | + SendsStuff api = |
| 218 | + Feign.builder() |
| 219 | + .client(new TestProtocolVersionClient(protocolVersionName)) |
| 220 | + .logger(logger) |
| 221 | + .logLevel(logLevel) |
| 222 | + .target(SendsStuff.class, "http://localhost:" + server.getPort()); |
| 223 | + |
| 224 | + api.login("netflix", "denominator", "password"); |
| 225 | + } |
| 226 | + } |
| 227 | + |
162 | 228 | @RunWith(Parameterized.class) |
163 | 229 | public static class ReadTimeoutEmitsTest extends LoggerTest { |
164 | 230 |
|
@@ -495,4 +561,25 @@ public void evaluate() throws Throwable { |
495 | 561 | }; |
496 | 562 | } |
497 | 563 | } |
| 564 | + |
| 565 | + private static final class TestProtocolVersionClient extends Client.Default { |
| 566 | + private final String protocolVersionName; |
| 567 | + |
| 568 | + public TestProtocolVersionClient(String protocolVersionName) { |
| 569 | + super(null, null); |
| 570 | + this.protocolVersionName = protocolVersionName; |
| 571 | + } |
| 572 | + |
| 573 | + @Override |
| 574 | + Response convertResponse(HttpURLConnection connection, Request request) throws IOException { |
| 575 | + Response response = super.convertResponse(connection, request); |
| 576 | + if (nonNull((protocolVersionName))) { |
| 577 | + response = |
| 578 | + response.toBuilder() |
| 579 | + .protocolVersion(enumForName(ProtocolVersion.class, protocolVersionName)) |
| 580 | + .build(); |
| 581 | + } |
| 582 | + return response; |
| 583 | + } |
| 584 | + } |
498 | 585 | } |
0 commit comments