Commit 9168cd41 authored by Patrick Monette's avatar Patrick Monette Committed by Commit Bot

Add a test for DetermineFinalResponseUrl()

Bug: 986188, 1048332
Change-Id: I94391cb298fa16cb68ee47169559e3b449f02465
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2062703Reviewed-by: default avatarHiroki Nakagawa <nhiroki@chromium.org>
Commit-Queue: Patrick Monette <pmonette@chromium.org>
Cr-Commit-Position: refs/heads/master@{#743347}
parent 7254ad8e
......@@ -57,30 +57,6 @@ namespace content {
namespace {
// Calculate the final response URL from the redirect chain, URLs fetched by the
// service worker and the initial request URL. The logic is mostly based on what
// blink::ResourceResponse::ResponseUrl() does.
GURL DetermineFinalResponseUrl(
const GURL& initial_request_url,
blink::mojom::WorkerMainScriptLoadParams* main_script_load_params) {
DCHECK(main_script_load_params);
network::mojom::URLResponseHead* url_response_head =
main_script_load_params->response_head.get();
// First check the URL list from the service worker.
if (!url_response_head->url_list_via_service_worker.empty()) {
DCHECK(url_response_head->was_fetched_via_service_worker);
return url_response_head->url_list_via_service_worker.back();
}
// Then check the list of redirects.
if (!main_script_load_params->redirect_infos.empty())
return main_script_load_params->redirect_infos.back().new_url;
// No redirection happened. The initial request URL was used for the response.
return initial_request_url;
}
} // namespace
......@@ -445,4 +421,26 @@ void WorkerScriptFetchInitiator::DidCreateScriptLoader(
std::move(controller_service_worker_object_host), final_response_url);
}
GURL WorkerScriptFetchInitiator::DetermineFinalResponseUrl(
const GURL& initial_request_url,
blink::mojom::WorkerMainScriptLoadParams* main_script_load_params) {
DCHECK(main_script_load_params);
network::mojom::URLResponseHead* url_response_head =
main_script_load_params->response_head.get();
// First check the URL list from the service worker.
if (!url_response_head->url_list_via_service_worker.empty()) {
DCHECK(url_response_head->was_fetched_via_service_worker);
return url_response_head->url_list_via_service_worker.back();
}
// Then check the list of redirects.
if (!main_script_load_params->redirect_infos.empty())
return main_script_load_params->redirect_infos.back().new_url;
// No redirection happened. The initial request URL was used for the response.
return initial_request_url;
}
} // namespace content
......@@ -12,6 +12,7 @@
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "content/common/content_export.h"
#include "content/public/browser/browser_thread.h"
#include "services/network/public/mojom/fetch_api.mojom.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
......@@ -51,7 +52,7 @@ struct SubresourceLoaderParams;
// for WorkerScriptFetcher.
// TODO(falken): These are all static functions, it should just be a namespace
// or merged elsewhere.
class WorkerScriptFetchInitiator {
class CONTENT_EXPORT WorkerScriptFetchInitiator {
public:
using CompletionCallback = base::OnceCallback<void(
bool success,
......@@ -97,6 +98,9 @@ class WorkerScriptFetchInitiator {
bool filesystem_url_support);
private:
FRIEND_TEST_ALL_PREFIXES(WorkerScriptFetchInitiatorTest,
DetermineFinalResponseUrl);
// Adds additional request headers to |resource_request|. Must be called on
// the UI thread.
static void AddAdditionalRequestHeaders(
......@@ -129,6 +133,15 @@ class WorkerScriptFetchInitiator {
blink::mojom::WorkerMainScriptLoadParamsPtr main_script_load_params,
base::Optional<SubresourceLoaderParams> subresource_loader_params,
bool success);
// Calculate the final response URL from the redirect chain, URLs fetched by
// the service worker and the initial request URL. The logic is mostly based
// on what blink::ResourceResponse::ResponseUrl() does.
//
// Exposed for testing.
static GURL DetermineFinalResponseUrl(
const GURL& initial_request_url,
blink::mojom::WorkerMainScriptLoadParams* main_script_load_params);
};
} // namespace content
......
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/worker_host/worker_script_fetch_initiator.h"
#include <vector>
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/mojom/worker/worker_main_script_load_params.mojom.h"
#include "url/gurl.h"
namespace content {
namespace {
blink::mojom::WorkerMainScriptLoadParamsPtr CreateParams(
const std::vector<GURL>& url_list_via_service_worker,
const std::vector<GURL>& redirect_infos) {
blink::mojom::WorkerMainScriptLoadParamsPtr main_script_load_params =
blink::mojom::WorkerMainScriptLoadParams::New();
main_script_load_params->response_head =
network::mojom::URLResponseHead::New();
if (!url_list_via_service_worker.empty()) {
main_script_load_params->response_head->was_fetched_via_service_worker =
true;
main_script_load_params->response_head->url_list_via_service_worker =
url_list_via_service_worker;
}
for (const GURL& url : redirect_infos) {
net::RedirectInfo redirect_info;
redirect_info.new_url = url;
main_script_load_params->redirect_infos.push_back(redirect_info);
}
return main_script_load_params;
}
} // namespace
TEST(WorkerScriptFetchInitiatorTest, DetermineFinalResponseUrl) {
struct TestCase {
GURL initial_request_url;
std::vector<GURL> url_list_via_service_worker;
std::vector<GURL> redirect_infos;
GURL expected_final_response_url;
};
static const std::vector<TestCase> kTestCases = {
{
GURL("https://initial.com"),
{},
{},
GURL("https://initial.com"),
},
{
GURL("https://initial.com"),
{GURL("https://url_list_1.com"), GURL("https://url_list_2.com")},
{},
GURL("https://url_list_2.com"),
},
{
GURL("https://initial.com"),
{},
{GURL("https://redirect_1.com"), GURL("https://redirect_2.com")},
GURL("https://redirect_2.com"),
},
{
GURL("https://initial.com"),
{GURL("https://url_list_1.com"), GURL("https://url_list_2.com")},
{GURL("https://redirect_1.com"), GURL("https://redirect_2.com")},
GURL("https://url_list_2.com"),
},
};
for (const auto& test_case : kTestCases) {
blink::mojom::WorkerMainScriptLoadParamsPtr main_script_load_params =
CreateParams(test_case.url_list_via_service_worker,
test_case.redirect_infos);
GURL final_response_url =
WorkerScriptFetchInitiator::DetermineFinalResponseUrl(
test_case.initial_request_url, main_script_load_params.get());
EXPECT_EQ(final_response_url, test_case.expected_final_response_url);
}
}
} // namespace content
......@@ -1860,6 +1860,7 @@ test("content_unittests") {
"../browser/worker_host/shared_worker_host_unittest.cc",
"../browser/worker_host/shared_worker_instance_unittest.cc",
"../browser/worker_host/shared_worker_service_impl_unittest.cc",
"../browser/worker_host/worker_script_fetch_initiator_unittest.cc",
"../browser/worker_host/worker_script_loader_factory_unittest.cc",
"../child/blink_platform_impl_unittest.cc",
"../child/dwrite_font_proxy/dwrite_font_proxy_win_unittest.cc",
......
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