Skip to content

Commit 7797453

Browse files
committed
Refactor resolver tests
Extract NamedValueArgumentResolverTests. Move form data vs query params tests into HttpRequestValues. See gh-28386
1 parent 2794553 commit 7797453

10 files changed

+416
-439
lines changed

spring-web/src/test/java/org/springframework/web/service/invoker/CookieValueArgumentResolverTests.java

+8-125
Original file line numberDiff line numberDiff line change
@@ -17,125 +17,39 @@
1717
package org.springframework.web.service.invoker;
1818

1919
import java.util.List;
20-
import java.util.Map;
21-
import java.util.Optional;
2220

23-
import org.apache.groovy.util.Maps;
2421
import org.junit.jupiter.api.Test;
2522

26-
import org.springframework.lang.Nullable;
2723
import org.springframework.util.ObjectUtils;
2824
import org.springframework.web.bind.annotation.CookieValue;
2925
import org.springframework.web.service.annotation.GetExchange;
3026

3127
import static org.assertj.core.api.Assertions.assertThat;
32-
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3328

3429

3530
/**
3631
* Unit tests for {@link RequestHeaderArgumentResolver}.
32+
* <p>For base class functionality, see {@link NamedValueArgumentResolverTests}.
3733
*
3834
* @author Rossen Stoyanchev
3935
*/
4036
class CookieValueArgumentResolverTests {
4137

42-
private final TestHttpClientAdapter clientAdapter = new TestHttpClientAdapter();
38+
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
4339

44-
private final Service service = this.clientAdapter.createService(Service.class);
40+
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
4541

4642

47-
@Test
48-
void stringCookie() {
49-
this.service.executeString("test");
50-
assertCookie("cookie", "test");
51-
}
52-
53-
@Test
54-
void objectCookie() {
55-
this.service.execute(Boolean.TRUE);
56-
assertCookie("cookie", "true");
57-
}
58-
59-
@Test
60-
void listCookie() {
61-
this.service.executeList(List.of("test1", Boolean.TRUE, "test3"));
62-
assertCookie("multiValueCookie", "test1", "true", "test3");
63-
}
64-
65-
@Test
66-
void arrayCookie() {
67-
this.service.executeArray("test1", Boolean.FALSE, "test3");
68-
assertCookie("multiValueCookie", "test1", "false", "test3");
69-
}
70-
71-
@Test
72-
void namedCookie() {
73-
this.service.executeNamed("test");
74-
assertCookie("cookieRenamed", "test");
75-
}
76-
77-
@SuppressWarnings("ConstantConditions")
78-
@Test
79-
void nullCookieRequired() {
80-
assertThatIllegalArgumentException().isThrownBy(() -> this.service.executeString(null));
81-
}
82-
83-
@Test
84-
void nullCookieNotRequired() {
85-
this.service.executeNotRequired(null);
86-
assertCookie("cookie");
87-
}
88-
89-
@Test
90-
void nullCookieWithDefaultValue() {
91-
this.service.executeWithDefaultValue(null);
92-
assertCookie("cookie", "default");
93-
}
43+
// Base class functionality should be tested in NamedValueArgumentResolverTests.
9444

9545
@Test
96-
void optionalStringCookie() {
97-
this.service.executeOptional(Optional.of("test"));
98-
assertCookie("cookie", "test");
99-
}
100-
101-
@Test
102-
void optionalObjectCookie() {
103-
this.service.executeOptional(Optional.of(Boolean.TRUE));
104-
assertCookie("cookie", "true");
105-
}
106-
107-
@Test
108-
void optionalEmpty() {
109-
this.service.executeOptional(Optional.empty());
110-
assertCookie("cookie");
111-
}
112-
113-
@Test
114-
void optionalEmpthyWithDefaultValue() {
115-
this.service.executeOptionalWithDefaultValue(Optional.empty());
116-
assertCookie("cookie", "default");
117-
}
118-
119-
@Test
120-
void mapOfCookies() {
121-
this.service.executeMap(Maps.of("cookie1", "true", "cookie2", "false"));
122-
assertCookie("cookie1", "true");
123-
assertCookie("cookie2", "false");
124-
}
125-
126-
@Test
127-
void mapOfCookiesIsNull() {
128-
assertThatIllegalArgumentException().isThrownBy(() -> this.service.executeMap(null));
129-
}
130-
131-
@Test
132-
void mapOfCookiesHasOptionalValue() {
133-
this.service.executeMapWithOptionalValue(Map.of("cookie", Optional.of("test")));
46+
void cookieValue() {
47+
this.service.execute("test");
13448
assertCookie("cookie", "test");
13549
}
13650

13751
private void assertCookie(String key, String... values) {
138-
List<String> actualValues = this.clientAdapter.getRequestValues().getCookies().get(key);
52+
List<String> actualValues = this.client.getRequestValues().getCookies().get(key);
13953
if (ObjectUtils.isEmpty(values)) {
14054
assertThat(actualValues).isNull();
14155
}
@@ -145,41 +59,10 @@ private void assertCookie(String key, String... values) {
14559
}
14660

14761

148-
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
14962
private interface Service {
15063

15164
@GetExchange
152-
void executeString(@CookieValue String cookie);
153-
154-
@GetExchange
155-
void execute(@CookieValue Object cookie);
156-
157-
@GetExchange
158-
void executeList(@CookieValue List<Object> multiValueCookie);
159-
160-
@GetExchange
161-
void executeArray(@CookieValue Object... multiValueCookie);
162-
163-
@GetExchange
164-
void executeNamed(@CookieValue(name = "cookieRenamed") String cookie);
165-
166-
@GetExchange
167-
void executeNotRequired(@Nullable @CookieValue(required = false) String cookie);
168-
169-
@GetExchange
170-
void executeWithDefaultValue(@Nullable @CookieValue(defaultValue = "default") String cookie);
171-
172-
@GetExchange
173-
void executeOptional(@CookieValue Optional<Object> cookie);
174-
175-
@GetExchange
176-
void executeOptionalWithDefaultValue(@CookieValue(defaultValue = "default") Optional<Object> cookie);
177-
178-
@GetExchange
179-
void executeMap(@Nullable @CookieValue Map<String, String> cookie);
180-
181-
@GetExchange
182-
void executeMapWithOptionalValue(@CookieValue Map<String, Optional<String>> cookies);
65+
void execute(@CookieValue String cookie);
18366

18467
}
18568

spring-web/src/test/java/org/springframework/web/service/invoker/HttpMethodArgumentResolverTests.java

+10-24
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.junit.jupiter.api.Test;
2020

2121
import org.springframework.http.HttpMethod;
22-
import org.springframework.lang.Nullable;
2322
import org.springframework.web.service.annotation.GetExchange;
2423

2524
import static org.assertj.core.api.Assertions.assertThat;
@@ -29,40 +28,35 @@
2928
* Tests for {@link HttpMethodArgumentResolver}.
3029
*
3130
* @author Olga Maciaszek-Sharma
31+
* @author Rossen Stoyanchev
3232
*/
3333
public class HttpMethodArgumentResolverTests {
3434

35-
private final TestHttpClientAdapter clientAdapter = new TestHttpClientAdapter();
35+
private final TestHttpClientAdapter client = new TestHttpClientAdapter();
3636

37-
private final Service service = this.clientAdapter.createService(Service.class);
37+
private final Service service = HttpServiceProxyFactory.builder(this.client).build().createClient(Service.class);
3838

3939

4040
@Test
41-
void shouldResolveRequestMethodFromArgument() {
42-
this.service.execute(HttpMethod.GET);
43-
assertThat(getActualMethod()).isEqualTo(HttpMethod.GET);
41+
void requestMethodOverride() {
42+
this.service.execute(HttpMethod.POST);
43+
assertThat(getActualMethod()).isEqualTo(HttpMethod.POST);
4444
}
4545

4646
@Test
47-
void shouldIgnoreArgumentsNotMatchingType() {
47+
void ignoreOtherArgumentTypes() {
4848
this.service.execute("test");
4949
assertThat(getActualMethod()).isEqualTo(HttpMethod.GET);
5050
}
5151

5252
@Test
53-
void shouldOverrideMethodAnnotation() {
54-
this.service.executeGet(HttpMethod.POST);
55-
assertThat(getActualMethod()).isEqualTo(HttpMethod.POST);
56-
}
57-
58-
@Test
59-
void shouldIgnoreNullValue() {
60-
this.service.executeForNull(null);
53+
void ignoreNull() {
54+
this.service.execute((HttpMethod) null);
6155
assertThat(getActualMethod()).isEqualTo(HttpMethod.GET);
6256
}
6357

6458
private HttpMethod getActualMethod() {
65-
return this.clientAdapter.getRequestValues().getHttpMethod();
59+
return this.client.getRequestValues().getHttpMethod();
6660
}
6761

6862

@@ -71,17 +65,9 @@ private interface Service {
7165
@GetExchange
7266
void execute(HttpMethod method);
7367

74-
@GetExchange
75-
void executeGet(HttpMethod method);
76-
7768
@GetExchange
7869
void execute(String test);
7970

80-
@GetExchange
81-
void execute(HttpMethod firstMethod, HttpMethod secondMethod);
82-
83-
@GetExchange
84-
void executeForNull(@Nullable HttpMethod method);
8571
}
8672

8773
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2002-2022 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.web.service.invoker;
18+
19+
20+
import java.net.URI;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.ValueSource;
25+
26+
import org.springframework.http.HttpMethod;
27+
import org.springframework.http.MediaType;
28+
import org.springframework.web.util.UriComponentsBuilder;
29+
30+
import static java.nio.charset.StandardCharsets.UTF_8;
31+
import static org.assertj.core.api.Assertions.assertThat;
32+
33+
34+
/**
35+
* Unit tests for {@link HttpRequestValues}.
36+
*
37+
* @author Rossen Stoyanchev
38+
*/
39+
public class HttpRequestValuesTests {
40+
41+
@Test
42+
void defaultUri() {
43+
HttpRequestValues requestValues = HttpRequestValues.builder(HttpMethod.GET).build();
44+
45+
assertThat(requestValues.getUri()).isNull();
46+
assertThat(requestValues.getUriTemplate()).isEqualTo("");
47+
}
48+
49+
@ParameterizedTest
50+
@ValueSource(strings = {"POST", "PUT", "PATCH"})
51+
void requestParamAsFormData(String httpMethod) {
52+
53+
HttpRequestValues requestValues = HttpRequestValues.builder(HttpMethod.valueOf(httpMethod))
54+
.setContentType(MediaType.APPLICATION_FORM_URLENCODED)
55+
.addRequestParameter("param1", "1st value")
56+
.addRequestParameter("param2", "2nd value A", "2nd value B")
57+
.build();
58+
59+
Object body = requestValues.getBodyValue();
60+
assertThat(body).isNotNull().isInstanceOf(byte[].class);
61+
assertThat(new String((byte[]) body, UTF_8)).isEqualTo("param1=1st+value&param2=2nd+value+A&param2=2nd+value+B");
62+
}
63+
64+
@Test
65+
void requestParamAsQueryParamsInUriTemplate() {
66+
67+
HttpRequestValues requestValues = HttpRequestValues.builder(HttpMethod.POST)
68+
.setUriTemplate("/path")
69+
.addRequestParameter("param1", "1st value")
70+
.addRequestParameter("param2", "2nd value A", "2nd value B")
71+
.build();
72+
73+
String uriTemplate = requestValues.getUriTemplate();
74+
assertThat(uriTemplate).isNotNull();
75+
76+
assertThat(uriTemplate)
77+
.isEqualTo("/path?" +
78+
"{queryParam0}={queryParam0[0]}&" +
79+
"{queryParam1}={queryParam1[0]}&" +
80+
"{queryParam1}={queryParam1[1]}");
81+
82+
assertThat(requestValues.getUriVariables())
83+
.containsOnlyKeys("queryParam0", "queryParam1", "queryParam0[0]", "queryParam1[0]", "queryParam1[1]")
84+
.containsEntry("queryParam0", "param1")
85+
.containsEntry("queryParam1", "param2")
86+
.containsEntry("queryParam0[0]", "1st value")
87+
.containsEntry("queryParam1[0]", "2nd value A")
88+
.containsEntry("queryParam1[1]", "2nd value B");
89+
90+
URI uri = UriComponentsBuilder.fromUriString(uriTemplate)
91+
.encode()
92+
.build(requestValues.getUriVariables());
93+
94+
assertThat(uri.toString())
95+
.isEqualTo("/path?param1=1st%20value&param2=2nd%20value%20A&param2=2nd%20value%20B");
96+
}
97+
98+
@Test
99+
void requestParamAsQueryParamsInUri() {
100+
101+
HttpRequestValues requestValues = HttpRequestValues.builder(HttpMethod.POST)
102+
.setUri(URI.create("/path"))
103+
.addRequestParameter("param1", "1st value")
104+
.addRequestParameter("param2", "2nd value A", "2nd value B")
105+
.build();
106+
107+
assertThat(requestValues.getUri().toString())
108+
.isEqualTo("/path?param1=1st%20value&param2=2nd%20value%20A&param2=2nd%20value%20B");
109+
}
110+
111+
}

0 commit comments

Comments
 (0)