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.
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
#include <sstream>
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "include/base/cef_bind.h"
|
|
|
|
#include "include/cef_scheme.h"
|
|
|
|
#include "include/cef_task.h"
|
|
|
|
#include "include/cef_urlrequest.h"
|
2016-11-15 22:18:41 +01:00
|
|
|
#include "include/cef_waitable_event.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 00:52:42 +01:00
|
|
|
#include "tests/gtest/include/gtest/gtest.h"
|
|
|
|
#include "tests/shared/renderer/client_app_renderer.h"
|
2016-11-15 18:56:02 +01:00
|
|
|
#include "tests/unittests/file_util.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "tests/unittests/test_handler.h"
|
2015-02-18 18:28:56 +01:00
|
|
|
#include "tests/unittests/test_suite.h"
|
2014-07-15 00:18:51 +02:00
|
|
|
#include "tests/unittests/test_util.h"
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
using client::ClientAppRenderer;
|
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 {
|
|
|
|
|
|
|
|
// Unique values for URLRequest tests.
|
|
|
|
const char* kRequestTestUrl = "http://tests/URLRequestTest.Test";
|
|
|
|
const char* kRequestTestMsg = "URLRequestTest.Test";
|
|
|
|
const char* kRequestScheme = "urcustom";
|
|
|
|
const char* kRequestHost = "test";
|
|
|
|
const char* kRequestOrigin = "urcustom://test";
|
|
|
|
const char* kRequestSendCookieName = "urcookie_send";
|
|
|
|
const char* kRequestSaveCookieName = "urcookie_save";
|
|
|
|
|
|
|
|
enum RequestTestMode {
|
|
|
|
REQTEST_GET = 0,
|
|
|
|
REQTEST_GET_NODATA,
|
|
|
|
REQTEST_GET_ALLOWCOOKIES,
|
|
|
|
REQTEST_GET_REDIRECT,
|
2015-12-01 19:22:28 +01:00
|
|
|
REQTEST_GET_REFERRER,
|
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,
|
|
|
|
REQTEST_HEAD,
|
|
|
|
};
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
enum ContextTestMode {
|
|
|
|
CONTEXT_GLOBAL,
|
|
|
|
CONTEXT_INMEMORY,
|
|
|
|
CONTEXT_ONDISK,
|
|
|
|
};
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
struct RequestRunSettings {
|
|
|
|
RequestRunSettings()
|
|
|
|
: expect_upload_progress(false),
|
|
|
|
expect_download_progress(true),
|
|
|
|
expect_download_data(true),
|
|
|
|
expected_status(UR_SUCCESS),
|
|
|
|
expected_error_code(ERR_NONE),
|
|
|
|
expect_send_cookie(false),
|
|
|
|
expect_save_cookie(false),
|
|
|
|
expect_follow_redirect(true) {
|
|
|
|
}
|
|
|
|
|
|
|
|
// Request that will be sent.
|
|
|
|
CefRefPtr<CefRequest> request;
|
|
|
|
|
|
|
|
// Response that will be returned by the scheme handler.
|
|
|
|
CefRefPtr<CefResponse> response;
|
|
|
|
|
|
|
|
// Optional response data that will be returned by the scheme handler.
|
|
|
|
std::string response_data;
|
|
|
|
|
|
|
|
// If true upload progress notification will be expected.
|
|
|
|
bool expect_upload_progress;
|
|
|
|
|
|
|
|
// If true download progress notification will be expected.
|
|
|
|
bool expect_download_progress;
|
|
|
|
|
|
|
|
// If true download data will be expected.
|
|
|
|
bool expect_download_data;
|
|
|
|
|
|
|
|
// Expected status value.
|
|
|
|
CefURLRequest::Status expected_status;
|
|
|
|
|
|
|
|
// Expected error code value.
|
|
|
|
CefURLRequest::ErrorCode expected_error_code;
|
|
|
|
|
|
|
|
// If true the request cookie should be sent to the server.
|
|
|
|
bool expect_send_cookie;
|
|
|
|
|
|
|
|
// If true the response cookie should be save.
|
|
|
|
bool expect_save_cookie;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
bool expect_follow_redirect;
|
|
|
|
};
|
|
|
|
|
|
|
|
void SetUploadData(CefRefPtr<CefRequest> request,
|
|
|
|
const std::string& data) {
|
|
|
|
CefRefPtr<CefPostData> postData = CefPostData::Create();
|
|
|
|
CefRefPtr<CefPostDataElement> element = CefPostDataElement::Create();
|
|
|
|
element->SetToBytes(data.size(), data.c_str());
|
|
|
|
postData->AddElement(element);
|
|
|
|
request->SetPostData(postData);
|
|
|
|
}
|
|
|
|
|
2013-11-25 18:31:59 +01:00
|
|
|
void SetUploadFile(CefRefPtr<CefRequest> request,
|
2016-11-15 18:56:02 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
void GetUploadData(CefRefPtr<CefRequest> request,
|
|
|
|
std::string& data) {
|
|
|
|
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.
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetTestCookie(CefRefPtr<CefRequestContext> request_context) {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
class Callback : public CefSetCookieCallback {
|
|
|
|
public:
|
2016-11-15 22:18:41 +01:00
|
|
|
explicit Callback(CefRefPtr<CefWaitableEvent> event)
|
2016-11-15 18:56:02 +01:00
|
|
|
: event_(event) {
|
|
|
|
EXPECT_TRUE(event_);
|
|
|
|
}
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
void OnComplete(bool success) override {
|
|
|
|
EXPECT_TRUE(success);
|
|
|
|
event_->Signal();
|
2016-11-15 18:56:02 +01:00
|
|
|
event_ = nullptr;
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Callback);
|
|
|
|
};
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(false, false);
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
CefCookie cookie;
|
|
|
|
CefString(&cookie.name) = kRequestSendCookieName;
|
|
|
|
CefString(&cookie.value) = "send-cookie-value";
|
|
|
|
CefString(&cookie.domain) = kRequestHost;
|
|
|
|
CefString(&cookie.path) = "/";
|
|
|
|
cookie.has_expires = false;
|
|
|
|
EXPECT_TRUE(request_context->GetDefaultCookieManager(NULL)->SetCookie(
|
2016-11-15 22:18:41 +01:00
|
|
|
kRequestOrigin, cookie, new Callback(event)));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
// Wait for the Callback.
|
2016-11-15 22:18:41 +01:00
|
|
|
event->TimedWait(2000);
|
|
|
|
EXPECT_TRUE(event->IsSignaled());
|
2015-03-02 21:25:14 +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,
|
|
|
|
bool* cookie_exists) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
class Visitor : public CefCookieVisitor {
|
|
|
|
public:
|
2016-11-15 22:18:41 +01:00
|
|
|
Visitor(CefRefPtr<CefWaitableEvent> event, bool* cookie_exists)
|
2012-06-19 18:29:49 +02:00
|
|
|
: event_(event),
|
|
|
|
cookie_exists_(cookie_exists) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(event_);
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
2014-11-12 20:25:15 +01:00
|
|
|
~Visitor() override {
|
2012-06-19 18:29:49 +02:00
|
|
|
event_->Signal();
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01: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) {
|
|
|
|
*cookie_exists_ = true;
|
|
|
|
deleteCookie = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event_;
|
2012-06-19 18:29:49 +02:00
|
|
|
bool* cookie_exists_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(Visitor);
|
|
|
|
};
|
|
|
|
|
2016-11-15 22:18:41 +01:00
|
|
|
CefRefPtr<CefWaitableEvent> event =
|
|
|
|
CefWaitableEvent::CreateWaitableEvent(false, false);
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefCookieManager> cookie_manager =
|
2015-03-02 21:25:14 +01:00
|
|
|
request_context->GetDefaultCookieManager(NULL);
|
|
|
|
cookie_manager->VisitUrlCookies(
|
2016-11-15 22:18:41 +01:00
|
|
|
kRequestOrigin, true, new Visitor(event, cookie_exists));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
// Wait for the Visitor.
|
2016-11-15 22:18:41 +01:00
|
|
|
event->TimedWait(2000);
|
|
|
|
EXPECT_TRUE(event->IsSignaled());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Serves request responses.
|
|
|
|
class RequestSchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestSchemeHandler(const RequestRunSettings& settings)
|
|
|
|
: settings_(settings),
|
|
|
|
offset_(0) {
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
// HEAD requests are identical to GET requests except no response data is
|
|
|
|
// sent.
|
|
|
|
if (request->GetMethod() == "HEAD")
|
|
|
|
settings_.response_data.clear();
|
|
|
|
|
|
|
|
CefRequest::HeaderMap headerMap;
|
2013-10-30 18:42:05 +01:00
|
|
|
CefRequest::HeaderMap::iterator headerIter;
|
2012-06-19 18:29:49 +02:00
|
|
|
request->GetHeaderMap(headerMap);
|
|
|
|
|
2013-10-30 18:42:05 +01:00
|
|
|
// Check if the default headers were sent.
|
|
|
|
headerIter = headerMap.find("User-Agent");
|
|
|
|
EXPECT_TRUE(headerIter != headerMap.end() && !headerIter->second.empty());
|
|
|
|
headerIter = headerMap.find("Accept-Language");
|
|
|
|
EXPECT_TRUE(headerIter != headerMap.end() && !headerIter->second.empty());
|
|
|
|
|
2015-02-18 18:28:56 +01:00
|
|
|
// Verify that we get the value that was set via
|
|
|
|
// CefSettings.accept_language_list in CefTestSuite::GetSettings().
|
|
|
|
EXPECT_STREQ(CEF_SETTINGS_ACCEPT_LANGUAGE,
|
|
|
|
headerIter->second.ToString().data());
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Check if the request cookie was sent.
|
|
|
|
bool has_send_cookie = false;
|
2013-10-30 18:42:05 +01:00
|
|
|
headerIter = headerMap.find("Cookie");
|
|
|
|
if (headerIter != headerMap.end()) {
|
|
|
|
std::string cookie = headerIter->second;
|
2012-06-19 18:29:49 +02:00
|
|
|
if (cookie.find(kRequestSendCookieName) != std::string::npos)
|
|
|
|
has_send_cookie = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_.expect_send_cookie)
|
|
|
|
EXPECT_TRUE(has_send_cookie);
|
|
|
|
else
|
|
|
|
EXPECT_FALSE(has_send_cookie);
|
|
|
|
|
|
|
|
// Continue immediately.
|
|
|
|
callback->Continue();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void GetResponseHeaders(CefRefPtr<CefResponse> response,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
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) {
|
2016-11-14 19:28:05 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << kRequestSaveCookieName << "=" << "save-cookie-value";
|
|
|
|
headerMap.insert(std::make_pair("Set-Cookie", ss.str()));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
response->SetHeaderMap(headerMap);
|
|
|
|
|
|
|
|
response_length = settings_.response_data.length();
|
|
|
|
}
|
|
|
|
|
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 {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
bool has_data = false;
|
|
|
|
bytes_read = 0;
|
|
|
|
|
|
|
|
size_t size = settings_.response_data.length();
|
|
|
|
if (offset_ < size) {
|
|
|
|
int transfer_size =
|
|
|
|
std::min(bytes_to_read, static_cast<int>(size - offset_));
|
|
|
|
memcpy(response_data_out,
|
|
|
|
settings_.response_data.c_str() + offset_,
|
|
|
|
transfer_size);
|
|
|
|
offset_ += transfer_size;
|
|
|
|
|
|
|
|
bytes_read = transfer_size;
|
|
|
|
has_data = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_data;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Cancel() override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RequestRunSettings settings_;
|
|
|
|
size_t offset_;
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestSchemeHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Serves redirect request responses.
|
|
|
|
class RequestRedirectSchemeHandler : public CefResourceHandler {
|
|
|
|
public:
|
|
|
|
explicit RequestRedirectSchemeHandler(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefResponse> response)
|
|
|
|
: request_(request),
|
|
|
|
response_(response) {
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool ProcessRequest(CefRefPtr<CefRequest> request,
|
|
|
|
CefRefPtr<CefCallback> callback) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
int64& response_length,
|
|
|
|
CefString& redirectUrl) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
|
|
|
|
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 {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
NOTREACHED();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void Cancel() override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
CefRefPtr<CefResponse> response_;
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestRedirectSchemeHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class RequestSchemeHandlerFactory : public CefSchemeHandlerFactory {
|
|
|
|
public:
|
|
|
|
RequestSchemeHandlerFactory() {
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefResourceHandler> Create(
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
const CefString& scheme_name,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefRequest> request) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_IO));
|
|
|
|
std::string url = request->GetURL();
|
|
|
|
|
|
|
|
// Try to find a test match.
|
|
|
|
{
|
|
|
|
HandlerMap::const_iterator it = handler_map_.find(url);
|
|
|
|
if (it != handler_map_.end())
|
|
|
|
return new RequestSchemeHandler(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to find a redirect match.
|
|
|
|
{
|
|
|
|
RedirectHandlerMap::const_iterator it = redirect_handler_map_.find(url);
|
|
|
|
if (it != redirect_handler_map_.end()) {
|
|
|
|
return new RequestRedirectSchemeHandler(it->second.first,
|
|
|
|
it->second.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddSchemeHandler(const RequestRunSettings& settings) {
|
|
|
|
// Verify that the scheme is correct.
|
|
|
|
std::string url = settings.request->GetURL();
|
|
|
|
EXPECT_EQ((unsigned long)0, url.find(kRequestScheme));
|
|
|
|
|
|
|
|
handler_map_.insert(std::make_pair(url, settings));
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRedirectSchemeHandler(CefRefPtr<CefRequest> redirect_request,
|
|
|
|
CefRefPtr<CefResponse> redirect_response) {
|
|
|
|
// Verify that the scheme is correct.
|
|
|
|
std::string url = redirect_request->GetURL();
|
|
|
|
EXPECT_EQ((unsigned long)0, url.find(kRequestScheme));
|
|
|
|
|
|
|
|
redirect_handler_map_.insert(
|
|
|
|
std::make_pair(url,
|
|
|
|
std::make_pair(redirect_request, redirect_response)));
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef std::map<std::string, RequestRunSettings> HandlerMap;
|
|
|
|
HandlerMap handler_map_;
|
|
|
|
|
|
|
|
typedef std::map<std::string,
|
|
|
|
std::pair<CefRefPtr<CefRequest>, CefRefPtr<CefResponse> > >
|
|
|
|
RedirectHandlerMap;
|
|
|
|
RedirectHandlerMap redirect_handler_map_;
|
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestSchemeHandlerFactory);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Implementation of CefURLRequestClient that stores response information.
|
|
|
|
class RequestClient : public CefURLRequestClient {
|
|
|
|
public:
|
|
|
|
class Delegate {
|
|
|
|
public:
|
|
|
|
// Used to notify the handler when the request has completed.
|
|
|
|
virtual void OnRequestComplete(CefRefPtr<RequestClient> client) =0;
|
|
|
|
protected:
|
|
|
|
virtual ~Delegate() {}
|
|
|
|
};
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
explicit RequestClient(Delegate* delegate)
|
|
|
|
: delegate_(delegate),
|
|
|
|
request_complete_ct_(0),
|
|
|
|
upload_progress_ct_(0),
|
|
|
|
download_progress_ct_(0),
|
|
|
|
download_data_ct_(0),
|
|
|
|
upload_total_(0),
|
|
|
|
download_total_(0) {
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnRequestComplete(CefRefPtr<CefURLRequest> request) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
request_complete_ct_++;
|
|
|
|
|
|
|
|
request_ = request->GetRequest();
|
|
|
|
EXPECT_TRUE(request_->IsReadOnly());
|
|
|
|
status_ = request->GetRequestStatus();
|
|
|
|
error_code_ = request->GetRequestError();
|
|
|
|
response_ = request->GetResponse();
|
2014-09-27 01:48:19 +02:00
|
|
|
EXPECT_TRUE(response_.get());
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(response_->IsReadOnly());
|
|
|
|
|
|
|
|
delegate_->OnRequestComplete(this);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnUploadProgress(CefRefPtr<CefURLRequest> request,
|
2015-01-12 21:46:31 +01:00
|
|
|
int64 current,
|
|
|
|
int64 total) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
upload_progress_ct_++;
|
|
|
|
upload_total_ = total;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnDownloadProgress(CefRefPtr<CefURLRequest> request,
|
2015-01-12 21:46:31 +01:00
|
|
|
int64 current,
|
|
|
|
int64 total) override {
|
2013-08-27 21:16:24 +02:00
|
|
|
response_ = request->GetResponse();
|
2014-09-27 01:48:19 +02:00
|
|
|
EXPECT_TRUE(response_.get());
|
2013-08-27 21:16:24 +02:00
|
|
|
EXPECT_TRUE(response_->IsReadOnly());
|
2012-06-19 18:29:49 +02:00
|
|
|
download_progress_ct_++;
|
|
|
|
download_total_ = total;
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnDownloadData(CefRefPtr<CefURLRequest> request,
|
|
|
|
const void* data,
|
|
|
|
size_t data_length) override {
|
2013-08-27 21:16:24 +02:00
|
|
|
response_ = request->GetResponse();
|
2014-09-27 01:48:19 +02:00
|
|
|
EXPECT_TRUE(response_.get());
|
2013-08-27 21:16:24 +02:00
|
|
|
EXPECT_TRUE(response_->IsReadOnly());
|
2012-06-19 18:29:49 +02:00
|
|
|
download_data_ct_++;
|
|
|
|
download_data_ += std::string(static_cast<const char*>(data), data_length);
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool GetAuthCredentials(bool isProxy,
|
|
|
|
const CefString& host,
|
|
|
|
int port,
|
|
|
|
const CefString& realm,
|
|
|
|
const CefString& scheme,
|
|
|
|
CefRefPtr<CefAuthCallback> callback) override {
|
2013-08-27 20:58:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
private:
|
|
|
|
Delegate* delegate_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
int request_complete_ct_;
|
|
|
|
int upload_progress_ct_;
|
|
|
|
int download_progress_ct_;
|
|
|
|
int download_data_ct_;
|
|
|
|
|
|
|
|
uint64 upload_total_;
|
|
|
|
uint64 download_total_;
|
|
|
|
std::string download_data_;
|
|
|
|
CefRefPtr<CefRequest> request_;
|
|
|
|
CefURLRequest::Status status_;
|
|
|
|
CefURLRequest::ErrorCode error_code_;
|
|
|
|
CefRefPtr<CefResponse> response_;
|
|
|
|
|
|
|
|
private:
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestClient);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 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:
|
2016-11-15 18:56:02 +01:00
|
|
|
typedef base::Callback<void(const base::Closure&)> TestCallback;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
// Delegate methods will be called on the same thread that constructed the
|
|
|
|
// RequestTestRunner object.
|
2012-06-19 18:29:49 +02:00
|
|
|
class Delegate {
|
|
|
|
public:
|
2016-11-15 18:56:02 +01:00
|
|
|
// Setup has completed.
|
|
|
|
virtual void OnRunnerSetupComplete() =0;
|
|
|
|
|
|
|
|
// Run has completed.
|
|
|
|
virtual void OnRunnerRunComplete() =0;
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
protected:
|
|
|
|
virtual ~Delegate() {}
|
|
|
|
};
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
RequestTestRunner(Delegate* delegate,
|
|
|
|
bool is_browser_process)
|
2012-06-19 18:29:49 +02:00
|
|
|
: delegate_(delegate),
|
|
|
|
is_browser_process_(is_browser_process) {
|
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());
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Helper macro for registering test callbacks.
|
|
|
|
#define REGISTER_TEST(test_mode, setup_method, run_method) \
|
|
|
|
RegisterTest(test_mode, \
|
2015-03-02 21:25:14 +01:00
|
|
|
base::Bind(&RequestTestRunner::setup_method, this), \
|
|
|
|
base::Bind(&RequestTestRunner::run_method, this));
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
// Register the test callbacks.
|
|
|
|
REGISTER_TEST(REQTEST_GET, SetupGetTest, GenericRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_GET_NODATA, SetupGetNoDataTest, GenericRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_GET_ALLOWCOOKIES, SetupGetAllowCookiesTest,
|
|
|
|
GenericRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_GET_REDIRECT, SetupGetRedirectTest, GenericRunTest);
|
2015-12-01 19:22:28 +01:00
|
|
|
REGISTER_TEST(REQTEST_GET_REFERRER, SetupGetReferrerTest, GenericRunTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
REGISTER_TEST(REQTEST_POST, SetupPostTest, GenericRunTest);
|
2013-11-25 18:31:59 +01:00
|
|
|
REGISTER_TEST(REQTEST_POST_FILE, SetupPostFileTest, GenericRunTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
REGISTER_TEST(REQTEST_POST_WITHPROGRESS, SetupPostWithProgressTest,
|
|
|
|
GenericRunTest);
|
|
|
|
REGISTER_TEST(REQTEST_HEAD, SetupHeadTest, GenericRunTest);
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void Destroy() {
|
|
|
|
owner_task_runner_ = nullptr;
|
|
|
|
request_context_ = nullptr;
|
|
|
|
}
|
|
|
|
|
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_;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Called in both the browser and render process to setup the test.
|
|
|
|
void SetupTest(RequestTestMode test_mode) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
|
|
|
const base::Closure& complete_callback =
|
|
|
|
base::Bind(&RequestTestRunner::SetupComplete, this);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMap::const_iterator it = test_map_.find(test_mode);
|
|
|
|
if (it != test_map_.end()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
it->second.setup.Run(
|
|
|
|
base::Bind(&RequestTestRunner::SetupContinue, this,
|
|
|
|
complete_callback));
|
2012-06-19 18:29:49 +02:00
|
|
|
} else {
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
2016-11-15 18:56:02 +01:00
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Called in either the browser or render process to run the test.
|
|
|
|
void RunTest(RequestTestMode test_mode) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(owner_task_runner_->BelongsToCurrentThread());
|
|
|
|
|
|
|
|
const base::Closure& complete_callback =
|
|
|
|
base::Bind(&RequestTestRunner::RunComplete, this);
|
2012-06-19 18:29:49 +02:00
|
|
|
TestMap::const_iterator it = test_map_.find(test_mode);
|
|
|
|
if (it != test_map_.end()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
it->second.run.Run(complete_callback);
|
2012-06-19 18:29:49 +02:00
|
|
|
} else {
|
|
|
|
// Unknown test.
|
|
|
|
ADD_FAILURE();
|
2016-11-15 18:56:02 +01:00
|
|
|
complete_callback.Run();
|
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.
|
|
|
|
void SetupContinue(const base::Closure& complete_callback) {
|
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
|
|
|
base::Bind(&RequestTestRunner::SetupContinue, this,
|
|
|
|
complete_callback)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_browser_process_)
|
|
|
|
AddSchemeHandler();
|
|
|
|
|
|
|
|
complete_callback.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupGetTestShared() {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(MakeSchemeURL("GetTest.html"));
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupGetTest(const base::Closure& complete_callback) {
|
|
|
|
SetupGetTestShared();
|
|
|
|
complete_callback.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupGetNoDataTest(const base::Closure& 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
|
|
|
|
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupGetAllowCookiesTest(const base::Closure& 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.
|
2014-04-04 18:50:38 +02:00
|
|
|
settings_.request->SetFlags(UR_FLAG_ALLOW_CACHED_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
|
|
|
|
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupGetRedirectTest(const base::Closure& 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();
|
|
|
|
settings_.redirect_request->SetURL(MakeSchemeURL("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", settings_.request->GetURL()));
|
|
|
|
settings_.redirect_response->SetHeaderMap(headerMap);
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupGetReferrerTest(const base::Closure& complete_callback) {
|
2015-12-01 19:22:28 +01:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(MakeSchemeURL("GetTest.html"));
|
|
|
|
settings_.request->SetMethod("GET");
|
|
|
|
|
|
|
|
// The referrer URL must be HTTP or HTTPS. This is enforced by
|
|
|
|
// GURL::GetAsReferrer() called from URLRequest::SetReferrer().
|
|
|
|
settings_.request->SetReferrer(
|
|
|
|
"http://tests.com/referrer.html",
|
|
|
|
REFERRER_POLICY_NO_REFERRER_WHEN_DOWNGRADE);
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
complete_callback.Run();
|
2015-12-01 19:22:28 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupPostTestShared() {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(MakeSchemeURL("PostTest.html"));
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupPostTest(const base::Closure& complete_callback) {
|
|
|
|
SetupPostTestShared();
|
|
|
|
complete_callback.Run();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupPostFileTest(const base::Closure& complete_callback) {
|
|
|
|
// This test is only supported in the browser process.
|
|
|
|
EXPECT_TRUE(is_browser_process_);
|
|
|
|
|
2013-11-25 18:31:59 +01:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(MakeSchemeURL("PostFileTest.html"));
|
|
|
|
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
|
|
|
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestRunner::SetupPostFileTestContinue, this,
|
|
|
|
complete_callback));
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetupPostFileTestContinue(const base::Closure& complete_callback) {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.CreateUniqueTempDir());
|
|
|
|
const std::string& path =
|
|
|
|
file_util::JoinPath(post_file_tmpdir_.GetPath(), "example.txt");
|
|
|
|
const char content[] = "HELLO FRIEND!";
|
|
|
|
int write_ct = file_util::WriteFile(path, content, sizeof(content) - 1);
|
|
|
|
EXPECT_EQ(static_cast<int>(sizeof(content) - 1), write_ct);
|
|
|
|
SetUploadFile(settings_.request, path);
|
|
|
|
|
|
|
|
complete_callback.Run();
|
2013-11-25 18:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupPostWithProgressTest(const base::Closure& 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
|
|
|
|
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupHeadTest(const base::Closure& complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
settings_.request = CefRequest::Create();
|
|
|
|
settings_.request->SetURL(MakeSchemeURL("HeadTest.html"));
|
|
|
|
settings_.request->SetMethod("HEAD");
|
|
|
|
|
|
|
|
settings_.response = CefResponse::Create();
|
|
|
|
settings_.response->SetMimeType("text/html");
|
|
|
|
settings_.response->SetStatus(200);
|
|
|
|
settings_.response->SetStatusText("OK");
|
|
|
|
|
|
|
|
// The scheme handler will disregard this value when it returns the result.
|
|
|
|
settings_.response_data = "HEAD TEST SUCCESS";
|
|
|
|
|
|
|
|
settings_.expect_download_progress = false;
|
|
|
|
settings_.expect_download_data = false;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
complete_callback.Run();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generic test runner.
|
2016-11-15 18:56:02 +01:00
|
|
|
void GenericRunTest(const base::Closure& complete_callback) {
|
2012-06-19 18:29:49 +02:00
|
|
|
class Test : public RequestClient::Delegate {
|
|
|
|
public:
|
2016-11-15 18:56:02 +01:00
|
|
|
Test(const RequestRunSettings& settings,
|
|
|
|
const base::Closure& complete_callback)
|
|
|
|
: settings_(settings),
|
|
|
|
complete_callback_(complete_callback) {
|
|
|
|
EXPECT_FALSE(complete_callback_.is_null());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnRequestComplete(CefRefPtr<RequestClient> client) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefRequest> expected_request;
|
|
|
|
CefRefPtr<CefResponse> expected_response;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
if (settings_.redirect_request.get())
|
|
|
|
expected_request = settings_.redirect_request;
|
2012-06-19 18:29:49 +02:00
|
|
|
else
|
2016-11-15 18:56:02 +01:00
|
|
|
expected_request = settings_.request;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
if (settings_.redirect_response.get() &&
|
|
|
|
!settings_.expect_follow_redirect) {
|
2012-06-19 18:29:49 +02:00
|
|
|
// A redirect response was sent but the redirect is not expected to be
|
|
|
|
// followed.
|
2016-11-15 18:56:02 +01:00
|
|
|
expected_response = settings_.redirect_response;
|
2012-06-19 18:29:49 +02:00
|
|
|
} else {
|
2016-11-15 18:56:02 +01:00
|
|
|
expected_response = settings_.response;
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TestRequestEqual(expected_request, client->request_, false);
|
|
|
|
|
|
|
|
EXPECT_EQ(settings_.expected_status, client->status_);
|
|
|
|
EXPECT_EQ(settings_.expected_error_code, client->error_code_);
|
|
|
|
TestResponseEqual(expected_response, client->response_, true);
|
|
|
|
|
|
|
|
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);
|
|
|
|
EXPECT_EQ(upload_data.size(), client->upload_total_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->upload_progress_ct_);
|
|
|
|
EXPECT_EQ((uint64)0, client->upload_total_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_.expect_download_progress) {
|
|
|
|
EXPECT_LE(1, client->download_progress_ct_);
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_EQ(settings_.response_data.size(),
|
2012-06-19 18:29:49 +02:00
|
|
|
client->download_total_);
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->download_progress_ct_);
|
|
|
|
EXPECT_EQ((uint64)0, client->download_total_);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (settings_.expect_download_data) {
|
|
|
|
EXPECT_LE(1, client->download_data_ct_);
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_STREQ(settings_.response_data.c_str(),
|
2012-06-19 18:29:49 +02:00
|
|
|
client->download_data_.c_str());
|
|
|
|
} else {
|
|
|
|
EXPECT_EQ(0, client->download_data_ct_);
|
|
|
|
EXPECT_TRUE(client->download_data_.empty());
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
complete_callback_.Run();
|
|
|
|
complete_callback_.Reset();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RequestRunSettings settings_;
|
2016-11-15 18:56:02 +01:00
|
|
|
base::Closure complete_callback_;
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
CefRefPtr<CefRequest> request;
|
2014-09-27 01:48:19 +02:00
|
|
|
if (settings_.redirect_request.get())
|
2012-06-19 18:29:49 +02:00
|
|
|
request = settings_.redirect_request;
|
|
|
|
else
|
|
|
|
request = settings_.request;
|
|
|
|
EXPECT_TRUE(request.get());
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<RequestClient> client =
|
2016-11-15 18:56:02 +01:00
|
|
|
new RequestClient(new Test(settings_, complete_callback));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
CefURLRequest::Create(request, client.get(), request_context_);
|
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));
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void SetupComplete() {
|
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
|
|
|
base::Bind(&RequestTestRunner::SetupComplete, this)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
delegate_->OnRunnerSetupComplete();
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Destroy the current test. Called when the test is complete.
|
2016-11-15 18:56:02 +01:00
|
|
|
void RunComplete() {
|
|
|
|
if (!post_file_tmpdir_.IsEmpty()) {
|
|
|
|
EXPECT_TRUE(is_browser_process_);
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestRunner::RunCompleteDeleteTempDirectory, this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Continue with test completion.
|
|
|
|
RunCompleteContinue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunCompleteDeleteTempDirectory() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.Delete());
|
|
|
|
EXPECT_TRUE(post_file_tmpdir_.IsEmpty());
|
|
|
|
|
|
|
|
// Continue with test completion.
|
|
|
|
RunCompleteContinue();
|
|
|
|
}
|
|
|
|
|
|
|
|
void RunCompleteContinue() {
|
|
|
|
if (!owner_task_runner_->BelongsToCurrentThread()) {
|
|
|
|
owner_task_runner_->PostTask(CefCreateClosureTask(
|
|
|
|
base::Bind(&RequestTestRunner::RunCompleteContinue, this)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
if (scheme_factory_.get()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(is_browser_process_);
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Remove the factory registration.
|
2015-03-02 21:25:14 +01:00
|
|
|
request_context_->RegisterSchemeHandlerFactory(
|
|
|
|
kRequestScheme, kRequestHost, NULL);
|
2012-06-19 18:29:49 +02:00
|
|
|
scheme_factory_ = NULL;
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
delegate_->OnRunnerRunComplete();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return an appropriate scheme URL for the specified |path|.
|
|
|
|
std::string MakeSchemeURL(const std::string& path) {
|
2016-11-14 19:28:05 +01:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << kRequestOrigin << "/" << path;
|
|
|
|
return ss.str();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add a scheme handler for the current test. Called during test setup.
|
|
|
|
void AddSchemeHandler() {
|
|
|
|
// Scheme handlers are only registered in the browser process.
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(is_browser_process_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
if (!scheme_factory_.get()) {
|
|
|
|
// Add the factory registration.
|
|
|
|
scheme_factory_ = new RequestSchemeHandlerFactory();
|
2015-03-02 21:25:14 +01:00
|
|
|
request_context_->RegisterSchemeHandlerFactory(
|
|
|
|
kRequestScheme, kRequestHost, scheme_factory_.get());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
EXPECT_TRUE(settings_.request.get());
|
|
|
|
EXPECT_TRUE(settings_.response.get());
|
|
|
|
|
|
|
|
scheme_factory_->AddSchemeHandler(settings_);
|
|
|
|
|
|
|
|
if (settings_.redirect_request.get()) {
|
|
|
|
scheme_factory_->AddRedirectSchemeHandler(settings_.redirect_request,
|
|
|
|
settings_.redirect_response);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Delegate* delegate_;
|
|
|
|
bool is_browser_process_;
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
// Primary thread runner for the object that owns us. In the browser process
|
|
|
|
// this will be the UI thread and in the renderer process this will be the
|
|
|
|
// RENDERER thread.
|
|
|
|
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
|
|
|
|
|
|
|
struct TestEntry {
|
|
|
|
TestCallback setup;
|
|
|
|
TestCallback run;
|
|
|
|
};
|
|
|
|
typedef std::map<RequestTestMode, TestEntry> TestMap;
|
|
|
|
TestMap test_map_;
|
|
|
|
|
|
|
|
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_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Renderer side.
|
2015-01-31 05:41:36 +01:00
|
|
|
class RequestRendererTest : public ClientAppRenderer::Delegate,
|
2012-06-19 18:29:49 +02:00
|
|
|
public RequestTestRunner::Delegate {
|
|
|
|
public:
|
2016-11-15 18:56:02 +01:00
|
|
|
RequestRendererTest() {
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnProcessMessageReceived(
|
2015-01-31 05:41:36 +01:00
|
|
|
CefRefPtr<ClientAppRenderer> app,
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefProcessId source_process,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefProcessMessage> message) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
if (message->GetName() == kRequestTestMsg) {
|
2016-11-15 18:56:02 +01:00
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER));
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
app_ = app;
|
|
|
|
browser_ = browser;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
test_mode_ =
|
2012-06-19 18:29:49 +02:00
|
|
|
static_cast<RequestTestMode>(message->GetArgumentList()->GetInt(0));
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
test_runner_ = new RequestTestRunner(this, false);
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Setup the test. This will create the objects that we test against but
|
|
|
|
// not register any scheme handlers (because we're in the render process).
|
2016-11-15 18:56:02 +01:00
|
|
|
test_runner_->SetupTest(test_mode_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message not handled.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
private:
|
|
|
|
void OnRunnerSetupComplete() override {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER));
|
|
|
|
|
|
|
|
// Run the test.
|
|
|
|
test_runner_->RunTest(test_mode_);
|
|
|
|
}
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Return from the test.
|
2016-11-15 18:56:02 +01:00
|
|
|
void OnRunnerRunComplete() override {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_RENDERER));
|
|
|
|
|
2012-06-19 18:29:49 +02:00
|
|
|
// Check if the test has failed.
|
|
|
|
bool result = !TestFailed();
|
|
|
|
|
|
|
|
// Return the result to the browser process.
|
|
|
|
CefRefPtr<CefProcessMessage> return_msg =
|
|
|
|
CefProcessMessage::Create(kRequestTestMsg);
|
|
|
|
EXPECT_TRUE(return_msg->GetArgumentList()->SetBool(0, result));
|
|
|
|
EXPECT_TRUE(browser_->SendProcessMessage(PID_BROWSER, return_msg));
|
|
|
|
|
|
|
|
app_ = NULL;
|
|
|
|
browser_ = NULL;
|
|
|
|
}
|
|
|
|
|
2015-01-31 05:41:36 +01:00
|
|
|
CefRefPtr<ClientAppRenderer> app_;
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefBrowser> browser_;
|
2016-11-15 18:56:02 +01:00
|
|
|
RequestTestMode test_mode_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<RequestTestRunner> test_runner_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2014-07-15 00:18:51 +02:00
|
|
|
IMPLEMENT_REFCOUNTING(RequestRendererTest);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Browser side.
|
|
|
|
class RequestTestHandler : public TestHandler,
|
|
|
|
public RequestTestRunner::Delegate {
|
|
|
|
public:
|
|
|
|
RequestTestHandler(RequestTestMode test_mode,
|
2015-03-02 21:25:14 +01:00
|
|
|
ContextTestMode context_mode,
|
2012-06-19 18:29:49 +02:00
|
|
|
bool test_in_browser,
|
|
|
|
const char* test_url)
|
|
|
|
: test_mode_(test_mode),
|
2015-03-02 21:25:14 +01:00
|
|
|
context_mode_(context_mode),
|
2012-06-19 18:29:49 +02:00
|
|
|
test_in_browser_(test_in_browser),
|
2016-11-15 18:56:02 +01:00
|
|
|
test_url_(test_url) {
|
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.
|
|
|
|
SetTestTimeout();
|
|
|
|
|
|
|
|
// Start pre-setup actions.
|
|
|
|
PreSetupStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupStart() {
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestHandler::PreSetupFileTasks, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupFileTasks() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
|
|
|
if (context_mode_ == CONTEXT_ONDISK) {
|
|
|
|
EXPECT_TRUE(context_tmpdir_.CreateUniqueTempDir());
|
|
|
|
context_tmpdir_path_ = context_tmpdir_.GetPath();
|
|
|
|
EXPECT_FALSE(context_tmpdir_path_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::Bind(&RequestTestHandler::PreSetupContinue, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreSetupContinue() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
|
|
|
|
test_runner_ = new RequestTestRunner(this, true);
|
|
|
|
|
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 {
|
|
|
|
// Don't end the test until the temporary request context has been
|
|
|
|
// destroyed.
|
|
|
|
SetSignalCompletionWhenAllBrowsersClose(false);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new temporary request context.
|
|
|
|
CefRefPtr<CefRequestContext> request_context =
|
|
|
|
CefRequestContext::CreateContext(settings,
|
|
|
|
new RequestContextHandler(this));
|
|
|
|
EXPECT_TRUE(request_context.get());
|
|
|
|
test_runner_->SetRequestContext(request_context);
|
|
|
|
|
|
|
|
// Set the schemes that are allowed to store cookies.
|
|
|
|
std::vector<CefString> supported_schemes;
|
|
|
|
supported_schemes.push_back(kRequestScheme);
|
|
|
|
|
|
|
|
// Continue the test once supported schemes has been set.
|
|
|
|
request_context->GetDefaultCookieManager(NULL)->SetSupportedSchemes(
|
2016-11-15 18:56:02 +01:00
|
|
|
supported_schemes,
|
|
|
|
new SupportedSchemesCompletionCallback(
|
|
|
|
base::Bind(&RequestTestHandler::PreSetupComplete, this)));
|
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,
|
2016-11-15 18:56:02 +01:00
|
|
|
base::Bind(&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
|
|
|
|
// register any scheme handlers.
|
2015-03-02 21:25:14 +01:00
|
|
|
test_runner_->SetupTest(test_mode_);
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Browser process setup is complete.
|
|
|
|
void OnRunnerSetupComplete() override {
|
|
|
|
// Start post-setup actions.
|
|
|
|
PostSetupStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostSetupStart() {
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestHandler::PostSetupFileTasks, this));
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void PostSetupFileTasks() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
|
|
|
// Don't use WaitableEvent on the UI thread.
|
|
|
|
SetTestCookie(test_runner_->GetRequestContext());
|
|
|
|
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::Bind(&RequestTestHandler::PostSetupComplete, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostSetupComplete() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
if (test_in_browser_) {
|
|
|
|
// Run the test now.
|
|
|
|
test_runner_->RunTest(test_mode_);
|
|
|
|
} else {
|
|
|
|
EXPECT_TRUE(test_url_ != NULL);
|
|
|
|
AddResource(test_url_, "<html><body>TEST</body></html>", "text/html");
|
|
|
|
|
|
|
|
// Create a browser to run the test in the renderer process.
|
|
|
|
CreateBrowser(test_url_, test_runner_->GetRequestContext());
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnLoadEnd(CefRefPtr<CefBrowser> browser,
|
|
|
|
CefRefPtr<CefFrame> frame,
|
|
|
|
int httpStatusCode) override {
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_FALSE(test_in_browser_);
|
2012-06-19 18:29:49 +02:00
|
|
|
if (frame->IsMain()) {
|
2015-03-02 21:25:14 +01:00
|
|
|
CefRefPtr<CefProcessMessage> test_message =
|
|
|
|
CefProcessMessage::Create(kRequestTestMsg);
|
|
|
|
EXPECT_TRUE(test_message->GetArgumentList()->SetInt(0, test_mode_));
|
|
|
|
|
|
|
|
// Send a message to the renderer process to run the test.
|
|
|
|
EXPECT_TRUE(browser->SendProcessMessage(PID_RENDERER, test_message));
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool OnProcessMessageReceived(
|
2012-06-19 18:29:49 +02:00
|
|
|
CefRefPtr<CefBrowser> browser,
|
|
|
|
CefProcessId source_process,
|
2014-11-12 20:25:15 +01:00
|
|
|
CefRefPtr<CefProcessMessage> message) override {
|
2012-06-19 18:29:49 +02:00
|
|
|
EXPECT_TRUE(browser.get());
|
|
|
|
EXPECT_EQ(PID_RENDERER, source_process);
|
|
|
|
EXPECT_TRUE(message.get());
|
|
|
|
EXPECT_TRUE(message->IsReadOnly());
|
2015-03-02 21:25:14 +01:00
|
|
|
EXPECT_FALSE(test_in_browser_);
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
got_message_.yes();
|
|
|
|
|
|
|
|
if (message->GetArgumentList()->GetBool(0))
|
|
|
|
got_success_.yes();
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
// Renderer process test is complete.
|
|
|
|
PostRunStart();
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
// Browser process test is complete.
|
|
|
|
void OnRunnerRunComplete() override {
|
|
|
|
PostRunStart();
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRunStart() {
|
|
|
|
CefPostTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestHandler::PostRunFileTasks, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRunFileTasks() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
// Don't use WaitableEvent on the UI thread.
|
2012-06-19 18:29:49 +02:00
|
|
|
bool has_save_cookie = false;
|
2016-11-15 18:56:02 +01:00
|
|
|
GetTestCookie(test_runner_->GetRequestContext(), &has_save_cookie);
|
|
|
|
EXPECT_EQ(test_runner_->settings_.expect_save_cookie, has_save_cookie);
|
|
|
|
|
|
|
|
CefPostTask(TID_UI, base::Bind(&RequestTestHandler::PostRunComplete, this));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostRunComplete() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_UI));
|
|
|
|
DestroyTest();
|
|
|
|
}
|
2012-06-19 18:29:49 +02:00
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
void DestroyTest() override {
|
2012-06-19 18:29:49 +02:00
|
|
|
TestHandler::DestroyTest();
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
// Need to call TestComplete() explicitly if testing in the browser and
|
|
|
|
// using the global context. Otherwise, TestComplete() will be called when
|
|
|
|
// the browser is destroyed (for render test + global context) or when the
|
|
|
|
// temporary context is destroyed.
|
|
|
|
const bool call_test_complete =
|
|
|
|
(test_in_browser_ && context_mode_ == CONTEXT_GLOBAL);
|
|
|
|
|
|
|
|
// Release our reference to the context. Do not access any object members
|
|
|
|
// after this call because |this| might be deleted.
|
2016-11-15 18:56:02 +01:00
|
|
|
test_runner_->Destroy();
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
if (call_test_complete)
|
2016-11-15 18:56:02 +01:00
|
|
|
OnTestComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
void OnTestComplete() {
|
|
|
|
if (!CefCurrentlyOn(TID_UI)) {
|
|
|
|
CefPostTask(TID_UI,
|
|
|
|
base::Bind(&RequestTestHandler::OnTestComplete, this));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!context_tmpdir_.IsEmpty()) {
|
|
|
|
// Wait a bit for cache file handles to close after browser or request
|
|
|
|
// context destruction.
|
|
|
|
CefPostDelayedTask(TID_FILE,
|
|
|
|
base::Bind(&RequestTestHandler::PostTestCompleteFileTasks, this),
|
|
|
|
100);
|
|
|
|
} else {
|
2015-03-02 21:25:14 +01:00
|
|
|
TestComplete();
|
2016-11-15 18:56:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostTestCompleteFileTasks() {
|
|
|
|
EXPECT_TRUE(CefCurrentlyOn(TID_FILE));
|
|
|
|
|
|
|
|
EXPECT_TRUE(context_tmpdir_.Delete());
|
|
|
|
EXPECT_TRUE(context_tmpdir_.IsEmpty());
|
|
|
|
|
|
|
|
CefPostTask(TID_UI, base::Bind(&RequestTestHandler::TestComplete, this));
|
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)
|
|
|
|
: test_handler_(test_handler) {
|
|
|
|
}
|
|
|
|
~RequestContextHandler() override {
|
2016-11-15 18:56:02 +01:00
|
|
|
test_handler_->OnTestComplete();
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
private:
|
|
|
|
CefRefPtr<RequestTestHandler> test_handler_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestContextHandler);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Continue the rest once supported schemes have been set.
|
|
|
|
class SupportedSchemesCompletionCallback : public CefCompletionCallback {
|
|
|
|
public:
|
|
|
|
explicit SupportedSchemesCompletionCallback(
|
2016-11-15 18:56:02 +01:00
|
|
|
const base::Closure& complete_callback)
|
|
|
|
: complete_callback_(complete_callback) {
|
|
|
|
EXPECT_FALSE(complete_callback_.is_null());
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
void OnComplete() override {
|
2016-11-15 18:56:02 +01:00
|
|
|
complete_callback_.Run();
|
|
|
|
complete_callback_.Reset();
|
2015-03-02 21:25:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2016-11-15 18:56:02 +01:00
|
|
|
base::Closure complete_callback_;
|
2015-03-02 21:25:14 +01:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(SupportedSchemesCompletionCallback);
|
|
|
|
};
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
RequestTestMode test_mode_;
|
2015-03-02 21:25:14 +01:00
|
|
|
ContextTestMode context_mode_;
|
2012-06-19 18:29:49 +02:00
|
|
|
bool test_in_browser_;
|
|
|
|
const char* test_url_;
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
scoped_refptr<RequestTestRunner> test_runner_;
|
|
|
|
|
2016-11-15 18:56:02 +01:00
|
|
|
CefScopedTempDir context_tmpdir_;
|
|
|
|
CefString context_tmpdir_path_;
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Only used when the test runs in the render process.
|
|
|
|
TrackCallback got_message_;
|
|
|
|
TrackCallback got_success_;
|
2015-09-09 16:05:39 +02:00
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(RequestTestHandler);
|
2012-06-19 18:29:49 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
|
|
|
|
// Entry point for creating URLRequest renderer test objects.
|
|
|
|
// Called from client_app_delegates.cc.
|
2015-01-31 05:41:36 +01:00
|
|
|
void CreateURLRequestRendererTests(ClientAppRenderer::DelegateSet& delegates) {
|
2012-06-19 18:29:49 +02:00
|
|
|
delegates.insert(new RequestRendererTest);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Entry point for registering custom schemes.
|
|
|
|
// Called from client_app_delegates.cc.
|
|
|
|
void RegisterURLRequestCustomSchemes(
|
|
|
|
CefRefPtr<CefSchemeRegistrar> registrar,
|
|
|
|
std::vector<CefString>& cookiable_schemes) {
|
|
|
|
registrar->AddCustomScheme(kRequestScheme, true, false, false);
|
|
|
|
cookiable_schemes.push_back(kRequestScheme);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Helpers for defining URLRequest tests.
|
2015-03-02 21:25:14 +01:00
|
|
|
#define REQ_TEST_EX(name, test_mode, context_mode, test_in_browser, test_url) \
|
2012-06-19 18:29:49 +02:00
|
|
|
TEST(URLRequestTest, name) { \
|
|
|
|
CefRefPtr<RequestTestHandler> handler = \
|
2015-03-02 21:25:14 +01:00
|
|
|
new RequestTestHandler(test_mode, context_mode, test_in_browser, \
|
|
|
|
test_url); \
|
2012-06-19 18:29:49 +02:00
|
|
|
handler->ExecuteTest(); \
|
|
|
|
if (!test_in_browser) { \
|
|
|
|
EXPECT_TRUE(handler->got_message_); \
|
|
|
|
EXPECT_TRUE(handler->got_success_); \
|
|
|
|
} \
|
2015-01-10 00:40:26 +01:00
|
|
|
ReleaseAndWaitForDestructor(handler); \
|
2012-06-19 18:29:49 +02:00
|
|
|
}
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
#define REQ_TEST(name, test_mode, context_mode, test_in_browser) \
|
|
|
|
REQ_TEST_EX(name, test_mode, context_mode, test_in_browser, kRequestTestUrl)
|
2012-06-19 18:29:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Define the tests.
|
2015-03-02 21:25:14 +01:00
|
|
|
#define REQ_TEST_SET(suffix, context_mode) \
|
|
|
|
REQ_TEST(BrowserGET##suffix, REQTEST_GET, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserGETNoData##suffix, REQTEST_GET_NODATA, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserGETAllowCookies##suffix, \
|
|
|
|
REQTEST_GET_ALLOWCOOKIES, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserGETRedirect##suffix, \
|
|
|
|
REQTEST_GET_REDIRECT, context_mode, true); \
|
2015-12-01 19:22:28 +01:00
|
|
|
REQ_TEST(BrowserGETReferrer##suffix, \
|
|
|
|
REQTEST_GET_REFERRER, context_mode, true); \
|
2015-03-02 21:25:14 +01:00
|
|
|
REQ_TEST(BrowserPOST##suffix, REQTEST_POST, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserPOSTFile##suffix, REQTEST_POST_FILE, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserPOSTWithProgress##suffix, \
|
|
|
|
REQTEST_POST_WITHPROGRESS, context_mode, true); \
|
|
|
|
REQ_TEST(BrowserHEAD##suffix, REQTEST_HEAD, context_mode, true); \
|
|
|
|
REQ_TEST(RendererGET##suffix, REQTEST_GET, context_mode, false); \
|
|
|
|
REQ_TEST(RendererGETNoData##suffix, \
|
|
|
|
REQTEST_GET_NODATA, context_mode, false); \
|
|
|
|
REQ_TEST(RendererGETAllowCookies##suffix, \
|
|
|
|
REQTEST_GET_ALLOWCOOKIES, context_mode, false); \
|
|
|
|
REQ_TEST(RendererGETRedirect##suffix, \
|
|
|
|
REQTEST_GET_REDIRECT, context_mode, false); \
|
2015-12-01 19:22:28 +01:00
|
|
|
REQ_TEST(RendererGETReferrer##suffix, \
|
|
|
|
REQTEST_GET_REFERRER, context_mode, false); \
|
2015-03-02 21:25:14 +01:00
|
|
|
REQ_TEST(RendererPOST##suffix, REQTEST_POST, context_mode, false); \
|
|
|
|
REQ_TEST(RendererPOSTWithProgress##suffix, \
|
|
|
|
REQTEST_POST_WITHPROGRESS, context_mode, false); \
|
|
|
|
REQ_TEST(RendererHEAD##suffix, REQTEST_HEAD, context_mode, false)
|
|
|
|
|
|
|
|
REQ_TEST_SET(ContextGlobal, CONTEXT_GLOBAL);
|
|
|
|
REQ_TEST_SET(ContextInMemory, CONTEXT_INMEMORY);
|
|
|
|
REQ_TEST_SET(ContextOnDisk, CONTEXT_ONDISK);
|
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() {
|
2014-07-15 20:10:40 +02:00
|
|
|
CefPostTask(TID_UI, base::Bind(&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());
|
|
|
|
|
|
|
|
// Let the call stack unwind before signaling completion.
|
|
|
|
CefPostTask(TID_UI,
|
2014-07-15 20:10:40 +02:00
|
|
|
base::Bind(&InvalidURLTestClient::CompleteOnUIThread, this));
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
void OnUploadProgress(CefRefPtr<CefURLRequest> request,
|
2015-01-12 21:46:31 +01:00
|
|
|
int64 current,
|
|
|
|
int64 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,
|
2015-01-12 21:46:31 +01:00
|
|
|
int64 current,
|
|
|
|
int64 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.
|
|
|
|
}
|
|
|
|
|
2014-11-12 20:25:15 +01:00
|
|
|
bool GetAuthCredentials(
|
2014-02-11 00:28:58 +01:00
|
|
|
bool isProxy,
|
|
|
|
const CefString& host,
|
|
|
|
int port,
|
|
|
|
const CefString& realm,
|
|
|
|
const CefString& scheme,
|
2014-11-12 20:25:15 +01:00
|
|
|
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");
|
|
|
|
|
2015-03-02 21:25:14 +01:00
|
|
|
CefURLRequest::Create(request, this, NULL);
|
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.
|
|
|
|
TEST(URLRequestTest, BrowserInvalidURL) {
|
|
|
|
CefRefPtr<InvalidURLTestClient> client = new InvalidURLTestClient();
|
|
|
|
client->RunTest();
|
|
|
|
|
|
|
|
// Verify that there's only one reference to the client.
|
2014-07-15 00:18:51 +02:00
|
|
|
EXPECT_TRUE(client->HasOneRef());
|
2014-02-11 00:28:58 +01:00
|
|
|
}
|