Commit aa273990 authored by Xinghui Lu's avatar Xinghui Lu Committed by Commit Bot

Add unit tests for FillRequestProto()

Separate request proto fill in logic from the origin function, which
make it easier for unit testing. The test is mainly for url sanitization.

Bug: 1017440
Change-Id: I8626aebb8459972b248330a396e1e23a40dfb474
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1877813
Commit-Queue: Xinghui Lu <xinghuilu@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Cr-Commit-Position: refs/heads/master@{#709143}
parent b283b57e
......@@ -52,13 +52,10 @@ void RealTimeUrlLookupService::StartLookup(
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
DCHECK(url.is_valid());
RTLookupRequest request;
request.set_url(SanitizeURL(url).spec());
request.set_lookup_type(RTLookupRequest::NAVIGATION);
std::string req_data, req_base64;
request.SerializeToString(&req_data);
base::Base64UrlEncode(req_data, base::Base64UrlEncodePolicy::INCLUDE_PADDING,
&req_base64);
std::unique_ptr<RTLookupRequest> request = FillRequestProto(url);
std::string req_data;
request->SerializeToString(&req_data);
// TODO(vakh): Add the correct chrome_policy field below.
net::NetworkTrafficAnnotationTag traffic_annotation =
net::DefineNetworkTrafficAnnotation("safe_browsing_realtime_url_lookup",
......@@ -112,7 +109,7 @@ void RealTimeUrlLookupService::StartLookup(
pending_requests_[owned_loader.release()] = std::move(response_callback);
std::move(request_callback).Run(std::make_unique<RTLookupRequest>(request));
std::move(request_callback).Run(std::move(request));
}
RealTimeUrlLookupService::~RealTimeUrlLookupService() {
......@@ -154,6 +151,15 @@ bool RealTimeUrlLookupService::CanCheckUrl(const GURL& url) const {
return url.SchemeIsHTTPOrHTTPS();
}
std::unique_ptr<RTLookupRequest> RealTimeUrlLookupService::FillRequestProto(
const GURL& url) {
auto request = std::make_unique<RTLookupRequest>();
request->set_url(SanitizeURL(url).spec());
request->set_lookup_type(RTLookupRequest::NAVIGATION);
// TODO(crbug.com/1017499): Set ChromeUserPopulation.
return request;
}
void RealTimeUrlLookupService::ExitBackoff() {
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
ResetFailures();
......
......@@ -81,6 +81,8 @@ class RealTimeUrlLookupService {
void OnURLLoaderComplete(network::SimpleURLLoader* url_loader,
std::unique_ptr<std::string> response_body);
std::unique_ptr<RTLookupRequest> FillRequestProto(const GURL& url);
// Helper function to return a weak pointer.
base::WeakPtr<RealTimeUrlLookupService> GetWeakPtr();
......
......@@ -31,6 +31,9 @@ class RealTimeUrlLookupServiceTest : public PlatformTest {
void HandleLookupError() { rt_service_->HandleLookupError(); }
void HandleLookupSuccess() { rt_service_->HandleLookupSuccess(); }
bool IsInBackoffMode() { return rt_service_->IsInBackoffMode(); }
std::unique_ptr<RTLookupRequest> FillRequestProto(const GURL& url) {
return rt_service_->FillRequestProto(url);
}
network::TestURLLoaderFactory test_url_loader_factory_;
scoped_refptr<network::SharedURLLoaderFactory> test_shared_loader_factory_;
......@@ -38,6 +41,23 @@ class RealTimeUrlLookupServiceTest : public PlatformTest {
content::BrowserTaskEnvironment task_environment_;
};
TEST_F(RealTimeUrlLookupServiceTest, TestFillRequestProto) {
struct SanitizeUrlCase {
const char* url;
const char* expected_url;
} sanitize_url_cases[] = {
{"http://example.com/", "http://example.com/"},
{"http://user:pass@example.com/", "http://example.com/"},
{"http://%123:bar@example.com/", "http://example.com/"},
{"http://example.com#123", "http://example.com/"}};
for (size_t i = 0; i < base::size(sanitize_url_cases); i++) {
GURL url(sanitize_url_cases[i].url);
auto result = FillRequestProto(url);
EXPECT_EQ(sanitize_url_cases[i].expected_url, result->url());
EXPECT_EQ(RTLookupRequest::NAVIGATION, result->lookup_type());
}
}
TEST_F(RealTimeUrlLookupServiceTest, TestBackoffAndTimerReset) {
// Not in backoff at the beginning.
ASSERT_FALSE(IsInBackoffMode());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment