20
20
import static org .springframework .test .util .AssertionErrors .assertEquals ;
21
21
22
22
/**
23
- * Assertions on the status of a response.
23
+ * Assertions on the response status .
24
24
*
25
25
* @author Rossen Stoyanchev
26
26
* @since 5.0
27
- * @see ResponseAssertions#status ()
27
+ * @see WebTestClient.ResponseSpec#expectStatus ()
28
28
*/
29
29
@ SuppressWarnings ("unused" )
30
30
public class StatusAssertions {
31
31
32
- private final HttpStatus httpStatus ;
32
+ private final ExchangeResult <?> exchangeResult ;
33
33
34
34
private final WebTestClient .ResponseSpec responseSpec ;
35
35
36
36
37
- StatusAssertions (HttpStatus status , WebTestClient .ResponseSpec responseSpec ) {
38
- this .httpStatus = status ;
37
+ StatusAssertions (ExchangeResult <?> exchangeResult , WebTestClient .ResponseSpec responseSpec ) {
38
+ this .exchangeResult = exchangeResult ;
39
39
this .responseSpec = responseSpec ;
40
40
}
41
41
@@ -44,157 +44,172 @@ public class StatusAssertions {
44
44
* Assert the response status as an {@link HttpStatus}.
45
45
*/
46
46
public WebTestClient .ResponseSpec isEqualTo (HttpStatus status ) {
47
- assertEquals ("Response status" , status , this . httpStatus );
47
+ assertEquals ("Response status" , status , getStatus () );
48
48
return this .responseSpec ;
49
49
}
50
50
51
51
/**
52
52
* Assert the response status as an integer.
53
53
*/
54
54
public WebTestClient .ResponseSpec isEqualTo (int status ) {
55
- assertEquals ("Response status" , status , this . httpStatus .value ());
55
+ assertEquals ("Response status" , status , getStatus () .value ());
56
56
return this .responseSpec ;
57
57
}
58
58
59
59
/**
60
60
* Assert the response status code is {@code HttpStatus.OK} (200).
61
61
*/
62
62
public WebTestClient .ResponseSpec isOk () {
63
- assertEquals ("Status" , HttpStatus .OK , this . httpStatus );
63
+ assertEquals ("Status" , HttpStatus .OK , getStatus () );
64
64
return this .responseSpec ;
65
65
}
66
66
67
67
/**
68
68
* Assert the response status code is {@code HttpStatus.CREATED} (201).
69
69
*/
70
70
public WebTestClient .ResponseSpec isCreated () {
71
- assertEquals ("Status" , HttpStatus .CREATED , this . httpStatus );
71
+ assertEquals ("Status" , HttpStatus .CREATED , getStatus () );
72
72
return this .responseSpec ;
73
73
}
74
74
75
75
/**
76
76
* Assert the response status code is {@code HttpStatus.ACCEPTED} (202).
77
77
*/
78
78
public WebTestClient .ResponseSpec isAccepted () {
79
- assertEquals ("Status" , HttpStatus .ACCEPTED , this . httpStatus );
79
+ assertEquals ("Status" , HttpStatus .ACCEPTED , getStatus () );
80
80
return this .responseSpec ;
81
81
}
82
82
83
83
/**
84
84
* Assert the response status code is {@code HttpStatus.NO_CONTENT} (204).
85
85
*/
86
86
public WebTestClient .ResponseSpec isNoContent () {
87
- assertEquals ("Status" , HttpStatus .NO_CONTENT , this . httpStatus );
87
+ assertEquals ("Status" , HttpStatus .NO_CONTENT , getStatus () );
88
88
return this .responseSpec ;
89
89
}
90
90
91
91
/**
92
92
* Assert the response status code is {@code HttpStatus.FOUND} (302).
93
93
*/
94
94
public WebTestClient .ResponseSpec isFound () {
95
- assertEquals ("Status" , HttpStatus .FOUND , this . httpStatus );
95
+ assertEquals ("Status" , HttpStatus .FOUND , getStatus () );
96
96
return this .responseSpec ;
97
97
}
98
98
99
99
/**
100
100
* Assert the response status code is {@code HttpStatus.SEE_OTHER} (303).
101
101
*/
102
102
public WebTestClient .ResponseSpec isSeeOther () {
103
- assertEquals ("Status" , HttpStatus .SEE_OTHER , this . httpStatus );
103
+ assertEquals ("Status" , HttpStatus .SEE_OTHER , getStatus () );
104
104
return this .responseSpec ;
105
105
}
106
106
107
107
/**
108
108
* Assert the response status code is {@code HttpStatus.NOT_MODIFIED} (304).
109
109
*/
110
110
public WebTestClient .ResponseSpec isNotModified () {
111
- assertEquals ("Status" , HttpStatus .NOT_MODIFIED , this . httpStatus );
111
+ assertEquals ("Status" , HttpStatus .NOT_MODIFIED , getStatus () );
112
112
return this .responseSpec ;
113
113
}
114
114
115
115
/**
116
116
* Assert the response status code is {@code HttpStatus.TEMPORARY_REDIRECT} (307).
117
117
*/
118
118
public WebTestClient .ResponseSpec isTemporaryRedirect () {
119
- assertEquals ("Status" , HttpStatus .TEMPORARY_REDIRECT , this . httpStatus );
119
+ assertEquals ("Status" , HttpStatus .TEMPORARY_REDIRECT , getStatus () );
120
120
return this .responseSpec ;
121
121
}
122
122
123
123
/**
124
124
* Assert the response status code is {@code HttpStatus.PERMANENT_REDIRECT} (308).
125
125
*/
126
126
public WebTestClient .ResponseSpec isPermanentRedirect () {
127
- assertEquals ("Status" , HttpStatus .PERMANENT_REDIRECT , this . httpStatus );
127
+ assertEquals ("Status" , HttpStatus .PERMANENT_REDIRECT , getStatus () );
128
128
return this .responseSpec ;
129
129
}
130
130
131
131
/**
132
132
* Assert the response status code is {@code HttpStatus.BAD_REQUEST} (400).
133
133
*/
134
134
public WebTestClient .ResponseSpec isBadRequest () {
135
- assertEquals ("Status" , HttpStatus .BAD_REQUEST , this .httpStatus );
135
+ assertEquals ("Status" , HttpStatus .BAD_REQUEST , getStatus ());
136
+ return this .responseSpec ;
137
+ }
138
+
139
+ /**
140
+ * Assert the response status code is {@code HttpStatus.UNAUTHORIZED} (401).
141
+ */
142
+ public WebTestClient .ResponseSpec isUnauthorized () {
143
+ assertEquals ("Status" , HttpStatus .UNAUTHORIZED , getStatus ());
136
144
return this .responseSpec ;
137
145
}
138
146
139
147
/**
140
148
* Assert the response status code is {@code HttpStatus.NOT_FOUND} (404).
141
149
*/
142
150
public WebTestClient .ResponseSpec isNotFound () {
143
- assertEquals ("Status" , HttpStatus .NOT_FOUND , this . httpStatus );
151
+ assertEquals ("Status" , HttpStatus .NOT_FOUND , getStatus () );
144
152
return this .responseSpec ;
145
153
}
146
154
147
155
/**
148
156
* Assert the response error message.
149
157
*/
150
158
public WebTestClient .ResponseSpec reasonEquals (String reason ) {
151
- assertEquals ("Response status reason" , reason , this . httpStatus .getReasonPhrase ());
159
+ assertEquals ("Response status reason" , reason , getStatus () .getReasonPhrase ());
152
160
return this .responseSpec ;
153
161
}
154
162
155
163
/**
156
164
* Assert the response status code is in the 1xx range.
157
165
*/
158
166
public WebTestClient .ResponseSpec is1xxInformational () {
159
- String message = "Range for response status value " + this . httpStatus ;
160
- assertEquals (message , HttpStatus .Series .INFORMATIONAL , this . httpStatus .series ());
167
+ String message = "Range for response status value " + getStatus () ;
168
+ assertEquals (message , HttpStatus .Series .INFORMATIONAL , getStatus () .series ());
161
169
return this .responseSpec ;
162
170
}
163
171
164
172
/**
165
173
* Assert the response status code is in the 2xx range.
166
174
*/
167
175
public WebTestClient .ResponseSpec is2xxSuccessful () {
168
- String message = "Range for response status value " + this . httpStatus ;
169
- assertEquals (message , HttpStatus .Series .SUCCESSFUL , this . httpStatus .series ());
176
+ String message = "Range for response status value " + getStatus () ;
177
+ assertEquals (message , HttpStatus .Series .SUCCESSFUL , getStatus () .series ());
170
178
return this .responseSpec ;
171
179
}
172
180
173
181
/**
174
182
* Assert the response status code is in the 3xx range.
175
183
*/
176
184
public WebTestClient .ResponseSpec is3xxRedirection () {
177
- String message = "Range for response status value " + this . httpStatus ;
178
- assertEquals (message , HttpStatus .Series .REDIRECTION , this . httpStatus .series ());
185
+ String message = "Range for response status value " + getStatus () ;
186
+ assertEquals (message , HttpStatus .Series .REDIRECTION , getStatus () .series ());
179
187
return this .responseSpec ;
180
188
}
181
189
182
190
/**
183
191
* Assert the response status code is in the 4xx range.
184
192
*/
185
193
public WebTestClient .ResponseSpec is4xxClientError () {
186
- String message = "Range for response status value " + this . httpStatus ;
187
- assertEquals (message , HttpStatus .Series .CLIENT_ERROR , this . httpStatus .series ());
194
+ String message = "Range for response status value " + getStatus () ;
195
+ assertEquals (message , HttpStatus .Series .CLIENT_ERROR , getStatus () .series ());
188
196
return this .responseSpec ;
189
197
}
190
198
191
199
/**
192
200
* Assert the response status code is in the 5xx range.
193
201
*/
194
202
public WebTestClient .ResponseSpec is5xxServerError () {
195
- String message = "Range for response status value " + this . httpStatus ;
196
- assertEquals (message , HttpStatus .Series .SERVER_ERROR , this . httpStatus .series ());
203
+ String message = "Range for response status value " + getStatus () ;
204
+ assertEquals (message , HttpStatus .Series .SERVER_ERROR , getStatus () .series ());
197
205
return this .responseSpec ;
198
206
}
199
207
208
+
209
+ // Private methods
210
+
211
+ private HttpStatus getStatus () {
212
+ return this .exchangeResult .getStatus ();
213
+ }
214
+
200
215
}
0 commit comments