20
20
import java .time .ZoneId ;
21
21
import java .time .ZonedDateTime ;
22
22
import java .time .temporal .ChronoUnit ;
23
+ import java .util .Arrays ;
24
+ import java .util .Collection ;
23
25
import java .util .List ;
24
26
25
- import org .assertj .core .api .AbstractMapAssert ;
27
+ import org .assertj .core .api .AbstractCollectionAssert ;
28
+ import org .assertj .core .api .AbstractObjectAssert ;
26
29
import org .assertj .core .api .Assertions ;
30
+ import org .assertj .core .api .ObjectAssert ;
31
+ import org .assertj .core .presentation .Representation ;
32
+ import org .assertj .core .presentation .StandardRepresentation ;
27
33
28
34
import org .springframework .http .HttpHeaders ;
29
35
34
40
* @author Stephane Nicoll
35
41
* @since 6.2
36
42
*/
37
- public class HttpHeadersAssert extends AbstractMapAssert <HttpHeadersAssert , HttpHeaders , String , List < String > > {
43
+ public class HttpHeadersAssert extends AbstractObjectAssert <HttpHeadersAssert , HttpHeaders > {
38
44
39
45
private static final ZoneId GMT = ZoneId .of ("GMT" );
40
46
47
+ private final AbstractCollectionAssert <?, Collection <? extends String >, String , ObjectAssert <String >> namesAssert ;
48
+
41
49
42
50
public HttpHeadersAssert (HttpHeaders actual ) {
43
51
super (actual , HttpHeadersAssert .class );
44
52
as ("HTTP headers" );
53
+ withRepresentation (new Representation () {
54
+ @ Override
55
+ public String toStringOf (Object object ) {
56
+ if (object instanceof HttpHeaders headers ) {
57
+ return headers .toString ();
58
+ }
59
+ return StandardRepresentation .STANDARD_REPRESENTATION .toStringOf (object );
60
+ }
61
+ });
62
+ this .namesAssert = Assertions .assertThat (actual .headerNames ())
63
+ .as ("HTTP header names" );
45
64
}
46
65
47
66
/**
48
67
* Verify that the actual HTTP headers contain a header with the given
49
68
* {@code name}.
50
69
* @param name the name of an expected HTTP header
51
- * @see #containsKey
52
70
*/
53
71
public HttpHeadersAssert containsHeader (String name ) {
54
- return containsKey (name );
72
+ this .namesAssert
73
+ .as ("check headers contain HTTP header '%s'" , name )
74
+ .contains (name );
75
+ return this .myself ;
55
76
}
56
77
57
78
/**
58
79
* Verify that the actual HTTP headers contain the headers with the given
59
80
* {@code names}.
60
81
* @param names the names of expected HTTP headers
61
- * @see #containsKeys
62
82
*/
63
83
public HttpHeadersAssert containsHeaders (String ... names ) {
64
- return containsKeys (names );
84
+ this .namesAssert
85
+ .as ("check headers contain HTTP headers '%s'" , Arrays .toString (names ))
86
+ .contains (names );
87
+ return this .myself ;
88
+ }
89
+
90
+ /**
91
+ * Verify that the actual HTTP headers contain only the headers with the
92
+ * given {@code names}, in any order and in a case-insensitive manner.
93
+ * @param names the names of expected HTTP headers
94
+ */
95
+ public HttpHeadersAssert containsOnlyHeaders (String ... names ) {
96
+ this .namesAssert
97
+ .as ("check headers contains only HTTP headers '%s'" , Arrays .toString (names ))
98
+ .containsOnly (names );
99
+ return this .myself ;
65
100
}
66
101
67
102
/**
68
103
* Verify that the actual HTTP headers do not contain a header with the
69
104
* given {@code name}.
70
105
* @param name the name of an HTTP header that should not be present
71
- * @see #doesNotContainKey
72
106
*/
73
107
public HttpHeadersAssert doesNotContainHeader (String name ) {
74
- return doesNotContainKey (name );
108
+ this .namesAssert
109
+ .as ("check headers does not contain HTTP header '%s'" , name )
110
+ .doesNotContain (name );
111
+ return this .myself ;
75
112
}
76
113
77
114
/**
78
115
* Verify that the actual HTTP headers do not contain any of the headers
79
116
* with the given {@code names}.
80
117
* @param names the names of HTTP headers that should not be present
81
- * @see #doesNotContainKeys
82
118
*/
83
119
public HttpHeadersAssert doesNotContainsHeaders (String ... names ) {
84
- return doesNotContainKeys (names );
120
+ this .namesAssert
121
+ .as ("check headers does not contain HTTP headers '%s'" , Arrays .toString (names ))
122
+ .doesNotContain (names );
123
+ return this .myself ;
85
124
}
86
125
87
126
/**
@@ -91,7 +130,7 @@ public HttpHeadersAssert doesNotContainsHeaders(String... names) {
91
130
* @param value the expected value of the header
92
131
*/
93
132
public HttpHeadersAssert hasValue (String name , String value ) {
94
- containsKey (name );
133
+ containsHeader (name );
95
134
Assertions .assertThat (this .actual .getFirst (name ))
96
135
.as ("check primary value for HTTP header '%s'" , name )
97
136
.isEqualTo (value );
@@ -105,7 +144,7 @@ public HttpHeadersAssert hasValue(String name, String value) {
105
144
* @param value the expected value of the header
106
145
*/
107
146
public HttpHeadersAssert hasValue (String name , long value ) {
108
- containsKey (name );
147
+ containsHeader (name );
109
148
Assertions .assertThat (this .actual .getFirst (name ))
110
149
.as ("check primary long value for HTTP header '%s'" , name )
111
150
.asLong ().isEqualTo (value );
@@ -119,11 +158,149 @@ public HttpHeadersAssert hasValue(String name, long value) {
119
158
* @param value the expected value of the header
120
159
*/
121
160
public HttpHeadersAssert hasValue (String name , Instant value ) {
122
- containsKey (name );
161
+ containsHeader (name );
123
162
Assertions .assertThat (this .actual .getFirstZonedDateTime (name ))
124
163
.as ("check primary date value for HTTP header '%s'" , name )
125
164
.isCloseTo (ZonedDateTime .ofInstant (value , GMT ), Assertions .within (999 , ChronoUnit .MILLIS ));
126
165
return this .myself ;
127
166
}
128
167
168
+ /**
169
+ * Verify that the given header has a full list of values exactly equal to
170
+ * the given list of values, and in the same order.
171
+ * @param name the considered header name (case-insensitive)
172
+ * @param values the exhaustive list of expected values
173
+ */
174
+ public HttpHeadersAssert hasExactlyValues (String name , List <String > values ) {
175
+ containsHeader (name );
176
+ Assertions .assertThat (this .actual .get (name ))
177
+ .as ("check all values of HTTP header '%s'" , name )
178
+ .containsExactlyElementsOf (values );
179
+ return this .myself ;
180
+ }
181
+
182
+ /**
183
+ * Verify that the given header has a full list of values exactly equal to
184
+ * the given list of values, in any order.
185
+ * @param name the considered header name (case-insensitive)
186
+ * @param values the exhaustive list of expected values
187
+ */
188
+ public HttpHeadersAssert hasExactlyValuesInAnyOrder (String name , List <String > values ) {
189
+ containsHeader (name );
190
+ Assertions .assertThat (this .actual .get (name ))
191
+ .as ("check all values of HTTP header '%s' in any order" , name )
192
+ .containsExactlyInAnyOrderElementsOf (values );
193
+ return this .myself ;
194
+ }
195
+
196
+ /**
197
+ * Verify that headers are empty and no headers are present.
198
+ */
199
+ public HttpHeadersAssert isEmpty () {
200
+ this .namesAssert
201
+ .as ("check headers are empty" )
202
+ .isEmpty ();
203
+ return this .myself ;
204
+ }
205
+
206
+ /**
207
+ * Verify that headers are not empty and at least one header is present.
208
+ */
209
+ public HttpHeadersAssert isNotEmpty () {
210
+ this .namesAssert
211
+ .as ("check headers are not empty" )
212
+ .isNotEmpty ();
213
+ return this .myself ;
214
+ }
215
+
216
+ /**
217
+ * Verify that there is exactly {@code expected} headers present, when
218
+ * considering header names in a case-insensitive manner.
219
+ * @param expected the expected number of headers
220
+ */
221
+ public HttpHeadersAssert hasSize (int expected ) {
222
+ this .namesAssert
223
+ .as ("check headers have size '%i'" , expected )
224
+ .hasSize (expected );
225
+ return this .myself ;
226
+ }
227
+
228
+ /**
229
+ * Verify that the number of headers present is strictly greater than the
230
+ * given boundary, when considering header names in a case-insensitive
231
+ * manner.
232
+ * @param boundary the given value to compare actual header size to
233
+ */
234
+ public HttpHeadersAssert hasSizeGreaterThan (int boundary ) {
235
+ this .namesAssert
236
+ .as ("check headers have size > '%i'" , boundary )
237
+ .hasSizeGreaterThan (boundary );
238
+ return this .myself ;
239
+ }
240
+
241
+ /**
242
+ * Verify that the number of headers present is greater or equal to the
243
+ * given boundary, when considering header names in a case-insensitive
244
+ * manner.
245
+ * @param boundary the given value to compare actual header size to
246
+ */
247
+ public HttpHeadersAssert hasSizeGreaterThanOrEqualTo (int boundary ) {
248
+ this .namesAssert
249
+ .as ("check headers have size >= '%i'" , boundary )
250
+ .hasSizeGreaterThanOrEqualTo (boundary );
251
+ return this .myself ;
252
+ }
253
+
254
+ /**
255
+ * Verify that the number of headers present is strictly less than the
256
+ * given boundary, when considering header names in a case-insensitive
257
+ * manner.
258
+ * @param boundary the given value to compare actual header size to
259
+ */
260
+ public HttpHeadersAssert hasSizeLessThan (int boundary ) {
261
+ this .namesAssert
262
+ .as ("check headers have size < '%i'" , boundary )
263
+ .hasSizeLessThan (boundary );
264
+ return this .myself ;
265
+ }
266
+
267
+ /**
268
+ * Verify that the number of headers present is less than or equal to the
269
+ * given boundary, when considering header names in a case-insensitive
270
+ * manner.
271
+ * @param boundary the given value to compare actual header size to
272
+ */
273
+ public HttpHeadersAssert hasSizeLessThanOrEqualTo (int boundary ) {
274
+ this .namesAssert
275
+ .as ("check headers have size <= '%i'" , boundary )
276
+ .hasSizeLessThanOrEqualTo (boundary );
277
+ return this .myself ;
278
+ }
279
+
280
+ /**
281
+ * Verify that the number of headers present is between the given boundaries
282
+ * (inclusive), when considering header names in a case-insensitive manner.
283
+ * @param lowerBoundary the lower boundary compared to which actual size
284
+ * should be greater than or equal to
285
+ * @param higherBoundary the higher boundary compared to which actual size
286
+ * should be less than or equal to
287
+ */
288
+ public HttpHeadersAssert hasSizeBetween (int lowerBoundary , int higherBoundary ) {
289
+ this .namesAssert
290
+ .as ("check headers have size between '%i' and '%i'" , lowerBoundary , higherBoundary )
291
+ .hasSizeBetween (lowerBoundary , higherBoundary );
292
+ return this .myself ;
293
+ }
294
+
295
+ /**
296
+ * Verify that the number actual headers is the same as in the given
297
+ * {@code HttpHeaders}.
298
+ * @param other the {@code HttpHeaders} to compare size with
299
+ */
300
+ public HttpHeadersAssert hasSameSizeAs (HttpHeaders other ) {
301
+ this .namesAssert
302
+ .as ("check headers have same size as '%s'" , other )
303
+ .hasSize (other .size ());
304
+ return this .myself ;
305
+ }
129
306
}
0 commit comments