2012-06-19 18:29:49 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that
|
|
|
|
// can be found in the LICENSE file.
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
#include <algorithm>
|
2012-06-19 18:29:49 +02:00
|
|
|
#include <map>
|
2021-06-19 21:54:45 +02:00
|
|
|
#include <memory>
|
2012-06-19 18:29:49 +02:00
|
|
|
#include <sstream>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2019-07-12 22:44:43 +02:00
|
|
|
#include "include/cef_parser.h"
|
2019-04-24 04:50:25 +02:00
|
|
|
#include "include/cef_request_context_handler.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/cef_scheme.h"
|
2017-11-17 17:33:50 +01:00
|
|
|
#include "include/cef_server.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/cef_task.h"
|
|
|
|
#include "include/cef_urlrequest.h"
|
2016-11-15 22:18:41 +01:00
|
|
|
#include "include/cef_waitable_event.h"
|
2024-06-01 04:53:09 +02:00
|
|
|
#include "include/test/cef_test_helpers.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-15 18:56:02 +01:00
|
|
|
#include "include/wrapper/cef_scoped_temp_dir.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
2020-08-20 00:27:27 +02:00
|
|
|
#include "tests/ceftests/test_request.h"
|
2022-07-31 01:53:48 +02:00
|
|
|
#include "tests/ceftests/test_server_observer.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_suite.h"
|
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
2020-09-25 03:40:47 +02:00
|
|
|
#include "tests/shared/browser/client_app_browser.h"
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "tests/shared/browser/file_util.h"
|
2022-08-02 22:30:37 +02:00
|
|
|
#include "tests/shared/common/string_util.h"
|
2015-01-23 20:09:34 +01:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// How to add a new test:
|
|
|
|
// 1. Add a new value to the RequestTestMode enumeration.
|
|
|
|
// 2. Add methods to set up and run the test in RequestTestRunner.
|
|
|
|
// 3. Add a line for the test in the RequestTestRunner constructor.
|
|
|
|
// 4. Add lines for the test in the "Define the tests" section at the bottom of
|
|
|
|
// the file.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
// Browser-side app delegate.
|
|
|
|
class URLRequestBrowserTest : public client::ClientAppBrowser::Delegate {
|
|
|
|
public:
|
2024-01-20 18:00:09 +01:00
|
|
|
URLRequestBrowserTest() = default;
|
2020-09-25 03:40:47 +02:00
|
|
|
|
|
|
|
void OnBeforeCommandLineProcessing(
|
|
|
|
CefRefPtr<client::ClientAppBrowser> app,
|
|
|
|
CefRefPtr<CefCommandLine> command_line) override {
|
2024-06-26 02:12:37 +02:00
|
|
|
// Delegate auth callbacks to GetAuthCredentials.
|
2020-09-25 03:40:47 +02:00
|
|
|
command_line->AppendSwitch("disable-chrome-login-prompt");
|
2021-04-07 00:09:45 +02:00
|
|
|
|
|
|
|
// Disable component extensions that require creation of a background
|
|
|
|
// WebContents because they slow down test runs.
|
|
|
|
command_line->AppendSwitch(
|
|
|
|
"disable-component-extensions-with-background-pages");
|
2020-09-25 03:40:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(URLRequestBrowserTest);
|
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// TEST DATA
|
|
|
|
|
|
|
|
// Custom scheme handler backend.
|
|
|
|
const char kRequestSchemeCustom[] = "urcustom";
|
|
|
|
const char kRequestHostCustom[] = "test";
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Server backend.
|
2022-07-31 03:17:40 +02:00
|
|
|
const char* kRequestAddressServer = test_server::kHttpServerAddress;
|
2023-06-01 16:06:15 +02:00
|
|
|
const uint16_t kRequestPortServer = test_server::kHttpServerPort;
|
2022-07-31 03:17:40 +02:00
|
|
|
const char kRequestSchemeServer[] = "http";
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
const char kRequestSendCookieName[] = "urcookie_send";
|
|
|
|
const char kRequestSaveCookieName[] = "urcookie_save";
|
|
|
|
|
|
|
|
const char kCacheControlHeader[] = "cache-control";
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
enum RequestTestMode {
|
|
|
|
REQTEST_GET = 0,
|
|
|
|
REQTEST_GET_NODATA,
|
2020-04-20 19:19:42 +02:00
|
|
|
REQTEST_GET_PARTIAL_CONTENT,
|
2012-06-19 18:29:49 +02:00
|
|
|
REQTEST_GET_ALLOWCOOKIES,
|
|
|
|
REQTEST_GET_REDIRECT,
|
2018-04-10 19:04:57 +02:00
|
|
|
REQTEST_GET_REDIRECT_STOP,
|
2019-01-11 16:12:06 +01:00
|
|
|
REQTEST_GET_REDIRECT_LOCATION,
|
2015-12-01 19:22:28 +01:00
|
|
|
REQTEST_GET_REFERRER,
|
2019-07-12 22:44:43 +02:00
|
|
|
REQTEST_GET_AUTH,
|
2012-06-19 18:29:49 +02:00
|
|
|
REQTEST_POST,
|
2013-11-25 18:31:59 +01:00
|
|
|
REQTEST_POST_FILE,
|
2012-06-19 18:29:49 +02:00
|
|
|
REQTEST_POST_WITHPROGRESS,
|
2019-07-11 22:36:27 +02:00
|
|
|
REQTEST_POST_REDIRECT,
|
|
|
|
REQTEST_POST_REDIRECT_TOGET,
|
2012-06-19 18:29:49 +02:00
|
|
|
REQTEST_HEAD,
|
2017-11-17 17:33:50 +01:00
|
|
|
REQTEST_CACHE_WITH_CONTROL,
|
|
|
|
REQTEST_CACHE_WITHOUT_CONTROL,
|
|
|
|
REQTEST_CACHE_SKIP_FLAG,
|
|
|
|
REQTEST_CACHE_SKIP_HEADER,
|
|
|
|
REQTEST_CACHE_ONLY_FAILURE_FLAG,
|
|
|
|
REQTEST_CACHE_ONLY_FAILURE_HEADER,
|
|
|
|
REQTEST_CACHE_ONLY_SUCCESS_FLAG,
|
|
|
|
REQTEST_CACHE_ONLY_SUCCESS_HEADER,
|
2019-03-07 23:05:36 +01:00
|
|
|
REQTEST_CACHE_DISABLE_FLAG,
|
|
|
|
REQTEST_CACHE_DISABLE_HEADER,
|
2019-05-31 11:50:12 +02:00
|
|
|
REQTEST_INCOMPLETE_PROCESS_REQUEST,
|
|
|
|
REQTEST_INCOMPLETE_READ_RESPONSE,
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
enum ContextTestMode {
|
|
|
|
CONTEXT_GLOBAL,
|
|
|
|
CONTEXT_INMEMORY,
|
|
|
|
CONTEXT_ONDISK,
|
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Defines test expectations for a request.
|
2012-06-19 18:29:49 +02:00
|
|
|
struct RequestRunSettings {
|
2024-01-20 18:00:09 +01:00
|
|
|
RequestRunSettings() = default;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Set expectations for request failure.
|
|
|
|
void SetRequestFailureExpected(cef_errorcode_t error_code) {
|
|
|
|
expect_upload_progress = false;
|
|
|
|
expect_download_progress = false;
|
|
|
|
expect_download_data = false;
|
|
|
|
expected_status = UR_FAILED;
|
|
|
|
expected_error_code = error_code;
|
|
|
|
response = nullptr;
|
|
|
|
response_data.clear();
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Request that will be sent.
|
|
|
|
CefRefPtr<CefRequest> request;
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Response that will be returned by the backend.
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefResponse> response;
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Optional response data that will be returned by the backend.
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string response_data;
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Create an incomplete request to test shutdown behavior.
|
|
|
|
enum IncompleteType {
|
|
|
|
INCOMPLETE_NONE,
|
|
|
|
INCOMPLETE_PROCESS_REQUEST,
|
|
|
|
INCOMPLETE_READ_RESPONSE,
|
|
|
|
};
|
|
|
|
IncompleteType incomplete_type = INCOMPLETE_NONE;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// If true upload progress notification will be expected.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_upload_progress = false;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// If true download progress notification will be expected.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_download_progress = true;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// If true download data will be expected.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_download_data = true;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2020-04-20 19:19:42 +02:00
|
|
|
// The offset from what we passed that we expect to receive.
|
|
|
|
size_t expected_download_offset = 0;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Expected status value.
|
2019-05-31 11:50:12 +02:00
|
|
|
CefURLRequest::Status expected_status = UR_SUCCESS;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Expected error code value.
|
2019-05-31 11:50:12 +02:00
|
|
|
CefURLRequest::ErrorCode expected_error_code = ERR_NONE;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// If true the request cookie should be sent to the server.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_send_cookie = false;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// If true the response cookie should be saved.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_save_cookie = false;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
// If true the test will begin by requiring Basic authentication and then
|
|
|
|
// continue with the actual request. The UR_FLAG_ALLOW_STORED_CREDENTIALS
|
|
|
|
// flag must be set on the request. When using the global request context
|
|
|
|
// CefRequestContext::ClearHttpAuthCredentials should be called to avoid
|
|
|
|
// leaking state across test runs. Authentication is only supported with
|
|
|
|
// browser-initiated requests and the server backend.
|
|
|
|
bool expect_authentication = false;
|
|
|
|
std::string username;
|
|
|
|
std::string password;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// If specified the test will begin with this redirect request and response.
|
|
|
|
CefRefPtr<CefRequest> redirect_request;
|
|
|
|
CefRefPtr<CefResponse> redirect_response;
|
|
|
|
|
|
|
|
// If true the redirect is expected to be followed.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_follow_redirect = true;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2018-01-03 22:14:29 +01:00
|
|
|
// If true the response is expected to be served from cache.
|
2019-05-31 11:50:12 +02:00
|
|
|
bool expect_response_was_cached = false;
|
2018-01-03 22:14:29 +01:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// The expected number of requests to send, or -1 if unspecified.
|
|
|
|
// Used only with the server backend.
|
2019-05-31 11:50:12 +02:00
|
|
|
int expected_send_count = -1;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// The expected number of requests to receive, or -1 if unspecified.
|
|
|
|
// Used only with the server backend.
|
2019-05-31 11:50:12 +02:00
|
|
|
int expected_receive_count = -1;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
using NextRequestCallback =
|
|
|
|
base::OnceCallback<void(int /* next_send_count */,
|
|
|
|
base::OnceClosure /* complete_callback */)>;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// If non-null this callback will be executed before subsequent requests are
|
|
|
|
// sent.
|
|
|
|
NextRequestCallback setup_next_request;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Manages the map of request URL to test expectations.
|
|
|
|
class RequestDataMap {
|
|
|
|
public:
|
|
|
|
struct Entry {
|
|
|
|
enum Type {
|
|
|
|
TYPE_UNKNOWN,
|
|
|
|
TYPE_NORMAL,
|
|
|
|
TYPE_REDIRECT,
|
|
|
|
};
|
|
|
|
|
2024-01-20 18:00:09 +01:00
|
|
|
explicit Entry(Type entry_type) : type(entry_type) {}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
Type type;
|
|
|
|
|
|
|
|
// Used with TYPE_NORMAL.
|
|
|
|
// |settings| is not owned by this object.
|
2024-01-20 18:00:09 +01:00
|
|
|
RequestRunSettings* settings = nullptr;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Used with TYPE_REDIRECT.
|
|
|
|
CefRefPtr<CefRequest> redirect_request;
|
|
|
|
CefRefPtr<CefResponse> redirect_response;
|
|
|
|
};
|
|
|
|
|
|
|
|
RequestDataMap() : owner_task_runner_(CefTaskRunner::GetForCurrentThread()) {}
|
|
|
|
|
|
|
|
// Pass ownership to the specified |task_runner| thread.
|
|
|
|
// If |task_runner| is nullptr the test is considered destroyed.
|
|
|
|
void SetOwnerTaskRunner(CefRefPtr<CefTaskRunner> task_runner) {
|
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
owner_task_runner_ = task_runner;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSchemeHandler(RequestRunSettings* settings) {
|
|
|
|
EXPECT_TRUE(settings);
|
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
|
|
|
const std::string& url = settings->request->GetURL();
|
|
|
|
data_map_.insert(std::make_pair(url, settings));
|
|
|
|
|
|
|
|
if (settings->redirect_request) {
|
|
|
|
const std::string& redirect_url = settings->redirect_request->GetURL();
|
|
|
|
redirect_data_map_.insert(std::make_pair(
|
|
|
|
redirect_url, std::make_pair(settings->redirect_request,
|
|
|
|
settings->redirect_response)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry Find(const std::string& url) {
|
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
|
|
|
Entry entry(Entry::TYPE_UNKNOWN);
|
|
|
|
|
|
|
|
// Try to find a test match.
|
|
|
|
{
|
|
|
|
DataMap::const_iterator it = data_map_.find(url);
|
|
|
|
if (it != data_map_.end()) {
|
|
|
|
entry.type = Entry::TYPE_NORMAL;
|
|
|
|
entry.settings = it->second;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a redirect match.
|
|
|
|
{
|
|
|
|
RedirectDataMap::const_iterator it = redirect_data_map_.find(url);
|
|
|
|
if (it != redirect_data_map_.end()) {
|
|
|
|
entry.type = Entry::TYPE_REDIRECT;
|
|
|
|
entry.redirect_request = it->second.first;
|
|
|
|
entry.redirect_response = it->second.second;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE() << "url: " << url;
|
|
|
|
return entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t size() const { return data_map_.size() + redirect_data_map_.size(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefTaskRunner> owner_task_runner_;
|
|
|
|
|
|
|
|
// The below members are only accessed on the |owner_task_runner_| thread.
|
|
|
|
|
|
|
|
// RequestRunSettings pointer is not owned by this object.
|
|
|
|
typedef std::map<std::string, RequestRunSettings*> DataMap;
|
|
|
|
DataMap data_map_;
|
|
|
|
|
|
|
|
typedef std::map<std::string,
|
|
|
|
std::pair<CefRefPtr<CefRequest>, CefRefPtr<CefResponse>>>
|
|
|
|
RedirectDataMap;
|
|
|
|
RedirectDataMap redirect_data_map_;
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
class TestCompletionCallback : public CefCompletionCallback {
|
|
|
|
public:
|
2021-06-19 21:54:45 +02:00
|
|
|
explicit TestCompletionCallback(base::OnceClosure complete_callback)
|
|
|
|
: complete_callback_(std::move(complete_callback)) {
|
2019-07-12 22:44:43 +02:00
|
|
|
EXPECT_FALSE(complete_callback_.is_null());
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void OnComplete() override { std::move(complete_callback_).Run(); }
|
2019-07-12 22:44:43 +02:00
|
|
|
|
|
|
|
private:
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback_;
|
2019-07-12 22:44:43 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TestCompletionCallback);
|
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
std::string GetRequestScheme(bool server_backend) {
|
|
|
|
return server_backend ? kRequestSchemeServer : kRequestSchemeCustom;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetRequestHost(bool server_backend, bool with_port) {
|
|
|
|
if (server_backend) {
|
|
|
|
if (with_port) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << kRequestAddressServer << ":" << kRequestPortServer;
|
|
|
|
return ss.str();
|
|
|
|
} else {
|
|
|
|
return kRequestAddressServer;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return kRequestHostCustom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GetRequestOrigin(bool server_backend) {
|
|
|
|
return GetRequestScheme(server_backend) + "://" +
|
|
|
|
GetRequestHost(server_backend, true);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void SetUploadData(CefRefPtr<CefRequest> request, const std::string& data) {
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData = CefPostData::Create();
|
|
|
|
CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
|
|
|
|
element->SetToBytes(data.size(), data.c_str());
|
|
|
|
postData->AddElement(element);
|
|
|
|
request->SetPostData(postData);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void SetUploadFile(CefRefPtr<CefRequest> request, const std::string& file) {
|
2013-11-25 18:31:59 +01:00
|
|
|
CefRefPtr<CefPostData> postData = CefPostData::Create();
|
|
|
|
CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
|
2016-11-15 18:56:02 +01:00
|
|
|
element->SetToFile(file);
|
2013-11-25 18:31:59 +01:00
|
|
|
postData->AddElement(element);
|
|
|
|
request->SetPostData(postData);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void GetUploadData(CefRefPtr<CefRequest> request, std::string& data) {
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData = request->GetPostData();
|
|
|
|
EXPECT_TRUE(postData.get());
|
|
|
|
CefPostData::ElementVector elements;
|
|
|
|
postData->GetElements(elements);
|
|
|
|
EXPECT_EQ((size_t)1, elements.size());
|
|
|
|
CefRefPtr<CefPostDataElement> element = elements[0];
|
|
|
|
EXPECT_TRUE(element.get());
|
|
|
|
|
|
|
|
size_t size = element->GetBytesCount();
|
|
|
|
|
|
|
|
data.resize(size);
|
|
|
|
EXPECT_EQ(size, data.size());
|
|
|
|
EXPECT_EQ(size, element->GetBytes(size, const_cast<char*>(data.c_str())));
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Set a cookie so that we can test if it's sent with the request.
|
2017-11-17 17:33:50 +01:00
|
|
|
void SetTestCookie(CefRefPtr<CefRequestContext> request_context,
|
2018-03-20 21:15:08 +01:00
|
|
|
bool server_backend,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure callback) {
|
2015-03-02 21:25:14 +01:00
|
|
|
class Callback : public CefSetCookieCallback {
|
|
|
|
public:
|
2021-06-19 21:54:45 +02:00
|
|
|
explicit Callback(base::OnceClosure callback)
|
|
|
|
: callback_(std::move(callback)) {
|
2018-03-20 21:15:08 +01:00
|
|
|
EXPECT_FALSE(callback_.is_null());
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
void OnComplete(bool success) override {
|
|
|
|
EXPECT_TRUE(success);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(callback_).Run();
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure callback_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
CefCookie cookie;
|
|
|
|
CefString(&cookie.name) = kRequestSendCookieName;
|
|
|
|
CefString(&cookie.value) = "send-cookie-value";
|
2017-11-17 17:33:50 +01:00
|
|
|
CefString(&cookie.domain) = GetRequestHost(server_backend, false);
|
2015-03-02 21:25:14 +01:00
|
|
|
CefString(&cookie.path) = "/";
|
|
|
|
cookie.has_expires = false;
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(request_context->GetCookieManager(nullptr)->SetCookie(
|
2021-06-19 21:54:45 +02:00
|
|
|
GetRequestOrigin(server_backend), cookie,
|
|
|
|
new Callback(std::move(callback))));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
using GetTestCookieCallback =
|
|
|
|
base::OnceCallback<void(bool /* cookie exists */)>;
|
2018-03-20 21:15:08 +01:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Tests if the save cookie has been set. If set, it will be deleted at the same
|
|
|
|
// time.
|
2015-03-02 21:25:14 +01:00
|
|
|
void GetTestCookie(CefRefPtr<CefRequestContext> request_context,
|
2018-03-20 21:15:08 +01:00
|
|
|
bool server_backend,
|
2021-06-19 21:54:45 +02:00
|
|
|
GetTestCookieCallback callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
class Visitor : public CefCookieVisitor {
|
|
|
|
public:
|
2021-06-19 21:54:45 +02:00
|
|
|
explicit Visitor(GetTestCookieCallback callback)
|
2024-01-20 18:00:09 +01:00
|
|
|
: callback_(std::move(callback)) {
|
2018-03-20 21:15:08 +01:00
|
|
|
EXPECT_FALSE(callback_.is_null());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
2021-06-19 21:54:45 +02:00
|
|
|
~Visitor() override { std::move(callback_).Run(cookie_exists_); }
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool Visit(const CefCookie& cookie,
|
|
|
|
int count,
|
|
|
|
int total,
|
|
|
|
bool& deleteCookie) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string cookie_name = CefString(&cookie.name);
|
|
|
|
if (cookie_name == kRequestSaveCookieName) {
|
2018-03-20 21:15:08 +01:00
|
|
|
cookie_exists_ = true;
|
2012-06-19 18:29:49 +02:00
|
|
|
deleteCookie = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-03-20 21:15:08 +01:00
|
|
|
GetTestCookieCallback callback_;
|
2024-01-20 18:00:09 +01:00
|
|
|
bool cookie_exists_ = false;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
|
|
|
};
|
|
|
|
|
|
|
|
CefRefPtr<CefCookieManager> cookie_manager =
|
2020-01-15 14:34:01 +01:00
|
|
|
request_context->GetCookieManager(nullptr);
|
2017-11-17 17:33:50 +01:00
|
|
|
cookie_manager->VisitUrlCookies(GetRequestOrigin(server_backend), true,
|
2021-06-19 21:54:45 +02:00
|
|
|
new Visitor(std::move(callback)));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
std::string GetHeaderValue(const CefRequest::HeaderMap& header_map,
|
|
|
|
const std::string& header_name_lower) {
|
|
|
|
CefRequest::HeaderMap::const_iterator it = header_map.begin();
|
|
|
|
for (; it != header_map.end(); ++it) {
|
2022-08-02 22:30:37 +02:00
|
|
|
std::string name = client::AsciiStrToLower(it->first);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (name == header_name_lower) {
|
2017-11-17 17:33:50 +01:00
|
|
|
return it->second;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify normal request expectations.
|
|
|
|
void VerifyNormalRequest(const RequestRunSettings* settings,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
bool server_backend) {
|
|
|
|
// Shouldn't get here if we're not following redirects.
|
|
|
|
EXPECT_TRUE(settings->expect_follow_redirect);
|
|
|
|
|
|
|
|
// Verify that the request was sent correctly.
|
|
|
|
TestRequestEqual(settings->request, request, true);
|
|
|
|
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
request->GetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
// Check if the default headers were sent.
|
|
|
|
EXPECT_FALSE(GetHeaderValue(headerMap, "user-agent").empty());
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
// CEF_SETTINGS_ACCEPT_LANGUAGE value from CefSettings.accept_language_list
|
|
|
|
// set in CefTestSuite::GetSettings() and expanded internally by
|
|
|
|
// ComputeAcceptLanguageFromPref.
|
2024-06-01 04:53:09 +02:00
|
|
|
const std::string& accept_language =
|
|
|
|
GetHeaderValue(headerMap, "accept-language");
|
|
|
|
if (CefIsFeatureEnabledForTests("ReduceAcceptLanguage")) {
|
|
|
|
EXPECT_TRUE(accept_language == "en-GB" ||
|
|
|
|
accept_language == "en-GB,en;q=0.9")
|
|
|
|
<< accept_language;
|
|
|
|
} else {
|
|
|
|
EXPECT_STREQ("en-GB,en;q=0.9", accept_language.data());
|
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
if (server_backend) {
|
|
|
|
EXPECT_FALSE(GetHeaderValue(headerMap, "accept-encoding").empty());
|
|
|
|
EXPECT_STREQ(GetRequestHost(true, true).c_str(),
|
|
|
|
GetHeaderValue(headerMap, "host").c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the request cookie was sent.
|
|
|
|
const std::string& cookie_value = GetHeaderValue(headerMap, "cookie");
|
|
|
|
bool has_send_cookie = false;
|
|
|
|
if (!cookie_value.empty() &&
|
|
|
|
cookie_value.find(kRequestSendCookieName) != std::string::npos) {
|
|
|
|
has_send_cookie = true;
|
|
|
|
}
|
|
|
|
|
2020-04-24 23:48:16 +02:00
|
|
|
EXPECT_EQ(settings->expect_send_cookie, has_send_cookie);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Populate normal response contents.
|
|
|
|
void GetNormalResponse(const RequestRunSettings* settings,
|
|
|
|
CefRefPtr<CefResponse> response) {
|
|
|
|
EXPECT_TRUE(settings->response);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!settings->response) {
|
2017-11-17 17:33:50 +01:00
|
|
|
return;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
response->SetStatus(settings->response->GetStatus());
|
|
|
|
response->SetStatusText(settings->response->GetStatusText());
|
|
|
|
response->SetMimeType(settings->response->GetMimeType());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
settings->response->GetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
if (settings->expect_save_cookie) {
|
|
|
|
std::stringstream ss;
|
2024-01-20 18:00:09 +01:00
|
|
|
ss << kRequestSaveCookieName << "=" << "save-cookie-value";
|
2017-11-17 17:33:50 +01:00
|
|
|
headerMap.insert(std::make_pair("Set-Cookie", ss.str()));
|
|
|
|
}
|
|
|
|
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
}
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
// Based on https://en.wikipedia.org/wiki/Basic_access_authentication#Protocol
|
|
|
|
void GetAuthResponse(CefRefPtr<CefResponse> response) {
|
|
|
|
response->SetStatus(401);
|
|
|
|
response->SetStatusText("Unauthorized");
|
|
|
|
response->SetMimeType("text/html");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(
|
|
|
|
std::make_pair("WWW-Authenticate", "Basic realm=\"Test Realm\""));
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAuthorized(CefRefPtr<CefRequest> request,
|
|
|
|
const std::string& username,
|
|
|
|
const std::string& password) {
|
|
|
|
const std::string& authHeader = request->GetHeaderByName("Authorization");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (authHeader.empty()) {
|
2019-07-12 22:44:43 +02:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-07-12 22:44:43 +02:00
|
|
|
|
|
|
|
if (authHeader.find("Basic ") == 0) {
|
|
|
|
const std::string& base64 = authHeader.substr(6);
|
|
|
|
CefRefPtr<CefBinaryValue> data = CefBase64Decode(base64);
|
|
|
|
EXPECT_TRUE(data);
|
|
|
|
if (!data) {
|
|
|
|
LOG(ERROR) << "Failed to decode Authorization value: " << base64;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string decoded;
|
|
|
|
decoded.resize(data->GetSize());
|
|
|
|
data->GetData(&decoded[0], data->GetSize(), 0);
|
|
|
|
|
|
|
|
const std::string& expected = username + ":" + password;
|
|
|
|
EXPECT_STREQ(expected.c_str(), decoded.c_str());
|
|
|
|
return decoded == expected;
|
|
|
|
}
|
|
|
|
|
|
|
|
LOG(ERROR) << "Unexpected Authorization value: " << authHeader;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// SCHEME HANDLER BACKEND
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Serves request responses.
|
2019-12-31 13:24:10 +01:00
|
|
|
class RequestSchemeHandlerOld : public CefResourceHandler {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
2019-12-31 13:24:10 +01:00
|
|
|
RequestSchemeHandlerOld(RequestRunSettings* settings,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
|
|
|
: settings_(settings), destroy_callback_(std::move(destroy_callback)) {}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
~RequestSchemeHandlerOld() override {
|
2019-07-29 23:27:12 +02:00
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2017-11-17 17:33:50 +01:00
|
|
|
VerifyNormalRequest(settings_, request, false);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// HEAD requests are identical to GET requests except no response data is
|
|
|
|
// sent.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (request->GetMethod() != "HEAD") {
|
2017-11-17 17:33:50 +01:00
|
|
|
response_data_ = settings_->response_data;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Continue immediately.
|
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefString& redirectUrl) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2017-11-17 17:33:50 +01:00
|
|
|
GetNormalResponse(settings_, response);
|
|
|
|
response_length = response_data_.length();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
bool ReadResponse(void* data_out,
|
2014-11-12 20:25:15 +01:00
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
bool has_data = false;
|
|
|
|
bytes_read = 0;
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
size_t size = response_data_.length();
|
2012-06-19 18:29:49 +02:00
|
|
|
if (offset_ < size) {
|
|
|
|
int transfer_size =
|
|
|
|
std::min(bytes_to_read, static_cast<int>(size - offset_));
|
2019-12-31 13:24:10 +01:00
|
|
|
memcpy(data_out, response_data_.c_str() + offset_, transfer_size);
|
2012-06-19 18:29:49 +02:00
|
|
|
offset_ += transfer_size;
|
|
|
|
|
|
|
|
bytes_read = transfer_size;
|
|
|
|
has_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_data;
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
private:
|
|
|
|
// |settings_| is not owned by this object.
|
|
|
|
RequestRunSettings* settings_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
std::string response_data_;
|
|
|
|
size_t offset_ = 0;
|
|
|
|
|
|
|
|
int cancel_ct_ = 0;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestSchemeHandlerOld);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RequestSchemeHandlerOld);
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestSchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
RequestSchemeHandler(RequestRunSettings* settings,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
|
|
|
: settings_(settings), destroy_callback_(std::move(destroy_callback)) {}
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
~RequestSchemeHandler() override {
|
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Open(CefRefPtr<CefRequest> request,
|
|
|
|
bool& handle_request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO));
|
|
|
|
VerifyNormalRequest(settings_, request, false);
|
|
|
|
|
|
|
|
// HEAD requests are identical to GET requests except no response data is
|
|
|
|
// sent.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (request->GetMethod() != "HEAD") {
|
2019-12-31 13:24:10 +01:00
|
|
|
response_data_ = settings_->response_data;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
// Continue immediately.
|
|
|
|
handle_request = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2019-12-31 13:24:10 +01:00
|
|
|
CefString& redirectUrl) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
GetNormalResponse(settings_, response);
|
2020-04-20 19:19:42 +02:00
|
|
|
response_length = response_data_.length() - offset_;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
bool Skip(int64_t bytes_to_skip,
|
|
|
|
int64_t& bytes_skipped,
|
2020-04-20 19:19:42 +02:00
|
|
|
CefRefPtr<CefResourceSkipCallback> callback) override {
|
|
|
|
size_t size = response_data_.length();
|
|
|
|
if (offset_ < size) {
|
|
|
|
bytes_skipped =
|
2023-06-01 16:06:15 +02:00
|
|
|
std::min(bytes_to_skip, static_cast<int64_t>(size - offset_));
|
2020-04-20 19:19:42 +02:00
|
|
|
offset_ += bytes_skipped;
|
|
|
|
} else {
|
|
|
|
bytes_skipped = ERR_FAILED;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_skipped > 0;
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefResourceReadCallback> callback) override {
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
// Default to response complete.
|
|
|
|
bool has_data = false;
|
|
|
|
bytes_read = 0;
|
|
|
|
|
|
|
|
size_t size = response_data_.length();
|
|
|
|
if (offset_ < size) {
|
|
|
|
int transfer_size =
|
|
|
|
std::min(bytes_to_read, static_cast<int>(size - offset_));
|
|
|
|
memcpy(data_out, response_data_.c_str() + offset_, transfer_size);
|
|
|
|
offset_ += transfer_size;
|
|
|
|
|
|
|
|
bytes_read = transfer_size;
|
|
|
|
has_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_data;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
bytes_read = -2;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
private:
|
2017-11-17 17:33:50 +01:00
|
|
|
// |settings_| is not owned by this object.
|
|
|
|
RequestRunSettings* settings_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
std::string response_data_;
|
2019-05-31 11:50:12 +02:00
|
|
|
size_t offset_ = 0;
|
|
|
|
|
|
|
|
int cancel_ct_ = 0;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestSchemeHandler);
|
2019-12-31 13:24:10 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(RequestSchemeHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Serves redirect request responses.
|
2019-12-31 13:24:10 +01:00
|
|
|
class RequestRedirectSchemeHandlerOld : public CefResourceHandler {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
2019-12-31 13:24:10 +01:00
|
|
|
RequestRedirectSchemeHandlerOld(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
2019-05-31 11:50:12 +02:00
|
|
|
: request_(request),
|
|
|
|
response_(response),
|
2021-06-19 21:54:45 +02:00
|
|
|
destroy_callback_(std::move(destroy_callback)) {}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
~RequestRedirectSchemeHandlerOld() override {
|
2019-07-29 23:27:12 +02:00
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Verify that the request was sent correctly.
|
|
|
|
TestRequestEqual(request_, request, true);
|
|
|
|
|
|
|
|
// Continue immediately.
|
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefString& redirectUrl) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
response->SetStatus(response_->GetStatus());
|
|
|
|
response->SetStatusText(response_->GetStatusText());
|
|
|
|
response->SetMimeType(response_->GetMimeType());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
response_->GetHeaderMap(headerMap);
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
response_length = 0;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ReadResponse(void* response_data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
2012-06-19 18:29:49 +02:00
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
CefRefPtr<CefResponse> response_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
int cancel_ct_ = 0;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestRedirectSchemeHandlerOld);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(RequestRedirectSchemeHandlerOld);
|
|
|
|
};
|
|
|
|
|
|
|
|
class RequestRedirectSchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
RequestRedirectSchemeHandler(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
2019-12-31 13:24:10 +01:00
|
|
|
: request_(request),
|
|
|
|
response_(response),
|
2021-06-19 21:54:45 +02:00
|
|
|
destroy_callback_(std::move(destroy_callback)) {}
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
~RequestRedirectSchemeHandler() override {
|
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Open(CefRefPtr<CefRequest> request,
|
|
|
|
bool& handle_request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
// Verify that the request was sent correctly.
|
|
|
|
TestRequestEqual(request_, request, true);
|
|
|
|
|
|
|
|
// Continue immediately.
|
|
|
|
handle_request = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2019-12-31 13:24:10 +01:00
|
|
|
CefString& redirectUrl) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
response->SetStatus(response_->GetStatus());
|
|
|
|
response->SetStatusText(response_->GetStatusText());
|
|
|
|
response->SetMimeType(response_->GetMimeType());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
response_->GetHeaderMap(headerMap);
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
response_length = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefResourceReadCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
bytes_read = -1;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
bytes_read = -2;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
private:
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
CefRefPtr<CefResponse> response_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
int cancel_ct_ = 0;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestRedirectSchemeHandler);
|
2019-12-31 13:24:10 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(RequestRedirectSchemeHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Resource handler implementation that never completes. Used to test
|
|
|
|
// destruction handling behavior for in-progress requests.
|
2019-12-31 13:24:10 +01:00
|
|
|
class IncompleteSchemeHandlerOld : public CefResourceHandler {
|
2019-05-31 11:50:12 +02:00
|
|
|
public:
|
2019-12-31 13:24:10 +01:00
|
|
|
IncompleteSchemeHandlerOld(RequestRunSettings* settings,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
|
|
|
: settings_(settings), destroy_callback_(std::move(destroy_callback)) {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_NE(settings_->incomplete_type, RequestRunSettings::INCOMPLETE_NONE);
|
|
|
|
}
|
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
~IncompleteSchemeHandlerOld() override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_EQ(1, process_request_ct_);
|
2019-07-29 23:27:12 +02:00
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
if (settings_->incomplete_type ==
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE) {
|
|
|
|
EXPECT_EQ(1, get_response_headers_ct_);
|
|
|
|
EXPECT_EQ(1, read_response_ct_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, get_response_headers_ct_);
|
|
|
|
EXPECT_EQ(0, read_response_ct_);
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
process_request_ct_++;
|
|
|
|
|
|
|
|
if (settings_->incomplete_type ==
|
|
|
|
RequestRunSettings::INCOMPLETE_PROCESS_REQUEST) {
|
|
|
|
// Never release or execute this callback.
|
|
|
|
incomplete_callback_ = callback;
|
|
|
|
} else {
|
|
|
|
callback->Continue();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2019-05-31 11:50:12 +02:00
|
|
|
CefString& redirectUrl) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
EXPECT_EQ(settings_->incomplete_type,
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE);
|
|
|
|
|
|
|
|
get_response_headers_ct_++;
|
|
|
|
|
|
|
|
response->SetStatus(settings_->response->GetStatus());
|
|
|
|
response->SetStatusText(settings_->response->GetStatusText());
|
|
|
|
response->SetMimeType(settings_->response->GetMimeType());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
settings_->response->GetHeaderMap(headerMap);
|
|
|
|
settings_->response->SetHeaderMap(headerMap);
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
response_length = static_cast<int64_t>(settings_->response_data.size());
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
EXPECT_EQ(settings_->incomplete_type,
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE);
|
|
|
|
|
|
|
|
read_response_ct_++;
|
|
|
|
|
|
|
|
// Never release or execute this callback.
|
|
|
|
incomplete_callback_ = callback;
|
|
|
|
bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RequestRunSettings* const settings_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
int process_request_ct_ = 0;
|
|
|
|
int get_response_headers_ct_ = 0;
|
|
|
|
int read_response_ct_ = 0;
|
|
|
|
int cancel_ct_ = 0;
|
|
|
|
|
|
|
|
CefRefPtr<CefCallback> incomplete_callback_;
|
|
|
|
|
2019-12-31 13:24:10 +01:00
|
|
|
IMPLEMENT_REFCOUNTING(IncompleteSchemeHandlerOld);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(IncompleteSchemeHandlerOld);
|
|
|
|
};
|
|
|
|
|
|
|
|
class IncompleteSchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
IncompleteSchemeHandler(RequestRunSettings* settings,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback)
|
|
|
|
: settings_(settings), destroy_callback_(std::move(destroy_callback)) {
|
2019-12-31 13:24:10 +01:00
|
|
|
EXPECT_NE(settings_->incomplete_type, RequestRunSettings::INCOMPLETE_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
~IncompleteSchemeHandler() override {
|
|
|
|
EXPECT_EQ(1, open_ct_);
|
|
|
|
EXPECT_EQ(1, cancel_ct_);
|
|
|
|
|
|
|
|
if (settings_->incomplete_type ==
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE) {
|
|
|
|
EXPECT_EQ(1, get_response_headers_ct_);
|
|
|
|
EXPECT_EQ(1, read_ct_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, get_response_headers_ct_);
|
|
|
|
EXPECT_EQ(0, read_ct_);
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(destroy_callback_).Run();
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Open(CefRefPtr<CefRequest> request,
|
|
|
|
bool& handle_request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
open_ct_++;
|
|
|
|
|
|
|
|
if (settings_->incomplete_type ==
|
|
|
|
RequestRunSettings::INCOMPLETE_PROCESS_REQUEST) {
|
|
|
|
// Never release or execute this callback.
|
|
|
|
incomplete_open_callback_ = callback;
|
|
|
|
} else {
|
|
|
|
// Continue immediately.
|
|
|
|
handle_request = true;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t& response_length,
|
2019-12-31 13:24:10 +01:00
|
|
|
CefString& redirectUrl) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
EXPECT_EQ(settings_->incomplete_type,
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE);
|
|
|
|
|
|
|
|
get_response_headers_ct_++;
|
|
|
|
|
|
|
|
response->SetStatus(settings_->response->GetStatus());
|
|
|
|
response->SetStatusText(settings_->response->GetStatusText());
|
|
|
|
response->SetMimeType(settings_->response->GetMimeType());
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
settings_->response->GetHeaderMap(headerMap);
|
|
|
|
settings_->response->SetHeaderMap(headerMap);
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
response_length = static_cast<int64_t>(settings_->response_data.size());
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Read(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefResourceReadCallback> callback) override {
|
|
|
|
EXPECT_FALSE(CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO));
|
|
|
|
EXPECT_EQ(settings_->incomplete_type,
|
|
|
|
RequestRunSettings::INCOMPLETE_READ_RESPONSE);
|
|
|
|
|
|
|
|
read_ct_++;
|
|
|
|
|
|
|
|
// Never release or execute this callback.
|
|
|
|
incomplete_read_callback_ = callback;
|
|
|
|
bytes_read = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ReadResponse(void* data_out,
|
|
|
|
int bytes_to_read,
|
|
|
|
int& bytes_read,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
bytes_read = -2;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Cancel() override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
cancel_ct_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RequestRunSettings* const settings_;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure destroy_callback_;
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
int open_ct_ = 0;
|
|
|
|
int get_response_headers_ct_ = 0;
|
|
|
|
int read_ct_ = 0;
|
|
|
|
int cancel_ct_ = 0;
|
|
|
|
|
|
|
|
CefRefPtr<CefCallback> incomplete_open_callback_;
|
|
|
|
CefRefPtr<CefResourceReadCallback> incomplete_read_callback_;
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(IncompleteSchemeHandler);
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(IncompleteSchemeHandler);
|
|
|
|
};
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
class RequestSchemeHandlerFactory : public CefSchemeHandlerFactory {
|
|
|
|
public:
|
2024-01-20 18:00:09 +01:00
|
|
|
RequestSchemeHandlerFactory() = default;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefString& scheme_name,
|
|
|
|
CefRefPtr<CefRequest> request) override {
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
handler_create_ct_++;
|
2021-06-19 21:54:45 +02:00
|
|
|
auto destroy_callback =
|
|
|
|
base::BindOnce(&RequestSchemeHandlerFactory::OnHandlerDestroyed, this);
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
RequestDataMap::Entry entry = data_map_.Find(request->GetURL());
|
|
|
|
if (entry.type == RequestDataMap::Entry::TYPE_NORMAL) {
|
2019-05-31 11:50:12 +02:00
|
|
|
if (entry.settings->incomplete_type ==
|
|
|
|
RequestRunSettings::INCOMPLETE_NONE) {
|
2019-12-31 13:24:10 +01:00
|
|
|
if (TestOldResourceAPI()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return new RequestSchemeHandlerOld(entry.settings,
|
|
|
|
std::move(destroy_callback));
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
2021-06-19 21:54:45 +02:00
|
|
|
return new RequestSchemeHandler(entry.settings,
|
|
|
|
std::move(destroy_callback));
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
2019-12-31 13:24:10 +01:00
|
|
|
|
|
|
|
if (TestOldResourceAPI()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return new IncompleteSchemeHandlerOld(entry.settings,
|
|
|
|
std::move(destroy_callback));
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
2021-06-19 21:54:45 +02:00
|
|
|
return new IncompleteSchemeHandler(entry.settings,
|
|
|
|
std::move(destroy_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
} else if (entry.type == RequestDataMap::Entry::TYPE_REDIRECT) {
|
2019-12-31 13:24:10 +01:00
|
|
|
if (TestOldResourceAPI()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
return new RequestRedirectSchemeHandlerOld(entry.redirect_request,
|
|
|
|
entry.redirect_response,
|
|
|
|
std::move(destroy_callback));
|
2019-12-31 13:24:10 +01:00
|
|
|
}
|
2021-06-19 21:54:45 +02:00
|
|
|
return new RequestRedirectSchemeHandler(entry.redirect_request,
|
|
|
|
entry.redirect_response,
|
|
|
|
std::move(destroy_callback));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
2017-11-17 17:33:50 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetOwnerTaskRunner(CefRefPtr<CefTaskRunner> task_runner) {
|
|
|
|
data_map_.SetOwnerTaskRunner(task_runner);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
void AddSchemeHandler(RequestRunSettings* settings) {
|
|
|
|
const std::string& scheme = GetRequestScheme(false);
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Verify that the scheme is correct.
|
2017-11-17 17:33:50 +01:00
|
|
|
const std::string& url = settings->request->GetURL();
|
|
|
|
EXPECT_EQ(0U, url.find(scheme));
|
|
|
|
|
|
|
|
if (settings->redirect_request) {
|
|
|
|
// Verify that the scheme is correct.
|
|
|
|
const std::string& redirect_url = settings->redirect_request->GetURL();
|
|
|
|
EXPECT_EQ(0U, redirect_url.find(scheme));
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
data_map_.AddSchemeHandler(settings);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
void OnHandlerDestroyed() {
|
|
|
|
if (!CefCurrentlyOn(TID_IO)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_IO,
|
|
|
|
base::BindOnce(
|
|
|
|
&RequestSchemeHandlerFactory::OnHandlerDestroyed, this));
|
2019-05-31 11:50:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
handler_destroy_ct_++;
|
|
|
|
|
|
|
|
MaybeShutdown();
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void Shutdown(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
if (!CefCurrentlyOn(TID_IO)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_IO, base::BindOnce(&RequestSchemeHandlerFactory::Shutdown,
|
|
|
|
this, std::move(complete_callback)));
|
2017-11-17 17:33:50 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
EXPECT_TRUE(shutdown_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
shutdown_callback_ = std::move(complete_callback);
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
data_map_.SetOwnerTaskRunner(nullptr);
|
2019-05-31 11:50:12 +02:00
|
|
|
|
|
|
|
MaybeShutdown();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2019-05-31 11:50:12 +02:00
|
|
|
void MaybeShutdown() {
|
|
|
|
if (!shutdown_callback_.is_null() &&
|
|
|
|
handler_create_ct_ == handler_destroy_ct_) {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(shutdown_callback_).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
RequestDataMap data_map_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
int handler_create_ct_ = 0;
|
|
|
|
int handler_destroy_ct_ = 0;
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure shutdown_callback_;
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestSchemeHandlerFactory);
|
2019-12-31 13:24:10 +01:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(RequestSchemeHandlerFactory);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// SERVER BACKEND
|
|
|
|
|
|
|
|
// HTTP server handler.
|
2020-08-20 00:27:27 +02:00
|
|
|
class RequestServerHandler : public test_server::ObserverHelper {
|
2017-11-17 17:33:50 +01:00
|
|
|
public:
|
2024-01-20 18:00:09 +01:00
|
|
|
RequestServerHandler() = default;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2024-01-20 18:00:09 +01:00
|
|
|
~RequestServerHandler() override { RunCompleteCallback(false); }
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Must be called before CreateServer().
|
|
|
|
void AddSchemeHandler(RequestRunSettings* settings) {
|
|
|
|
EXPECT_FALSE(initialized_);
|
|
|
|
data_map_.AddSchemeHandler(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Must be called before CreateServer().
|
|
|
|
void SetExpectedRequestCount(int count) {
|
|
|
|
EXPECT_FALSE(initialized_);
|
2022-07-29 22:05:20 +02:00
|
|
|
expected_http_request_ct_ = count;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// |complete_callback| will be executed on the UI thread after the server is
|
|
|
|
// started.
|
2021-06-19 21:54:45 +02:00
|
|
|
void CreateServer(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
if (expected_http_request_ct_ < 0) {
|
2017-11-17 17:33:50 +01:00
|
|
|
// Default to the assumption of one request per registered URL.
|
2017-12-21 21:43:29 +01:00
|
|
|
SetExpectedRequestCount(static_cast<int>(data_map_.size()));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_FALSE(initialized_);
|
|
|
|
initialized_ = true;
|
|
|
|
|
|
|
|
EXPECT_TRUE(complete_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
complete_callback_ = std::move(complete_callback);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2022-07-31 03:17:40 +02:00
|
|
|
Initialize(/*https_server=*/false);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Results in a call to VerifyResults() and eventual execution of the
|
|
|
|
// |complete_callback| on the UI thread via RequestServerHandler destruction.
|
2021-06-19 21:54:45 +02:00
|
|
|
void ShutdownServer(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
|
|
|
|
EXPECT_TRUE(complete_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
complete_callback_ = std::move(complete_callback);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
Shutdown();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
void OnInitialized(const std::string& server_origin) override {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
EXPECT_STREQ(server_origin.c_str(), GetRequestOrigin(true).c_str());
|
|
|
|
EXPECT_FALSE(got_initialized_);
|
|
|
|
got_initialized_.yes();
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
RunCompleteCallback(true);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
void OnShutdown() override {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
EXPECT_FALSE(got_shutdown_);
|
|
|
|
got_shutdown_.yes();
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
data_map_.SetOwnerTaskRunner(nullptr);
|
|
|
|
|
|
|
|
VerifyResults();
|
2020-08-20 00:27:27 +02:00
|
|
|
|
|
|
|
delete this;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2022-08-02 03:14:12 +02:00
|
|
|
bool OnTestServerRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback) override {
|
2020-08-20 00:27:27 +02:00
|
|
|
EXPECT_UI_THREAD();
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Log the requests for better error reporting.
|
|
|
|
request_log_ += request->GetMethod().ToString() + " " +
|
|
|
|
request->GetURL().ToString() + "\n";
|
|
|
|
|
|
|
|
actual_http_request_ct_++;
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
return HandleRequest(request, response_callback);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void VerifyResults() {
|
2020-08-20 00:27:27 +02:00
|
|
|
EXPECT_TRUE(got_initialized_);
|
|
|
|
EXPECT_TRUE(got_shutdown_);
|
2022-07-29 22:05:20 +02:00
|
|
|
EXPECT_EQ(expected_http_request_ct_, actual_http_request_ct_)
|
|
|
|
<< request_log_;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
bool HandleRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
RequestDataMap::Entry entry = data_map_.Find(request->GetURL());
|
|
|
|
if (entry.type == RequestDataMap::Entry::TYPE_NORMAL) {
|
2019-07-12 22:44:43 +02:00
|
|
|
const bool needs_auth = entry.settings->expect_authentication &&
|
|
|
|
!IsAuthorized(request, entry.settings->username,
|
|
|
|
entry.settings->password);
|
|
|
|
if (needs_auth) {
|
2022-07-29 22:05:20 +02:00
|
|
|
return HandleAuthRequest(request, response_callback);
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
return HandleNormalRequest(request, response_callback, entry.settings);
|
2017-11-17 17:33:50 +01:00
|
|
|
} else if (entry.type == RequestDataMap::Entry::TYPE_REDIRECT) {
|
2022-07-29 22:05:20 +02:00
|
|
|
return HandleRedirectRequest(request, response_callback,
|
|
|
|
entry.redirect_request,
|
|
|
|
entry.redirect_response);
|
2017-11-17 17:33:50 +01:00
|
|
|
} else {
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE() << "url: " << request->GetURL().ToString();
|
|
|
|
}
|
2022-07-29 22:05:20 +02:00
|
|
|
return false;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
static bool HandleAuthRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback) {
|
2019-07-12 22:44:43 +02:00
|
|
|
CefRefPtr<CefResponse> response = CefResponse::Create();
|
|
|
|
GetAuthResponse(response);
|
2022-07-29 22:05:20 +02:00
|
|
|
response_callback.Run(response, std::string());
|
|
|
|
return true;
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
static bool HandleNormalRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback,
|
2020-08-20 00:27:27 +02:00
|
|
|
RequestRunSettings* settings) {
|
2017-11-17 17:33:50 +01:00
|
|
|
VerifyNormalRequest(settings, request, true);
|
|
|
|
|
|
|
|
CefRefPtr<CefResponse> response = CefResponse::Create();
|
|
|
|
GetNormalResponse(settings, response);
|
|
|
|
|
|
|
|
// HEAD requests are identical to GET requests except no response data is
|
|
|
|
// sent.
|
|
|
|
std::string response_data;
|
2020-04-20 19:19:42 +02:00
|
|
|
if (request->GetMethod() != "HEAD") {
|
|
|
|
size_t expected_offset = settings->expected_download_offset;
|
|
|
|
response_data = settings->response_data.substr(expected_offset);
|
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
response_callback.Run(response, response_data);
|
|
|
|
return true;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
static bool HandleRedirectRequest(CefRefPtr<CefRequest> request,
|
|
|
|
const ResponseCallback& response_callback,
|
2020-08-20 00:27:27 +02:00
|
|
|
CefRefPtr<CefRequest> redirect_request,
|
|
|
|
CefRefPtr<CefResponse> redirect_response) {
|
2020-04-02 23:05:07 +02:00
|
|
|
if (redirect_response->GetStatus() == 302) {
|
|
|
|
// Simulate wrong copying of POST-specific headers Content-Type and
|
|
|
|
// Content-Length. A 302 redirect should end up in a GET request and
|
|
|
|
// these headers should not propagate from a 302 POST-to-GET redirect.
|
|
|
|
CefResponse::HeaderMap redirectHeaderMap;
|
|
|
|
redirect_response->GetHeaderMap(redirectHeaderMap);
|
|
|
|
redirectHeaderMap.insert(
|
|
|
|
std::make_pair("content-type", "application/x-www-form-urlencoded"));
|
|
|
|
redirectHeaderMap.insert(std::make_pair("content-length", "0"));
|
|
|
|
redirect_response->SetHeaderMap(redirectHeaderMap);
|
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Verify that the request was sent correctly.
|
|
|
|
TestRequestEqual(redirect_request, request, true);
|
|
|
|
|
2022-07-29 22:05:20 +02:00
|
|
|
response_callback.Run(redirect_response, std::string());
|
|
|
|
return true;
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RunCompleteCallback(bool startup) {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
|
|
|
|
if (startup) {
|
2020-08-20 00:27:27 +02:00
|
|
|
// Transfer DataMap ownership to the UI thread.
|
|
|
|
data_map_.SetOwnerTaskRunner(CefTaskRunner::GetForCurrentThread());
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_FALSE(complete_callback_.is_null());
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback_).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
RequestDataMap data_map_;
|
|
|
|
|
2024-01-20 18:00:09 +01:00
|
|
|
bool initialized_ = false;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Only accessed on the UI thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback_;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// After initialization the below members are only accessed on the server
|
|
|
|
// thread.
|
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
TrackCallback got_initialized_;
|
|
|
|
TrackCallback got_shutdown_;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2024-01-20 18:00:09 +01:00
|
|
|
int expected_http_request_ct_ = -1;
|
|
|
|
int actual_http_request_ct_ = 0;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
std::string request_log_;
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// SHARED TEST RUNNER
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Executes the tests.
|
2015-03-02 21:25:14 +01:00
|
|
|
class RequestTestRunner : public base::RefCountedThreadSafe<RequestTestRunner> {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
2021-06-19 21:54:45 +02:00
|
|
|
using TestCallback = base::RepeatingCallback<void(base::OnceClosure)>;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
RequestTestRunner(bool is_server_backend,
|
2019-05-11 00:14:48 +02:00
|
|
|
bool use_frame_method,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure incomplete_request_callback)
|
2023-02-27 19:33:56 +01:00
|
|
|
: is_server_backend_(is_server_backend),
|
2019-05-11 00:14:48 +02:00
|
|
|
use_frame_method_(use_frame_method),
|
2021-06-19 21:54:45 +02:00
|
|
|
incomplete_request_callback_(std::move(incomplete_request_callback)) {
|
2016-11-15 18:56:02 +01:00
|
|
|
owner_task_runner_ = CefTaskRunner::GetForCurrentThread();
|
|
|
|
EXPECT_TRUE(owner_task_runner_.get());
|
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
2021-06-18 18:41:17 +02:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-18 18:41:17 +02:00
|
|
|
void Initialize() {
|
2017-05-17 11:29:28 +02:00
|
|
|
// Helper macro for registering test callbacks.
|
2021-06-19 21:54:45 +02:00
|
|
|
#define REGISTER_TEST(test_mode, setup_method, run_method) \
|
|
|
|
RegisterTest(test_mode, \
|
|
|
|
base::BindRepeating(&RequestTestRunner::setup_method, this), \
|
|
|
|
base::BindRepeating(&RequestTestRunner::run_method, this));
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Register the test callbacks.
|
2017-11-17 17:33:50 +01:00
|
|
|
REGISTER_TEST(REQTEST_GET, SetupGetTest, SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_GET_NODATA, SetupGetNoDataTest, SingleRunTest);
|
2020-04-20 19:19:42 +02:00
|
|
|
REGISTER_TEST(REQTEST_GET_PARTIAL_CONTENT, SetupGetPartialContentTest,
|
|
|
|
SingleRunTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
REGISTER_TEST(REQTEST_GET_ALLOWCOOKIES, SetupGetAllowCookiesTest,
|
2017-11-17 17:33:50 +01:00
|
|
|
SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_GET_REDIRECT, SetupGetRedirectTest, SingleRunTest);
|
2018-04-10 19:04:57 +02:00
|
|
|
REGISTER_TEST(REQTEST_GET_REDIRECT_STOP, SetupGetRedirectStopTest,
|
|
|
|
SingleRunTest);
|
2019-01-11 16:12:06 +01:00
|
|
|
REGISTER_TEST(REQTEST_GET_REDIRECT_LOCATION, SetupGetRedirectLocationTest,
|
|
|
|
SingleRunTest);
|
2017-11-17 17:33:50 +01:00
|
|
|
REGISTER_TEST(REQTEST_GET_REFERRER, SetupGetReferrerTest, SingleRunTest);
|
2019-07-12 22:44:43 +02:00
|
|
|
REGISTER_TEST(REQTEST_GET_AUTH, SetupGetAuthTest, SingleRunTest);
|
2017-11-17 17:33:50 +01:00
|
|
|
REGISTER_TEST(REQTEST_POST, SetupPostTest, SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_POST_FILE, SetupPostFileTest, SingleRunTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
REGISTER_TEST(REQTEST_POST_WITHPROGRESS, SetupPostWithProgressTest,
|
2017-11-17 17:33:50 +01:00
|
|
|
SingleRunTest);
|
2019-07-11 22:36:27 +02:00
|
|
|
REGISTER_TEST(REQTEST_POST_REDIRECT, SetupPostRedirectTest, SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_POST_REDIRECT_TOGET, SetupPostRedirectToGetTest,
|
|
|
|
SingleRunTest);
|
2017-11-17 17:33:50 +01:00
|
|
|
REGISTER_TEST(REQTEST_HEAD, SetupHeadTest, SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_WITH_CONTROL, SetupCacheWithControlTest,
|
|
|
|
MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_WITHOUT_CONTROL, SetupCacheWithoutControlTest,
|
|
|
|
MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_SKIP_FLAG, SetupCacheSkipFlagTest,
|
|
|
|
MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_SKIP_HEADER, SetupCacheSkipHeaderTest,
|
|
|
|
MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_ONLY_FAILURE_FLAG,
|
|
|
|
SetupCacheOnlyFailureFlagTest, MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_ONLY_FAILURE_HEADER,
|
|
|
|
SetupCacheOnlyFailureHeaderTest, MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_ONLY_SUCCESS_FLAG,
|
|
|
|
SetupCacheOnlySuccessFlagTest, MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_ONLY_SUCCESS_HEADER,
|
|
|
|
SetupCacheOnlySuccessHeaderTest, MultipleRunTest);
|
2019-03-07 23:05:36 +01:00
|
|
|
REGISTER_TEST(REQTEST_CACHE_DISABLE_FLAG, SetupCacheDisableFlagTest,
|
|
|
|
MultipleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_CACHE_DISABLE_HEADER, SetupCacheDisableHeaderTest,
|
|
|
|
MultipleRunTest);
|
2019-05-31 11:50:12 +02:00
|
|
|
REGISTER_TEST(REQTEST_INCOMPLETE_PROCESS_REQUEST,
|
|
|
|
SetupIncompleteProcessRequestTest, SingleRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_INCOMPLETE_READ_RESPONSE,
|
|
|
|
SetupIncompleteReadResponseTest, SingleRunTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void Destroy() {
|
|
|
|
owner_task_runner_ = nullptr;
|
|
|
|
request_context_ = nullptr;
|
2019-05-31 11:50:12 +02:00
|
|
|
incomplete_request_callback_.Reset();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Called in the browser process to set the request context that will be used
|
|
|
|
// when creating the URL request.
|
|
|
|
void SetRequestContext(CefRefPtr<CefRequestContext> request_context) {
|
|
|
|
request_context_ = request_context;
|
|
|
|
}
|
|
|
|
CefRefPtr<CefRequestContext> GetRequestContext() const {
|
|
|
|
return request_context_;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Called to setup the test.
|
2017-11-17 17:33:50 +01:00
|
|
|
void SetupTest(RequestTestMode test_mode,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMap::const_iterator it = test_map_.find(test_mode);
|
|
|
|
if (it != test_map_.end()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
auto safe_complete_callback =
|
|
|
|
base::BindOnce(&RequestTestRunner::CompleteOnCorrectThread, this,
|
|
|
|
std::move(complete_callback));
|
|
|
|
it->second.setup.Run(base::BindOnce(&RequestTestRunner::SetupContinue,
|
|
|
|
this,
|
|
|
|
std::move(safe_complete_callback)));
|
2012-06-19 18:29:49 +02:00
|
|
|
} else {
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Called to run the test.
|
2017-11-17 17:33:50 +01:00
|
|
|
void RunTest(RequestTestMode test_mode,
|
2019-05-11 00:14:48 +02:00
|
|
|
CefRefPtr<CefFrame> frame,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
frame_ = frame;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMap::const_iterator it = test_map_.find(test_mode);
|
|
|
|
if (it != test_map_.end()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
auto safe_complete_callback =
|
|
|
|
base::BindOnce(&RequestTestRunner::CompleteOnCorrectThread, this,
|
|
|
|
std::move(complete_callback));
|
|
|
|
it->second.run.Run(std::move(safe_complete_callback));
|
2012-06-19 18:29:49 +02:00
|
|
|
} else {
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Called to shut down the test.
|
2021-06-19 21:54:45 +02:00
|
|
|
void ShutdownTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
auto safe_complete_callback =
|
|
|
|
base::BindOnce(&RequestTestRunner::CompleteOnCorrectThread, this,
|
|
|
|
std::move(complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
if (!post_file_tmpdir_.IsEmpty()) {
|
2020-09-25 03:40:47 +02:00
|
|
|
CefPostTask(TID_FILE_USER_VISIBLE,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::RunDeleteTempDirectory,
|
|
|
|
this, std::move(safe_complete_callback)));
|
2017-11-17 17:33:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue with test shutdown.
|
2021-06-19 21:54:45 +02:00
|
|
|
RunShutdown(std::move(safe_complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
private:
|
2016-11-15 18:56:02 +01:00
|
|
|
// Continued after |settings_| is populated for the test.
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupContinue(base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
|
|
|
base::BindOnce(&RequestTestRunner::SetupContinue, this,
|
|
|
|
std::move(complete_callback))));
|
2016-11-15 18:56:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
SetupTestBackend(std::move(complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2019-01-11 16:12:06 +01:00
|
|
|
std::string GetTestPath(const std::string& name) {
|
2023-02-27 19:33:56 +01:00
|
|
|
return std::string("/Browser") + name;
|
2019-01-11 16:12:06 +01:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
std::string GetTestURL(const std::string& name) {
|
|
|
|
// Avoid name duplication between tests running in different processes.
|
|
|
|
// Otherwise we'll get unexpected state leakage (cache hits) when running
|
|
|
|
// multiple tests.
|
2019-01-11 16:12:06 +01:00
|
|
|
return GetRequestOrigin(is_server_backend_) + GetTestPath(name);
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupGetTestShared() {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetURL(GetTestURL("GetTest.html"));
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request->SetMethod("GET");
|
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
|
|
|
settings_.response_data = "GET TEST SUCCESS";
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetTest(base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupGetTestShared();
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetNoDataTest(base::OnceClosure complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// Start with the normal get test.
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupGetTestShared();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Disable download data notifications.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_NO_DOWNLOAD_DATA);
|
|
|
|
|
|
|
|
settings_.expect_download_data = false;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetPartialContentTest(base::OnceClosure complete_callback) {
|
2020-04-20 19:19:42 +02:00
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
|
|
|
|
|
|
|
// Skip first 4 bytes of content and expect to receive the rest
|
|
|
|
settings_.request->SetHeaderByName("Range", "bytes=4-", true);
|
|
|
|
settings_.response->SetHeaderByName("Content-Range", "bytes 4-8/8", true);
|
|
|
|
settings_.response->SetStatus(206);
|
|
|
|
settings_.response->SetStatusText("Partial Content");
|
|
|
|
settings_.expected_download_offset = 4;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2020-04-20 19:19:42 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetAllowCookiesTest(base::OnceClosure complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// Start with the normal get test.
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupGetTestShared();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Send cookies.
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetFlags(UR_FLAG_ALLOW_STORED_CREDENTIALS);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
settings_.expect_save_cookie = true;
|
|
|
|
settings_.expect_send_cookie = true;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetRedirectTest(base::OnceClosure complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// Start with the normal get test.
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupGetTestShared();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Add a redirect request.
|
|
|
|
settings_.redirect_request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.redirect_request->SetURL(GetTestURL("redirect.html"));
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.redirect_request->SetMethod("GET");
|
|
|
|
|
|
|
|
settings_.redirect_response = CefResponse::Create();
|
|
|
|
settings_.redirect_response->SetMimeType("text/html");
|
|
|
|
settings_.redirect_response->SetStatus(302);
|
|
|
|
settings_.redirect_response->SetStatusText("Found");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair("Location", settings_.request->GetURL()));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetRedirectStopTest(base::OnceClosure complete_callback) {
|
2018-04-10 19:04:57 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(GetTestURL("GetTest.html"));
|
|
|
|
settings_.request->SetMethod("GET");
|
|
|
|
|
|
|
|
// With the test server only the status is expected
|
|
|
|
// on stop redirects.
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetStatus(302);
|
2023-02-27 19:33:56 +01:00
|
|
|
settings_.response->SetStatusText("Found");
|
2018-04-10 19:04:57 +02:00
|
|
|
|
|
|
|
// Add a redirect request.
|
|
|
|
settings_.redirect_request = CefRequest::Create();
|
|
|
|
settings_.redirect_request->SetURL(GetTestURL("redirect.html"));
|
|
|
|
settings_.redirect_request->SetMethod("GET");
|
|
|
|
settings_.redirect_request->SetFlags(UR_FLAG_STOP_ON_REDIRECT);
|
|
|
|
|
|
|
|
settings_.redirect_response = CefResponse::Create();
|
|
|
|
settings_.redirect_response->SetMimeType("text/html");
|
|
|
|
settings_.redirect_response->SetStatus(302);
|
|
|
|
settings_.redirect_response->SetStatusText("Found");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair("Location", settings_.request->GetURL()));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
settings_.expected_status = UR_CANCELED;
|
|
|
|
settings_.expected_error_code = ERR_ABORTED;
|
|
|
|
settings_.expect_download_data = false;
|
|
|
|
settings_.expect_download_progress = false;
|
|
|
|
settings_.expected_send_count = 1;
|
|
|
|
settings_.expected_receive_count = 1;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2018-04-10 19:04:57 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetRedirectLocationTest(base::OnceClosure complete_callback) {
|
2019-01-11 16:12:06 +01:00
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
|
|
|
|
|
|
|
// Add a redirect request.
|
|
|
|
settings_.redirect_request = CefRequest::Create();
|
|
|
|
settings_.redirect_request->SetURL(GetTestURL("redirect.html"));
|
|
|
|
settings_.redirect_request->SetMethod("GET");
|
|
|
|
|
|
|
|
settings_.redirect_response = CefResponse::Create();
|
|
|
|
settings_.redirect_response->SetMimeType("text/html");
|
|
|
|
settings_.redirect_response->SetStatus(302);
|
|
|
|
settings_.redirect_response->SetStatusText("Found");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair("LoCaTioN", GetTestPath("GetTest.html")));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-01-11 16:12:06 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetReferrerTest(base::OnceClosure complete_callback) {
|
2015-12-01 19:22:28 +01:00
|
|
|
settings_.request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetURL(GetTestURL("GetTest.html"));
|
2015-12-01 19:22:28 +01:00
|
|
|
settings_.request->SetMethod("GET");
|
|
|
|
|
|
|
|
// The referrer URL must be HTTP or HTTPS. This is enforced by
|
|
|
|
// GURL::GetAsReferrer() called from URLRequest::SetReferrer().
|
2023-04-29 00:17:03 +02:00
|
|
|
settings_.request->SetReferrer("https://tests.com/referrer.html",
|
2017-07-27 01:19:27 +02:00
|
|
|
REFERRER_POLICY_DEFAULT);
|
2015-12-01 19:22:28 +01:00
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
|
|
|
settings_.response_data = "GET TEST SUCCESS";
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2015-12-01 19:22:28 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupGetAuthTest(base::OnceClosure complete_callback) {
|
2019-07-12 22:44:43 +02:00
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
|
|
|
|
|
|
|
// Require Basic authentication.
|
|
|
|
settings_.expect_authentication = true;
|
|
|
|
settings_.username = "user";
|
|
|
|
settings_.password = "pass";
|
|
|
|
|
|
|
|
// This flag is required to support credentials, which means we'll also get
|
|
|
|
// the cookies.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_ALLOW_STORED_CREDENTIALS);
|
|
|
|
settings_.expect_save_cookie = true;
|
|
|
|
settings_.expect_send_cookie = true;
|
|
|
|
|
|
|
|
// The authentication request will come first, then the actual request.
|
|
|
|
settings_.expected_receive_count = 2;
|
|
|
|
settings_.expected_send_count = 2;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupPostTestShared() {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetURL(GetTestURL("PostTest.html"));
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request->SetMethod("POST");
|
|
|
|
SetUploadData(settings_.request, "the_post_data");
|
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
|
|
|
settings_.response_data = "POST TEST SUCCESS";
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostTest(base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupPostTestShared();
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostFileTest(base::OnceClosure complete_callback) {
|
2013-11-25 18:31:59 +01:00
|
|
|
settings_.request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetURL(GetTestURL("PostFileTest.html"));
|
2013-11-25 18:31:59 +01:00
|
|
|
settings_.request->SetMethod("POST");
|
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
|
|
|
settings_.response_data = "POST TEST SUCCESS";
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
CefPostTask(TID_FILE_USER_VISIBLE,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupPostFileTestContinue,
|
|
|
|
this, std::move(complete_callback)));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostFileTestContinue(base::OnceClosure complete_callback) {
|
2020-09-25 03:40:47 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE_USER_VISIBLE));
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.CreateUniqueTempDir());
|
|
|
|
const std::string& path =
|
2017-08-04 00:55:19 +02:00
|
|
|
client::file_util::JoinPath(post_file_tmpdir_.GetPath(), "example.txt");
|
2016-11-15 18:56:02 +01:00
|
|
|
const char content[] = "HELLO FRIEND!";
|
2017-08-04 00:55:19 +02:00
|
|
|
int write_ct =
|
|
|
|
client::file_util::WriteFile(path, content, sizeof(content) - 1);
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_EQ(static_cast<int>(sizeof(content) - 1), write_ct);
|
|
|
|
SetUploadFile(settings_.request, path);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2013-11-25 18:31:59 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostWithProgressTest(base::OnceClosure complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// Start with the normal post test.
|
2016-11-15 18:56:02 +01:00
|
|
|
SetupPostTestShared();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Enable upload progress notifications.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_REPORT_UPLOAD_PROGRESS);
|
|
|
|
|
|
|
|
settings_.expect_upload_progress = true;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostRedirectTest(base::OnceClosure complete_callback) {
|
2019-07-11 22:36:27 +02:00
|
|
|
// Start with the normal post test.
|
|
|
|
SetupPostTestShared();
|
|
|
|
|
|
|
|
// Add a redirect request.
|
|
|
|
settings_.redirect_request = CefRequest::Create();
|
|
|
|
settings_.redirect_request->SetURL(GetTestURL("redirect.html"));
|
|
|
|
settings_.redirect_request->SetMethod("POST");
|
|
|
|
SetUploadData(settings_.redirect_request, "the_post_data");
|
|
|
|
|
|
|
|
settings_.redirect_response = CefResponse::Create();
|
|
|
|
settings_.redirect_response->SetMimeType("text/html");
|
|
|
|
// Only 307 is supported for redirecting the same method and post data.
|
|
|
|
settings_.redirect_response->SetStatus(307);
|
|
|
|
settings_.redirect_response->SetStatusText("Found");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair("Location", settings_.request->GetURL()));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-07-11 22:36:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupPostRedirectToGetTest(base::OnceClosure complete_callback) {
|
2019-07-11 22:36:27 +02:00
|
|
|
// Start with the normal post test.
|
|
|
|
SetupPostTestShared();
|
|
|
|
|
|
|
|
// The expected result after redirect is a GET request without POST data.
|
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(GetTestURL("PostTest.html"));
|
|
|
|
settings_.request->SetMethod("GET");
|
|
|
|
|
|
|
|
// Add a redirect request.
|
|
|
|
settings_.redirect_request = CefRequest::Create();
|
|
|
|
settings_.redirect_request->SetURL(GetTestURL("redirect.html"));
|
|
|
|
settings_.redirect_request->SetMethod("POST");
|
|
|
|
SetUploadData(settings_.redirect_request, "the_post_data");
|
|
|
|
|
|
|
|
settings_.redirect_response = CefResponse::Create();
|
|
|
|
settings_.redirect_response->SetMimeType("text/html");
|
|
|
|
// Redirect codes other than 307 will cause conversion to GET and removal
|
|
|
|
// of POST data.
|
|
|
|
settings_.redirect_response->SetStatus(302);
|
|
|
|
settings_.redirect_response->SetStatusText("Found");
|
|
|
|
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair("Location", settings_.request->GetURL()));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-07-11 22:36:27 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupHeadTest(base::OnceClosure complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetURL(GetTestURL("HeadTest.html"));
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request->SetMethod("HEAD");
|
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// The backend will disregard this value when it returns the result.
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.response_data = "HEAD TEST SUCCESS";
|
|
|
|
|
|
|
|
settings_.expect_download_progress = false;
|
|
|
|
settings_.expect_download_data = false;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
void SetupCacheShared(const std::string& name, bool with_cache_control) {
|
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Specify a unique URL.
|
|
|
|
settings_.request->SetURL(GetTestURL(name));
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
if (with_cache_control) {
|
|
|
|
// Allow the page to be cached for 10 seconds.
|
|
|
|
CefResponse::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "max-age=10"));
|
|
|
|
settings_.response->SetHeaderMap(headerMap);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheWithControlTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheWithControlTest.html", true);
|
|
|
|
|
|
|
|
// Send multiple requests. With the Cache-Control response header the 2nd+
|
|
|
|
// should receive cached data.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 1;
|
2018-01-03 22:14:29 +01:00
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheWithControlTestNext, this);
|
2018-01-03 22:14:29 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2018-01-03 22:14:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheWithControlTestNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2018-01-03 22:14:29 +01:00
|
|
|
// Only handle from the cache.
|
|
|
|
settings_.expect_response_was_cached = true;
|
|
|
|
|
|
|
|
// The following requests will use the same setup, so no more callbacks
|
|
|
|
// are required.
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheWithoutControlTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheWithoutControlTest.html", false);
|
|
|
|
|
|
|
|
// Send multiple requests. Without the Cache-Control response header all
|
|
|
|
// should be received.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 3;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheSkipFlagTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheSkipFlagTest.html", true);
|
|
|
|
|
|
|
|
// Skip the cache despite the the Cache-Control response header.
|
2019-03-07 23:05:36 +01:00
|
|
|
// This will not read from the cache, but still write to the cache.
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.request->SetFlags(UR_FLAG_SKIP_CACHE);
|
|
|
|
|
2019-03-07 23:05:36 +01:00
|
|
|
// Send multiple requests. The 1st request will be handled normally,
|
|
|
|
// but not result in any reads from the cache. The 2nd request will
|
|
|
|
// expect a cached response and the 3nd request will skip the cache
|
|
|
|
// again.
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.expected_send_count = 3;
|
2019-03-07 23:05:36 +01:00
|
|
|
settings_.expected_receive_count = 2;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheSkipFlagTestNext, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheSkipFlagTestNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheSkipFlagTest.html", true);
|
|
|
|
|
|
|
|
// Expect a cached response.
|
|
|
|
settings_.expect_response_was_cached = true;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheSkipFlagTestLast, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheSkipFlagTestLast(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheSkipFlagTest.html", true);
|
|
|
|
|
|
|
|
// Skip the cache despite the the Cache-Control response header.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_SKIP_CACHE);
|
|
|
|
|
|
|
|
// Expect the cache to be skipped.
|
|
|
|
settings_.expect_response_was_cached = false;
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheSkipHeaderTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheSkipHeaderTest.html", true);
|
|
|
|
|
|
|
|
// Skip the cache despite the the Cache-Control response header.
|
2019-03-07 23:05:36 +01:00
|
|
|
// This will not read from the cache, but still write to the cache.
|
2017-11-17 17:33:50 +01:00
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "no-cache"));
|
|
|
|
settings_.request->SetHeaderMap(headerMap);
|
|
|
|
|
2019-03-07 23:05:36 +01:00
|
|
|
// Send multiple requests. The 1st request will be handled normally,
|
|
|
|
// but not result in any reads from the cache. The 2nd request will
|
|
|
|
// expect a cached response and the 3nd request will skip the cache
|
|
|
|
// again.
|
2017-11-17 17:33:50 +01:00
|
|
|
settings_.expected_send_count = 3;
|
2019-03-07 23:05:36 +01:00
|
|
|
settings_.expected_receive_count = 2;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheSkipHeaderTestNext, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheSkipHeaderTestNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheSkipHeaderTest.html", true);
|
|
|
|
|
|
|
|
// Expect a cached response.
|
|
|
|
settings_.expect_response_was_cached = true;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheSkipHeaderTestLast, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheSkipHeaderTestLast(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheSkipHeaderTest.html", true);
|
|
|
|
|
|
|
|
// Skip the cache despite the the Cache-Control response header.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_SKIP_CACHE);
|
|
|
|
|
|
|
|
// Expect the cache to be skipped.
|
|
|
|
settings_.expect_response_was_cached = false;
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheOnlyFailureFlagTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheOnlyFailureFlagTest.html", true);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_ONLY_FROM_CACHE);
|
|
|
|
|
|
|
|
// Send multiple requests. All should fail because there's no entry in the
|
|
|
|
// cache currently.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 0;
|
|
|
|
|
|
|
|
// The request is expected to fail.
|
|
|
|
settings_.SetRequestFailureExpected(ERR_CACHE_MISS);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheOnlyFailureHeaderTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheOnlyFailureFlagTest.html", true);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "only-if-cached"));
|
|
|
|
settings_.request->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
// Send multiple requests. All should fail because there's no entry in the
|
|
|
|
// cache currently.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 0;
|
|
|
|
|
|
|
|
// The request is expected to fail.
|
|
|
|
settings_.SetRequestFailureExpected(ERR_CACHE_MISS);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheOnlySuccessFlagTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheOnlySuccessFlagTest.html", false);
|
|
|
|
|
|
|
|
// Send multiple requests. The 1st request will be handled normally. The
|
|
|
|
// 2nd+ requests will be configured by SetupCacheOnlySuccessFlagNext to
|
|
|
|
// require cached data.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 1;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheOnlySuccessFlagNext, this);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheOnlySuccessFlagNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheOnlySuccessFlagTest.html", false);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_ONLY_FROM_CACHE);
|
2018-01-03 22:14:29 +01:00
|
|
|
settings_.expect_response_was_cached = true;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// The following requests will use the same setup, so no more callbacks
|
|
|
|
// are required.
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheOnlySuccessHeaderTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
SetupCacheShared("CacheOnlySuccessHeaderTest.html", false);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Send multiple requests. The 1st request will be handled normally. The
|
|
|
|
// 2nd+ requests will be configured by SetupCacheOnlySuccessHeaderNext to
|
|
|
|
// require cached data.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 1;
|
2021-06-19 21:54:45 +02:00
|
|
|
settings_.setup_next_request = base::BindOnce(
|
|
|
|
&RequestTestRunner::SetupCacheOnlySuccessHeaderNext, this);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheOnlySuccessHeaderNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheOnlySuccessHeaderTest.html", false);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "only-if-cached"));
|
|
|
|
settings_.request->SetHeaderMap(headerMap);
|
2018-01-03 22:14:29 +01:00
|
|
|
settings_.expect_response_was_cached = true;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// The following requests will use the same setup, so no more callbacks
|
|
|
|
// are required.
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheDisableFlagTest(base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
SetupCacheShared("CacheDisableFlagTest.html", true);
|
|
|
|
|
|
|
|
// Disable the cache despite the the Cache-Control response header.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_DISABLE_CACHE);
|
|
|
|
|
|
|
|
// Send multiple requests. The 1st request will be handled normally,
|
|
|
|
// but not result in any reads from or writes to the cache.
|
|
|
|
// Therefore all following requests that are set to be only handled
|
|
|
|
// from the cache should fail.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 1;
|
|
|
|
settings_.setup_next_request =
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::SetupCacheDisableFlagTestNext, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheDisableFlagTestNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheDisableFlagTest.html", true);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
settings_.request->SetFlags(UR_FLAG_ONLY_FROM_CACHE);
|
|
|
|
|
|
|
|
// The request is expected to fail.
|
|
|
|
settings_.SetRequestFailureExpected(ERR_CACHE_MISS);
|
|
|
|
|
|
|
|
// The following requests will use the same setup, so no more callbacks
|
|
|
|
// are required.
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupCacheDisableHeaderTest(base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
SetupCacheShared("CacheDisableHeaderTest.html", true);
|
|
|
|
|
|
|
|
// Disable the cache despite the the Cache-Control response header.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "no-store"));
|
|
|
|
settings_.request->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
// Send multiple requests. The 1st request will be handled normally,
|
|
|
|
// but not result in any reads from or writes to the cache.
|
|
|
|
// Therefore all following requests that are set to be only handled
|
|
|
|
// from the cache should fail.
|
|
|
|
settings_.expected_send_count = 3;
|
|
|
|
settings_.expected_receive_count = 1;
|
2021-06-19 21:54:45 +02:00
|
|
|
settings_.setup_next_request = base::BindOnce(
|
|
|
|
&RequestTestRunner::SetupCacheDisableHeaderTestNext, this);
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetupCacheDisableHeaderTestNext(int next_send_count,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure complete_callback) {
|
2019-03-07 23:05:36 +01:00
|
|
|
// Recreate the request object because the existing object will now be
|
|
|
|
// read-only.
|
|
|
|
EXPECT_TRUE(settings_.request->IsReadOnly());
|
|
|
|
SetupCacheShared("CacheDisableHeaderTest.html", true);
|
|
|
|
|
|
|
|
// Only handle from the cache.
|
|
|
|
CefRequest::HeaderMap headerMap;
|
|
|
|
headerMap.insert(std::make_pair(kCacheControlHeader, "only-if-cached"));
|
|
|
|
settings_.request->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
// The request is expected to fail.
|
|
|
|
settings_.SetRequestFailureExpected(ERR_CACHE_MISS);
|
|
|
|
|
|
|
|
// The following requests will use the same setup, so no more callbacks
|
|
|
|
// are required.
|
2021-06-19 21:54:45 +02:00
|
|
|
EXPECT_TRUE(settings_.setup_next_request.is_null());
|
2019-03-07 23:05:36 +01:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-03-07 23:05:36 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupIncompleteProcessRequestTest(base::OnceClosure complete_callback) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
|
|
|
|
|
|
|
settings_.incomplete_type = RequestRunSettings::INCOMPLETE_PROCESS_REQUEST;
|
|
|
|
|
|
|
|
// There will be no response and the request will be aborted.
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response_data.clear();
|
|
|
|
settings_.expected_error_code = ERR_ABORTED;
|
|
|
|
settings_.expected_status = UR_FAILED;
|
|
|
|
settings_.expect_download_progress = false;
|
|
|
|
settings_.expect_download_data = false;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupIncompleteReadResponseTest(base::OnceClosure complete_callback) {
|
2019-05-31 11:50:12 +02:00
|
|
|
// Start with the normal get test.
|
|
|
|
SetupGetTestShared();
|
|
|
|
|
|
|
|
settings_.incomplete_type = RequestRunSettings::INCOMPLETE_READ_RESPONSE;
|
|
|
|
|
|
|
|
// There will be a response but the request will be aborted without
|
|
|
|
// receiving any data.
|
2022-07-29 22:05:20 +02:00
|
|
|
settings_.response_data = test_server::kIncompleteDoNotSendData;
|
2019-05-31 11:50:12 +02:00
|
|
|
settings_.expected_error_code = ERR_ABORTED;
|
|
|
|
settings_.expected_status = UR_FAILED;
|
2023-02-27 19:33:56 +01:00
|
|
|
settings_.expect_download_progress = true;
|
2019-05-31 11:50:12 +02:00
|
|
|
settings_.expect_download_data = false;
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Send a request. |complete_callback| will be executed on request completion.
|
2021-06-19 21:54:45 +02:00
|
|
|
void SendRequest(test_request::RequestDoneCallback done_callback) {
|
2020-08-20 00:27:27 +02:00
|
|
|
test_request::SendConfig config;
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (settings_.redirect_request) {
|
2020-08-20 00:27:27 +02:00
|
|
|
config.request_ = settings_.redirect_request;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2020-08-20 00:27:27 +02:00
|
|
|
config.request_ = settings_.request;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2020-08-20 00:27:27 +02:00
|
|
|
EXPECT_TRUE(config.request_.get());
|
2019-07-12 22:44:43 +02:00
|
|
|
|
2019-07-29 23:27:12 +02:00
|
|
|
// Not delegating to CefRequestHandler::GetAuthCredentials.
|
|
|
|
if (!use_frame_method_ && settings_.expect_authentication) {
|
2020-08-20 00:27:27 +02:00
|
|
|
config.has_credentials_ = true;
|
|
|
|
config.username_ = settings_.username;
|
|
|
|
config.password_ = settings_.password;
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
if (use_frame_method_) {
|
|
|
|
EXPECT_TRUE(frame_);
|
2020-08-20 00:27:27 +02:00
|
|
|
config.frame_ = frame_;
|
2019-05-11 00:14:48 +02:00
|
|
|
} else {
|
2020-08-20 00:27:27 +02:00
|
|
|
config.request_context_ = request_context_;
|
2019-05-11 00:14:48 +02:00
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
test_request::Send(config, std::move(done_callback));
|
2020-08-20 00:27:27 +02:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
if (settings_.incomplete_type != RequestRunSettings::INCOMPLETE_NONE) {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(incomplete_request_callback_).Run();
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Verify a response.
|
2020-08-20 00:27:27 +02:00
|
|
|
void VerifyResponse(const test_request::State* const client) {
|
2017-11-17 17:33:50 +01:00
|
|
|
CefRefPtr<CefRequest> expected_request;
|
|
|
|
CefRefPtr<CefResponse> expected_response;
|
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (settings_.redirect_request) {
|
2017-11-17 17:33:50 +01:00
|
|
|
expected_request = settings_.redirect_request;
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2017-11-17 17:33:50 +01:00
|
|
|
expected_request = settings_.request;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
if (settings_.redirect_response && !settings_.expect_follow_redirect) {
|
|
|
|
// A redirect response was sent but the redirect is not expected to be
|
|
|
|
// followed.
|
|
|
|
expected_response = settings_.redirect_response;
|
|
|
|
} else {
|
|
|
|
expected_response = settings_.response;
|
|
|
|
}
|
|
|
|
|
|
|
|
TestRequestEqual(expected_request, client->request_, false);
|
|
|
|
|
|
|
|
EXPECT_EQ(settings_.expected_status, client->status_);
|
|
|
|
EXPECT_EQ(settings_.expected_error_code, client->error_code_);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (expected_response && client->response_) {
|
2017-11-17 17:33:50 +01:00
|
|
|
TestResponseEqual(expected_response, client->response_, true);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2018-01-03 22:14:29 +01:00
|
|
|
EXPECT_EQ(settings_.expect_response_was_cached,
|
|
|
|
client->response_was_cached_);
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_EQ(1, client->request_complete_ct_);
|
|
|
|
|
|
|
|
if (settings_.expect_upload_progress) {
|
|
|
|
EXPECT_LE(1, client->upload_progress_ct_);
|
|
|
|
|
|
|
|
std::string upload_data;
|
|
|
|
GetUploadData(expected_request, upload_data);
|
2023-06-01 16:06:15 +02:00
|
|
|
EXPECT_EQ((int64_t)upload_data.size(), client->upload_total_);
|
2017-11-17 17:33:50 +01:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->upload_progress_ct_);
|
2019-05-11 00:14:48 +02:00
|
|
|
EXPECT_EQ(0, client->upload_total_);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_.expect_download_progress) {
|
|
|
|
EXPECT_LE(1, client->download_progress_ct_);
|
2023-06-01 16:06:15 +02:00
|
|
|
EXPECT_EQ((int64_t)(settings_.response_data.size() -
|
|
|
|
settings_.expected_download_offset),
|
2020-04-20 19:19:42 +02:00
|
|
|
client->download_total_);
|
2017-11-17 17:33:50 +01:00
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->download_progress_ct_);
|
2019-05-11 00:14:48 +02:00
|
|
|
EXPECT_EQ(0, client->download_total_);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_.expect_download_data) {
|
2020-04-20 19:19:42 +02:00
|
|
|
size_t expected_offset = settings_.expected_download_offset;
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_LE(1, client->download_data_ct_);
|
2020-04-20 19:19:42 +02:00
|
|
|
EXPECT_STREQ(settings_.response_data.substr(expected_offset).c_str(),
|
2017-11-17 17:33:50 +01:00
|
|
|
client->download_data_.c_str());
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->download_data_ct_);
|
|
|
|
EXPECT_TRUE(client->download_data_.empty());
|
|
|
|
}
|
2019-07-12 22:44:43 +02:00
|
|
|
|
|
|
|
if (settings_.expect_authentication) {
|
|
|
|
EXPECT_EQ(1, client->auth_credentials_ct_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->auth_credentials_ct_);
|
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a test with a single request.
|
2021-06-19 21:54:45 +02:00
|
|
|
void SingleRunTest(base::OnceClosure complete_callback) {
|
|
|
|
SendRequest(base::BindOnce(&RequestTestRunner::SingleRunTestComplete, this,
|
|
|
|
std::move(complete_callback)));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void SingleRunTestComplete(base::OnceClosure complete_callback,
|
2020-08-20 00:27:27 +02:00
|
|
|
const test_request::State& completed_client) {
|
|
|
|
VerifyResponse(&completed_client);
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run a test with multiple requests.
|
2021-06-19 21:54:45 +02:00
|
|
|
void MultipleRunTest(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_GT(settings_.expected_send_count, 0);
|
|
|
|
EXPECT_GE(settings_.expected_receive_count, 0);
|
2021-06-19 21:54:45 +02:00
|
|
|
MultipleRunTestContinue(std::move(complete_callback), 1);
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void MultipleRunTestContinue(base::OnceClosure complete_callback,
|
2017-11-17 17:33:50 +01:00
|
|
|
int send_count) {
|
|
|
|
// Send the next request.
|
2021-06-19 21:54:45 +02:00
|
|
|
SendRequest(base::BindOnce(&RequestTestRunner::MultipleRunTestNext, this,
|
|
|
|
std::move(complete_callback), send_count));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void MultipleRunTestNext(base::OnceClosure complete_callback,
|
2017-11-17 17:33:50 +01:00
|
|
|
int send_count,
|
2020-08-20 00:27:27 +02:00
|
|
|
const test_request::State& completed_client) {
|
2017-11-17 17:33:50 +01:00
|
|
|
// Verify the completed request.
|
2020-08-20 00:27:27 +02:00
|
|
|
VerifyResponse(&completed_client);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
if (send_count == settings_.expected_send_count) {
|
|
|
|
// All requests complete.
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const int next_send_count = send_count + 1;
|
2021-06-19 21:54:45 +02:00
|
|
|
auto continue_callback =
|
|
|
|
base::BindOnce(&RequestTestRunner::MultipleRunTestContinue, this,
|
|
|
|
std::move(complete_callback), next_send_count);
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
if (!settings_.setup_next_request.is_null()) {
|
|
|
|
// Provide an opportunity to modify expectations before the next request.
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(settings_.setup_next_request)
|
|
|
|
.Run(next_send_count, std::move(continue_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
} else {
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(continue_callback).Run();
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Register a test. Called in the constructor.
|
|
|
|
void RegisterTest(RequestTestMode test_mode,
|
|
|
|
TestCallback setup,
|
|
|
|
TestCallback run) {
|
|
|
|
TestEntry entry = {setup, run};
|
|
|
|
test_map_.insert(std::make_pair(test_mode, entry));
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void CompleteOnCorrectThread(base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestRunner::CompleteOnCorrectThread, this,
|
|
|
|
std::move(complete_callback))));
|
2016-11-15 18:56:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void RunDeleteTempDirectory(base::OnceClosure complete_callback) {
|
2020-09-25 03:40:47 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE_USER_VISIBLE));
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.Delete());
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.IsEmpty());
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Continue with test shutdown.
|
2021-06-19 21:54:45 +02:00
|
|
|
RunShutdown(std::move(complete_callback));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void RunShutdown(base::OnceClosure complete_callback) {
|
2016-11-15 18:56:02 +01:00
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
2021-06-19 21:54:45 +02:00
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
|
|
|
base::BindOnce(&RequestTestRunner::RunShutdown, this,
|
|
|
|
std::move(complete_callback))));
|
2016-11-15 18:56:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
ShutdownTestBackend(std::move(complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the backend for the current test. Called during test setup.
|
2021-06-19 21:54:45 +02:00
|
|
|
void SetupTestBackend(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_TRUE(settings_.request.get());
|
|
|
|
EXPECT_TRUE(settings_.response.get() ||
|
|
|
|
settings_.expected_status == UR_FAILED);
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
if (is_server_backend_) {
|
|
|
|
StartServer(std::move(complete_callback));
|
|
|
|
} else {
|
|
|
|
AddSchemeHandler(std::move(complete_callback));
|
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void StartServer(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_FALSE(server_handler_);
|
|
|
|
|
|
|
|
server_handler_ = new RequestServerHandler();
|
|
|
|
|
|
|
|
server_handler_->AddSchemeHandler(&settings_);
|
|
|
|
if (settings_.expected_receive_count >= 0) {
|
|
|
|
server_handler_->SetExpectedRequestCount(
|
|
|
|
settings_.expected_receive_count);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
server_handler_->CreateServer(std::move(complete_callback));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void AddSchemeHandler(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_FALSE(scheme_factory_);
|
|
|
|
|
|
|
|
// Add the factory registration.
|
|
|
|
scheme_factory_ = new RequestSchemeHandlerFactory();
|
|
|
|
request_context_->RegisterSchemeHandlerFactory(GetRequestScheme(false),
|
|
|
|
GetRequestHost(false, false),
|
|
|
|
scheme_factory_.get());
|
|
|
|
|
|
|
|
scheme_factory_->AddSchemeHandler(&settings_);
|
|
|
|
|
|
|
|
// Any further calls will come from the IO thread.
|
|
|
|
scheme_factory_->SetOwnerTaskRunner(CefTaskRunner::GetForThread(TID_IO));
|
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
std::move(complete_callback).Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Shutdown the backend for the current test. Called during test shutdown.
|
2021-06-19 21:54:45 +02:00
|
|
|
void ShutdownTestBackend(base::OnceClosure complete_callback) {
|
|
|
|
if (is_server_backend_) {
|
|
|
|
ShutdownServer(std::move(complete_callback));
|
|
|
|
} else {
|
|
|
|
RemoveSchemeHandler(std::move(complete_callback));
|
|
|
|
}
|
2017-11-17 17:33:50 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void ShutdownServer(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_TRUE(server_handler_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2020-08-20 00:27:27 +02:00
|
|
|
// |server_handler_| will delete itself after shutdown.
|
2021-06-19 21:54:45 +02:00
|
|
|
server_handler_->ShutdownServer(std::move(complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
server_handler_ = nullptr;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2021-06-19 21:54:45 +02:00
|
|
|
void RemoveSchemeHandler(base::OnceClosure complete_callback) {
|
2017-11-17 17:33:50 +01:00
|
|
|
EXPECT_TRUE(scheme_factory_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Remove the factory registration.
|
|
|
|
request_context_->RegisterSchemeHandlerFactory(
|
2020-01-15 14:34:01 +01:00
|
|
|
GetRequestScheme(false), GetRequestHost(false, false), nullptr);
|
2021-06-19 21:54:45 +02:00
|
|
|
scheme_factory_->Shutdown(std::move(complete_callback));
|
2017-11-17 17:33:50 +01:00
|
|
|
scheme_factory_ = nullptr;
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
const bool is_server_backend_;
|
|
|
|
const bool use_frame_method_;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Used with incomplete request tests.
|
2021-06-19 21:54:45 +02:00
|
|
|
base::OnceClosure incomplete_request_callback_;
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Primary thread runner (UI thread) for the object that owns us.
|
2016-11-15 18:56:02 +01:00
|
|
|
CefRefPtr<CefTaskRunner> owner_task_runner_;
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefRequestContext> request_context_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
// Frame that originates the request. May be nullptr.
|
|
|
|
CefRefPtr<CefFrame> frame_;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
struct TestEntry {
|
|
|
|
TestCallback setup;
|
|
|
|
TestCallback run;
|
|
|
|
};
|
|
|
|
typedef std::map<RequestTestMode, TestEntry> TestMap;
|
|
|
|
TestMap test_map_;
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Server backend.
|
2020-08-20 00:27:27 +02:00
|
|
|
RequestServerHandler* server_handler_ = nullptr;
|
2017-11-17 17:33:50 +01:00
|
|
|
|
|
|
|
// Scheme handler backend.
|
2012-06-19 18:29:49 +02:00
|
|
|
std::string scheme_name_;
|
|
|
|
CefRefPtr<RequestSchemeHandlerFactory> scheme_factory_;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir post_file_tmpdir_;
|
2013-11-25 18:31:59 +01:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
|
|
|
RequestRunSettings settings_;
|
|
|
|
};
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
class RequestTestHandler : public TestHandler {
|
2012-06-19 18:29:49 +02:00
|
|
|
public:
|
|
|
|
RequestTestHandler(RequestTestMode test_mode,
|
2015-03-02 21:25:14 +01:00
|
|
|
ContextTestMode context_mode,
|
2017-11-17 17:33:50 +01:00
|
|
|
bool test_server_backend,
|
2020-02-10 18:10:17 +01:00
|
|
|
bool test_frame_method)
|
2017-05-17 11:29:28 +02:00
|
|
|
: test_mode_(test_mode),
|
|
|
|
context_mode_(context_mode),
|
2017-11-17 17:33:50 +01:00
|
|
|
test_server_backend_(test_server_backend),
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method_(test_frame_method),
|
2020-02-10 18:10:17 +01:00
|
|
|
test_url_(GetRequestOrigin(test_server_backend) +
|
|
|
|
"/URLRequestTest.Test") {}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2016-11-15 18:56:02 +01:00
|
|
|
// Time out the test after a reasonable period of time.
|
2023-01-13 19:49:33 +01:00
|
|
|
SetTestTimeout(5000);
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
// Start pre-setup actions.
|
|
|
|
PreSetupStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupStart() {
|
2020-09-25 03:40:47 +02:00
|
|
|
CefPostTask(TID_FILE_USER_VISIBLE,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PreSetupFileTasks, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupFileTasks() {
|
2020-09-25 03:40:47 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE_USER_VISIBLE));
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
if (context_mode_ == CONTEXT_ONDISK) {
|
2021-04-07 00:09:45 +02:00
|
|
|
EXPECT_TRUE(context_tmpdir_.CreateUniqueTempDirUnderPath(
|
|
|
|
CefTestSuite::GetInstance()->root_cache_path()));
|
2016-11-15 18:56:02 +01:00
|
|
|
context_tmpdir_path_ = context_tmpdir_.GetPath();
|
|
|
|
EXPECT_FALSE(context_tmpdir_path_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefPostTask(TID_UI,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PreSetupContinue, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupContinue() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
test_runner_ = new RequestTestRunner(
|
2023-02-27 19:33:56 +01:00
|
|
|
test_server_backend_, test_frame_method_,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::OnIncompleteRequest, this));
|
2021-06-18 18:41:17 +02:00
|
|
|
test_runner_->Initialize();
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2023-10-18 00:16:06 +02:00
|
|
|
// Configure the number of times that SignalTestCompletion() will be called.
|
|
|
|
// We need to call it at least 1 time if we don't create a browser.
|
|
|
|
size_t completion_count = test_frame_method_ ? 0U : 1U;
|
|
|
|
if (context_mode_ != CONTEXT_GLOBAL) {
|
|
|
|
// Don't end the test until the temporary request context has been
|
|
|
|
// destroyed.
|
|
|
|
completion_count++;
|
|
|
|
}
|
|
|
|
if (completion_count > 0U) {
|
|
|
|
SetSignalTestCompletionCount(completion_count);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
// Get or create the request context.
|
|
|
|
if (context_mode_ == CONTEXT_GLOBAL) {
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
CefRequestContext::GetGlobalContext();
|
|
|
|
EXPECT_TRUE(request_context.get());
|
|
|
|
test_runner_->SetRequestContext(request_context);
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
PreSetupComplete();
|
2015-03-02 21:25:14 +01:00
|
|
|
} else {
|
|
|
|
CefRequestContextSettings settings;
|
|
|
|
|
|
|
|
if (context_mode_ == CONTEXT_ONDISK) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_FALSE(context_tmpdir_.IsEmpty());
|
|
|
|
CefString(&settings.cache_path) = context_tmpdir_path_;
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2021-04-09 20:34:45 +02:00
|
|
|
if (!test_server_backend_) {
|
|
|
|
// Set the schemes that are allowed to store cookies.
|
|
|
|
CefString(&settings.cookieable_schemes_list) = GetRequestScheme(false);
|
|
|
|
}
|
|
|
|
|
2021-04-07 00:09:45 +02:00
|
|
|
// Create a new temporary request context. Calls OnContextInitialized.
|
|
|
|
CefRequestContext::CreateContext(settings,
|
|
|
|
new RequestContextHandler(this));
|
|
|
|
}
|
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2021-04-07 00:09:45 +02:00
|
|
|
void OnContextInitialized(CefRefPtr<CefRequestContext> request_context) {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
EXPECT_TRUE(request_context.get());
|
|
|
|
test_runner_->SetRequestContext(request_context);
|
2021-04-09 20:34:45 +02:00
|
|
|
PreSetupComplete();
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void PreSetupComplete() {
|
2015-03-02 21:25:14 +01:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PreSetupComplete, this));
|
2015-03-02 21:25:14 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Setup the test. This will create the objects that we test against and
|
2017-11-17 17:33:50 +01:00
|
|
|
// register the backend.
|
|
|
|
test_runner_->SetupTest(
|
2021-06-19 21:54:45 +02:00
|
|
|
test_mode_, base::BindOnce(&RequestTestHandler::OnSetupComplete, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Browser process setup is complete.
|
2017-11-17 17:33:50 +01:00
|
|
|
void OnSetupComplete() {
|
2016-11-15 18:56:02 +01:00
|
|
|
// Start post-setup actions.
|
2018-03-20 21:15:08 +01:00
|
|
|
SetTestCookie(test_runner_->GetRequestContext(), test_server_backend_,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PostSetupComplete, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void PostSetupComplete() {
|
2018-03-20 21:15:08 +01:00
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PostSetupComplete, this));
|
2018-03-20 21:15:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
if (test_frame_method_) {
|
2015-03-02 21:25:14 +01:00
|
|
|
AddResource(test_url_, "<html><body>TEST</body></html>", "text/html");
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Create the browser who's main frame will be the initiator for the
|
|
|
|
// request.
|
2015-03-02 21:25:14 +01:00
|
|
|
CreateBrowser(test_url_, test_runner_->GetRequestContext());
|
2023-02-27 19:33:56 +01:00
|
|
|
} else {
|
|
|
|
// Run the test now.
|
|
|
|
test_running_ = true;
|
|
|
|
test_runner_->RunTest(
|
|
|
|
test_mode_, nullptr /* frame */,
|
|
|
|
base::BindOnce(&RequestTestHandler::OnRunComplete, this));
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2021-09-27 11:09:45 +02:00
|
|
|
ReturnValue OnBeforeResourceLoad(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2019-05-11 00:14:48 +02:00
|
|
|
if (test_running_ && test_frame_method_) {
|
|
|
|
EXPECT_TRUE(frame);
|
2024-01-26 03:12:43 +01:00
|
|
|
EXPECT_STREQ(test_frame_->GetIdentifier().ToString().c_str(),
|
|
|
|
frame->GetIdentifier().ToString().c_str());
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_resource_load_ct_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TestHandler::OnBeforeResourceLoad(browser, frame, request, callback);
|
|
|
|
}
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
bool GetAuthCredentials(CefRefPtr<CefBrowser> browser,
|
|
|
|
const CefString& origin_url,
|
|
|
|
bool isProxy,
|
|
|
|
const CefString& host,
|
|
|
|
int port,
|
|
|
|
const CefString& realm,
|
|
|
|
const CefString& scheme,
|
|
|
|
CefRefPtr<CefAuthCallback> callback) override {
|
|
|
|
EXPECT_TRUE(test_frame_method_);
|
|
|
|
auth_credentials_ct_++;
|
|
|
|
if (test_runner_->settings_.expect_authentication) {
|
|
|
|
callback->Continue(test_runner_->settings_.username,
|
|
|
|
test_runner_->settings_.password);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2023-02-27 19:33:56 +01:00
|
|
|
if (test_frame_method_) {
|
2019-05-11 00:14:48 +02:00
|
|
|
// Run the test now.
|
|
|
|
test_frame_ = frame;
|
|
|
|
test_running_ = true;
|
|
|
|
test_runner_->RunTest(
|
|
|
|
test_mode_, frame,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::OnRunComplete, this));
|
2019-05-11 00:14:48 +02:00
|
|
|
return;
|
|
|
|
}
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Incomplete tests will not complete normally. Instead, we trigger a browser
|
|
|
|
// close to abort in-progress requests.
|
|
|
|
void OnIncompleteRequest() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(
|
|
|
|
&RequestTestHandler::OnIncompleteRequest, this));
|
2019-05-31 11:50:12 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(test_frame_method_);
|
|
|
|
EXPECT_NE(RequestRunSettings::INCOMPLETE_NONE,
|
|
|
|
test_runner_->settings_.incomplete_type);
|
|
|
|
|
|
|
|
// TestComplete will eventually be called from DestroyTest instead of being
|
|
|
|
// triggered by browser destruction.
|
|
|
|
CefPostDelayedTask(
|
2021-06-19 21:54:45 +02:00
|
|
|
TID_UI, base::BindOnce(&TestHandler::CloseBrowser, GetBrowser(), false),
|
2019-10-04 14:36:06 +02:00
|
|
|
1000);
|
2019-05-31 11:50:12 +02:00
|
|
|
}
|
|
|
|
|
2023-02-27 19:33:56 +01:00
|
|
|
// Test run is complete.
|
2017-11-17 17:33:50 +01:00
|
|
|
void OnRunComplete() {
|
2018-03-20 21:15:08 +01:00
|
|
|
GetTestCookie(test_runner_->GetRequestContext(), test_server_backend_,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::PostRunComplete, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
void PostRunComplete(bool has_save_cookie) {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(&RequestTestHandler::PostRunComplete,
|
|
|
|
this, has_save_cookie));
|
2018-03-20 21:15:08 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_EQ(test_runner_->settings_.expect_save_cookie, has_save_cookie);
|
|
|
|
|
2017-11-17 17:33:50 +01:00
|
|
|
// Shut down the browser side of the test.
|
|
|
|
test_runner_->ShutdownTest(
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::MaybeClearAuthCredentials, this));
|
2019-07-12 22:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MaybeClearAuthCredentials() {
|
|
|
|
if (test_runner_->settings_.expect_authentication &&
|
|
|
|
context_mode_ == CONTEXT_GLOBAL) {
|
|
|
|
// Clear the HTTP authentication cache to avoid leaking state between
|
|
|
|
// test runs when using the global request context.
|
|
|
|
test_runner_->GetRequestContext()->ClearHttpAuthCredentials(
|
|
|
|
new TestCompletionCallback(
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::DestroyTest, this)));
|
2019-07-12 22:44:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DestroyTest();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void DestroyTest() override {
|
2019-07-29 23:27:12 +02:00
|
|
|
if (test_frame_method_) {
|
|
|
|
// Expect at least 1 call to OnBeforeResourceLoad for every test.
|
|
|
|
// Redirect tests may get multiple calls.
|
|
|
|
EXPECT_LE(1, test_frame_resource_load_ct_);
|
2019-05-11 00:14:48 +02:00
|
|
|
}
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
// CefRequestHandler::GetAuthCredentials should be called after
|
|
|
|
// CefURLRequestClient::GetAuthCredentials when the request has an
|
|
|
|
// associated frame.
|
2023-02-27 19:33:56 +01:00
|
|
|
if (test_frame_method_ && test_runner_->settings_.expect_authentication) {
|
2019-07-12 22:44:43 +02:00
|
|
|
EXPECT_EQ(1, auth_credentials_ct_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, auth_credentials_ct_);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
TestHandler::DestroyTest();
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Release references to the context and handler.
|
2016-11-15 18:56:02 +01:00
|
|
|
test_runner_->Destroy();
|
2015-03-02 21:25:14 +01:00
|
|
|
|
2023-10-18 00:16:06 +02:00
|
|
|
// These tests don't create a browser that would signal implicitly.
|
|
|
|
if (!test_frame_method_) {
|
2016-11-15 18:56:02 +01:00
|
|
|
OnTestComplete();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void OnTestComplete() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI,
|
2021-06-19 21:54:45 +02:00
|
|
|
base::BindOnce(&RequestTestHandler::OnTestComplete, this));
|
2016-11-15 18:56:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context_tmpdir_.IsEmpty()) {
|
2021-04-07 00:09:45 +02:00
|
|
|
// Temp directory will be deleted on application shutdown.
|
|
|
|
context_tmpdir_.Take();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
2023-10-18 00:16:06 +02:00
|
|
|
SignalTestCompletion();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
private:
|
|
|
|
// Used with temporary request contexts to signal test completion once the
|
|
|
|
// temporary context has been destroyed.
|
|
|
|
class RequestContextHandler : public CefRequestContextHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestContextHandler(CefRefPtr<RequestTestHandler> test_handler)
|
2017-05-17 11:29:28 +02:00
|
|
|
: test_handler_(test_handler) {}
|
|
|
|
~RequestContextHandler() override { test_handler_->OnTestComplete(); }
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2021-04-07 00:09:45 +02:00
|
|
|
void OnRequestContextInitialized(
|
|
|
|
CefRefPtr<CefRequestContext> request_context) override {
|
|
|
|
test_handler_->OnContextInitialized(request_context);
|
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
private:
|
|
|
|
CefRefPtr<RequestTestHandler> test_handler_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestContextHandler);
|
|
|
|
};
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
const RequestTestMode test_mode_;
|
|
|
|
const ContextTestMode context_mode_;
|
|
|
|
const bool test_server_backend_;
|
|
|
|
const bool test_frame_method_;
|
2020-02-10 18:10:17 +01:00
|
|
|
const std::string test_url_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<RequestTestRunner> test_runner_;
|
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
bool test_running_ = false;
|
|
|
|
|
|
|
|
CefRefPtr<CefFrame> test_frame_;
|
|
|
|
int test_frame_resource_load_ct_ = 0;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir context_tmpdir_;
|
|
|
|
CefString context_tmpdir_path_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
public:
|
2019-07-12 22:44:43 +02:00
|
|
|
int auth_credentials_ct_ = 0;
|
2019-05-31 11:50:12 +02:00
|
|
|
|
2015-09-09 16:05:39 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestTestHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Entry point for registering custom schemes.
|
|
|
|
// Called from client_app_delegates.cc.
|
2021-04-09 20:34:45 +02:00
|
|
|
void RegisterURLRequestCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar) {
|
2017-11-17 17:33:50 +01:00
|
|
|
const std::string& scheme = GetRequestScheme(false);
|
2019-02-26 17:45:12 +01:00
|
|
|
registrar->AddCustomScheme(
|
|
|
|
scheme, CEF_SCHEME_OPTION_STANDARD | CEF_SCHEME_OPTION_CORS_ENABLED);
|
2021-04-09 20:34:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Entry point for registering cookieable schemes.
|
|
|
|
// Called from client_app_delegates.cc.
|
|
|
|
void RegisterURLRequestCookieableSchemes(
|
|
|
|
std::vector<std::string>& cookieable_schemes) {
|
|
|
|
const std::string& scheme = GetRequestScheme(false);
|
|
|
|
cookieable_schemes.push_back(scheme);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers for defining URLRequest tests.
|
2023-02-27 19:33:56 +01:00
|
|
|
#define REQ_TEST(name, test_mode, context_mode, test_server_backend, \
|
|
|
|
test_frame_method) \
|
|
|
|
TEST(URLRequestTest, name) { \
|
|
|
|
CefRefPtr<RequestTestHandler> handler = new RequestTestHandler( \
|
|
|
|
test_mode, context_mode, test_server_backend, test_frame_method); \
|
|
|
|
handler->ExecuteTest(); \
|
|
|
|
ReleaseAndWaitForDestructor(handler); \
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Define the tests.
|
2019-05-11 00:14:48 +02:00
|
|
|
#define REQ_TEST_SET_EX(suffix, context_mode, test_server_backend, \
|
|
|
|
test_frame_method) \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQ_TEST(BrowserGET##suffix, REQTEST_GET, context_mode, test_server_backend, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST(BrowserGETNoData##suffix, REQTEST_GET_NODATA, context_mode, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_server_backend, test_frame_method) \
|
2020-04-20 19:19:42 +02:00
|
|
|
REQ_TEST(BrowserGETPartialContent##suffix, REQTEST_GET_PARTIAL_CONTENT, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
2017-11-17 17:33:50 +01:00
|
|
|
REQ_TEST(BrowserGETAllowCookies##suffix, REQTEST_GET_ALLOWCOOKIES, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
2017-11-17 17:33:50 +01:00
|
|
|
REQ_TEST(BrowserGETRedirect##suffix, REQTEST_GET_REDIRECT, context_mode, \
|
2023-02-27 19:33:56 +01:00
|
|
|
test_server_backend, test_frame_method) \
|
2018-04-10 19:04:57 +02:00
|
|
|
REQ_TEST(BrowserGETRedirectStop##suffix, REQTEST_GET_REDIRECT_STOP, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
2019-01-11 16:12:06 +01:00
|
|
|
REQ_TEST(BrowserGETRedirectLocation##suffix, REQTEST_GET_REDIRECT_LOCATION, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
2017-11-17 17:33:50 +01:00
|
|
|
REQ_TEST(BrowserGETReferrer##suffix, REQTEST_GET_REFERRER, context_mode, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_server_backend, test_frame_method) \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQ_TEST(BrowserPOST##suffix, REQTEST_POST, context_mode, \
|
|
|
|
test_server_backend, test_frame_method) \
|
|
|
|
REQ_TEST(BrowserPOSTFile##suffix, REQTEST_POST_FILE, context_mode, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_server_backend, test_frame_method) \
|
2017-11-17 17:33:50 +01:00
|
|
|
REQ_TEST(BrowserPOSTWithProgress##suffix, REQTEST_POST_WITHPROGRESS, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
2019-07-11 22:36:27 +02:00
|
|
|
REQ_TEST(BrowserPOSTRedirect##suffix, REQTEST_POST_REDIRECT, context_mode, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_server_backend, test_frame_method) \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQ_TEST(BrowserPOSTRedirectToGET##suffix, REQTEST_POST_REDIRECT_TOGET, \
|
|
|
|
context_mode, test_server_backend, test_frame_method) \
|
|
|
|
REQ_TEST(BrowserHEAD##suffix, REQTEST_HEAD, context_mode, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_server_backend, test_frame_method)
|
|
|
|
|
|
|
|
#define REQ_TEST_SET(suffix, test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextGlobalCustom##suffix, CONTEXT_GLOBAL, false, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextInMemoryCustom##suffix, CONTEXT_INMEMORY, false, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextOnDiskCustom##suffix, CONTEXT_ONDISK, false, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextGlobalServer##suffix, CONTEXT_GLOBAL, true, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextInMemoryServer##suffix, CONTEXT_INMEMORY, true, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_SET_EX(ContextOnDiskServer##suffix, CONTEXT_ONDISK, true, \
|
|
|
|
test_frame_method)
|
|
|
|
|
|
|
|
REQ_TEST_SET(WithoutFrame, false)
|
|
|
|
REQ_TEST_SET(WithFrame, true)
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2019-05-31 11:50:12 +02:00
|
|
|
// Define tests that can only run with a frame.
|
|
|
|
#define REQ_TEST_FRAME_SET_EX(suffix, context_mode, test_server_backend) \
|
|
|
|
REQ_TEST(BrowserIncompleteProcessRequest##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_INCOMPLETE_PROCESS_REQUEST, context_mode, \
|
2019-05-31 11:50:12 +02:00
|
|
|
test_server_backend, true) \
|
|
|
|
REQ_TEST(BrowserIncompleteReadResponse##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_INCOMPLETE_READ_RESPONSE, context_mode, \
|
2019-05-31 11:50:12 +02:00
|
|
|
test_server_backend, true)
|
|
|
|
|
|
|
|
#define REQ_TEST_FRAME_SET() \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextGlobalCustomWithFrame, CONTEXT_GLOBAL, false) \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextInMemoryCustomWithFrame, CONTEXT_INMEMORY, \
|
|
|
|
false) \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextOnDiskCustomWithFrame, CONTEXT_ONDISK, false) \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextGlobalServerWithFrame, CONTEXT_GLOBAL, true) \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextInMemoryServerWithFrame, CONTEXT_INMEMORY, \
|
|
|
|
true) \
|
|
|
|
REQ_TEST_FRAME_SET_EX(ContextOnDiskServerWithFrame, CONTEXT_ONDISK, true)
|
|
|
|
|
|
|
|
REQ_TEST_FRAME_SET()
|
|
|
|
|
2019-07-12 22:44:43 +02:00
|
|
|
// Cache and authentication tests can only be run with the server backend.
|
2019-05-11 00:14:48 +02:00
|
|
|
#define REQ_TEST_CACHE_SET_EX(suffix, context_mode, test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheWithControl##suffix, REQTEST_CACHE_WITH_CONTROL, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, true, test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheWithoutControl##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_CACHE_WITHOUT_CONTROL, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheSkipFlag##suffix, REQTEST_CACHE_SKIP_FLAG, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, true, test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheSkipHeader##suffix, REQTEST_CACHE_SKIP_HEADER, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, true, test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheOnlyFailureFlag##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_CACHE_ONLY_FAILURE_FLAG, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheOnlyFailureHeader##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_CACHE_ONLY_FAILURE_HEADER, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheOnlySuccessFlag##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_CACHE_ONLY_SUCCESS_FLAG, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheOnlySuccessHeader##suffix, \
|
2023-02-27 19:33:56 +01:00
|
|
|
REQTEST_CACHE_ONLY_SUCCESS_HEADER, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheDisableFlag##suffix, REQTEST_CACHE_DISABLE_FLAG, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, true, test_frame_method) \
|
2019-03-07 23:05:36 +01:00
|
|
|
REQ_TEST(BrowserGETCacheDisableHeader##suffix, REQTEST_CACHE_DISABLE_HEADER, \
|
2023-02-27 19:33:56 +01:00
|
|
|
context_mode, true, test_frame_method) \
|
|
|
|
REQ_TEST(BrowserGETAuth##suffix, REQTEST_GET_AUTH, context_mode, true, \
|
2019-05-11 00:14:48 +02:00
|
|
|
test_frame_method)
|
2017-11-17 17:33:50 +01:00
|
|
|
|
2019-05-11 00:14:48 +02:00
|
|
|
#define REQ_TEST_CACHE_SET(suffix, test_frame_method) \
|
|
|
|
REQ_TEST_CACHE_SET_EX(ContextGlobalServer##suffix, CONTEXT_GLOBAL, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_CACHE_SET_EX(ContextInMemoryServer##suffix, CONTEXT_INMEMORY, \
|
|
|
|
test_frame_method) \
|
|
|
|
REQ_TEST_CACHE_SET_EX(ContextOnDiskServer##suffix, CONTEXT_ONDISK, \
|
|
|
|
test_frame_method)
|
|
|
|
|
|
|
|
REQ_TEST_CACHE_SET(WithoutFrame, false)
|
|
|
|
REQ_TEST_CACHE_SET(WithFrame, true)
|
2014-02-11 00:28:58 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class InvalidURLTestClient : public CefURLRequestClient {
|
|
|
|
public:
|
2016-11-15 22:18:41 +01:00
|
|
|
InvalidURLTestClient() {
|
|
|
|
event_ = CefWaitableEvent::CreateWaitableEvent(true, false);
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void RunTest() {
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::BindOnce(&InvalidURLTestClient::RunOnUIThread, this));
|
2014-02-11 00:28:58 +01:00
|
|
|
|
|
|
|
// Wait for the test to complete.
|
2016-11-15 22:18:41 +01:00
|
|
|
event_->Wait();
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnRequestComplete(CefRefPtr<CefURLRequest> client) override {
|
2014-02-11 00:28:58 +01:00
|
|
|
EXPECT_EQ(UR_FAILED, client->GetRequestStatus());
|
2019-07-29 23:27:12 +02:00
|
|
|
EXPECT_EQ(ERR_UNKNOWN_URL_SCHEME, client->GetRequestError());
|
2014-02-11 00:28:58 +01:00
|
|
|
|
|
|
|
// Let the call stack unwind before signaling completion.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(
|
|
|
|
&InvalidURLTestClient::CompleteOnUIThread, this));
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnUploadProgress(CefRefPtr<CefURLRequest> request,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t current,
|
|
|
|
int64_t total) override {
|
2014-02-11 00:28:58 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
|
2023-06-01 16:06:15 +02:00
|
|
|
int64_t current,
|
|
|
|
int64_t total) override {
|
2014-02-11 00:28:58 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnDownloadData(CefRefPtr<CefURLRequest> request,
|
|
|
|
const void* data,
|
|
|
|
size_t data_length) override {
|
2014-02-11 00:28:58 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool GetAuthCredentials(bool isProxy,
|
|
|
|
const CefString& host,
|
|
|
|
int port,
|
|
|
|
const CefString& realm,
|
|
|
|
const CefString& scheme,
|
|
|
|
CefRefPtr<CefAuthCallback> callback) override {
|
2014-02-11 00:28:58 +01:00
|
|
|
EXPECT_TRUE(false); // Not reached.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void RunOnUIThread() {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
CefRefPtr<CefRequest> request = CefRequest::Create();
|
|
|
|
request->SetMethod("GET");
|
|
|
|
request->SetURL("foo://invalidurl");
|
|
|
|
|
2020-01-15 14:34:01 +01:00
|
|
|
CefURLRequest::Create(request, this, nullptr);
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void CompleteOnUIThread() {
|
|
|
|
EXPECT_UI_THREAD();
|
|
|
|
// Signal that the test is complete.
|
2016-11-15 22:18:41 +01:00
|
|
|
event_->Signal();
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2014-02-11 00:28:58 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(InvalidURLTestClient);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify that failed requests do not leak references.
|
2017-05-17 11:29:28 +02:00
|
|
|
TEST(URLRequestTest, BrowserInvalidURL) {
|
2014-02-11 00:28:58 +01:00
|
|
|
CefRefPtr<InvalidURLTestClient> client = new InvalidURLTestClient();
|
|
|
|
client->RunTest();
|
|
|
|
}
|
2020-09-25 03:40:47 +02:00
|
|
|
|
2022-02-18 21:43:30 +01:00
|
|
|
// Entry point for creating URLRequest browser test objects.
|
2020-09-25 03:40:47 +02:00
|
|
|
// Called from client_app_delegates.cc.
|
|
|
|
void CreateURLRequestBrowserTests(
|
|
|
|
client::ClientAppBrowser::DelegateSet& delegates) {
|
|
|
|
delegates.insert(new URLRequestBrowserTest);
|
|
|
|
}
|