1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <algorithm>
17 #include <cstring>
18
19 #include "gtest/gtest.h"
20
21 #include "http_cache_request.h"
22 #include "http_cache_response.h"
23 #include "http_cache_strategy.h"
24 #include "netstack_log.h"
25
26 using namespace OHOS::NetStack::Http;
27
28 class HttpCacheStrategyTest : public testing::Test {
29 public:
SetUpTestCase()30 static void SetUpTestCase() {}
31
TearDownTestCase()32 static void TearDownTestCase() {}
33
SetUp()34 virtual void SetUp() {}
35
TearDown()36 virtual void TearDown() {}
37 };
38
39 namespace {
40 HWTEST_F(HttpCacheStrategyTest, cacheRequestNoCache, testing::ext::TestSize.Level1)
41 {
42 HttpRequestOptions requestOptions;
43 requestOptions.SetHeader("cache-control", "no-cache");
44 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
45
46 HttpResponse response;
47 response.SetResponseCode(200);
48 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
49 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
50 responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT";
51
52 HttpCacheStrategy cacheStrategy(requestOptions);
53 CacheStatus status = cacheStrategy.RunStrategy(response);
54 NETSTACK_LOGI("status = %{public}d", status);
55
56 EXPECT_EQ(status, 2);
57 }
58
59 HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeLastModifiedBranch, testing::ext::TestSize.Level1)
60 {
61 HttpRequestOptions requestOptions;
62 requestOptions.SetHeader("cache-control", "min-fresh=20");
63 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
64
65 HttpResponse response;
66 response.SetResponseCode(200);
67 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
68 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
69 responseHeader["Cache-Control"] = "public";
70 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
71 responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT";
72
73 HttpCacheStrategy cacheStrategy(requestOptions);
74 CacheStatus status = cacheStrategy.RunStrategy(response);
75 NETSTACK_LOGI("status = %{public}d", status);
76
77 EXPECT_EQ(status, STALE);
78 }
79
80 HWTEST_F(HttpCacheStrategyTest, cacheResponseNoCache, testing::ext::TestSize.Level1)
81 {
82 HttpRequestOptions requestOptions;
83 requestOptions.SetHeader("cache-control", "min-fresh=20");
84 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
85
86 HttpResponse response;
87 response.SetResponseCode(200);
88 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
89 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
90 responseHeader["Cache-Control"] = "no-cache";
91
92 HttpCacheStrategy cacheStrategy(requestOptions);
93 CacheStatus status = cacheStrategy.RunStrategy(response);
94 NETSTACK_LOGI("status = %{public}d", status);
95
96 EXPECT_EQ(status, 1);
97 }
98
99 HWTEST_F(HttpCacheStrategyTest, cacheRequestOnlyIfCached, testing::ext::TestSize.Level1)
100 {
101 HttpRequestOptions requestOptions;
102 requestOptions.SetHeader("cache-control", "only-if-cached");
103 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
104
105 HttpResponse response;
106 response.SetResponseCode(200);
107 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
108 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
109 responseHeader["Cache-Control"] = "max-age=70";
110
111 HttpCacheStrategy cacheStrategy(requestOptions);
112 CacheStatus status = cacheStrategy.RunStrategy(response);
113 NETSTACK_LOGI("status = %{public}d", status);
114
115 EXPECT_EQ(status, 0);
116 }
117
118 HWTEST_F(HttpCacheStrategyTest, isCacheable, testing::ext::TestSize.Level1)
119 {
120 HttpRequestOptions requestOptions;
121 requestOptions.SetHeader("cache-control", "min-fresh=20");
122 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
123
124 HttpResponse response;
125 response.SetResponseCode(303);
126 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
127 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
128 responseHeader["Cache-Control"] = "max-age=70";
129
130 HttpCacheStrategy cacheStrategy(requestOptions);
131 CacheStatus status = cacheStrategy.RunStrategy(response);
132 NETSTACK_LOGI("status = %{public}d", status);
133
134 EXPECT_EQ(status, 2);
135 }
136
137 HWTEST_F(HttpCacheStrategyTest, isCacheable_OK, testing::ext::TestSize.Level1)
138 {
139 HttpRequestOptions requestOptions;
140 requestOptions.SetHeader("cache-control", "min-fresh=20");
141 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
142
143 HttpResponse response;
144 response.SetResponseCode(200);
145 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
146 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
147 responseHeader["Cache-Control"] = "max-age=70";
148
149 HttpCacheStrategy cacheStrategy(requestOptions);
150 CacheStatus status = cacheStrategy.RunStrategy(response);
151 NETSTACK_LOGI("status = %{public}d", status);
152
153 EXPECT_EQ(status, STALE);
154 }
155
156 HWTEST_F(HttpCacheStrategyTest, requestIfModifiedSinceStr, testing::ext::TestSize.Level1)
157 {
158 HttpRequestOptions requestOptions;
159 requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT");
160 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
161
162 HttpResponse response;
163 response.SetResponseCode(200);
164 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
165 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
166 responseHeader["Cache-Control"] = "max-age=70";
167
168 HttpCacheStrategy cacheStrategy(requestOptions);
169 CacheStatus status = cacheStrategy.RunStrategy(response);
170 NETSTACK_LOGI("status = %{public}d", status);
171
172 EXPECT_EQ(status, 2);
173 }
174
175 HWTEST_F(HttpCacheStrategyTest, requestgetIfNoneMatch, testing::ext::TestSize.Level1)
176 {
177 HttpRequestOptions requestOptions;
178 requestOptions.SetHeader("if-none-match", "A6E52F1D544D9DAFB552163A1CF8AD10");
179 requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT");
180 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
181
182 HttpResponse response;
183 response.SetResponseCode(200);
184 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
185 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
186 responseHeader["Cache-Control"] = "max-age=70";
187
188 HttpCacheStrategy cacheStrategy(requestOptions);
189 CacheStatus status = cacheStrategy.RunStrategy(response);
190 NETSTACK_LOGI("status = %{public}d", status);
191
192 EXPECT_EQ(status, 2);
193 }
194
195 HWTEST_F(HttpCacheStrategyTest, requestgetIfNoneMatchAndIfModifiedSinceStr, testing::ext::TestSize.Level1)
196 {
197 HttpRequestOptions requestOptions;
198 requestOptions.SetHeader("if-none-match", "A6E52F1D544D9DAFB552163A1CF8AD10");
199 requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT");
200 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
201
202 HttpResponse response;
203 response.SetResponseCode(200);
204 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
205 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
206 responseHeader["Cache-Control"] = "max-age=70";
207
208 HttpCacheStrategy cacheStrategy(requestOptions);
209 CacheStatus status = cacheStrategy.RunStrategy(response);
210 NETSTACK_LOGI("status = %{public}d", status);
211
212 EXPECT_EQ(status, 2);
213 }
214
215 HWTEST_F(HttpCacheStrategyTest, strategyMaxAgeBranch, testing::ext::TestSize.Level1) // test
216 {
217 HttpRequestOptions requestOptions;
218 requestOptions.SetHeader("cache-control", "max-age=10");
219 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:19 GMT");
220
221 HttpResponse response;
222 response.SetResponseCode(200);
223 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
224 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
225 responseHeader["Cache-Control"] = "max-age=70";
226
227 HttpCacheStrategy cacheStrategy(requestOptions);
228 CacheStatus status = cacheStrategy.RunStrategy(response);
229 NETSTACK_LOGI("status = %{public}d", status);
230
231 EXPECT_EQ(status, STALE);
232 }
233
234 HWTEST_F(HttpCacheStrategyTest, CompareNumber_1, testing::ext::TestSize.Level1)
235 {
236 HttpRequestOptions requestOptions;
237 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
238
239 HttpResponse response;
240 response.SetResponseCode(200);
241 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
242 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
243 responseHeader["age"] = "33781";
244
245 HttpCacheStrategy cacheStrategy(requestOptions);
246 CacheStatus status = cacheStrategy.RunStrategy(response);
247 NETSTACK_LOGI("status = %{public}d", status);
248
249 EXPECT_EQ(status, 1);
250 }
251
252 HWTEST_F(HttpCacheStrategyTest, CompareNumber_1_2, testing::ext::TestSize.Level1)
253 {
254 HttpRequestOptions requestOptions;
255 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
256
257 HttpResponse response;
258 response.SetResponseCode(200);
259 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
260 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
261 responseHeader["age"] = "33781";
262 responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e";
263
264 HttpCacheStrategy cacheStrategy(requestOptions);
265 CacheStatus status = cacheStrategy.RunStrategy(response);
266 NETSTACK_LOGI("status = %{public}d", status);
267
268 EXPECT_EQ(status, 1);
269 }
270
271 HWTEST_F(HttpCacheStrategyTest, CompareNumber_2, testing::ext::TestSize.Level1)
272 {
273 HttpRequestOptions requestOptions;
274 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
275 requestOptions.SetHeader("cache-control", "max-age=10");
276
277 HttpResponse response;
278 response.SetResponseCode(200);
279 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
280 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
281 responseHeader["age"] = "10";
282 responseHeader["cache-control"] = "private";
283 responseHeader["expires"] = "Mon, 16 May 2022 10:31:58 GMT";
284
285 HttpCacheStrategy cacheStrategy(requestOptions);
286 CacheStatus status = cacheStrategy.RunStrategy(response);
287 NETSTACK_LOGI("status = %{public}d", status);
288
289 EXPECT_EQ(status, 1);
290 }
291
292 HWTEST_F(HttpCacheStrategyTest, CompareNumber_3, testing::ext::TestSize.Level1)
293 {
294 HttpRequestOptions requestOptions;
295 requestOptions.SetRequestTime("Mon, 16 May 2022 09:32:59 GMT");
296 requestOptions.SetHeader("cache-control", "no-cache");
297
298 HttpResponse response;
299 response.SetResponseCode(200);
300 response.SetResponseTime("Mon, 16 May 2022 09:33:59 GMT");
301 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
302 responseHeader["age"] = "0";
303
304 HttpCacheStrategy cacheStrategy(requestOptions);
305 CacheStatus status = cacheStrategy.RunStrategy(response);
306 NETSTACK_LOGI("status = %{public}d", status);
307
308 EXPECT_EQ(status, 2);
309 }
310
311 HWTEST_F(HttpCacheStrategyTest, CompareNumber_4, testing::ext::TestSize.Level1)
312 {
313 HttpRequestOptions requestOptions;
314 requestOptions.SetRequestTime("Mon, 16 May 2022 09:32:59 GMT");
315 requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT");
316
317 HttpResponse response;
318 response.SetResponseCode(200);
319 response.SetResponseTime("Mon, 16 May 2022 09:33:59 GMT");
320 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
321 responseHeader["age"] = "33781";
322
323 HttpCacheStrategy cacheStrategy(requestOptions);
324 CacheStatus status = cacheStrategy.RunStrategy(response);
325 NETSTACK_LOGI("status = %{public}d", status);
326
327 EXPECT_EQ(status, 2);
328 }
329
330 HWTEST_F(HttpCacheStrategyTest, CompareNumber_5, testing::ext::TestSize.Level1)
331 {
332 HttpRequestOptions requestOptions;
333 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
334 requestOptions.SetHeader("cache-control", "min-fresh=20");
335
336 HttpResponse response;
337 response.SetResponseCode(200);
338 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
339 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
340 responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT";
341
342 HttpCacheStrategy cacheStrategy(requestOptions);
343 CacheStatus status = cacheStrategy.RunStrategy(response);
344 NETSTACK_LOGI("status = %{public}d", status);
345
346 EXPECT_EQ(status, STALE);
347 }
348
349 HWTEST_F(HttpCacheStrategyTest, CompareNumber_6, testing::ext::TestSize.Level1)
350 {
351 HttpRequestOptions requestOptions;
352 requestOptions.SetRequestTime("Thu, 20 May 2022 09:35:59 GMT");
353 requestOptions.SetHeader("cache-control", "min-fresh=20");
354
355 HttpResponse response;
356 response.SetResponseCode(200);
357 response.SetResponseTime("Thu, 20 May 2022 09:36:30 GMT");
358 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
359 responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT";
360 responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT";
361
362 HttpCacheStrategy cacheStrategy(requestOptions);
363 CacheStatus status = cacheStrategy.RunStrategy(response);
364 NETSTACK_LOGI("status = %{public}d", status);
365
366 EXPECT_EQ(status, STALE);
367 }
368
369 HWTEST_F(HttpCacheStrategyTest, CompareNumber_7, testing::ext::TestSize.Level1)
370 {
371 HttpRequestOptions requestOptions;
372 requestOptions.SetRequestTime("Thu, 20 May 2022 09:35:59 GMT");
373 requestOptions.SetHeader("cache-control", "min-fresh=20");
374
375 HttpResponse response;
376 response.SetResponseCode(200);
377 response.SetResponseTime("Thu, 20 May 2022 09:36:30 GMT");
378 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
379 responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT";
380 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
381 responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT";
382
383 HttpCacheStrategy cacheStrategy(requestOptions);
384 CacheStatus status = cacheStrategy.RunStrategy(response);
385 NETSTACK_LOGI("status = %{public}d", status);
386
387 EXPECT_EQ(status, STALE);
388 }
389
390 HWTEST_F(HttpCacheStrategyTest, CompareNumber_8, testing::ext::TestSize.Level1)
391 {
392 HttpRequestOptions requestOptions;
393 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
394 requestOptions.SetHeader("cache-control", "min-fresh=20");
395
396 HttpResponse response;
397 response.SetResponseCode(200);
398 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
399
400 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
401 responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT";
402 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
403 responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e";
404 responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT";
405
406 HttpCacheStrategy cacheStrategy(requestOptions);
407 CacheStatus status = cacheStrategy.RunStrategy(response);
408 NETSTACK_LOGI("status = %{public}d", status);
409
410 EXPECT_EQ(status, STALE);
411 }
412
413 HWTEST_F(HttpCacheStrategyTest, CompareNumber_9, testing::ext::TestSize.Level1)
414 {
415 HttpRequestOptions requestOptions;
416 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
417 requestOptions.SetHeader("cache-control", "min-fresh=20");
418
419 HttpResponse response;
420 response.SetResponseCode(200);
421 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
422
423 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
424 responseHeader["age"] = "60";
425
426 HttpCacheStrategy cacheStrategy(requestOptions);
427 CacheStatus status = cacheStrategy.RunStrategy(response);
428 NETSTACK_LOGI("status = %{public}d", status);
429
430 EXPECT_EQ(status, 1);
431 }
432
433 HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeLastModifiedNoDateBranch, testing::ext::TestSize.Level1)
434 {
435 HttpRequestOptions requestOptions;
436 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
437 requestOptions.SetHeader("cache-control", "min-fresh=20");
438
439 HttpResponse response;
440 response.SetResponseCode(200);
441 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
442
443 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
444 responseHeader["cache-control"] = "public";
445 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
446
447 HttpCacheStrategy cacheStrategy(requestOptions);
448 CacheStatus status = cacheStrategy.RunStrategy(response);
449 NETSTACK_LOGI("status = %{public}d", status);
450
451 EXPECT_EQ(status, 1);
452 }
453
454 HWTEST_F(HttpCacheStrategyTest, cache110WarningBranch, testing::ext::TestSize.Level1)
455 {
456 HttpRequestOptions requestOptions;
457 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
458 requestOptions.SetHeader("cache-control", "min-fresh=60, max-stale=2000");
459
460 HttpResponse response;
461 response.SetResponseCode(200);
462 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
463
464 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
465 responseHeader["cache-control"] = "max-age=60, max-stale=500000000";
466 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
467 responseHeader["date"] = "Fri, 20 May 2022 09:50:29 GMT";
468
469 HttpCacheStrategy cacheStrategy(requestOptions);
470 CacheStatus status = cacheStrategy.RunStrategy(response);
471 NETSTACK_LOGI("status = %{public}d", status);
472
473 EXPECT_EQ(status, STALE);
474 }
475
switchTest(ResponseCode code)476 CacheStatus switchTest(ResponseCode code)
477 {
478 HttpRequestOptions requestOptions;
479 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
480 requestOptions.SetHeader("cache-control", "min-fresh=60, max-stale=2000");
481
482 HttpResponse response;
483 response.SetResponseCode(static_cast<uint32_t>(code));
484 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
485
486 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
487 responseHeader["cache-control"] = "max-age=60, max-stale=500000000";
488 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
489 responseHeader["date"] = "Fri, 20 May 2022 09:50:29 GMT";
490
491 HttpCacheStrategy cacheStrategy(requestOptions);
492 CacheStatus status = cacheStrategy.RunStrategy(response);
493 return status;
494 }
495
496 HWTEST_F(HttpCacheStrategyTest, cacheSwitchBranch, testing::ext::TestSize.Level1)
497 {
498 CacheStatus result;
499 std::vector<ResponseCode> respCode = {ResponseCode::OK, ResponseCode::NOT_AUTHORITATIVE,
500 ResponseCode::NO_CONTENT, ResponseCode::MULT_CHOICE,
501 ResponseCode::MOVED_PERM, ResponseCode::NOT_FOUND,
502 ResponseCode::BAD_METHOD, ResponseCode::GONE,
503 ResponseCode::REQ_TOO_LONG, ResponseCode::NOT_IMPLEMENTED};
504
505 for (const auto &iterRespCode : respCode) {
506 NETSTACK_LOGI("respCode:%d", iterRespCode);
507 }
508
509 for (const auto &iterRespCode : respCode) {
510 result = switchTest(iterRespCode);
511 EXPECT_EQ(result, STALE);
512 }
513 }
514
515 HWTEST_F(HttpCacheStrategyTest, cache113WarningBranch, testing::ext::TestSize.Level1)
516 {
517 HttpRequestOptions requestOptions;
518 requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT");
519 requestOptions.SetHeader("cache-control", "min-fresh=2, max-stale=9000000000");
520
521 HttpResponse response;
522 response.SetResponseCode(200);
523 response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT");
524
525 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
526 responseHeader["cache-control"] = "max-stale=5000000000000000";
527 responseHeader["last-modified"] = "Sat, 04 Jun 2022 09:56:21 GMT";
528 responseHeader["date"] = "Mon, 20 Jun 2022 09:56:21 GMT";
529
530 HttpCacheStrategy cacheStrategy(requestOptions);
531 CacheStatus status = cacheStrategy.RunStrategy(response);
532 NETSTACK_LOGI("status = %{public}d", status);
533
534 EXPECT_EQ(status, FRESH);
535 }
536
537 HWTEST_F(HttpCacheStrategyTest, reqHeaderEtagBranch, testing::ext::TestSize.Level1)
538 {
539 HttpRequestOptions requestOptions;
540 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
541 requestOptions.SetHeader("cache-control", "min-fresh=20");
542
543 HttpResponse response;
544 response.SetResponseCode(200);
545 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
546
547 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
548 responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT";
549 responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e";
550
551 HttpCacheStrategy cacheStrategy(requestOptions);
552 CacheStatus status = cacheStrategy.RunStrategy(response);
553 NETSTACK_LOGI("status = %{public}d", status);
554
555 EXPECT_EQ(status, STALE);
556 }
557
558 HWTEST_F(HttpCacheStrategyTest, reqHeaderLastModifiedBranch, testing::ext::TestSize.Level1)
559 {
560 HttpRequestOptions requestOptions;
561 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
562 requestOptions.SetHeader("cache-control", "min-fresh=20");
563
564 HttpResponse response;
565 response.SetResponseCode(200);
566 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
567
568 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
569 responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT";
570 responseHeader["last-modified"] = "Sat, 04 Jun 2022 09:56:21 GMT";
571
572 HttpCacheStrategy cacheStrategy(requestOptions);
573 CacheStatus status = cacheStrategy.RunStrategy(response);
574 NETSTACK_LOGI("status = %{public}d", status);
575
576 EXPECT_EQ(status, STALE);
577 }
578
579 HWTEST_F(HttpCacheStrategyTest, reqHeaderDateBranch, testing::ext::TestSize.Level1)
580 {
581 HttpRequestOptions requestOptions;
582 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
583 requestOptions.SetHeader("cache-control", "min-fresh=20");
584
585 HttpResponse response;
586 response.SetResponseCode(200);
587 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
588
589 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
590 responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT";
591 responseHeader["date"] = "Sat, 04 Jun 2022 09:56:21 GMT";
592
593 HttpCacheStrategy cacheStrategy(requestOptions);
594 CacheStatus status = cacheStrategy.RunStrategy(response);
595 NETSTACK_LOGI("status = %{public}d", status);
596
597 EXPECT_EQ(status, STALE);
598 }
599
600 HWTEST_F(HttpCacheStrategyTest, headerNull, testing::ext::TestSize.Level1)
601 {
602 HttpRequestOptions requestOptions;
603 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
604
605 HttpResponse response;
606 response.SetResponseCode(200);
607 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
608
609 HttpCacheStrategy cacheStrategy(requestOptions);
610 CacheStatus status = cacheStrategy.RunStrategy(response);
611 NETSTACK_LOGI("status = %{public}d", status);
612
613 EXPECT_EQ(status, STALE);
614 }
615
616 HWTEST_F(HttpCacheStrategyTest, requestTimeEmpty, testing::ext::TestSize.Level1)
617 {
618 HttpRequestOptions requestOptions;
619
620 HttpResponse response;
621 response.SetResponseCode(200);
622 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
623
624 HttpCacheStrategy cacheStrategy(requestOptions);
625 CacheStatus status = cacheStrategy.RunStrategy(response);
626 NETSTACK_LOGI("status = %{public}d", status);
627
628 EXPECT_EQ(status, STALE);
629 }
630
631 HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeEnd, testing::ext::TestSize.Level1)
632 {
633 HttpRequestOptions requestOptions;
634 requestOptions.SetRequestTime("Thu, 19 May 2022 08:19:59 GMT");
635
636 HttpResponse response;
637 response.SetResponseCode(200);
638 response.SetResponseTime("Thu, 19 May 2022 08:21:59 GMT");
639
640 HttpCacheStrategy cacheStrategy(requestOptions);
641 CacheStatus status = cacheStrategy.RunStrategy(response);
642 NETSTACK_LOGI("status = %{public}d", status);
643
644 EXPECT_EQ(status, STALE);
645 }
646
647 HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition, testing::ext::TestSize.Level1)
648 {
649 HttpRequestOptions requestOptions;
650 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
651 requestOptions.SetHeader("cache-control", "min-fresh=20");
652
653 HttpResponse response;
654 response.SetResponseCode(static_cast<uint32_t>(ResponseCode::MOVED_TEMP));
655 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
656
657 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
658 responseHeader["cache-control"] = "private";
659 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
660
661 HttpCacheStrategy cacheStrategy(requestOptions);
662 CacheStatus status = cacheStrategy.RunStrategy(response);
663 NETSTACK_LOGI("status = %{public}d", status);
664
665 EXPECT_EQ(status, STALE);
666 }
667
668 HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeDelta, testing::ext::TestSize.Level1)
669 {
670 HttpRequestOptions requestOptions;
671 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
672 requestOptions.SetHeader("cache-control", "min-fresh=20");
673
674 HttpResponse response;
675 response.SetResponseCode(200);
676 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
677
678 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
679 responseHeader["last-modified"] = "Mon, 18 Jul 2022 10:55:14 GMT";
680
681 HttpCacheStrategy cacheStrategy(requestOptions);
682 CacheStatus status = cacheStrategy.RunStrategy(response);
683 NETSTACK_LOGI("status = %{public}d", status);
684
685 EXPECT_EQ(status, STALE);
686 }
687
688 HWTEST_F(HttpCacheStrategyTest, isCacheUsefulMaxStaleMillis, testing::ext::TestSize.Level1)
689 {
690 HttpRequestOptions requestOptions;
691 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
692 requestOptions.SetHeader("cache-control", "max-stale=20");
693
694 HttpResponse response;
695 response.SetResponseCode(200);
696 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
697
698 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
699 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
700
701 HttpCacheStrategy cacheStrategy(requestOptions);
702 CacheStatus status = cacheStrategy.RunStrategy(response);
703 NETSTACK_LOGI("status = %{public}d", status);
704
705 EXPECT_EQ(status, STALE);
706 }
707
708 HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition2, testing::ext::TestSize.Level1)
709 {
710 HttpRequestOptions requestOptions;
711 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
712 requestOptions.SetHeader("cache-control", "min-fresh=20, no-store");
713
714 HttpResponse response;
715 response.SetResponseCode(200);
716 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
717
718 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
719 responseHeader["cache-control"] = "private";
720 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
721
722 HttpCacheStrategy cacheStrategy(requestOptions);
723 CacheStatus status = cacheStrategy.RunStrategy(response);
724 NETSTACK_LOGI("status = %{public}d", status);
725
726 EXPECT_EQ(status, DENY);
727 }
728
729 HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition3, testing::ext::TestSize.Level1)
730 {
731 HttpRequestOptions requestOptions;
732 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
733 requestOptions.SetHeader("cache-control", "min-fresh=20");
734
735 HttpResponse response;
736 response.SetResponseCode(200);
737 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
738
739 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
740 responseHeader["cache-control"] = "private, no-store";
741 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
742
743 HttpCacheStrategy cacheStrategy(requestOptions);
744 CacheStatus status = cacheStrategy.RunStrategy(response);
745 NETSTACK_LOGI("status = %{public}d", status);
746
747 EXPECT_EQ(status, DENY);
748 }
749
750 HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition4, testing::ext::TestSize.Level1)
751 {
752 HttpRequestOptions requestOptions;
753 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
754 requestOptions.SetHeader("cache-control", "min-fresh=20");
755
756 HttpResponse response;
757 response.SetResponseCode(200);
758 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
759
760 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
761 responseHeader["cache-control"] = "private, must-revalidate";
762 responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT";
763
764 HttpCacheStrategy cacheStrategy(requestOptions);
765 CacheStatus status = cacheStrategy.RunStrategy(response);
766 NETSTACK_LOGI("status = %{public}d", status);
767
768 EXPECT_EQ(status, STALE);
769 }
770
771 HWTEST_F(HttpCacheStrategyTest, CompareNumber_6_2, testing::ext::TestSize.Level1)
772 {
773 HttpRequestOptions requestOptions;
774 requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT");
775 requestOptions.SetHeader("cache-control", "min-fresh=20");
776
777 HttpResponse response;
778 response.SetResponseCode(200);
779 response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT");
780
781 auto &responseHeader = const_cast<std::map<std::string, std::string> &>(response.GetHeader());
782 responseHeader["cache-control"] = "no-cache";
783 responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT";
784 responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT";
785
786 HttpCacheStrategy cacheStrategy(requestOptions);
787 CacheStatus status = cacheStrategy.RunStrategy(response);
788 NETSTACK_LOGI("status = %{public}d", status);
789
790 EXPECT_EQ(status, STALE);
791 }
792 } // namespace