|
| 1 | +/* |
| 2 | + * Copyright 2002-present the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.test.web.servlet.client; |
| 18 | + |
| 19 | +import java.io.IOException; |
| 20 | +import java.lang.annotation.ElementType; |
| 21 | +import java.lang.annotation.Retention; |
| 22 | +import java.lang.annotation.RetentionPolicy; |
| 23 | +import java.lang.annotation.Target; |
| 24 | +import java.util.function.Function; |
| 25 | +import java.util.stream.Stream; |
| 26 | + |
| 27 | +import mockwebserver3.MockResponse; |
| 28 | +import mockwebserver3.MockWebServer; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.params.ParameterizedTest; |
| 31 | +import org.junit.jupiter.params.provider.Arguments; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | + |
| 34 | +import org.springframework.http.client.ClientHttpRequestFactory; |
| 35 | +import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; |
| 36 | +import org.springframework.http.client.JdkClientHttpRequestFactory; |
| 37 | +import org.springframework.http.client.JettyClientHttpRequestFactory; |
| 38 | +import org.springframework.http.client.ReactorClientHttpRequestFactory; |
| 39 | +import org.springframework.http.client.SimpleClientHttpRequestFactory; |
| 40 | + |
| 41 | +import static org.junit.jupiter.params.provider.Arguments.argumentSet; |
| 42 | + |
| 43 | +/** |
| 44 | + * Integration tests for {@link RestTestClient} against a live server. |
| 45 | + */ |
| 46 | +class RestTestClientIntegrationTests { |
| 47 | + |
| 48 | + private RestTestClient client; |
| 49 | + |
| 50 | + @Retention(RetentionPolicy.RUNTIME) |
| 51 | + @Target(ElementType.METHOD) |
| 52 | + @ParameterizedTest |
| 53 | + @MethodSource("clientHttpRequestFactories") |
| 54 | + @interface ParameterizedRestClientTest { |
| 55 | + } |
| 56 | + |
| 57 | + static Stream<Arguments> clientHttpRequestFactories() { |
| 58 | + return Stream.of( |
| 59 | + argumentSet("JDK HttpURLConnection", new SimpleClientHttpRequestFactory()), |
| 60 | + argumentSet("Jetty", new JettyClientHttpRequestFactory()), |
| 61 | + argumentSet("JDK HttpClient", new JdkClientHttpRequestFactory()), |
| 62 | + argumentSet("Reactor Netty", new ReactorClientHttpRequestFactory()), |
| 63 | + argumentSet("HttpComponents", new HttpComponentsClientHttpRequestFactory()) |
| 64 | + ); |
| 65 | + } |
| 66 | + |
| 67 | + private MockWebServer server; |
| 68 | + |
| 69 | + private RestTestClient testClient; |
| 70 | + |
| 71 | + |
| 72 | + private void startServer(ClientHttpRequestFactory requestFactory) throws IOException { |
| 73 | + this.server = new MockWebServer(); |
| 74 | + this.server.start(); |
| 75 | + this.testClient = RestTestClient.bindToServer(requestFactory) |
| 76 | + .baseUrl(this.server.url("/").toString()) |
| 77 | + .build(); |
| 78 | + } |
| 79 | + |
| 80 | + @ParameterizedRestClientTest // gh-35784 |
| 81 | + void sequentialRequestsNotConsumingBody(ClientHttpRequestFactory requestFactory) throws IOException { |
| 82 | + startServer(requestFactory); |
| 83 | + for (int i = 0; i < 10; i++) { |
| 84 | + prepareResponse(builder -> |
| 85 | + builder.setHeader("Content-Type", "text/plain").body("Hello Spring!")); |
| 86 | + this.testClient.get().uri("/").exchange().expectStatus().isOk(); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + private void prepareResponse(Function<MockResponse.Builder, MockResponse.Builder> f) { |
| 91 | + MockResponse.Builder builder = new MockResponse.Builder(); |
| 92 | + this.server.enqueue(f.apply(builder).build()); |
| 93 | + } |
| 94 | + |
| 95 | + @AfterEach |
| 96 | + void shutdown() throws IOException { |
| 97 | + if (server != null) { |
| 98 | + this.server.close(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | +} |
0 commit comments