Skip to content

Commit 875e7f8

Browse files
committed
Match multiple values in HeaderAssertions
Closes gh-23878
1 parent e88eb0e commit 875e7f8

File tree

2 files changed

+100
-10
lines changed

2 files changed

+100
-10
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/HeaderAssertions.java

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
1717
package org.springframework.test.web.reactive.server;
1818

1919
import java.util.Arrays;
20+
import java.util.List;
2021
import java.util.function.Consumer;
2122

2223
import org.hamcrest.Matcher;
@@ -28,6 +29,7 @@
2829
import org.springframework.http.MediaType;
2930
import org.springframework.lang.Nullable;
3031
import org.springframework.test.util.AssertionErrors;
32+
import org.springframework.util.CollectionUtils;
3133

3234
/**
3335
* Assertions on headers of the response.
@@ -70,6 +72,33 @@ public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
7072
return this.responseSpec;
7173
}
7274

75+
/**
76+
* Match all values of the response header with the given regex
77+
* patterns which are applied to the values of the header in the
78+
* same order. Note that the number of pattenrs must match the
79+
* number of actual values.
80+
* @param name the header name
81+
* @param patterns one or more regex patterns, one per expected value
82+
* @since 5.3
83+
*/
84+
public WebTestClient.ResponseSpec valuesMatch(String name, String... patterns) {
85+
this.exchangeResult.assertWithDiagnostics(() -> {
86+
List<String> values = getRequiredValues(name);
87+
AssertionErrors.assertTrue(
88+
getMessage(name) + " has fewer or more values " + values +
89+
" than number of patterns to match with " + Arrays.toString(patterns),
90+
values.size() == patterns.length);
91+
for (int i = 0; i < values.size(); i++) {
92+
String value = values.get(i);
93+
String pattern = patterns[i];
94+
AssertionErrors.assertTrue(
95+
getMessage(name) + "[" + i + "]='" + value + "' does not match '" + pattern + "'",
96+
value.matches(pattern));
97+
}
98+
});
99+
return this.responseSpec;
100+
}
101+
73102
/**
74103
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
75104
* @param name the header name
@@ -83,7 +112,19 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
83112
}
84113

85114
/**
86-
* Consume the first value of the response header.
115+
* Assert all values of the response header with a Hamcrest {@link Matcher}.
116+
* @param name the header name
117+
* @param matcher the matcher to use
118+
* @since 5.3
119+
*/
120+
public WebTestClient.ResponseSpec values(String name, Matcher<Iterable<String>> matcher) {
121+
List<String> values = getRequiredValues(name);
122+
this.exchangeResult.assertWithDiagnostics(() -> MatcherAssert.assertThat(values, matcher));
123+
return this.responseSpec;
124+
}
125+
126+
/**
127+
* Consume the first value of the named response header.
87128
* @param name the header name
88129
* @param consumer the consumer to use
89130
* @since 5.1
@@ -94,12 +135,28 @@ public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer)
94135
return this.responseSpec;
95136
}
96137

138+
/**
139+
* Consume all values of the named response header.
140+
* @param name the header name
141+
* @param consumer the consumer to use
142+
* @since 5.3
143+
*/
144+
public WebTestClient.ResponseSpec values(String name, Consumer<List<String>> consumer) {
145+
List<String> values = getRequiredValues(name);
146+
this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(values));
147+
return this.responseSpec;
148+
}
149+
97150
private String getRequiredValue(String name) {
98-
String value = getHeaders().getFirst(name);
99-
if (value == null) {
151+
return getRequiredValues(name).get(0);
152+
}
153+
154+
private List<String> getRequiredValues(String name) {
155+
List<String> values = getHeaders().get(name);
156+
if (CollectionUtils.isEmpty(values)) {
100157
AssertionErrors.fail(getMessage(name) + " not found");
101158
}
102-
return value;
159+
return values;
103160
}
104161

105162
/**

spring-test/src/test/java/org/springframework/test/web/reactive/server/HeaderAssertionTests.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static org.assertj.core.api.Assertions.assertThat;
3737
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3838
import static org.hamcrest.Matchers.containsString;
39+
import static org.hamcrest.Matchers.hasItems;
3940
import static org.mockito.Mockito.mock;
4041

4142
/**
@@ -97,11 +98,33 @@ public void valueMatches() {
9798
assertions.valueMatches("Content-Type", ".*UTF-8.*");
9899

99100
// Wrong pattern
100-
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->
101-
assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
102-
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
103-
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
104-
"[.*ISO-8859-1.*]"));
101+
assertThatExceptionOfType(AssertionError.class)
102+
.isThrownBy(() -> assertions.valueMatches("Content-Type", ".*ISO-8859-1.*"))
103+
.satisfies(ex -> assertThat(ex.getCause()).hasMessage("Response header " +
104+
"'Content-Type'=[application/json;charset=UTF-8] does not match " +
105+
"[.*ISO-8859-1.*]"));
106+
}
107+
108+
@Test
109+
public void valuesMatch() {
110+
HttpHeaders headers = new HttpHeaders();
111+
headers.add("foo", "value1");
112+
headers.add("foo", "value2");
113+
headers.add("foo", "value3");
114+
HeaderAssertions assertions = headerAssertions(headers);
115+
116+
assertions.valuesMatch("foo", "val.*1", "val.*2", "val.*3");
117+
118+
assertThatExceptionOfType(AssertionError.class)
119+
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5"))
120+
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
121+
"Response header 'foo' has fewer or more values [value1, value2, value3] " +
122+
"than number of patterns to match with [.*, val.*5]"));
123+
124+
assertThatExceptionOfType(AssertionError.class)
125+
.isThrownBy(() -> assertions.valuesMatch("foo", ".*", "val.*5", ".*"))
126+
.satisfies(ex -> assertThat(ex.getCause()).hasMessage(
127+
"Response header 'foo'[1]='value2' does not match 'val.*5'"));
105128
}
106129

107130
@Test
@@ -113,6 +136,16 @@ public void valueMatcher() {
113136
assertions.value("foo", containsString("a"));
114137
}
115138

139+
@Test
140+
public void valuesMatcher() {
141+
HttpHeaders headers = new HttpHeaders();
142+
headers.add("foo", "bar");
143+
headers.add("foo", "baz");
144+
HeaderAssertions assertions = headerAssertions(headers);
145+
146+
assertions.values("foo", hasItems("bar", "baz"));
147+
}
148+
116149
@Test
117150
public void exists() {
118151
HttpHeaders headers = new HttpHeaders();

0 commit comments

Comments
 (0)