Skip to content

Commit a8de8ac

Browse files
committed
Merge branch '6.2.x'
Closes gh-34286
2 parents 7fedb9c + cecebd0 commit a8de8ac

File tree

2 files changed

+42
-39
lines changed

2 files changed

+42
-39
lines changed

spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public HttpHeadersAssert containsHeaders(String... names) {
8686
*/
8787
public HttpHeadersAssert containsOnlyHeaders(String... names) {
8888
this.namesAssert
89-
.as("check headers contains only HTTP headers '%s'", Arrays.toString(names))
89+
.as("check headers contain only HTTP headers '%s'", Arrays.toString(names))
9090
.containsOnly(names);
9191
return this.myself;
9292
}
@@ -98,7 +98,7 @@ public HttpHeadersAssert containsOnlyHeaders(String... names) {
9898
*/
9999
public HttpHeadersAssert doesNotContainHeader(String name) {
100100
this.namesAssert
101-
.as("check headers does not contain HTTP header '%s'", name)
101+
.as("check headers do not contain HTTP header '%s'", name)
102102
.doesNotContain(name);
103103
return this.myself;
104104
}
@@ -111,31 +111,32 @@ public HttpHeadersAssert doesNotContainHeader(String name) {
111111
*/
112112
public HttpHeadersAssert doesNotContainHeaders(String... names) {
113113
this.namesAssert
114-
.as("check headers does not contain HTTP headers '%s'", Arrays.toString(names))
114+
.as("check headers do not contain HTTP headers '%s'", Arrays.toString(names))
115115
.doesNotContain(names);
116116
return this.myself;
117117
}
118118

119119
/**
120120
* Verify that the actual HTTP headers contain a header with the given
121121
* {@code name} that satisfies the given {@code valueRequirements}.
122-
* @param name the name of an HTTP header that should not be present
122+
* @param name the name of the header
123123
* @param valueRequirements the group of assertions to run against the
124124
* values of the header with the given name
125+
* @since 7.0
125126
*/
126127
@SuppressWarnings("unchecked")
127128
public HttpHeadersAssert hasHeaderSatisfying(String name, Consumer<List<String>> valueRequirements) {
128129
containsHeader(name);
129130
Assertions.assertThat(this.actual.get(name))
130-
.as("check primary value for HTTP header '%s'", name)
131+
.as("check all values for HTTP header '%s'", name)
131132
.satisfies(values -> valueRequirements.accept((List<String>) values));
132133
return this.myself;
133134
}
134135

135136
/**
136137
* Verify that the actual HTTP headers contain a header with the given
137138
* {@code name} and {@link String} primary {@code value}.
138-
* @param name the name of the cookie
139+
* @param name the name of the header
139140
* @param value the expected value of the header
140141
*/
141142
public HttpHeadersAssert hasValue(String name, String value) {
@@ -148,8 +149,8 @@ public HttpHeadersAssert hasValue(String name, String value) {
148149

149150
/**
150151
* Verify that the actual HTTP headers contain a header with the given
151-
* {@code name} and {@link Long} primary {@code value}.
152-
* @param name the name of the cookie
152+
* {@code name} and {@code long} primary {@code value}.
153+
* @param name the name of the header
153154
* @param value the expected value of the header
154155
*/
155156
public HttpHeadersAssert hasValue(String name, long value) {
@@ -163,7 +164,7 @@ public HttpHeadersAssert hasValue(String name, long value) {
163164
/**
164165
* Verify that the actual HTTP headers contain a header with the given
165166
* {@code name} and {@link Instant} primary {@code value}.
166-
* @param name the name of the cookie
167+
* @param name the name of the header
167168
* @param value the expected value of the header
168169
*/
169170
public HttpHeadersAssert hasValue(String name, Instant value) {
@@ -178,7 +179,7 @@ public HttpHeadersAssert hasValue(String name, Instant value) {
178179
* Verify that the actual HTTP headers contain a header with the given
179180
* {@code name} and {@link String} primary {@code value}.
180181
* <p>This assertion fails if the header has secondary values.
181-
* @param name the name of the cookie
182+
* @param name the name of the header
182183
* @param value the expected only value of the header
183184
* @since 7.0
184185
*/
@@ -189,9 +190,9 @@ public HttpHeadersAssert hasSingleValue(String name, String value) {
189190

190191
/**
191192
* Verify that the actual HTTP headers contain a header with the given
192-
* {@code name} and {@link Long} primary {@code value}.
193+
* {@code name} and {@code long} primary {@code value}.
193194
* <p>This assertion fails if the header has secondary values.
194-
* @param name the name of the cookie
195+
* @param name the name of the header
195196
* @param value the expected value of the header
196197
* @since 7.0
197198
*/
@@ -204,7 +205,7 @@ public HttpHeadersAssert hasSingleValue(String name, long value) {
204205
* Verify that the actual HTTP headers contain a header with the given
205206
* {@code name} and {@link Instant} primary {@code value}.
206207
* <p>This assertion fails if the header has secondary values.
207-
* @param name the name of the cookie
208+
* @param name the name of the header
208209
* @param value the expected value of the header
209210
* @since 7.0
210211
*/
@@ -265,7 +266,7 @@ public HttpHeadersAssert isNotEmpty() {
265266
}
266267

267268
/**
268-
* Verify that there is exactly {@code expected} headers present, when
269+
* Verify that there are exactly {@code expected} headers present, when
269270
* considering header names in a case-insensitive manner.
270271
* @param expected the expected number of headers
271272
*/
@@ -293,11 +294,13 @@ public HttpHeadersAssert hasSameSizeAs(HttpHeaders other) {
293294
private HttpHeadersAssert doesNotHaveSecondaryValues(String name) {
294295
containsHeader(name);
295296
List<String> values = this.actual.get(name);
296-
int size = (values != null) ? values.size() : 0;
297-
Assertions.assertThat(size)
298-
.withFailMessage("Expected HTTP header '%s' to be present " +
299-
"without secondary values, but found <%s> secondary values", name, size - 1)
300-
.isOne();
297+
if (values != null && !values.isEmpty()) {
298+
int size = values.size();
299+
Assertions.assertThat(size)
300+
.withFailMessage("Expected HTTP header '%s' to be present " +
301+
"without secondary values, but found <%s> secondary value(s)", name, size - 1)
302+
.isOne();
303+
}
301304
return this.myself;
302305
}
303306

spring-test/src/test/java/org/springframework/test/http/HttpHeadersAssertTests.java

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void containsOnlyHeadersWithMissingOne() {
8484
headers.add("name2", "value2");
8585
assertThatExceptionOfType(AssertionError.class)
8686
.isThrownBy(() -> assertThat(headers).containsOnlyHeaders("name1", "name2", "name3"))
87-
.withMessageContainingAll("check headers contains only HTTP headers",
87+
.withMessageContainingAll("check headers contain only HTTP headers",
8888
"could not find the following element(s)", "[\"name3\"]");
8989
}
9090

@@ -96,7 +96,7 @@ void containsOnlyHeadersWithExtraOne() {
9696
headers.add("name3", "value3");
9797
assertThatExceptionOfType(AssertionError.class)
9898
.isThrownBy(() -> assertThat(headers).containsOnlyHeaders("name1", "name2"))
99-
.withMessageContainingAll("check headers contains only HTTP headers",
99+
.withMessageContainingAll("check headers contain only HTTP headers",
100100
"the following element(s) were unexpected", "[\"name3\"]");
101101
}
102102

@@ -185,10 +185,10 @@ void hasValueWithNoStringMatch() {
185185

186186
@Test
187187
void hasValueWithNonPresentHeader() {
188-
HttpHeaders map = new HttpHeaders();
189-
map.add("test-header", "a");
188+
HttpHeaders headers = new HttpHeaders();
189+
headers.add("test-header", "a");
190190
assertThatExceptionOfType(AssertionError.class)
191-
.isThrownBy(() -> assertThat(map).hasValue("wrong-name", "a"))
191+
.isThrownBy(() -> assertThat(headers).hasValue("wrong-name", "a"))
192192
.withMessageContainingAll("HTTP header", "test-header", "wrong-name");
193193
}
194194

@@ -201,19 +201,19 @@ void hasValueWithLongMatch() {
201201

202202
@Test
203203
void hasValueWithLongMatchOnSecondaryValue() {
204-
HttpHeaders map = new HttpHeaders();
205-
map.addAll("header", List.of("123", "456", "789"));
204+
HttpHeaders headers = new HttpHeaders();
205+
headers.addAll("header", List.of("123", "456", "789"));
206206
assertThatExceptionOfType(AssertionError.class)
207-
.isThrownBy(() -> assertThat(map).hasValue("header", 456))
207+
.isThrownBy(() -> assertThat(headers).hasValue("header", 456))
208208
.withMessageContainingAll("check primary long value for HTTP header 'header'", "123", "456");
209209
}
210210

211211
@Test
212212
void hasValueWithNoLongMatch() {
213-
HttpHeaders map = new HttpHeaders();
214-
map.addAll("header", List.of("123", "456", "789"));
213+
HttpHeaders headers = new HttpHeaders();
214+
headers.addAll("header", List.of("123", "456", "789"));
215215
assertThatExceptionOfType(AssertionError.class)
216-
.isThrownBy(() -> assertThat(map).hasValue("wrong-name", 456))
216+
.isThrownBy(() -> assertThat(headers).hasValue("wrong-name", 456))
217217
.withMessageContainingAll("HTTP header", "header", "wrong-name");
218218
}
219219

@@ -228,20 +228,20 @@ void hasValueWithInstantMatch() {
228228
@Test
229229
void hasValueWithNoInstantMatch() {
230230
Instant instant = Instant.now();
231-
HttpHeaders map = new HttpHeaders();
232-
map.setInstant("header", instant);
231+
HttpHeaders headers = new HttpHeaders();
232+
headers.setInstant("header", instant);
233233
assertThatExceptionOfType(AssertionError.class)
234-
.isThrownBy(() -> assertThat(map).hasValue("wrong-name", instant.minusSeconds(30)))
234+
.isThrownBy(() -> assertThat(headers).hasValue("wrong-name", instant.minusSeconds(30)))
235235
.withMessageContainingAll("HTTP header", "header", "wrong-name");
236236
}
237237

238238
@Test
239239
void hasValueWithNoInstantMatchOneSecOfDifference() {
240240
Instant instant = Instant.now();
241-
HttpHeaders map = new HttpHeaders();
242-
map.setInstant("header", instant);
241+
HttpHeaders headers = new HttpHeaders();
242+
headers.setInstant("header", instant);
243243
assertThatExceptionOfType(AssertionError.class)
244-
.isThrownBy(() -> assertThat(map).hasValue("wrong-name", instant.minusSeconds(1)))
244+
.isThrownBy(() -> assertThat(headers).hasValue("wrong-name", instant.minusSeconds(1)))
245245
.withMessageContainingAll("HTTP header", "header", "wrong-name");
246246
}
247247

@@ -259,7 +259,7 @@ void hasSingleValueWithSecondaryValues() {
259259
assertThatExceptionOfType(AssertionError.class)
260260
.isThrownBy(() -> assertThat(headers).hasSingleValue("header", "first"))
261261
.withMessage("Expected HTTP header 'header' to be present without secondary values, " +
262-
"but found <2> secondary values");
262+
"but found <2> secondary value(s)");
263263
}
264264

265265
@Test
@@ -276,7 +276,7 @@ void hasSingleValueWithLongMatchButSecondaryValues() {
276276
assertThatExceptionOfType(AssertionError.class)
277277
.isThrownBy(() -> assertThat(headers).hasSingleValue("header", 123))
278278
.withMessage("Expected HTTP header 'header' to be present without secondary values, " +
279-
"but found <2> secondary values");
279+
"but found <2> secondary value(s)");
280280
}
281281

282282
@Test
@@ -296,7 +296,7 @@ void hasSingleValueWithInstantAndSecondaryValues() {
296296
assertThatExceptionOfType(AssertionError.class)
297297
.isThrownBy(() -> assertThat(headers).hasSingleValue("header", instant.minusSeconds(30)))
298298
.withMessage("Expected HTTP header 'header' to be present without secondary values, " +
299-
"but found <1> secondary values");
299+
"but found <1> secondary value(s)");
300300
}
301301

302302
@Test

0 commit comments

Comments
 (0)