28
28
import org .springframework .http .HttpHeaders ;
29
29
import org .springframework .http .HttpStatus ;
30
30
import org .springframework .http .HttpStatus .Series ;
31
+ import org .springframework .http .MediaType ;
31
32
import org .springframework .test .http .HttpHeadersAssert ;
33
+ import org .springframework .test .http .MediaTypeAssert ;
32
34
import org .springframework .util .LinkedMultiValueMap ;
33
35
import org .springframework .util .MultiValueMap ;
34
36
import org .springframework .util .function .SingletonSupplier ;
48
50
public abstract class AbstractHttpServletResponseAssert <R extends HttpServletResponse , SELF extends AbstractHttpServletResponseAssert <R , SELF , ACTUAL >, ACTUAL >
49
51
extends AbstractObjectAssert <SELF , ACTUAL > {
50
52
51
- private final Supplier <AbstractIntegerAssert <?>> statusAssert ;
53
+ private final Supplier <MediaTypeAssert > contentTypeAssertSupplier ;
52
54
53
55
private final Supplier <HttpHeadersAssert > headersAssertSupplier ;
54
56
57
+ private final Supplier <AbstractIntegerAssert <?>> statusAssert ;
58
+
55
59
56
60
protected AbstractHttpServletResponseAssert (ACTUAL actual , Class <?> selfType ) {
57
61
super (actual , selfType );
58
- this .statusAssert = SingletonSupplier .of (() -> Assertions . assertThat (getResponse ().getStatus ()). as ( "HTTP status code" ));
62
+ this .contentTypeAssertSupplier = SingletonSupplier .of (() -> new MediaTypeAssert (getResponse ().getContentType () ));
59
63
this .headersAssertSupplier = SingletonSupplier .of (() -> new HttpHeadersAssert (getHttpHeaders (getResponse ())));
64
+ this .statusAssert = SingletonSupplier .of (() -> Assertions .assertThat (getResponse ().getStatus ()).as ("HTTP status code" ));
65
+ }
66
+
67
+ private static HttpHeaders getHttpHeaders (HttpServletResponse response ) {
68
+ MultiValueMap <String , String > headers = new LinkedMultiValueMap <>();
69
+ response .getHeaderNames ().forEach (name -> headers .put (name , new ArrayList <>(response .getHeaders (name ))));
70
+ return new HttpHeaders (headers );
60
71
}
61
72
62
73
/**
@@ -67,6 +78,14 @@ protected AbstractHttpServletResponseAssert(ACTUAL actual, Class<?> selfType) {
67
78
*/
68
79
protected abstract R getResponse ();
69
80
81
+ /**
82
+ * Return a new {@linkplain MediaTypeAssert assertion} object that uses the
83
+ * response's {@linkplain MediaType content type} as the object to test.
84
+ */
85
+ public MediaTypeAssert contentType () {
86
+ return this .contentTypeAssertSupplier .get ();
87
+ }
88
+
70
89
/**
71
90
* Return a new {@linkplain HttpHeadersAssert assertion} object that uses
72
91
* {@link HttpHeaders} as the object to test. The returned assertion
@@ -84,6 +103,82 @@ public HttpHeadersAssert headers() {
84
103
return this .headersAssertSupplier .get ();
85
104
}
86
105
106
+ // Content-type shortcuts
107
+
108
+ /**
109
+ * Verify that the response's {@code Content-Type} is equal to the given value.
110
+ * @param contentType the expected content type
111
+ */
112
+ public SELF hasContentType (MediaType contentType ) {
113
+ contentType ().isEqualTo (contentType );
114
+ return this .myself ;
115
+ }
116
+
117
+ /**
118
+ * Verify that the response's {@code Content-Type} is equal to the given
119
+ * string representation.
120
+ * @param contentType the expected content type
121
+ */
122
+ public SELF hasContentType (String contentType ) {
123
+ contentType ().isEqualTo (contentType );
124
+ return this .myself ;
125
+ }
126
+
127
+ /**
128
+ * Verify that the response's {@code Content-Type} is
129
+ * {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with the
130
+ * given value.
131
+ * @param contentType the expected compatible content type
132
+ */
133
+ public SELF hasContentTypeCompatibleWith (MediaType contentType ) {
134
+ contentType ().isCompatibleWith (contentType );
135
+ return this .myself ;
136
+ }
137
+
138
+ /**
139
+ * Verify that the response's {@code Content-Type} is
140
+ * {@linkplain MediaType#isCompatibleWith(MediaType) compatible} with the
141
+ * given string representation.
142
+ * @param contentType the expected compatible content type
143
+ */
144
+ public SELF hasContentTypeCompatibleWith (String contentType ) {
145
+ contentType ().isCompatibleWith (contentType );
146
+ return this .myself ;
147
+ }
148
+
149
+ // Headers shortcuts
150
+
151
+ /**
152
+ * Verify that the response contains a header with the given {@code name}.
153
+ * @param name the name of an expected HTTP header
154
+ */
155
+ public SELF containsHeader (String name ) {
156
+ headers ().containsHeader (name );
157
+ return this .myself ;
158
+ }
159
+
160
+ /**
161
+ * Verify that the response does not contain a header with the given {@code name}.
162
+ * @param name the name of an HTTP header that should not be present
163
+ */
164
+ public SELF doesNotContainHeader (String name ) {
165
+ headers ().doesNotContainHeader (name );
166
+ return this .myself ;
167
+ }
168
+
169
+ /**
170
+ * Verify that the response contains a header with the given {@code name}
171
+ * and primary {@code value}.
172
+ * @param name the name of an expected HTTP header
173
+ * @param value the expected value of the header
174
+ */
175
+ public SELF hasHeader (String name , String value ) {
176
+ headers ().hasValue (name , value );
177
+ return this .myself ;
178
+ }
179
+
180
+ // Status
181
+
87
182
/**
88
183
* Verify that the HTTP status is equal to the specified status code.
89
184
* @param status the expected HTTP status code
@@ -159,10 +254,4 @@ private AbstractIntegerAssert<?> status() {
159
254
return this .statusAssert .get ();
160
255
}
161
256
162
- private static HttpHeaders getHttpHeaders (HttpServletResponse response ) {
163
- MultiValueMap <String , String > headers = new LinkedMultiValueMap <>();
164
- response .getHeaderNames ().forEach (name -> headers .put (name , new ArrayList <>(response .getHeaders (name ))));
165
- return new HttpHeaders (headers );
166
- }
167
-
168
257
}
0 commit comments