-
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 Co-authored-by: Francois Marier <[email protected]>
- Loading branch information
Showing
9 changed files
with
224 additions
and
13 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
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
/* 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 kPermittedHttpStatusCodes = 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 HttpStatusCodeClassToString(const int http_status_code_class) { | ||
return base::StringPrintf("%dxx", http_status_code_class); | ||
} | ||
|
||
} // namespace | ||
|
||
std::string HttpStatusCodeToString(const int http_status_code) { | ||
const int http_status_code_class = http_status_code / 100; | ||
|
||
// Check if the HTTP status code is in the permitted list of codes. | ||
if (const auto iter = kPermittedHttpStatusCodes.find(http_status_code); | ||
iter != kPermittedHttpStatusCodes.cend()) { | ||
// If the HTTP status code is allowed, return it as a string. | ||
return base::NumberToString(http_status_code); | ||
} | ||
|
||
// Return a data minimization status code corresponding to the class of the | ||
// original HTTP status code if the original code is not allowed. | ||
return HttpStatusCodeClassToString(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 HttpStatusCodeToString(int http_status_code); | ||
|
||
} // namespace brave_ads | ||
|
||
#endif // BRAVE_COMPONENTS_BRAVE_ADS_CORE_INTERNAL_COMMON_NET_HTTP_HTTP_STATUS_CODE_UTIL_H_ |
41 changes: 41 additions & 0 deletions
41
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,41 @@ | ||
/* 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, HttpStatusCodeToString) { | ||
// Act & Assert | ||
for (int i = 100; i <= 599; ++i) { | ||
const std::string http_status_code = HttpStatusCodeToString(i); | ||
|
||
// Permitted HTTP status codes, other othre codes are mapped to their class. | ||
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), http_status_code); | ||
} else { | ||
EXPECT_EQ(base::StringPrintf("%dxx", /*http_status_code_class*/ i / 100), | ||
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