Skip to content

Commit 55b8f06

Browse files
Fix OkHttp tests
When gh-7375 was merged in tests were ok locally and the PR build was also fine. But after merge, it broke the build. The failure is valid, the tests did not follow the implementation, this change fixes them. See gh-7373 See gh-7375
1 parent 140b83b commit 55b8f06

2 files changed

Lines changed: 13 additions & 13 deletions

File tree

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpMetricsEventListenerTest.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,16 @@
2222
import io.micrometer.core.instrument.Tags;
2323
import io.micrometer.core.instrument.simple.SimpleConfig;
2424
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
25-
import okhttp3.Cache;
26-
import okhttp3.OkHttpClient;
27-
import okhttp3.Request;
28-
import okhttp3.Response;
25+
import okhttp3.*;
2926
import org.junit.jupiter.api.Test;
3027
import org.junit.jupiter.api.extension.ExtendWith;
3128
import org.junit.jupiter.api.io.TempDir;
29+
import org.testcontainers.shaded.org.checkerframework.checker.nullness.qual.Nullable;
3230
import ru.lanwen.wiremock.ext.WiremockResolver;
3331

3432
import java.io.IOException;
3533
import java.nio.file.Path;
34+
import java.util.Optional;
3635
import java.util.concurrent.TimeUnit;
3736
import java.util.function.Function;
3837

@@ -53,9 +52,9 @@ class OkHttpMetricsEventListenerTest {
5352

5453
private static final String URI_EXAMPLE_VALUE = "uriExample";
5554

56-
private static final Function<Request, String> URI_MAPPER = req -> URI_EXAMPLE_VALUE;
55+
private static final Function<@Nullable Request, String> URI_MAPPER = req -> URI_EXAMPLE_VALUE;
5756

58-
private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
57+
private final MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
5958

6059
private OkHttpClient client = new OkHttpClient.Builder()
6160
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
@@ -146,7 +145,7 @@ void uriTagWorksWithUriMapper(@WiremockResolver.Wiremock WireMockServer server)
146145
server.stubFor(any(anyUrl()));
147146
OkHttpClient client = new OkHttpClient.Builder()
148147
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
149-
.uriMapper(req -> req.url().encodedPath())
148+
.uriMapper(req -> Optional.ofNullable(req).map(Request::url).map(HttpUrl::encodedPath).orElse("null"))
150149
.tags(Tags.of("foo", "bar"))
151150
.build())
152151
.build();
@@ -167,7 +166,8 @@ void contextSpecificTags(@WiremockResolver.Wiremock WireMockServer server) throw
167166
server.stubFor(any(anyUrl()));
168167
OkHttpClient client = new OkHttpClient.Builder()
169168
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
170-
.tag((req, res) -> Tag.of("another.uri", req.url().encodedPath()))
169+
.tag((req, res) -> Tag.of("another.uri",
170+
Optional.ofNullable(req).map(Request::url).map(HttpUrl::encodedPath).orElse("null")))
171171
.build())
172172
.build();
173173

@@ -268,7 +268,7 @@ private void testRequestTags(@WiremockResolver.Wiremock WireMockServer server, R
268268
server.stubFor(any(anyUrl()));
269269
OkHttpClient client = new OkHttpClient.Builder()
270270
.eventListener(OkHttpMetricsEventListener.builder(registry, "okhttp.requests")
271-
.uriMapper(req -> req.url().encodedPath())
271+
.uriMapper(req -> Optional.ofNullable(req).map(Request::url).map(HttpUrl::encodedPath).orElse("null"))
272272
.tags(Tags.of("foo", "bar"))
273273
.build())
274274
.build();

micrometer-core/src/test/java/io/micrometer/core/instrument/binder/okhttp3/OkHttpObservationInterceptorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ class OkHttpObservationInterceptorTest {
5959

6060
private static final String URI_EXAMPLE_VALUE = "uriExample";
6161

62-
private static final Function<Request, String> URI_MAPPER = req -> URI_EXAMPLE_VALUE;
62+
private static final Function<@Nullable Request, String> URI_MAPPER = req -> URI_EXAMPLE_VALUE;
6363

64-
private MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
64+
private final MeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock());
6565

66-
private TestObservationRegistry observationRegistry = TestObservationRegistry.create();
66+
private final TestObservationRegistry observationRegistry = TestObservationRegistry.create();
6767

68-
private TestHandler testHandler = new TestHandler();
68+
private final TestHandler testHandler = new TestHandler();
6969

7070
// tag::setup[]
7171
private OkHttpClient client = new OkHttpClient.Builder().addInterceptor(defaultInterceptorBuilder().build())

0 commit comments

Comments
 (0)