1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_LOADER_KEEP_ALIVE_REQUEST_BROWSERTEST_UTIL_H_
#define CHROME_BROWSER_LOADER_KEEP_ALIVE_REQUEST_BROWSERTEST_UTIL_H_
#include <memory>
#include <string>
#include <vector>
#include "base/test/scoped_feature_list.h"
#include "chrome/test/base/platform_browser_test.h"
#include "net/test/embedded_test_server/controllable_http_response.h"
#include "url/gurl.h"
namespace content {
class WebContents;
class RenderFrameHost;
class KeepAliveURLLoadersTestObserver;
} // namespace content
inline constexpr char kPrimaryHost[] = "a.com";
inline constexpr char kSecondaryHost[] = "b.com";
inline constexpr char kGoogleHost[] = "www.google.com";
inline constexpr char kKeepAliveEndpoint[] = "/beacon";
inline constexpr char16_t kPromiseResolvedPageTitle[] = u"Resolved";
inline constexpr char k200TextResponse[] =
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=utf-8\r\n"
"\r\n";
inline constexpr char k301ResponseTemplate[] =
"HTTP/1.1 301 Moved Permanently\r\n"
"Location: %s\r\n"
"\r\n";
// Encodes a relative URL to a string that can be used in a URL.
// `relative_url` must start with "/".
std::string EncodeRelativeURL(std::string_view relative_url);
// Base class for browser tests that need to test fetch keepalive requests.
//
// This class provides common utilities to set up the test environment and
// navigate to pages with fetch keepalive requests.
class ChromeKeepAliveRequestBrowserTestBase : public PlatformBrowserTest {
public:
ChromeKeepAliveRequestBrowserTestBase();
~ChromeKeepAliveRequestBrowserTestBase() override;
// Not copyable.
ChromeKeepAliveRequestBrowserTestBase(
const ChromeKeepAliveRequestBrowserTestBase&) = delete;
ChromeKeepAliveRequestBrowserTestBase& operator=(
const ChromeKeepAliveRequestBrowserTestBase&) = delete;
protected:
void SetUpOnMainThread() override;
void TearDownOnMainThread() override;
void SetUpCommandLine(base::CommandLine* command_line) override;
// Sets the test server to use HTTPS.
void SetUseHttps();
// Initializes the feature list with BackForwardCache features and
// `additional_features_and_params`.
void InitFeatureList(const std::vector<base::test::FeatureRefAndParams>&
additional_features_and_params);
// Navigates to a page specified by `keepalive_page_url`, which must fires a
// fetch keepalive request.
// The method then postpones the request handling until RFH of the page is
// fully unloaded (by navigating to another cross-origin page).
// After that, `response` will be sent back.
// `keepalive_request_handler` must handle the fetch keepalive request.
void LoadPageWithKeepAliveRequestAndSendResponseAfterUnload(
const GURL& keepalive_page_url,
net::test_server::ControllableHttpResponse* keepalive_request_handler,
const std::string& response);
[[nodiscard]] std::vector<
std::unique_ptr<net::test_server::ControllableHttpResponse>>
RegisterRequestHandlers(const std::vector<std::string>& relative_urls);
GURL GetKeepAlivePageURL(const std::string& host,
const std::string& target_relative_url,
const std::string& method);
GURL GetCrossOriginPageURL();
void DisableBackForwardCache(content::WebContents* web_contents);
content::WebContents* web_contents();
content::RenderFrameHost* current_frame_host();
net::EmbeddedTestServer* server();
content::KeepAliveURLLoadersTestObserver& loaders_observer();
private:
base::test::ScopedFeatureList feature_list_;
bool use_https_ = false;
const std::unique_ptr<net::EmbeddedTestServer> https_test_server_;
std::unique_ptr<content::KeepAliveURLLoadersTestObserver> loaders_observer_;
};
#endif // CHROME_BROWSER_LOADER_KEEP_ALIVE_REQUEST_BROWSERTEST_UTIL_H_
|