1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2020 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
17
17
package org .springframework .test .web .reactive .server ;
18
18
19
19
import java .util .Arrays ;
20
+ import java .util .List ;
20
21
import java .util .function .Consumer ;
21
22
22
23
import org .hamcrest .Matcher ;
28
29
import org .springframework .http .MediaType ;
29
30
import org .springframework .lang .Nullable ;
30
31
import org .springframework .test .util .AssertionErrors ;
32
+ import org .springframework .util .CollectionUtils ;
31
33
32
34
/**
33
35
* Assertions on headers of the response.
@@ -70,6 +72,33 @@ public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
70
72
return this .responseSpec ;
71
73
}
72
74
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
+
73
102
/**
74
103
* Assert the first value of the response header with a Hamcrest {@link Matcher}.
75
104
* @param name the header name
@@ -83,7 +112,19 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
83
112
}
84
113
85
114
/**
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.
87
128
* @param name the header name
88
129
* @param consumer the consumer to use
89
130
* @since 5.1
@@ -94,12 +135,28 @@ public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer)
94
135
return this .responseSpec ;
95
136
}
96
137
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
+
97
150
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 )) {
100
157
AssertionErrors .fail (getMessage (name ) + " not found" );
101
158
}
102
- return value ;
159
+ return values ;
103
160
}
104
161
105
162
/**
0 commit comments