Add rest-test-client module: RestTestClient across all four binding modes, TestRestTemplate relocation, gzip-hiding HTTP client surprise (post #44)
This commit is contained in:
+52
@@ -0,0 +1,52 @@
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user