|
| 1 | +package io.kafbat.ui.util; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | +import okhttp3.mockwebserver.MockResponse; |
| 7 | +import okhttp3.mockwebserver.MockWebServer; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.springframework.http.MediaType; |
| 12 | +import org.springframework.web.reactive.function.client.WebClient; |
| 13 | + |
| 14 | +class WebClientConfiguratorTest { |
| 15 | + |
| 16 | + private final MockWebServer mockWebServer = new MockWebServer(); |
| 17 | + |
| 18 | + @BeforeEach |
| 19 | + void startMockServer() throws IOException { |
| 20 | + mockWebServer.start(); |
| 21 | + } |
| 22 | + |
| 23 | + @AfterEach |
| 24 | + void stopMockServer() throws IOException { |
| 25 | + mockWebServer.close(); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void decodesStandardApplicationJson() { |
| 30 | + mockWebServer.enqueue(new MockResponse() |
| 31 | + .addHeader("Content-Type", "application/json") |
| 32 | + .setBody("{\"name\":\"test\"}")); |
| 33 | + |
| 34 | + WebClient client = new WebClientConfigurator().build(); |
| 35 | + String body = client.get() |
| 36 | + .uri(mockWebServer.url("/").toString()) |
| 37 | + .retrieve() |
| 38 | + .bodyToMono(String.class) |
| 39 | + .block(); |
| 40 | + |
| 41 | + assertThat(body).isEqualTo("{\"name\":\"test\"}"); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + void decodesVendorMediaTypeWhenConfigured() { |
| 46 | + MediaType srV1Json = MediaType.parseMediaType("application/vnd.schemaregistry.v1+json"); |
| 47 | + |
| 48 | + mockWebServer.enqueue(new MockResponse() |
| 49 | + .addHeader("Content-Type", "application/vnd.schemaregistry.v1+json") |
| 50 | + .setBody("{\"compatibilityLevel\":\"BACKWARD\"}")); |
| 51 | + |
| 52 | + WebClient client = new WebClientConfigurator() |
| 53 | + .configureAdditionalDecoderMediaTypes(srV1Json) |
| 54 | + .build(); |
| 55 | + |
| 56 | + CompatibilityConfig config = client.get() |
| 57 | + .uri(mockWebServer.url("/config").toString()) |
| 58 | + .retrieve() |
| 59 | + .bodyToMono(CompatibilityConfig.class) |
| 60 | + .block(); |
| 61 | + |
| 62 | + assertThat(config).isNotNull(); |
| 63 | + assertThat(config.compatibilityLevel()).isEqualTo("BACKWARD"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void decodesMultipleVendorMediaTypes() { |
| 68 | + MediaType srV1Json = MediaType.parseMediaType("application/vnd.schemaregistry.v1+json"); |
| 69 | + MediaType srJson = MediaType.parseMediaType("application/vnd.schemaregistry+json"); |
| 70 | + |
| 71 | + mockWebServer.enqueue(new MockResponse() |
| 72 | + .addHeader("Content-Type", "application/vnd.schemaregistry+json") |
| 73 | + .setBody("{\"compatibilityLevel\":\"FULL\"}")); |
| 74 | + |
| 75 | + WebClient client = new WebClientConfigurator() |
| 76 | + .configureAdditionalDecoderMediaTypes(srV1Json, srJson) |
| 77 | + .build(); |
| 78 | + |
| 79 | + CompatibilityConfig config = client.get() |
| 80 | + .uri(mockWebServer.url("/config").toString()) |
| 81 | + .retrieve() |
| 82 | + .bodyToMono(CompatibilityConfig.class) |
| 83 | + .block(); |
| 84 | + |
| 85 | + assertThat(config).isNotNull(); |
| 86 | + assertThat(config.compatibilityLevel()).isEqualTo("FULL"); |
| 87 | + } |
| 88 | + |
| 89 | + record CompatibilityConfig(String compatibilityLevel) { |
| 90 | + } |
| 91 | +} |
0 commit comments