From 262ed72348025a888ebc9f1b0710cc79084b61e1 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Tue, 26 Sep 2023 16:12:32 -0400 Subject: [PATCH] cefclient: Use HTTPS for test URLs Some features that we wish to test require HTTPS. Also, Chrome runtime is redirecting HTTP URLs to HTTPS due to HSTS. --- tests/cefclient/browser/main_context_impl.cc | 3 ++- tests/cefclient/browser/server_test.cc | 5 +---- tests/cefclient/browser/test_runner.cc | 20 +++++++++++-------- tests/cefclient/browser/test_runner.h | 5 ++++- tests/cefclient/resources/server.html | 4 ++-- tests/cefclient/resources/websocket.html | 2 +- tests/cefclient/resources/xmlhttprequest.html | 2 +- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/tests/cefclient/browser/main_context_impl.cc b/tests/cefclient/browser/main_context_impl.cc index 75118d902..d540b7f03 100644 --- a/tests/cefclient/browser/main_context_impl.cc +++ b/tests/cefclient/browser/main_context_impl.cc @@ -7,6 +7,7 @@ #include #include "include/cef_parser.h" +#include "tests/cefclient/browser/test_runner.h" #include "tests/shared/browser/client_app_browser.h" #include "tests/shared/common/client_switches.h" #include "tests/shared/common/string_util.h" @@ -128,7 +129,7 @@ MainContextImpl::MainContextImpl(CefRefPtr command_line, if (use_views_ && command_line->HasSwitch(switches::kHideFrame) && !command_line_->HasSwitch(switches::kUrl)) { // Use the draggable regions test as the default URL for frameless windows. - main_url_ = "http://tests/draggable"; + main_url_ = test_runner::GetTestURL("draggable"); } if (command_line_->HasSwitch(switches::kBackgroundColor)) { diff --git a/tests/cefclient/browser/server_test.cc b/tests/cefclient/browser/server_test.cc index 849976642..6101c3b35 100644 --- a/tests/cefclient/browser/server_test.cc +++ b/tests/cefclient/browser/server_test.cc @@ -31,9 +31,6 @@ const char kPortKey[] = "port"; const char kStatusKey[] = "status"; const char kMessageKey[] = "message"; -// Required URL for cefQuery execution. -const char kTestUrl[] = "http://tests/server"; - // Server default values. const char kServerAddress[] = "127.0.0.1"; const int kServerPortDefault = 8099; @@ -234,7 +231,7 @@ class Handler : public CefMessageRouterBrowserSide::Handler { // Only handle messages from the test URL. const std::string& url = frame->GetURL(); - if (url.find(kTestUrl) != 0) { + if (url.find(test_runner::GetTestURL("server")) != 0) { return false; } diff --git a/tests/cefclient/browser/test_runner.cc b/tests/cefclient/browser/test_runner.cc index a8df8b9ca..b702bd978 100644 --- a/tests/cefclient/browser/test_runner.cc +++ b/tests/cefclient/browser/test_runner.cc @@ -39,7 +39,7 @@ namespace { const char kTestHost[] = "tests"; const char kLocalHost[] = "localhost"; -const char kTestOrigin[] = "http://tests/"; +const char kTestOrigin[] = "https://tests/"; // Pages handled via StringResourceProvider. const char kTestGetSourcePage[] = "get_source.html"; @@ -102,18 +102,18 @@ void RunRequestTest(CefRefPtr browser) { // Create a new request CefRefPtr request(CefRequest::Create()); - if (browser->GetMainFrame()->GetURL().ToString().find("http://tests/") != 0) { + if (browser->GetMainFrame()->GetURL().ToString().find(kTestOrigin) != 0) { // The LoadRequest method will fail with "bad IPC message" reason // INVALID_INITIATOR_ORIGIN (213) unless you first navigate to the // request origin using some other mechanism (LoadURL, link click, etc). - Alert(browser, - "Please first navigate to a http://tests/ URL. " - "For example, first load Tests > Other Tests."); + Alert(browser, "Please first navigate to a " + std::string(kTestOrigin) + + " URL. " + "For example, first load Tests > Other Tests."); return; } // Set the request URL - request->SetURL("http://tests/request"); + request->SetURL(GetTestURL("request")); // Add post data to the request. The correct method and content- // type headers will be set by CEF. @@ -419,7 +419,7 @@ void MuteAudio(CefRefPtr browser, bool mute) { } void RunOtherTests(CefRefPtr browser) { - browser->GetMainFrame()->LoadURL("http://tests/other_tests"); + browser->GetMainFrame()->LoadURL(GetTestURL("other_tests")); } // Provider that dumps the request contents. @@ -679,7 +679,7 @@ CefRefPtr GetDumpResponse( } if (!origin.empty() && - (origin.find("http://" + std::string(kTestHost)) == 0 || + (origin.find("https://" + std::string(kTestHost)) == 0 || origin.find("http://" + std::string(kLocalHost)) == 0)) { // Allow cross-origin XMLHttpRequests from test origins. response_headers.insert( @@ -832,6 +832,10 @@ void Alert(CefRefPtr browser, const std::string& message) { frame->ExecuteJavaScript("alert('" + msg + "');", frame->GetURL(), 0); } +std::string GetTestURL(const std::string& path) { + return kTestOrigin + path; +} + bool IsTestURL(const std::string& url, const std::string& path) { CefURLParts parts; CefParseURL(url, parts); diff --git a/tests/cefclient/browser/test_runner.h b/tests/cefclient/browser/test_runner.h index ed0172dd8..5b6f01052 100644 --- a/tests/cefclient/browser/test_runner.h +++ b/tests/cefclient/browser/test_runner.h @@ -44,8 +44,11 @@ void SetupResourceManager(CefRefPtr resource_manager, // Show a JS alert message. void Alert(CefRefPtr browser, const std::string& message); +// Returns "https://tests/". +std::string GetTestURL(const std::string& path); + // Returns true if |url| is a test URL with the specified |path|. This matches -// both http://tests/ and http://localhost:xxxx/. +// both "https://tests/" and "http://localhost:xxxx/". bool IsTestURL(const std::string& url, const std::string& path); // Create all CefMessageRouterBrowserSide::Handler objects. They will be diff --git a/tests/cefclient/resources/server.html b/tests/cefclient/resources/server.html index 9e49e062e..676278975 100644 --- a/tests/cefclient/resources/server.html +++ b/tests/cefclient/resources/server.html @@ -24,7 +24,7 @@ function setButtonState(start_enabled, stop_enabled) { } function setup() { - if (location.origin != 'http://tests') { + if (location.origin != 'https://tests') { document.getElementById('warning').style.display = 'block'; return; } @@ -84,7 +84,7 @@ function openServer() {

This page starts an HTTP/WebSocket server on localhost with the specified port number. diff --git a/tests/cefclient/resources/websocket.html b/tests/cefclient/resources/websocket.html index e13abc0cf..91f35f2ed 100644 --- a/tests/cefclient/resources/websocket.html +++ b/tests/cefclient/resources/websocket.html @@ -87,7 +87,7 @@ function doSend() {

This page tests a WebSocket connection. diff --git a/tests/cefclient/resources/xmlhttprequest.html b/tests/cefclient/resources/xmlhttprequest.html index 8da20f3dd..655f48af4 100644 --- a/tests/cefclient/resources/xmlhttprequest.html +++ b/tests/cefclient/resources/xmlhttprequest.html @@ -30,7 +30,7 @@ function execXMLHttpRequest() }

-URL: +URL: