2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2011 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.
|
|
|
|
|
2013-09-10 21:42:53 +02:00
|
|
|
#include <map>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "include/cef_request.h"
|
2014-07-15 20:10:40 +02:00
|
|
|
#include "include/wrapper/cef_closure_task.h"
|
2016-11-18 18:31:21 +01:00
|
|
|
#include "tests/ceftests/test_handler.h"
|
|
|
|
#include "tests/ceftests/test_util.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
|
|
|
#include "tests/shared/renderer/client_app_renderer.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
using client::ClientAppRenderer;
|
2015-01-23 20:09:34 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Verify Set/Get methods for CefRequest, CefPostData and CefPostDataElement.
|
|
|
|
TEST(RequestTest, SetGet) {
|
|
|
|
// CefRequest CreateRequest
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefRequest> request(CefRequest::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(request.get() != nullptr);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(0U, request->GetIdentifier());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2020-03-04 01:29:39 +01:00
|
|
|
CefString url = "http://tests.com/run.html";
|
2012-04-03 03:34:16 +02:00
|
|
|
CefString method = "POST";
|
|
|
|
CefRequest::HeaderMap setHeaders, getHeaders;
|
|
|
|
setHeaders.insert(std::make_pair("HeaderA", "ValueA"));
|
|
|
|
setHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
|
|
|
|
// CefPostData CreatePostData
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData(CefPostData::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(postData.get() != nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostDataElement CreatePostDataElement
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element1(CefPostDataElement::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(element1.get() != nullptr);
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element2(CefPostDataElement::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(element2.get() != nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostDataElement SetToFile
|
|
|
|
CefString file = "c:\\path\\to\\file.ext";
|
|
|
|
element1->SetToFile(file);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(PDE_TYPE_FILE, element1->GetType());
|
|
|
|
EXPECT_EQ(file, element1->GetFile());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostDataElement SetToBytes
|
|
|
|
char bytes[] = "Test Bytes";
|
|
|
|
element2->SetToBytes(sizeof(bytes), bytes);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(PDE_TYPE_BYTES, element2->GetType());
|
|
|
|
EXPECT_EQ(sizeof(bytes), element2->GetBytesCount());
|
2012-04-03 03:34:16 +02:00
|
|
|
char bytesOut[sizeof(bytes)];
|
|
|
|
element2->GetBytes(sizeof(bytes), bytesOut);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_TRUE(!memcmp(bytes, bytesOut, sizeof(bytes)));
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostData AddElement
|
|
|
|
postData->AddElement(element1);
|
|
|
|
postData->AddElement(element2);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ((size_t)2, postData->GetElementCount());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostData RemoveElement
|
|
|
|
postData->RemoveElement(element1);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ((size_t)1, postData->GetElementCount());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefPostData RemoveElements
|
|
|
|
postData->RemoveElements();
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ((size_t)0, postData->GetElementCount());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
postData->AddElement(element1);
|
|
|
|
postData->AddElement(element2);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ((size_t)2, postData->GetElementCount());
|
2012-04-03 03:34:16 +02:00
|
|
|
CefPostData::ElementVector elements;
|
|
|
|
postData->GetElements(elements);
|
|
|
|
CefPostData::ElementVector::const_iterator it = elements.begin();
|
|
|
|
for (size_t i = 0; it != elements.end(); ++it, ++i) {
|
|
|
|
if (i == 0)
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataElementEqual(element1, (*it).get());
|
2012-04-03 03:34:16 +02:00
|
|
|
else if (i == 1)
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataElementEqual(element2, (*it).get());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// CefRequest SetURL
|
|
|
|
request->SetURL(url);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(url, request->GetURL());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefRequest SetMethod
|
|
|
|
request->SetMethod(method);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(method, request->GetMethod());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-12-01 19:22:28 +01:00
|
|
|
// CefRequest SetReferrer
|
|
|
|
CefString referrer = "http://tests.com/referrer.html";
|
|
|
|
CefRequest::ReferrerPolicy policy = REFERRER_POLICY_ORIGIN;
|
|
|
|
request->SetReferrer(referrer, policy);
|
2020-03-04 01:29:39 +01:00
|
|
|
EXPECT_STREQ("http://tests.com/",
|
|
|
|
request->GetReferrerURL().ToString().c_str());
|
2015-12-01 19:22:28 +01:00
|
|
|
EXPECT_EQ(policy, request->GetReferrerPolicy());
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// CefRequest SetHeaderMap
|
|
|
|
request->SetHeaderMap(setHeaders);
|
|
|
|
request->GetHeaderMap(getHeaders);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMapEqual(setHeaders, getHeaders, false);
|
2012-04-03 03:34:16 +02:00
|
|
|
getHeaders.clear();
|
|
|
|
|
|
|
|
// CefRequest SetPostData
|
|
|
|
request->SetPostData(postData);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataEqual(postData, request->GetPostData());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(0U, request->GetIdentifier());
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
request = CefRequest::Create();
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(request.get() != nullptr);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(0U, request->GetIdentifier());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
// CefRequest Set
|
|
|
|
request->Set(url, method, postData, setHeaders);
|
2015-03-11 19:44:11 +01:00
|
|
|
EXPECT_EQ(0U, request->GetIdentifier());
|
|
|
|
EXPECT_EQ(url, request->GetURL());
|
|
|
|
EXPECT_EQ(method, request->GetMethod());
|
2012-04-03 03:34:16 +02:00
|
|
|
request->GetHeaderMap(getHeaders);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMapEqual(setHeaders, getHeaders, false);
|
2012-04-03 03:34:16 +02:00
|
|
|
getHeaders.clear();
|
2012-06-19 18:29:49 +02:00
|
|
|
TestPostDataEqual(postData, request->GetPostData());
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 04:50:25 +02:00
|
|
|
TEST(RequestTest, SetGetHeaderByName) {
|
|
|
|
CefRefPtr<CefRequest> request(CefRequest::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(request.get() != nullptr);
|
2019-04-24 04:50:25 +02:00
|
|
|
|
|
|
|
CefRequest::HeaderMap headers, expectedHeaders;
|
|
|
|
|
|
|
|
request->SetHeaderByName("HeaderA", "ValueA", false);
|
|
|
|
request->SetHeaderByName("HeaderB", "ValueB", false);
|
|
|
|
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderA", "ValueA"));
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
|
|
|
|
// Case insensitive retrieval.
|
|
|
|
EXPECT_STREQ("ValueA",
|
|
|
|
request->GetHeaderByName("headera").ToString().c_str());
|
|
|
|
EXPECT_STREQ("ValueB",
|
|
|
|
request->GetHeaderByName("headerb").ToString().c_str());
|
|
|
|
EXPECT_STREQ("", request->GetHeaderByName("noexist").ToString().c_str());
|
|
|
|
|
|
|
|
request->GetHeaderMap(headers);
|
|
|
|
TestMapEqual(expectedHeaders, headers, false);
|
|
|
|
|
|
|
|
// Replace an existing value.
|
|
|
|
request->SetHeaderByName("HeaderA", "ValueANew", true);
|
|
|
|
|
|
|
|
expectedHeaders.clear();
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderA", "ValueANew"));
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
|
|
|
|
// Case insensitive retrieval.
|
|
|
|
EXPECT_STREQ("ValueANew",
|
|
|
|
request->GetHeaderByName("headerA").ToString().c_str());
|
|
|
|
|
|
|
|
request->GetHeaderMap(headers);
|
|
|
|
TestMapEqual(expectedHeaders, headers, false);
|
|
|
|
|
|
|
|
// Header with multiple values.
|
|
|
|
expectedHeaders.clear();
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderA", "ValueA1"));
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderA", "ValueA2"));
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
request->SetHeaderMap(expectedHeaders);
|
|
|
|
|
|
|
|
// When there are multiple values only the first is returned.
|
|
|
|
EXPECT_STREQ("ValueA1",
|
|
|
|
request->GetHeaderByName("headera").ToString().c_str());
|
|
|
|
|
|
|
|
// Don't overwrite the value.
|
|
|
|
request->SetHeaderByName("HeaderA", "ValueANew", false);
|
|
|
|
|
|
|
|
request->GetHeaderMap(headers);
|
|
|
|
TestMapEqual(expectedHeaders, headers, false);
|
|
|
|
|
|
|
|
// Overwrite the value (remove the duplicates).
|
|
|
|
request->SetHeaderByName("HeaderA", "ValueANew", true);
|
|
|
|
|
|
|
|
expectedHeaders.clear();
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderA", "ValueANew"));
|
|
|
|
expectedHeaders.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
|
|
|
|
request->GetHeaderMap(headers);
|
|
|
|
TestMapEqual(expectedHeaders, headers, false);
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
namespace {
|
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
const char kTestUrl[] = "http://tests.com/run.html";
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
void CreateRequest(CefRefPtr<CefRequest>& request) {
|
2012-06-19 18:29:49 +02:00
|
|
|
request = CefRequest::Create();
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(request.get() != nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
request->SetURL(kTestUrl);
|
2012-04-03 03:34:16 +02:00
|
|
|
request->SetMethod("POST");
|
|
|
|
|
2017-07-27 01:19:27 +02:00
|
|
|
request->SetReferrer("http://tests.com/main.html", REFERRER_POLICY_DEFAULT);
|
2015-12-01 19:22:28 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRequest::HeaderMap headers;
|
|
|
|
headers.insert(std::make_pair("HeaderA", "ValueA"));
|
|
|
|
headers.insert(std::make_pair("HeaderB", "ValueB"));
|
|
|
|
request->SetHeaderMap(headers);
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefPostData> postData(CefPostData::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(postData.get() != nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<CefPostDataElement> element1(CefPostDataElement::Create());
|
2020-01-15 14:34:01 +01:00
|
|
|
EXPECT_TRUE(element1.get() != nullptr);
|
2012-04-03 03:34:16 +02:00
|
|
|
char bytes[] = "Test Bytes";
|
|
|
|
element1->SetToBytes(sizeof(bytes), bytes);
|
|
|
|
postData->AddElement(element1);
|
|
|
|
|
|
|
|
request->SetPostData(postData);
|
|
|
|
}
|
|
|
|
|
|
|
|
class RequestSendRecvTestHandler : public TestHandler {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
RequestSendRecvTestHandler() : response_length_(0), request_id_(0U) {}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2015-12-04 19:59:14 +01:00
|
|
|
// Create the test request.
|
2012-04-03 03:34:16 +02:00
|
|
|
CreateRequest(request_);
|
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
const std::string& resource = "<html><body>SendRecv Test</body></html>";
|
|
|
|
response_length_ = static_cast<int64>(resource.size());
|
|
|
|
AddResource(kTestUrl, resource, "text/html");
|
|
|
|
|
|
|
|
// Create the browser.
|
2014-07-16 23:27:25 +02:00
|
|
|
CreateBrowser("about:blank");
|
2015-01-10 00:40:26 +01:00
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
|
|
|
SetTestTimeout();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnAfterCreated(CefRefPtr<CefBrowser> browser) override {
|
2012-04-03 03:34:16 +02:00
|
|
|
TestHandler::OnAfterCreated(browser);
|
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
// Load the test request.
|
2012-04-03 03:34:16 +02:00
|
|
|
browser->GetMainFrame()->LoadRequest(request_);
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:21:46 +02:00
|
|
|
cef_return_value_t OnBeforeResourceLoad(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
2021-09-27 11:09:45 +02:00
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2015-12-04 19:59:14 +01:00
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
request_id_ = request->GetIdentifier();
|
|
|
|
DCHECK_GT(request_id_, 0U);
|
|
|
|
|
|
|
|
TestRequest(request);
|
|
|
|
EXPECT_FALSE(request->IsReadOnly());
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
got_before_resource_load_.yes();
|
|
|
|
|
2015-04-02 17:21:46 +02:00
|
|
|
return RV_CONTINUE;
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> GetResourceHandler(
|
2012-04-03 03:34:16 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override {
|
2015-12-04 19:59:14 +01:00
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
TestRequest(request);
|
2019-04-24 04:50:25 +02:00
|
|
|
EXPECT_TRUE(request->IsReadOnly());
|
2015-12-04 19:59:14 +01:00
|
|
|
|
|
|
|
got_resource_handler_.yes();
|
|
|
|
|
|
|
|
return TestHandler::GetResourceHandler(browser, frame, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
TestRequest(request);
|
|
|
|
EXPECT_FALSE(request->IsReadOnly());
|
|
|
|
TestResponse(response);
|
|
|
|
EXPECT_TRUE(response->IsReadOnly());
|
|
|
|
|
|
|
|
got_resource_response_.yes();
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-12-05 03:58:56 +01:00
|
|
|
CefRefPtr<CefResponseFilter> GetResourceResponseFilter(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
|
|
|
TestRequest(request);
|
|
|
|
EXPECT_TRUE(request->IsReadOnly());
|
|
|
|
TestResponse(response);
|
|
|
|
EXPECT_TRUE(response->IsReadOnly());
|
|
|
|
|
|
|
|
got_resource_response_filter_.yes();
|
2020-01-15 14:34:01 +01:00
|
|
|
return nullptr;
|
2015-12-05 03:58:56 +01:00
|
|
|
}
|
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
void OnResourceLoadComplete(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response,
|
|
|
|
URLRequestStatus status,
|
|
|
|
int64 received_content_length) override {
|
|
|
|
EXPECT_IO_THREAD();
|
|
|
|
|
2020-09-25 03:40:47 +02:00
|
|
|
if (IsChromeRuntimeEnabled() && request->GetResourceType() == RT_FAVICON) {
|
|
|
|
// Ignore favicon requests.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
TestRequest(request);
|
|
|
|
EXPECT_TRUE(request->IsReadOnly());
|
|
|
|
TestResponse(response);
|
|
|
|
EXPECT_TRUE(response->IsReadOnly());
|
|
|
|
EXPECT_EQ(UR_SUCCESS, status);
|
|
|
|
EXPECT_EQ(response_length_, received_content_length);
|
|
|
|
|
|
|
|
got_resource_load_complete_.yes();
|
|
|
|
|
|
|
|
DestroyTest();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void TestRequest(CefRefPtr<CefRequest> request) {
|
2012-06-19 18:29:49 +02:00
|
|
|
TestRequestEqual(request_, request, true);
|
2015-12-04 19:59:14 +01:00
|
|
|
EXPECT_EQ(request_id_, request->GetIdentifier());
|
2013-09-10 21:42:53 +02:00
|
|
|
EXPECT_EQ(RT_MAIN_FRAME, request->GetResourceType());
|
2018-10-02 14:14:11 +02:00
|
|
|
EXPECT_EQ(TT_FORM_SUBMIT, request->GetTransitionType());
|
2015-12-04 19:59:14 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
void TestResponse(CefRefPtr<CefResponse> response) {
|
|
|
|
EXPECT_EQ(200, response->GetStatus());
|
|
|
|
EXPECT_STREQ("OK", response->GetStatusText().ToString().c_str());
|
|
|
|
EXPECT_STREQ("text/html", response->GetMimeType().ToString().c_str());
|
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
void DestroyTest() override {
|
|
|
|
EXPECT_TRUE(got_before_resource_load_);
|
|
|
|
EXPECT_TRUE(got_resource_handler_);
|
|
|
|
EXPECT_TRUE(got_resource_response_);
|
2019-05-19 21:21:20 +02:00
|
|
|
EXPECT_TRUE(got_resource_response_filter_);
|
2015-12-04 19:59:14 +01:00
|
|
|
EXPECT_TRUE(got_resource_load_complete_);
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2015-12-04 19:59:14 +01:00
|
|
|
TestHandler::DestroyTest();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CefRefPtr<CefRequest> request_;
|
2015-12-04 19:59:14 +01:00
|
|
|
int64 response_length_;
|
|
|
|
uint64 request_id_;
|
2012-04-03 03:34:16 +02:00
|
|
|
|
|
|
|
TrackCallback got_before_resource_load_;
|
|
|
|
TrackCallback got_resource_handler_;
|
2015-12-04 19:59:14 +01:00
|
|
|
TrackCallback got_resource_response_;
|
2015-12-05 03:58:56 +01:00
|
|
|
TrackCallback got_resource_response_filter_;
|
2015-12-04 19:59:14 +01:00
|
|
|
TrackCallback got_resource_load_complete_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestSendRecvTestHandler);
|
2012-04-03 03:34:16 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify send and recieve
|
|
|
|
TEST(RequestTest, SendRecv) {
|
|
|
|
CefRefPtr<RequestSendRecvTestHandler> handler =
|
|
|
|
new RequestSendRecvTestHandler();
|
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char kTypeTestOrigin[] = "http://tests-requesttt.com/";
|
2020-02-14 22:58:58 +01:00
|
|
|
const cef_transition_type_t kTransitionExplicitLoad =
|
|
|
|
static_cast<cef_transition_type_t>(TT_EXPLICIT | TT_DIRECT_LOAD_FLAG);
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
static struct TypeExpected {
|
|
|
|
const char* file;
|
2018-02-13 00:13:23 +01:00
|
|
|
bool navigation; // True if this expectation represents a navigation.
|
2013-09-10 21:42:53 +02:00
|
|
|
cef_transition_type_t transition_type;
|
|
|
|
cef_resource_type_t resource_type;
|
|
|
|
int expected_count;
|
|
|
|
} g_type_expected[] = {
|
2017-05-17 11:29:28 +02:00
|
|
|
// Initial main frame load due to browser creation.
|
2020-02-14 22:58:58 +01:00
|
|
|
{"main.html", true, kTransitionExplicitLoad, RT_MAIN_FRAME, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// Sub frame load.
|
2018-10-02 14:14:11 +02:00
|
|
|
{"sub.html", true, TT_AUTO_SUBFRAME, RT_SUB_FRAME, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// Stylesheet load.
|
2018-02-13 00:01:43 +01:00
|
|
|
{"style.css", false, TT_LINK, RT_STYLESHEET, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// Script load.
|
2018-02-13 00:01:43 +01:00
|
|
|
{"script.js", false, TT_LINK, RT_SCRIPT, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// Image load.
|
2018-02-13 00:01:43 +01:00
|
|
|
{"image.png", false, TT_LINK, RT_IMAGE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// Font load.
|
2018-02-13 00:01:43 +01:00
|
|
|
{"font.ttf", false, TT_LINK, RT_FONT_RESOURCE, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
// XHR load.
|
2018-02-13 00:01:43 +01:00
|
|
|
{"xhr.html", false, TT_LINK, RT_XHR, 1},
|
2013-09-10 21:42:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
class TypeExpectations {
|
|
|
|
public:
|
2018-02-13 00:13:23 +01:00
|
|
|
explicit TypeExpectations(bool navigation) : navigation_(navigation) {
|
2013-09-10 21:42:53 +02:00
|
|
|
// Build the map of relevant requests.
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2018-02-13 00:01:43 +01:00
|
|
|
if (navigation_ && g_type_expected[i].navigation != navigation_)
|
2013-09-10 21:42:53 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
request_count_.insert(std::make_pair(i, 0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify that a request has been received. Returns true if the request is
|
|
|
|
// something we care about.
|
|
|
|
bool GotRequest(CefRefPtr<CefRequest> request) {
|
|
|
|
const std::string& url = request->GetURL();
|
|
|
|
if (url.find(kTypeTestOrigin) != 0)
|
|
|
|
return false;
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
const std::string& file = url.substr(sizeof(kTypeTestOrigin) - 1);
|
2013-09-10 21:42:53 +02:00
|
|
|
cef_transition_type_t transition_type = request->GetTransitionType();
|
|
|
|
cef_resource_type_t resource_type = request->GetResourceType();
|
|
|
|
|
|
|
|
const int index = GetExpectedIndex(file, transition_type, resource_type);
|
2017-05-17 11:29:28 +02:00
|
|
|
EXPECT_GE(index, 0) << "File: " << file.c_str()
|
|
|
|
<< "; Navigation: " << navigation_
|
|
|
|
<< "; Transition Type: " << transition_type
|
|
|
|
<< "; Resource Type: " << resource_type;
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
RequestCount::iterator it = request_count_.find(index);
|
|
|
|
EXPECT_TRUE(it != request_count_.end());
|
|
|
|
|
|
|
|
const int actual_count = ++it->second;
|
|
|
|
const int expected_count = g_type_expected[index].expected_count;
|
|
|
|
EXPECT_LE(actual_count, expected_count)
|
2018-02-13 00:13:23 +01:00
|
|
|
<< "File: " << file.c_str() << "; Navigation: " << navigation_
|
2013-09-10 21:42:53 +02:00
|
|
|
<< "; Transition Type: " << transition_type
|
|
|
|
<< "; Resource Type: " << resource_type;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test if all expectations have been met.
|
|
|
|
bool IsDone(bool assert) {
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2018-02-13 00:01:43 +01:00
|
|
|
if (navigation_ && g_type_expected[i].navigation != navigation_)
|
2013-09-10 21:42:53 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
RequestCount::const_iterator it = request_count_.find(i);
|
|
|
|
EXPECT_TRUE(it != request_count_.end());
|
|
|
|
if (it->second != g_type_expected[i].expected_count) {
|
|
|
|
if (assert) {
|
|
|
|
EXPECT_EQ(g_type_expected[i].expected_count, it->second)
|
|
|
|
<< "File: " << g_type_expected[i].file
|
2013-09-12 19:44:54 +02:00
|
|
|
<< "; Navigation: " << navigation_
|
2013-09-10 21:42:53 +02:00
|
|
|
<< "; Transition Type: " << g_type_expected[i].transition_type
|
|
|
|
<< "; Resource Type: " << g_type_expected[i].resource_type;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
// Returns the index for the specified navigation.
|
|
|
|
int GetExpectedIndex(const std::string& file,
|
|
|
|
cef_transition_type_t transition_type,
|
|
|
|
cef_resource_type_t resource_type) {
|
2013-09-17 11:17:12 +02:00
|
|
|
for (int i = 0;
|
|
|
|
i < static_cast<int>(sizeof(g_type_expected) / sizeof(TypeExpected));
|
|
|
|
++i) {
|
2013-09-10 21:42:53 +02:00
|
|
|
if (g_type_expected[i].file == file &&
|
2013-09-12 19:44:54 +02:00
|
|
|
(!navigation_ || g_type_expected[i].navigation == navigation_) &&
|
2013-09-10 21:42:53 +02:00
|
|
|
g_type_expected[i].transition_type == transition_type &&
|
|
|
|
g_type_expected[i].resource_type == resource_type) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:44:54 +02:00
|
|
|
bool navigation_;
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Map of TypeExpected index to actual request count.
|
|
|
|
typedef std::map<int, int> RequestCount;
|
|
|
|
RequestCount request_count_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Browser side.
|
|
|
|
class TypeTestHandler : public TestHandler {
|
|
|
|
public:
|
2017-05-17 11:29:28 +02:00
|
|
|
TypeTestHandler()
|
2018-02-13 00:01:43 +01:00
|
|
|
: browse_expectations_(true),
|
|
|
|
load_expectations_(false),
|
|
|
|
get_expectations_(false),
|
2017-05-17 11:29:28 +02:00
|
|
|
completed_browser_side_(false),
|
|
|
|
destroyed_(false) {}
|
2013-09-10 21:42:53 +02:00
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void RunTest() override {
|
2013-09-10 21:42:53 +02:00
|
|
|
AddResource(std::string(kTypeTestOrigin) + "main.html",
|
2017-05-17 11:29:28 +02:00
|
|
|
"<html>"
|
|
|
|
"<head>"
|
|
|
|
"<link rel=\"stylesheet\" href=\"style.css\" type=\"text/css\">"
|
|
|
|
"<script type=\"text/javascript\" src=\"script.js\"></script>"
|
|
|
|
"</head>"
|
|
|
|
"<body><p>Main</p>"
|
|
|
|
"<script>xhr = new XMLHttpRequest();"
|
|
|
|
"xhr.open('GET', 'xhr.html', false);"
|
|
|
|
"xhr.send();</script>"
|
|
|
|
"<iframe src=\"sub.html\"></iframe>"
|
|
|
|
"<img src=\"image.png\">"
|
|
|
|
"</body></html>",
|
|
|
|
"text/html");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "sub.html", "<html>Sub</html>",
|
|
|
|
"text/html");
|
2013-09-10 21:42:53 +02:00
|
|
|
AddResource(std::string(kTypeTestOrigin) + "style.css",
|
2017-05-17 11:29:28 +02:00
|
|
|
"@font-face {"
|
|
|
|
" font-family: custom_font;"
|
|
|
|
" src: url('font.ttf');"
|
|
|
|
"}"
|
|
|
|
"p {"
|
|
|
|
" font-family: custom_font;"
|
|
|
|
"}",
|
|
|
|
"text/css");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "script.js", "<!-- -->",
|
|
|
|
"text/javascript");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "image.png", "<!-- -->",
|
|
|
|
"image/png");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "font.ttf", "<!-- -->",
|
|
|
|
"font/ttf");
|
|
|
|
AddResource(std::string(kTypeTestOrigin) + "xhr.html", "<html>XHR</html>",
|
|
|
|
"text/html");
|
2019-02-04 23:31:31 +01:00
|
|
|
AddResource(std::string(kTypeTestOrigin) + "fetch.html",
|
|
|
|
"<html>Fetch</html>", "text/html");
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
CreateBrowser(std::string(kTypeTestOrigin) + "main.html");
|
|
|
|
|
|
|
|
// Time out the test after a reasonable period of time.
|
2015-01-10 00:40:26 +01:00
|
|
|
SetTestTimeout();
|
2013-09-10 21:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
2018-04-27 17:11:53 +02:00
|
|
|
bool user_gesture,
|
2014-11-12 20:25:15 +01:00
|
|
|
bool is_redirect) override {
|
2013-09-12 19:44:54 +02:00
|
|
|
browse_expectations_.GotRequest(request);
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-02 17:21:46 +02:00
|
|
|
cef_return_value_t OnBeforeResourceLoad(
|
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
CefRefPtr<CefRequest> request,
|
2021-09-27 11:09:45 +02:00
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2013-09-12 19:44:54 +02:00
|
|
|
load_expectations_.GotRequest(request);
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2015-04-02 17:21:46 +02:00
|
|
|
return RV_CONTINUE;
|
2013-09-10 21:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> GetResourceHandler(
|
2013-09-10 21:42:53 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override {
|
2013-09-10 21:42:53 +02:00
|
|
|
if (get_expectations_.GotRequest(request) &&
|
|
|
|
get_expectations_.IsDone(false)) {
|
|
|
|
completed_browser_side_ = true;
|
|
|
|
// Destroy the test on the UI thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
CefPostTask(TID_UI, base::BindOnce(&TypeTestHandler::DestroyTest, this));
|
2013-09-10 21:42:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return TestHandler::GetResourceHandler(browser, frame, request);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-12 20:25:15 +01:00
|
|
|
void DestroyTest() override {
|
2013-09-12 19:44:54 +02:00
|
|
|
if (destroyed_)
|
2013-09-10 21:42:53 +02:00
|
|
|
return;
|
2013-09-12 19:44:54 +02:00
|
|
|
destroyed_ = true;
|
2013-09-10 21:42:53 +02:00
|
|
|
|
|
|
|
// Verify test expectations.
|
|
|
|
EXPECT_TRUE(completed_browser_side_);
|
2013-09-12 19:44:54 +02:00
|
|
|
EXPECT_TRUE(browse_expectations_.IsDone(true));
|
|
|
|
EXPECT_TRUE(load_expectations_.IsDone(true));
|
2013-09-10 21:42:53 +02:00
|
|
|
EXPECT_TRUE(get_expectations_.IsDone(true));
|
|
|
|
|
|
|
|
TestHandler::DestroyTest();
|
|
|
|
}
|
|
|
|
|
2013-09-12 19:44:54 +02:00
|
|
|
TypeExpectations browse_expectations_;
|
|
|
|
TypeExpectations load_expectations_;
|
2013-09-10 21:42:53 +02:00
|
|
|
TypeExpectations get_expectations_;
|
|
|
|
|
|
|
|
bool completed_browser_side_;
|
2013-09-12 19:44:54 +02:00
|
|
|
bool destroyed_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(TypeTestHandler);
|
2013-09-10 21:42:53 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Verify the order of navigation-related callbacks.
|
|
|
|
TEST(RequestTest, ResourceAndTransitionType) {
|
2017-05-17 11:29:28 +02:00
|
|
|
CefRefPtr<TypeTestHandler> handler = new TypeTestHandler();
|
2013-09-10 21:42:53 +02:00
|
|
|
handler->ExecuteTest();
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler);
|
2013-09-10 21:42:53 +02:00
|
|
|
}
|