diff --git a/CMakeLists.txt b/CMakeLists.txt index 32578ef07b83e8ddd245e55008f17cb91b5b2be7..05a16eb14cb5e817ca059f740b329119fa1bd5f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -68,4 +68,5 @@ add_subdirectory(test/napi/http) add_subdirectory(test/napi/socket) add_subdirectory(test/napi/fetch) add_subdirectory(test/napi/websocket) -add_subdirectory(test/utils/napi_utils/unittest) \ No newline at end of file +add_subdirectory(test/utils/napi_utils/unittest) +add_subdirectory(test/unittest/http/cache) \ No newline at end of file diff --git a/bundle.json b/bundle.json index 4bcccd6b60a3eff8d4ed14fc274a4b239de39ff4..91501525375ef9c64291932062682eb536d1d981 100644 --- a/bundle.json +++ b/bundle.json @@ -49,7 +49,8 @@ ], "inner_kits": [], "test": [ - "//foundation/communication/netstack/test/napi/socket:fuzztest" + "//foundation/communication/netstack/test/napi/socket:fuzztest", + "//foundation/communication/netstack/test/unittest/http/cache:unittest" ] } } diff --git a/frameworks/js/napi/http/cache/cache_constant/include/casche_constant.h b/frameworks/js/napi/http/cache/cache_constant/include/casche_constant.h index 19064b232d2959e52553d248bf445b994b31805d..27f25e545bc3c2721a91535bad6808415536db79 100644 --- a/frameworks/js/napi/http/cache/cache_constant/include/casche_constant.h +++ b/frameworks/js/napi/http/cache/cache_constant/include/casche_constant.h @@ -20,7 +20,7 @@ static constexpr const int DECIMAL = 10; static constexpr const char *SPLIT = ", "; static constexpr const char EQUAL = '='; -static constexpr const char *DENY = "no-cache"; +static constexpr const char *NO_CACHE = "no-cache"; static constexpr const char *NO_STORE = "no-store"; static constexpr const char *NO_TRANSFORM = "no-transform"; static constexpr const char *ONLY_IF_CACHED = "only-if-cached"; diff --git a/frameworks/js/napi/http/cache/cache_strategy/src/http_cache_request.cpp b/frameworks/js/napi/http/cache/cache_strategy/src/http_cache_request.cpp index f7a0692506b58ae97ce44f971d2d2b96f03c0191..6bb9601044dd30592061764ff288ede96d31e803 100644 --- a/frameworks/js/napi/http/cache/cache_strategy/src/http_cache_request.cpp +++ b/frameworks/js/napi/http/cache/cache_strategy/src/http_cache_request.cpp @@ -26,7 +26,7 @@ void HttpCacheRequest::ParseCacheControl(const std::string &cacheControl) auto vec = CommonUtils::Split(cacheControl, SPLIT); for (const auto &str : vec) { - if (str == DENY) { + if (str == NO_CACHE) { noCache_ = true; } else if (str == NO_STORE) { noStore_ = true; @@ -61,7 +61,7 @@ void HttpCacheRequest::ParseRequestHeader(const std::map #include -#include "napi/native_api.h" - static constexpr const char *WARNING = "Warning"; namespace OHOS::NetStack { diff --git a/test/unittest/http/cache/BUILD.gn b/test/unittest/http/cache/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d147ec4e735a8dac70d0f7e2d9d92cc4679bc2eb --- /dev/null +++ b/test/unittest/http/cache/BUILD.gn @@ -0,0 +1,64 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# 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 +# +# http://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. + +import("//build/ohos.gni") +import("//build/test.gni") + +SUBSYSTEM_DIR = "//foundation/communication" +NETSTACK_NAPI_ROOT = "$SUBSYSTEM_DIR/netstack/frameworks/js/napi/" + +utils_include = [ + "$SUBSYSTEM_DIR/netstack/utils/common_utils/include", + "$SUBSYSTEM_DIR/netstack/utils/log/include", + "//third_party/curl/include", +] + +common_deps = [ "//utils/native/base:utils" ] + +common_external_deps = [ "hiviewdfx_hilog_native:libhilog" ] + +ohos_unittest("http_cache_unittest") { + module_out_path = "netstack/http_cache_unittest" + + include_dirs = [ + "$NETSTACK_NAPI_ROOT/http/cache/cache_constant/include", + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/include", + "$NETSTACK_NAPI_ROOT/http/constant/include", + "$NETSTACK_NAPI_ROOT/http/options/include", + ] + include_dirs += utils_include + + deps = common_deps + + external_deps = common_external_deps + + sources = [ + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/src/http_cache_request.cpp", + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/src/http_cache_response.cpp", + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/src/http_cache_strategy.cpp", + "$NETSTACK_NAPI_ROOT/http/cache/cache_strategy/src/http_time.cpp", + "$NETSTACK_NAPI_ROOT/http/constant/src/constant.cpp", + "$NETSTACK_NAPI_ROOT/http/options/src/http_request_options.cpp", + "$NETSTACK_NAPI_ROOT/http/options/src/http_response.cpp", + "$SUBSYSTEM_DIR/netstack/utils/common_utils/src/netstack_common_utils.cpp", + "HttpCacheStrategyTest.cpp", + ] + + part_name = "netstack" + subsystem_name = "communication" +} + +group("unittest") { + testonly = true + deps = [ ":http_cache_unittest" ] +} diff --git a/test/unittest/http/cache/CMakeLists.txt b/test/unittest/http/cache/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..95bb2996fa8bf3f49fad8581b45f35b19ca9c534 --- /dev/null +++ b/test/unittest/http/cache/CMakeLists.txt @@ -0,0 +1,27 @@ +include_directories(../../../../frameworks/js/napi/http/cache/cache_strategy/include) +include_directories(../../../../frameworks/js/napi/http/cache/cache_constant/include) +include_directories(../../../../frameworks/js/napi/http/options/include) +include_directories(../../../../frameworks/js/napi/http/constant/include) +include_directories(../../../../utils/common_utils/include) +include_directories(../../../../utils/log/include) +include_directories(../../../../../../../third_party/curl/include) + +add_executable( + cache_test + HttpCacheStrategyTest.cpp + ../../../../frameworks/js/napi/http/options/src/http_response.cpp + ../../../../frameworks/js/napi/http/options/src/http_request_options.cpp + ../../../../frameworks/js/napi/http/constant/src/constant.cpp + ../../../../utils/common_utils/src/netstack_common_utils.cpp + ../../../napi/log/test_hilog.cpp + ../../../../frameworks/js/napi/http/cache/cache_strategy/src/http_time.cpp + ../../../../frameworks/js/napi/http/cache/cache_strategy/src/http_cache_request.cpp + ../../../../frameworks/js/napi/http/cache/cache_strategy/src/http_cache_response.cpp + ../../../../frameworks/js/napi/http/cache/cache_strategy/src/http_cache_strategy.cpp +) + +target_link_libraries(cache_test gtestd) +target_link_libraries(cache_test gmockd) +target_link_libraries(cache_test gtest_maind) +target_link_libraries(cache_test gmock_maind) +target_link_libraries(cache_test pthread) \ No newline at end of file diff --git a/test/unittest/http/cache/HttpCacheStrategyTest.cpp b/test/unittest/http/cache/HttpCacheStrategyTest.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9b070ee06ad2b65c2eb087b96f1616094b7d3f3d --- /dev/null +++ b/test/unittest/http/cache/HttpCacheStrategyTest.cpp @@ -0,0 +1,792 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * 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 + * + * http://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. + */ + +#include +#include + +#include "gtest/gtest.h" + +#include "http_cache_request.h" +#include "http_cache_response.h" +#include "http_cache_strategy.h" +#include "netstack_log.h" + +using namespace OHOS::NetStack; + +class HttpCacheStrategyTest : public testing::Test { +public: + static void SetUpTestCase() {} + + static void TearDownTestCase() {} + + virtual void SetUp() {} + + virtual void TearDown() {} +}; + +namespace { +HWTEST_F(HttpCacheStrategyTest, cacheRequestNoCache, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "no-cache"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeLastModifiedBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "min-fresh=20"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "public"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, cacheResponseNoCache, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "min-fresh=20"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "no-cache"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, cacheRequestOnlyIfCached, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "only-if-cached"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheable, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "min-fresh=20"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(303); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheable_OK, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "min-fresh=20"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, requestIfModifiedSinceStr, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, requestgetIfNoneMatch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("if-none-match", "A6E52F1D544D9DAFB552163A1CF8AD10"); + requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, requestgetIfNoneMatchAndIfModifiedSinceStr, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("if-none-match", "A6E52F1D544D9DAFB552163A1CF8AD10"); + requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, strategyMaxAgeBranch, testing::ext::TestSize.Level1) // test +{ + HttpRequestOptions requestOptions; + requestOptions.SetHeader("cache-control", "max-age=10"); + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:19 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["Cache-Control"] = "max-age=70"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_1, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "33781"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_1_2, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "33781"; + responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_2, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "max-age=10"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "10"; + responseHeader["cache-control"] = "private"; + responseHeader["expires"] = "Mon, 16 May 2022 10:31:58 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_3, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Mon, 16 May 2022 09:32:59 GMT"); + requestOptions.SetHeader("cache-control", "no-cache"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Mon, 16 May 2022 09:33:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "0"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_4, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Mon, 16 May 2022 09:32:59 GMT"); + requestOptions.SetHeader("if-modified-since", "Thu, 10 Feb 2022 10:55:14 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Mon, 16 May 2022 09:33:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "33781"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 2); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_5, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_6, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 20 May 2022 09:36:30 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_7, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 20 May 2022 09:36:30 GMT"); + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_8, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e"; + responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_9, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["age"] = "60"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeLastModifiedNoDateBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "public"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 1); +} + +HWTEST_F(HttpCacheStrategyTest, cache110WarningBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=60, max-stale=2000"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "max-age=60, max-stale=500000000"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:50:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, 0); +} + +CacheStatus switchTest(ResponseCode code) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=60, max-stale=2000"); + + HttpResponse response; + response.SetResponseCode(static_cast(code)); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "max-age=60, max-stale=500000000"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:50:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + return status; +} + +HWTEST_F(HttpCacheStrategyTest, cacheSwitchBranch, testing::ext::TestSize.Level1) +{ + CacheStatus result; + std::vector respCode = {ResponseCode::OK, ResponseCode::NOT_AUTHORITATIVE, + ResponseCode::NO_CONTENT, ResponseCode::MULT_CHOICE, + ResponseCode::MOVED_PERM, ResponseCode::NOT_FOUND, + ResponseCode::BAD_METHOD, ResponseCode::GONE, + ResponseCode::REQ_TOO_LONG, ResponseCode::NOT_IMPLEMENTED}; + + for (const auto &iterRespCode : respCode) { + NETSTACK_LOGI("respCode:%d", iterRespCode); + } + + for (const auto &iterRespCode : respCode) { + result = switchTest(iterRespCode); + EXPECT_EQ(result, FRESH); + } +} + +HWTEST_F(HttpCacheStrategyTest, cache113WarningBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:36:30 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=2, max-stale=9000000000"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "max-stale=5000000000000000"; + responseHeader["last-modified"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + responseHeader["date"] = "Mon, 20 Jun 2022 09:56:21 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, FRESH); +} + +HWTEST_F(HttpCacheStrategyTest, reqHeaderEtagBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT"; + responseHeader["etag"] = "6f6741d197947f9f10943d36c4d8210e"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, FRESH); +} + +HWTEST_F(HttpCacheStrategyTest, reqHeaderLastModifiedBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT"; + responseHeader["last-modified"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, FRESH); +} + +HWTEST_F(HttpCacheStrategyTest, reqHeaderDateBranch, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["expires"] = "Thu, 19 May 2022 08:22:26 GMT"; + responseHeader["date"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, headerNull, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, requestTimeEmpty, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeEnd, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Thur, 19 May 2022 08:19:59 GMT"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Thur, 19 May 2022 08:21:59 GMT"); + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(static_cast(ResponseCode::MOVED_TEMP)); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "private"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, computeFreshnessLifetimeDelta, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["last-modified"] = "Mon, 18 Jul 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheUsefulMaxStaleMillis, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "max-stale=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition2, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20, no-store"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "private"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, DENY); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition3, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "private, no-store"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, DENY); +} + +HWTEST_F(HttpCacheStrategyTest, isCacheableMovedTempIfCondition4, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "private, must-revalidate"; + responseHeader["last-modified"] = "Thu, 10 Feb 2022 10:55:14 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} + +HWTEST_F(HttpCacheStrategyTest, CompareNumber_6_2, testing::ext::TestSize.Level1) +{ + HttpRequestOptions requestOptions; + requestOptions.SetRequestTime("Fri, 20 May 2022 09:35:59 GMT"); + requestOptions.SetHeader("cache-control", "min-fresh=20"); + + HttpResponse response; + response.SetResponseCode(200); + response.SetResponseTime("Fri, 20 May 2022 09:36:30 GMT"); + + auto &responseHeader = const_cast &>(response.GetHeader()); + responseHeader["cache-control"] = "no-cache"; + responseHeader["expires"] = "Sat, 04 Jun 2022 09:56:21 GMT"; + responseHeader["date"] = "Fri, 20 May 2022 09:37:29 GMT"; + + HttpCacheStrategy cacheStrategy(requestOptions); + CacheStatus status = cacheStrategy.RunStrategy(response); + NETSTACK_LOGI("status = %{public}d", status); + + EXPECT_EQ(status, STALE); +} +} // namespace \ No newline at end of file