-
Notifications
You must be signed in to change notification settings - Fork 896
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ads] Followup to "Send HTTP response status for landed events as par…
…t of the confirmation token redemption" #36752
- Loading branch information
Showing
9 changed files
with
131 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
components/brave_ads/core/internal/common/net/http/http_status_code_util.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/brave_ads/core/internal/common/net/http/http_status_code_util.h" | ||
|
||
#include "base/containers/fixed_flat_set.h" | ||
#include "base/strings/string_number_conversions.h" | ||
#include "base/strings/stringprintf.h" | ||
|
||
namespace brave_ads { | ||
|
||
namespace { | ||
|
||
constexpr auto kAllowedHttpStatusCodes = base::MakeFixedFlatSet<int>({ | ||
400, // Bad Request. | ||
401, // Unauthorized. | ||
403, // Forbidden. | ||
404, // Not Found. | ||
408, // Request Timeout. | ||
429, // Too Many Requests. | ||
451, // Unavailable For Legal Reasons. | ||
500, // Internal Server Error. | ||
502, // Bad Gateway. | ||
503, // Service Unavailable. | ||
504 // Gateway Timeout. | ||
}); | ||
|
||
std::string ToPrivacyPreservingHttpStatusCodeClass( | ||
const int http_status_code_class) { | ||
return base::StringPrintf("%dxx", http_status_code_class); | ||
} | ||
|
||
} // namespace | ||
|
||
std::string ToPrivacyPreservingHttpStatusCode(const int http_status_code) { | ||
const int http_status_code_class = http_status_code / 100; | ||
|
||
// Check if the HTTP status code is in the allowed list of privacy-preserving | ||
// codes. | ||
if (const auto iter = kAllowedHttpStatusCodes.find(http_status_code); | ||
iter != kAllowedHttpStatusCodes.cend()) { | ||
// If the HTTP status code is allowed, return it as a string. | ||
return base::NumberToString(http_status_code); | ||
} | ||
|
||
// If the HTTP status code is not allowed, return a privacy-preserving status | ||
// code based on the class of the original status code. | ||
return ToPrivacyPreservingHttpStatusCodeClass(http_status_code_class); | ||
} | ||
|
||
} // namespace brave_ads |
17 changes: 17 additions & 0 deletions
17
components/brave_ads/core/internal/common/net/http/http_status_code_util.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_COMMON_NET_HTTP_HTTP_STATUS_CODE_UTIL_H_ | ||
#define BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_COMMON_NET_HTTP_HTTP_STATUS_CODE_UTIL_H_ | ||
|
||
#include <string> | ||
|
||
namespace brave_ads { | ||
|
||
std::string ToPrivacyPreservingHttpStatusCode(int http_status_code); | ||
|
||
} // namespace brave_ads | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_COMMON_NET_HTTP_HTTP_STATUS_CODE_UTIL_H_ |
40 changes: 40 additions & 0 deletions
40
components/brave_ads/core/internal/common/net/http/http_status_code_util_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* Copyright (c) 2024 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at https://mozilla.org/MPL/2.0/. */ | ||
|
||
#include "brave/components/brave_ads/core/internal/common/net/http/http_status_code_util.h" | ||
|
||
#include "base/strings/string_number_conversions.h" | ||
#include "base/strings/stringprintf.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
// npm run test -- brave_unit_tests --filter=BraveAds* | ||
|
||
namespace brave_ads { | ||
|
||
TEST(BraveAdsHttpStatusCodeUtilTest, ToPrivacyPreservingHttpStatusCode) { | ||
// Act & Assert | ||
for (int i = 100; i <= 599; i++) { | ||
const std::string privacy_preserving_http_status_code = | ||
ToPrivacyPreservingHttpStatusCode(i); | ||
if (i == 400 || // Bad Request. | ||
i == 401 || // Unauthorized. | ||
i == 403 || // Forbidden. | ||
i == 404 || // Not Found. | ||
i == 408 || // Request Timeout. | ||
i == 429 || // Too Many Requests. | ||
i == 451 || // Unavailable For Legal Reasons. | ||
i == 500 || // Internal Server Error. | ||
i == 502 || // Bad Gateway. | ||
i == 503 || // Service Unavailable. | ||
i == 504) { // Gateway Timeout. | ||
EXPECT_EQ(base::NumberToString(i), privacy_preserving_http_status_code); | ||
} else { | ||
EXPECT_EQ(base::StringPrintf("%dxx", /*http_status_code_class*/ i / 100), | ||
privacy_preserving_http_status_code); | ||
} | ||
} | ||
} | ||
|
||
} // namespace brave_ads |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters