/
WebClientHttpServiceProxyTests.java
150 lines (115 loc) · 4.28 KB
/
WebClientHttpServiceProxyTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive.function.client.support;
import java.io.IOException;
import java.net.URI;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.service.annotation.GetExchange;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link HttpServiceProxyFactory HTTP Service proxy}
* using {@link WebClient} and {@link MockWebServer}.
*
* @author Rossen Stoyanchev
*/
public class WebClientHttpServiceProxyTests {
private MockWebServer server;
@BeforeEach
void setUp() {
this.server = new MockWebServer();
}
@SuppressWarnings("ConstantConditions")
@AfterEach
void shutdown() throws IOException {
if (this.server != null) {
this.server.shutdown();
}
}
@Test
void greeting() throws Exception {
prepareResponse(response ->
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
StepVerifier.create(initHttpService().getGreeting())
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
}
@Test
void greetingWithRequestAttribute() {
Map<String, Object> attributes = new HashMap<>();
WebClient webClient = WebClient.builder()
.baseUrl(this.server.url("/").toString())
.filter((request, next) -> {
attributes.putAll(request.attributes());
return next.exchange(request);
})
.build();
prepareResponse(response ->
response.setHeader("Content-Type", "text/plain").setBody("Hello Spring!"));
StepVerifier.create(initHttpService(webClient).getGreetingWithAttribute("myAttributeValue"))
.expectNext("Hello Spring!")
.expectComplete()
.verify(Duration.ofSeconds(5));
assertThat(attributes).containsEntry("myAttribute", "myAttributeValue");
}
@Test // gh-29624
void uri() throws Exception {
String expectedBody = "hello";
prepareResponse(response -> response.setResponseCode(200).setBody(expectedBody));
URI dynamicUri = this.server.url("/greeting/123").uri();
String actualBody = initHttpService().getGreetingById(dynamicUri, "456");
assertThat(actualBody).isEqualTo(expectedBody);
assertThat(this.server.takeRequest().getRequestUrl().uri()).isEqualTo(dynamicUri);
}
private TestHttpService initHttpService() {
WebClient webClient = WebClient.builder().baseUrl(this.server.url("/").toString()).build();
return initHttpService(webClient);
}
private TestHttpService initHttpService(WebClient webClient) {
return HttpServiceProxyFactory.builder()
.clientAdapter(WebClientAdapter.forClient(webClient))
.build()
.createClient(TestHttpService.class);
}
private void prepareResponse(Consumer<MockResponse> consumer) {
MockResponse response = new MockResponse();
consumer.accept(response);
this.server.enqueue(response);
}
private interface TestHttpService {
@GetExchange("/greeting")
Mono<String> getGreeting();
@GetExchange("/greeting")
Mono<String> getGreetingWithAttribute(@RequestAttribute String myAttribute);
@GetExchange("/greetings/{id}")
String getGreetingById(@Nullable URI uri, @PathVariable String id);
}
}