-
Notifications
You must be signed in to change notification settings - Fork 37.7k
/
ServletWebRequestHttpMethodsTests.java
367 lines (282 loc) · 12.6 KB
/
ServletWebRequestHttpMethodsTests.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.context.request;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.time.ZonedDateTime;
import java.util.Date;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
import org.springframework.web.testfixture.servlet.MockHttpServletResponse;
import static java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Parameterized tests for {@link ServletWebRequest}.
*
* @author Juergen Hoeller
* @author Brian Clozel
* @author Markus Malkusch
* @author Sam Brannen
*/
class ServletWebRequestHttpMethodsTests {
private static final String CURRENT_TIME = "Wed, 9 Apr 2014 09:57:42 GMT";
private final MockHttpServletRequest servletRequest = new MockHttpServletRequest();
private final MockHttpServletResponse servletResponse = new MockHttpServletResponse();
private final ServletWebRequest request = new ServletWebRequest(servletRequest, servletResponse);
private final Date currentDate = new Date();
@ParameterizedHttpMethodTest
void checkNotModifiedNon2xxStatus(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
servletRequest.addHeader("If-Modified-Since", epochTime);
servletResponse.setStatus(304);
assertThat(request.checkNotModified(epochTime)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("Last-Modified")).isNull();
}
@ParameterizedHttpMethodTest // SPR-13516
void checkNotModifiedInvalidStatus(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
servletRequest.addHeader("If-Modified-Since", epochTime);
servletResponse.setStatus(0);
assertThat(request.checkNotModified(epochTime)).isFalse();
}
@ParameterizedHttpMethodTest // SPR-14559
void checkNotModifiedInvalidIfNoneMatchHeader(String method) {
setUpRequest(method);
String etag = "\"etagvalue\"";
servletRequest.addHeader("If-None-Match", "missingquotes");
assertThat(request.checkNotModified(etag)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedHeaderAlreadySet(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
servletRequest.addHeader("If-Modified-Since", epochTime);
servletResponse.addHeader("Last-Modified", CURRENT_TIME);
assertThat(request.checkNotModified(epochTime)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeaders("Last-Modified").size()).isEqualTo(1);
assertThat(servletResponse.getHeader("Last-Modified")).isEqualTo(CURRENT_TIME);
}
@ParameterizedHttpMethodTest
void checkNotModifiedTimestamp(String method) {
setUpRequest(method);
long epochTime = currentDate.getTime();
servletRequest.addHeader("If-Modified-Since", epochTime);
assertThat(request.checkNotModified(epochTime)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(currentDate.getTime() / 1000);
}
@ParameterizedHttpMethodTest
void checkModifiedTimestamp(String method) {
setUpRequest(method);
long oneMinuteAgo = currentDate.getTime() - (1000 * 60);
servletRequest.addHeader("If-Modified-Since", oneMinuteAgo);
assertThat(request.checkNotModified(currentDate.getTime())).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(currentDate.getTime() / 1000);
}
@ParameterizedHttpMethodTest
void checkNotModifiedETag(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
servletRequest.addHeader("If-None-Match", etag);
assertThat(request.checkNotModified(etag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedETagWithSeparatorChars(String method) {
setUpRequest(method);
String etag = "\"Foo, Bar\"";
servletRequest.addHeader("If-None-Match", etag);
assertThat(request.checkNotModified(etag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@ParameterizedHttpMethodTest
void checkModifiedETag(String method) {
setUpRequest(method);
String currentETag = "\"Foo\"";
String oldETag = "Bar";
servletRequest.addHeader("If-None-Match", oldETag);
assertThat(request.checkNotModified(currentETag)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(currentETag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedUnpaddedETag(String method) {
setUpRequest(method);
String etag = "Foo";
String paddedETag = String.format("\"%s\"", etag);
servletRequest.addHeader("If-None-Match", paddedETag);
assertThat(request.checkNotModified(etag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(paddedETag);
}
@ParameterizedHttpMethodTest
void checkModifiedUnpaddedETag(String method) {
setUpRequest(method);
String currentETag = "Foo";
String oldETag = "Bar";
servletRequest.addHeader("If-None-Match", oldETag);
assertThat(request.checkNotModified(currentETag)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(String.format("\"%s\"", currentETag));
}
@ParameterizedHttpMethodTest
void checkNotModifiedWildcardIsIgnored(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
servletRequest.addHeader("If-None-Match", "*");
assertThat(request.checkNotModified(etag)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedETagAndTimestamp(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
servletRequest.addHeader("If-None-Match", etag);
servletRequest.addHeader("If-Modified-Since", currentDate.getTime());
assertThat(request.checkNotModified(etag, currentDate.getTime())).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(currentDate.getTime() / 1000);
}
@ParameterizedHttpMethodTest // SPR-14224
void checkNotModifiedETagAndModifiedTimestamp(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
servletRequest.addHeader("If-None-Match", etag);
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.addHeader("If-Modified-Since", oneMinuteAgo);
assertThat(request.checkNotModified(etag, currentEpoch)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(currentDate.getTime() / 1000);
}
@ParameterizedHttpMethodTest
void checkModifiedETagAndNotModifiedTimestamp(String method) {
setUpRequest(method);
String currentETag = "\"Foo\"";
String oldETag = "\"Bar\"";
servletRequest.addHeader("If-None-Match", oldETag);
long epochTime = currentDate.getTime();
servletRequest.addHeader("If-Modified-Since", epochTime);
assertThat(request.checkNotModified(currentETag, epochTime)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(currentETag);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(currentDate.getTime() / 1000);
}
@ParameterizedHttpMethodTest
void checkNotModifiedETagWeakStrong(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
String weakETag = String.format("W/%s", etag);
servletRequest.addHeader("If-None-Match", etag);
assertThat(request.checkNotModified(weakETag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(weakETag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedETagStrongWeak(String method) {
setUpRequest(method);
String etag = "\"Foo\"";
servletRequest.addHeader("If-None-Match", String.format("W/%s", etag));
assertThat(request.checkNotModified(etag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@ParameterizedHttpMethodTest
void checkNotModifiedMultipleETags(String method) {
setUpRequest(method);
String etag = "\"Bar\"";
String multipleETags = String.format("\"Foo\", %s", etag);
servletRequest.addHeader("If-None-Match", multipleETags);
assertThat(request.checkNotModified(etag)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getHeader("ETag")).isEqualTo(etag);
}
@Test
void checkNotModifiedTimestampWithLengthPart() {
setUpRequest("GET");
long epochTime = ZonedDateTime.parse(CURRENT_TIME, RFC_1123_DATE_TIME).toInstant().toEpochMilli();
servletRequest.setMethod("GET");
servletRequest.addHeader("If-Modified-Since", "Wed, 09 Apr 2014 09:57:42 GMT; length=13774");
assertThat(request.checkNotModified(epochTime)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(304);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(epochTime / 1000);
}
@Test
void checkModifiedTimestampWithLengthPart() {
setUpRequest("GET");
long epochTime = ZonedDateTime.parse(CURRENT_TIME, RFC_1123_DATE_TIME).toInstant().toEpochMilli();
servletRequest.addHeader("If-Modified-Since", "Wed, 08 Apr 2014 09:57:42 GMT; length=13774");
assertThat(request.checkNotModified(epochTime)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(epochTime / 1000);
}
@Test
void checkNotModifiedTimestampConditionalPut() {
setUpRequest("PUT");
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.addHeader("If-UnModified-Since", currentEpoch);
assertThat(request.checkNotModified(oneMinuteAgo)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getHeader("Last-Modified")).isNull();
}
@Test
void checkNotModifiedTimestampConditionalPutConflict() {
setUpRequest("PUT");
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.addHeader("If-UnModified-Since", oneMinuteAgo);
assertThat(request.checkNotModified(currentEpoch)).isTrue();
assertThat(servletResponse.getStatus()).isEqualTo(412);
assertThat(servletResponse.getHeader("Last-Modified")).isNull();
}
@ParameterizedHttpMethodTest
void checkNotModifiedConditionalGet(String method) {
setUpRequest(method);
long currentEpoch = currentDate.getTime();
long oneMinuteAgo = currentEpoch - (1000 * 60);
servletRequest.addHeader("If-UnModified-Since", currentEpoch);
assertThat(request.checkNotModified(oneMinuteAgo)).isFalse();
assertThat(servletResponse.getStatus()).isEqualTo(200);
assertThat(servletResponse.getDateHeader("Last-Modified") / 1000).isEqualTo(oneMinuteAgo / 1000);
}
private void setUpRequest(String method) {
this.servletRequest.setMethod(method);
this.servletRequest.setRequestURI("https://example.org");
}
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@ParameterizedTest(name = "[{index}] {0}")
@ValueSource(strings = { "GET", "HEAD" })
@interface ParameterizedHttpMethodTest {
}
}