Files
spring-boot-demo/rest-test-client/src/test/java/com/ankurm/resttestclient/HttpClientChoiceComparisonTest.java
T

53 lines
2.1 KiB
Java

package com.ankurm.resttestclient;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Not a RestTestClient test -- this is plain {@code RestClient} against the same running
* server, isolating exactly one variable: which {@link org.springframework.http.client.ClientHttpRequestFactory}
* handles the socket. It exists to prove the claim made in {@link PersonEndToEndTest}: that
* "bind to a real server" does not by itself guarantee you can observe real HTTP-level
* behavior, because the client you get by default may have already undone it for you.
*/
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class HttpClientChoiceComparisonTest {
@LocalServerPort
int port;
@Test
void theJdkHttpClientDecodesGzipAndHidesTheHeader() {
RestClient client = RestClient.builder()
.requestFactory(new JdkClientHttpRequestFactory())
.baseUrl("http://localhost:" + port)
.build();
Boolean hasContentEncoding = client.get().uri("/api/persons")
.header("Accept-Encoding", "gzip")
.exchange((request, response) -> response.getHeaders().get("Content-Encoding") != null);
assertThat(hasContentEncoding).isFalse();
}
@Test
void theOldHttpUrlConnectionFactoryShowsTheRealHeader() {
RestClient client = RestClient.builder()
.requestFactory(new SimpleClientHttpRequestFactory())
.baseUrl("http://localhost:" + port)
.build();
String contentEncoding = client.get().uri("/api/persons")
.header("Accept-Encoding", "gzip")
.exchange((request, response) -> response.getHeaders().getFirst("Content-Encoding"));
assertThat(contentEncoding).isEqualTo("gzip");
}
}