Commit b624551f authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Revert "Port policy::UploadJobImpl to network::SimpleURLLoader"

This reverts commit 581330a9.

Reason for revert: SimpleURLLoaderTest.OnUploadProgressCallback/3 is flaky

Original change's description:
> Port policy::UploadJobImpl to network::SimpleURLLoader
> 
> This change ports UploadJobImpl from net::URLFetcher to SimpleURLLoader.
> 
> Since this request does not make use of the response a new method has
> been added to SimpleURLLoader, DownloadToNull. This download mode still
> reads the response body from the network but does not save it anywhere.
> This is useful for requests which upload data and would otherwise have
> to specify an arbitrary |max_body_size|. A maximum may still be set if
> the caller wants to limit the amount of data transferred over the
> network.
> 
> Bug: 773295
> Cq-Include-Trybots: luci.chromium.try:linux_mojo
> Change-Id: Iee1fdc6f7406066ced8c91122e22cd51ddcb1c5f
> Reviewed-on: https://chromium-review.googlesource.com/1161551
> Commit-Queue: Reilly Grant <reillyg@chromium.org>
> Reviewed-by: Matt Menke <mmenke@chromium.org>
> Reviewed-by: Julian Pastarmov <pastarmovj@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#581060}

TBR=pastarmovj@chromium.org,reillyg@chromium.org,mmenke@chromium.org

Change-Id: Ica09c85a12861d35efbdfd9a882d137bac6b9e56
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 773295, 872023
Cq-Include-Trybots: luci.chromium.try:linux_mojo
Reviewed-on: https://chromium-review.googlesource.com/1166288Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581389}
parent e00760c6
......@@ -53,6 +53,8 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob(
chromeos::DeviceOAuth2TokenService* device_oauth2_token_service =
chromeos::DeviceOAuth2TokenServiceFactory::Get();
scoped_refptr<net::URLRequestContextGetter> system_request_context =
g_browser_process->system_request_context();
std::string robot_account_id =
device_oauth2_token_service->GetRobotAccountId();
......@@ -78,7 +80,7 @@ std::unique_ptr<UploadJob> ScreenshotDelegate::CreateUploadJob(
)");
return std::unique_ptr<UploadJob>(new UploadJobImpl(
upload_url, robot_account_id, device_oauth2_token_service,
g_browser_process->shared_url_loader_factory(), delegate,
system_request_context, delegate,
base::WrapUnique(new UploadJobImpl::RandomMimeBoundaryGenerator),
traffic_annotation, base::ThreadTaskRunnerHandle::Get()));
}
......
......@@ -27,7 +27,6 @@
#include "components/policy/core/browser/browser_policy_connector.h"
#include "components/user_manager/user_manager.h"
#include "net/http/http_request_headers.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
namespace policy {
......@@ -145,6 +144,8 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob(
chromeos::DeviceOAuth2TokenService* device_oauth2_token_service =
chromeos::DeviceOAuth2TokenServiceFactory::Get();
scoped_refptr<net::URLRequestContextGetter> system_request_context =
g_browser_process->system_request_context();
std::string robot_account_id =
device_oauth2_token_service->GetRobotAccountId();
......@@ -172,7 +173,7 @@ std::unique_ptr<UploadJob> SystemLogDelegate::CreateUploadJob(
)");
return std::make_unique<UploadJobImpl>(
upload_url, robot_account_id, device_oauth2_token_service,
g_browser_process->shared_url_loader_factory(), delegate,
system_request_context, delegate,
std::make_unique<UploadJobImpl::RandomMimeBoundaryGenerator>(),
traffic_annotation, task_runner_);
}
......
......@@ -19,8 +19,7 @@
#include "net/base/mime_util.h"
#include "net/http/http_status_code.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "services/network/public/cpp/simple_url_loader.h"
#include "net/url_request/url_request_status.h"
namespace policy {
......@@ -28,7 +27,7 @@ namespace {
// Format for bearer tokens in HTTP requests to access OAuth 2.0 protected
// resources.
const char kAuthorizationHeaderFormat[] = "Bearer %s";
const char kAuthorizationHeaderFormat[] = "Authorization: Bearer %s";
// Value the "Content-Type" field will be set to in the POST request.
const char kUploadContentType[] = "multipart/form-data";
......@@ -152,7 +151,7 @@ UploadJobImpl::UploadJobImpl(
const GURL& upload_url,
const std::string& account_id,
OAuth2TokenService* token_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
scoped_refptr<net::URLRequestContextGetter> url_context_getter,
Delegate* delegate,
std::unique_ptr<MimeBoundaryGenerator> boundary_generator,
net::NetworkTrafficAnnotationTag traffic_annotation,
......@@ -161,7 +160,7 @@ UploadJobImpl::UploadJobImpl(
upload_url_(upload_url),
account_id_(account_id),
token_service_(token_service),
url_loader_factory_(std::move(url_loader_factory)),
url_context_getter_(url_context_getter),
delegate_(delegate),
boundary_generator_(std::move(boundary_generator)),
traffic_annotation_(traffic_annotation),
......@@ -170,7 +169,7 @@ UploadJobImpl::UploadJobImpl(
task_runner_(task_runner),
weak_factory_(this) {
DCHECK(token_service_);
DCHECK(url_loader_factory_);
DCHECK(url_context_getter_);
DCHECK(delegate_);
SYSLOG(INFO) << "Upload job created.";
if (!upload_url_.is_valid()) {
......@@ -300,7 +299,7 @@ bool UploadJobImpl::SetUpMultipart() {
return true;
}
void UploadJobImpl::CreateAndStartURLLoader(const std::string& access_token) {
void UploadJobImpl::CreateAndStartURLFetcher(const std::string& access_token) {
// Ensure that the content has been prepared and the upload url is valid.
DCHECK_EQ(PREPARING_CONTENT, state_);
SYSLOG(INFO) << "Starting URL fetcher.";
......@@ -309,20 +308,13 @@ void UploadJobImpl::CreateAndStartURLLoader(const std::string& access_token) {
content_type.append("; boundary=");
content_type.append(*mime_boundary_.get());
auto resource_request = std::make_unique<network::ResourceRequest>();
resource_request->method = "POST";
resource_request->url = upload_url_;
resource_request->headers.SetHeader(
net::HttpRequestHeaders::kAuthorization,
upload_fetcher_ = net::URLFetcher::Create(upload_url_, net::URLFetcher::POST,
this, traffic_annotation_);
upload_fetcher_->SetRequestContext(url_context_getter_.get());
upload_fetcher_->SetUploadData(content_type, *post_data_);
upload_fetcher_->AddExtraRequestHeader(
base::StringPrintf(kAuthorizationHeaderFormat, access_token.c_str()));
url_loader_ = network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation_);
url_loader_->AttachStringForUpload(*post_data_, content_type);
url_loader_->DownloadHeadersOnly(
url_loader_factory_.get(),
base::BindOnce(&UploadJobImpl::OnURLLoadComplete,
base::Unretained(this)));
upload_fetcher_->Start();
}
void UploadJobImpl::StartUpload() {
......@@ -334,7 +326,7 @@ void UploadJobImpl::StartUpload() {
state_ = ERROR;
return;
}
CreateAndStartURLLoader(access_token_);
CreateAndStartURLFetcher(access_token_);
state_ = UPLOADING;
}
......@@ -364,7 +356,7 @@ void UploadJobImpl::OnGetTokenFailure(
void UploadJobImpl::HandleError(ErrorCode error_code) {
retry_++;
url_loader_.reset();
upload_fetcher_.reset();
SYSLOG(ERROR) << "Upload failed, error code: " << error_code;
......@@ -405,31 +397,34 @@ void UploadJobImpl::HandleError(ErrorCode error_code) {
}
}
void UploadJobImpl::OnURLLoadComplete(
scoped_refptr<net::HttpResponseHeaders> headers) {
void UploadJobImpl::OnURLFetchComplete(const net::URLFetcher* source) {
DCHECK_EQ(upload_fetcher_.get(), source);
DCHECK_EQ(UPLOADING, state_);
SYSLOG(INFO) << "URL fetch completed.";
std::unique_ptr<network::SimpleURLLoader> url_loader = std::move(url_loader_);
if (!headers) {
SYSLOG(ERROR) << "SimpleURLLoader error " << url_loader->NetError();
const net::URLRequestStatus& status = source->GetStatus();
if (!status.is_success()) {
SYSLOG(ERROR) << "URLRequestStatus error " << status.error();
HandleError(NETWORK_ERROR);
} else if (headers->response_code() == net::HTTP_OK) {
// Successful upload
access_token_.clear();
post_data_.reset();
state_ = SUCCESS;
UMA_HISTOGRAM_EXACT_LINEAR(kUploadJobSuccessHistogram, retry_,
static_cast<int>(UploadJobSuccess::REQUEST_MAX));
delegate_->OnSuccess();
} else if (headers->response_code() == net::HTTP_UNAUTHORIZED) {
SYSLOG(ERROR) << "Unauthorized request.";
HandleError(AUTHENTICATION_ERROR);
} else {
SYSLOG(ERROR) << "POST request failed with HTTP status code "
<< headers->response_code() << ".";
HandleError(SERVER_ERROR);
const int response_code = source->GetResponseCode();
if (response_code == net::HTTP_OK) {
// Successful upload
upload_fetcher_.reset();
access_token_.clear();
post_data_.reset();
state_ = SUCCESS;
UMA_HISTOGRAM_EXACT_LINEAR(
kUploadJobSuccessHistogram, retry_,
static_cast<int>(UploadJobSuccess::REQUEST_MAX));
delegate_->OnSuccess();
} else if (response_code == net::HTTP_UNAUTHORIZED) {
SYSLOG(ERROR) << "Unauthorized request.";
HandleError(AUTHENTICATION_ERROR);
} else {
SYSLOG(ERROR) << "POST request failed with HTTP status code "
<< response_code << ".";
HandleError(SERVER_ERROR);
}
}
}
......
......@@ -15,28 +15,23 @@
#include "base/threading/thread_checker.h"
#include "chrome/browser/chromeos/policy/upload_job.h"
#include "google_apis/gaia/oauth2_token_service.h"
#include "net/traffic_annotation/network_traffic_annotation.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "net/url_request/url_request_context_getter.h"
#include "url/gurl.h"
namespace base {
class SequencedTaskRunner;
}
namespace net {
class HttpResponseHeaders;
}
namespace network {
class SharedURLLoaderFactory;
class SimpleURLLoader;
} // namespace network
namespace policy {
// This implementation of UploadJob uses the OAuth2TokenService to acquire
// access tokens for the device management (cloud-based policy) server scope and
// uses a SimpleURLLoader to upload data to the specified upload url.
class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// uses a URLFetcher to upload data to the specified upload url.
class UploadJobImpl : public UploadJob,
public OAuth2TokenService::Consumer,
public net::URLFetcherDelegate {
public:
// UploadJobImpl uses a MimeBoundaryGenerator to generate strings which
// mark the boundaries between data segments.
......@@ -61,15 +56,14 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// |task_runner| must belong to the same thread from which the constructor and
// all the public methods are called.
UploadJobImpl(
const GURL& upload_url,
const std::string& account_id,
OAuth2TokenService* token_service,
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory,
Delegate* delegate,
std::unique_ptr<MimeBoundaryGenerator> boundary_generator,
net::NetworkTrafficAnnotationTag traffic_annotation,
scoped_refptr<base::SequencedTaskRunner> task_runner);
UploadJobImpl(const GURL& upload_url,
const std::string& account_id,
OAuth2TokenService* token_service,
scoped_refptr<net::URLRequestContextGetter> url_context_getter,
Delegate* delegate,
std::unique_ptr<MimeBoundaryGenerator> boundary_generator,
net::NetworkTrafficAnnotationTag traffic_annotation,
scoped_refptr<base::SequencedTaskRunner> task_runner);
~UploadJobImpl() override;
// UploadJob:
......@@ -109,15 +103,15 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
void OnGetTokenFailure(const OAuth2TokenService::Request* request,
const GoogleServiceAuthError& error) override;
// Called when the SimpleURLLoader is finished.
void OnURLLoadComplete(scoped_refptr<net::HttpResponseHeaders> headers);
// net::URLFetcherDelegate:
void OnURLFetchComplete(const net::URLFetcher* source) override;
void HandleError(ErrorCode errorCode);
// Requests an access token for the upload scope.
void RequestAccessToken();
// Dispatches POST request.
// Dispatches POST request to URLFetcher.
void StartUpload();
// Constructs the body of the POST request by concatenating the
......@@ -128,9 +122,9 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// an error, clears |post_data_| and |mime_boundary_| and returns false.
bool SetUpMultipart();
// Assembles the request and starts the SimpleURLLoader. Fails if another
// upload is still in progress or the content was not successfully encoded.
void CreateAndStartURLLoader(const std::string& access_token);
// Assembles the request and starts the URLFetcher. Fails if another upload
// is still in progress or the content was not successfully encoded.
void CreateAndStartURLFetcher(const std::string& access_token);
// The URL to which the POST request should be directed.
const GURL upload_url_;
......@@ -141,8 +135,8 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
// The token service used to retrieve the access token.
OAuth2TokenService* const token_service_;
// This is used to initialize the network::SimpleURLLoader object.
const scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
// This is used to initialize the net::URLFetcher object.
const scoped_refptr<net::URLRequestContextGetter> url_context_getter_;
// The delegate to be notified of events.
Delegate* const delegate_;
......@@ -175,7 +169,7 @@ class UploadJobImpl : public UploadJob, public OAuth2TokenService::Consumer {
std::string access_token_;
// Helper to upload the data.
std::unique_ptr<network::SimpleURLLoader> url_loader_;
std::unique_ptr<net::URLFetcher> upload_fetcher_;
// The data chunks to be uploaded.
std::vector<std::unique_ptr<DataSegment>> data_segments_;
......
......@@ -27,7 +27,8 @@
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"
#include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
#include "services/network/test/test_shared_url_loader_factory.h"
#include "net/url_request/url_request_test_util.h"
#include "services/network/public/cpp/shared_url_loader_factory.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace policy {
......@@ -182,8 +183,8 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
// testing::Test:
void SetUp() override {
url_loader_factory_ =
base::MakeRefCounted<network::TestSharedURLLoaderFactory>();
request_context_getter_ = new net::TestURLRequestContextGetter(
base::ThreadTaskRunnerHandle::Get());
oauth2_service_.AddAccount("robot@gmail.com");
ASSERT_TRUE(test_server_.Start());
// Set retry delay to prevent timeouts
......@@ -200,9 +201,9 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
std::unique_ptr<UploadJobImpl::MimeBoundaryGenerator>
mime_boundary_generator) {
std::unique_ptr<UploadJob> upload_job(new UploadJobImpl(
GetServerURL(), kRobotAccountId, &oauth2_service_, url_loader_factory_,
this, std::move(mime_boundary_generator), TRAFFIC_ANNOTATION_FOR_TESTS,
base::ThreadTaskRunnerHandle::Get()));
GetServerURL(), kRobotAccountId, &oauth2_service_,
request_context_getter_.get(), this, std::move(mime_boundary_generator),
TRAFFIC_ANNOTATION_FOR_TESTS, base::ThreadTaskRunnerHandle::Get()));
std::map<std::string, std::string> header_entries;
header_entries.insert(std::make_pair(kCustomField1, "CUSTOM1"));
......@@ -219,7 +220,7 @@ class UploadJobTestBase : public testing::Test, public UploadJob::Delegate {
content::TestBrowserThreadBundle test_browser_thread_bundle_;
base::RunLoop run_loop_;
net::EmbeddedTestServer test_server_;
scoped_refptr<network::SharedURLLoaderFactory> url_loader_factory_;
scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
MockOAuth2TokenService oauth2_service_;
std::unique_ptr<UploadJob::ErrorCode> expected_error_;
......
......@@ -188,8 +188,6 @@ class SimpleURLLoaderImpl : public SimpleURLLoader,
void DownloadToStringOfUnboundedSizeUntilCrashAndDie(
mojom::URLLoaderFactory* url_loader_factory,
BodyAsStringCallback body_as_string_callback) override;
void DownloadHeadersOnly(mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) override;
void DownloadToFile(
mojom::URLLoaderFactory* url_loader_factory,
DownloadToFileCompleteCallback download_to_file_complete_callback,
......@@ -627,58 +625,6 @@ class SaveToStringBodyHandler : public BodyHandler,
DISALLOW_COPY_AND_ASSIGN(SaveToStringBodyHandler);
};
// BodyHandler that discards the response body.
class HeadersOnlyBodyHandler : public BodyHandler, public BodyReader::Delegate {
public:
HeadersOnlyBodyHandler(
SimpleURLLoaderImpl* simple_url_loader,
SimpleURLLoader::HeadersOnlyCallback headers_only_callback)
: BodyHandler(simple_url_loader),
headers_only_callback_(std::move(headers_only_callback)) {}
~HeadersOnlyBodyHandler() override {}
// BodyHandler implementation
void OnStartLoadingResponseBody(
mojo::ScopedDataPipeConsumerHandle body_data_pipe) override {
// TODO(crbug.com/871420): The request can be completed at this point
// however that requires more changes to SimpleURLLoader as OnComplete()
// will not have been called yet.
DCHECK(!body_reader_);
body_reader_ =
std::make_unique<BodyReader>(this, std::numeric_limits<int64_t>::max());
body_reader_->Start(std::move(body_data_pipe));
}
void NotifyConsumerOfCompletion(bool destroy_results) override {
body_reader_.reset();
std::move(headers_only_callback_)
.Run(simple_url_loader()->ResponseInfo()
? simple_url_loader()->ResponseInfo()->headers
: nullptr);
}
void PrepareToRetry(base::OnceClosure retry_callback) override {
body_reader_.reset();
std::move(retry_callback).Run();
}
private:
// BodyReader::Delegate implementation
net::Error OnDataRead(uint32_t length, const char* data) override {
return net::OK;
}
void OnDone(net::Error error, int64_t total_bytes) override {
simple_url_loader()->OnBodyHandlerDone(error, total_bytes);
}
SimpleURLLoader::HeadersOnlyCallback headers_only_callback_;
std::unique_ptr<BodyReader> body_reader_;
DISALLOW_COPY_AND_ASSIGN(HeadersOnlyBodyHandler);
};
// BodyHandler implementation for saving the response to a file
class SaveToFileBodyHandler : public BodyHandler {
public:
......@@ -1115,14 +1061,6 @@ void SimpleURLLoaderImpl::DownloadToStringOfUnboundedSizeUntilCrashAndDie(
Start(url_loader_factory);
}
void SimpleURLLoaderImpl::DownloadHeadersOnly(
mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) {
body_handler_ = std::make_unique<HeadersOnlyBodyHandler>(
this, std::move(headers_only_callback));
Start(url_loader_factory);
}
void SimpleURLLoaderImpl::DownloadToFile(
mojom::URLLoaderFactory* url_loader_factory,
DownloadToFileCompleteCallback download_to_file_complete_callback,
......@@ -1308,7 +1246,6 @@ void SimpleURLLoaderImpl::FinishWithResult(int net_error) {
request_state_->finished = true;
request_state_->net_error = net_error;
// If it's a partial download or an error was received, erase the body.
bool destroy_results =
request_state_->net_error != net::OK && !allow_partial_results_;
......@@ -1374,6 +1311,7 @@ void SimpleURLLoaderImpl::Retry() {
url_loader_.reset();
request_state_ = std::make_unique<RequestState>();
body_handler_->PrepareToRetry(base::BindOnce(
&SimpleURLLoaderImpl::StartRequest, weak_ptr_factory_.GetWeakPtr(),
url_loader_factory_ptr_.get()));
......
......@@ -18,15 +18,11 @@
class GURL;
template <class T>
class scoped_refptr;
namespace base {
class FilePath;
}
namespace net {
class HttpResponseHeaders;
struct NetworkTrafficAnnotationTag;
struct RedirectInfo;
} // namespace net
......@@ -89,14 +85,8 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
using BodyAsStringCallback =
base::OnceCallback<void(std::unique_ptr<std::string> response_body)>;
// Callback used when ignoring the response body. |headers| are the received
// HTTP headers, or nullptr if none were received. It is safe to delete the
// SimpleURLLoader during the callback.
using HeadersOnlyCallback =
base::OnceCallback<void(scoped_refptr<net::HttpResponseHeaders> headers)>;
// Callback used when downloading the response body to a file. On failure,
// |path| will be empty. It is safe to delete the SimpleURLLoader during the
// Callback used when download the response body to a file. On failure, |path|
// will be empty. It is safe to delete the SimpleURLLoader during the
// callback.
using DownloadToFileCompleteCallback =
base::OnceCallback<void(base::FilePath path)>;
......@@ -130,7 +120,7 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
virtual ~SimpleURLLoader();
// Starts the request using |url_loader_factory|. The SimpleURLLoader will
// Starts the request using |network_context|. The SimpleURLLoader will
// accumulate all downloaded data in an in-memory string of bounded size. If
// |max_body_size| is exceeded, the request will fail with
// net::ERR_INSUFFICIENT_RESOURCES. |max_body_size| must be no greater than 1
......@@ -155,14 +145,6 @@ class COMPONENT_EXPORT(NETWORK_CPP) SimpleURLLoader {
mojom::URLLoaderFactory* url_loader_factory,
BodyAsStringCallback body_as_string_callback) = 0;
// Starts the request using |url_loader_factory|. The SimpleURLLoader will
// discard the response body as it is received and |headers_only_callback|
// will be invoked on completion. It is safe to delete the SimpleURLLoader in
// this callback.
virtual void DownloadHeadersOnly(
mojom::URLLoaderFactory* url_loader_factory,
HeadersOnlyCallback headers_only_callback) = 0;
// SimpleURLLoader will download the entire response to a file at the
// specified path. File I/O will happen on another sequence, so it's safe to
// use this on any sequence.
......
......@@ -94,13 +94,7 @@ class SimpleLoaderTestHelper : public SimpleURLLoaderStreamConsumer {
public:
// What the response should be downloaded to. Running all tests for all types
// is more than strictly needed, but simplest just to cover all cases.
enum class DownloadType {
TO_STRING,
TO_FILE,
TO_TEMP_FILE,
HEADERS_ONLY,
AS_STREAM
};
enum class DownloadType { TO_STRING, TO_FILE, TO_TEMP_FILE, AS_STREAM };
explicit SimpleLoaderTestHelper(
std::unique_ptr<network::ResourceRequest> resource_request,
......@@ -183,12 +177,6 @@ class SimpleLoaderTestHelper : public SimpleURLLoaderStreamConsumer {
max_body_size);
}
break;
case DownloadType::HEADERS_ONLY:
simple_url_loader_->DownloadHeadersOnly(
url_loader_factory,
base::BindOnce(&SimpleLoaderTestHelper::DownloadedHeadersOnly,
base::Unretained(this)));
break;
case DownloadType::AS_STREAM:
// Downloading to stream doesn't support a max body size.
DCHECK_LT(max_body_size, 0);
......@@ -355,19 +343,6 @@ class SimpleLoaderTestHelper : public SimpleURLLoaderStreamConsumer {
run_loop_.Quit();
}
void DownloadedHeadersOnly(scoped_refptr<net::HttpResponseHeaders> headers) {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
EXPECT_FALSE(done_);
EXPECT_EQ(DownloadType::HEADERS_ONLY, download_type_);
EXPECT_FALSE(response_body_);
if (destroy_loader_on_complete_)
simple_url_loader_.reset();
done_ = true;
run_loop_.Quit();
}
// SimpleURLLoaderStreamConsumer implementation:
void OnDataReceived(base::StringPiece string_piece,
......@@ -674,11 +649,8 @@ TEST_P(SimpleURLLoaderTest, BasicRequest) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Expected Response", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Expected Response", *test_helper->response_body());
}
// Test that SimpleURLLoader handles data URLs, which don't have headers.
......@@ -690,11 +662,8 @@ TEST_P(SimpleURLLoaderTest, DataURL) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
ASSERT_TRUE(test_helper->simple_url_loader()->ResponseInfo());
EXPECT_FALSE(test_helper->simple_url_loader()->ResponseInfo()->headers);
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("foo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("foo", *test_helper->response_body());
}
// Make sure the class works when the size of the encoded and decoded bodies are
......@@ -706,11 +675,8 @@ TEST_P(SimpleURLLoaderTest, GzipBody) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("foo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("foo", *test_helper->response_body());
}
// Make sure redirects are followed.
......@@ -722,11 +688,8 @@ TEST_P(SimpleURLLoaderTest, Redirect) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
// Make sure OnRedirectCallback is invoked on a redirect.
......@@ -752,11 +715,8 @@ TEST_P(SimpleURLLoaderTest, OnRedirectCallback) {
base::Unretained(&response_head)));
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
EXPECT_EQ(1, num_redirects);
EXPECT_EQ(test_server_.GetURL("/echo"), redirect_info.new_url);
......@@ -781,10 +741,8 @@ TEST_P(SimpleURLLoaderTest, OnRedirectCallbackTwoRedirects) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
EXPECT_EQ(2, num_redirects);
}
......@@ -836,12 +794,8 @@ TEST_P(SimpleURLLoaderTest, UploadShortStringWithRedirect) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
// Make sure request really was redirected.
EXPECT_EQ(1, num_redirects);
}
......@@ -864,12 +818,8 @@ TEST_P(SimpleURLLoaderTest, UploadLongStringWithRedirect) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetLongUploadBody(), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetLongUploadBody(), *test_helper->response_body());
// Make sure request really was redirected.
EXPECT_EQ(1, num_redirects);
}
......@@ -899,14 +849,10 @@ TEST_P(SimpleURLLoaderTest,
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
// The "foo" header is removed since the SimpleURLLoader's redirect callback
// marks "foo" header to be removed.
EXPECT_EQ("None", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
// The "foo" header is removed since the SimpleURLLoader's redirect callback
// marks "foo" header to be removed.
EXPECT_EQ("None", *test_helper->response_body());
// Make sure request really was redirected.
EXPECT_EQ(1, num_redirects);
}
......@@ -933,14 +879,10 @@ TEST_P(SimpleURLLoaderTest,
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
// The "foo" header is not removed since the SimpleURLLoader's redirect
// callback marks "bar" header to be removed.
EXPECT_EQ("Expected Response", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
// The "foo" header is not removed since the SimpleURLLoader's redirect
// callback marks "bar" header to be removed.
EXPECT_EQ("Expected Response", *test_helper->response_body());
// Make sure request really was redirected.
EXPECT_EQ(1, num_redirects);
}
......@@ -1014,11 +956,8 @@ TEST_P(SimpleURLLoaderTest, DestroyLoaderInOnComplete) {
CreateHelper(std::move(resource_request));
test_helper->set_destroy_loader_on_complete(true);
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Expected Response", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Expected Response", *test_helper->response_body());
}
// Check the case where a URLLoaderFactory with a closed Mojo pipe was passed
......@@ -1062,11 +1001,8 @@ TEST_P(SimpleURLLoaderTest, HttpErrorStatusCodeResponseAllowed) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(400, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("Echo", *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, EmptyResponseBody) {
......@@ -1076,12 +1012,9 @@ TEST_P(SimpleURLLoaderTest, EmptyResponseBody) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(204, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
// A response body is sent from the NetworkService, but it's empty.
EXPECT_EQ("", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
// A response body is sent from the NetworkService, but it's empty.
EXPECT_EQ("", *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, BigResponseBody) {
......@@ -1097,12 +1030,9 @@ TEST_P(SimpleURLLoaderTest, BigResponseBody) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeMatchingLimit) {
......@@ -1119,12 +1049,9 @@ TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeMatchingLimit) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeBelowLimit) {
......@@ -1142,12 +1069,9 @@ TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeBelowLimit) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeAboveLimit) {
......@@ -1162,12 +1086,8 @@ TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeAboveLimit) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get(),
kResponseSize - 1);
if (GetParam() == SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
} else {
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
}
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
EXPECT_FALSE(test_helper->response_body());
}
......@@ -1186,17 +1106,11 @@ TEST_P(SimpleURLLoaderTest, ResponseBodyWithSizeAboveLimitPartialResponse) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get(),
kMaxResponseSize);
if (GetParam() == SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_FALSE(test_helper->response_body());
} else {
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(kMaxResponseSize, 'a'),
*test_helper->response_body());
EXPECT_EQ(kMaxResponseSize, test_helper->response_body()->length());
}
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(kMaxResponseSize, 'a'), *test_helper->response_body());
EXPECT_EQ(kMaxResponseSize, test_helper->response_body()->length());
}
// The next 4 tests duplicate the above 4, but with larger response sizes. This
......@@ -1214,12 +1128,9 @@ TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeMatchingLimit) {
kResponseSize);
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeBelowLimit) {
......@@ -1236,12 +1147,9 @@ TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeBelowLimit) {
kMaxResponseSize);
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kResponseSize, test_helper->response_body()->length());
EXPECT_EQ(std::string(kResponseSize, 'a'), *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeAboveLimit) {
......@@ -1256,12 +1164,8 @@ TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeAboveLimit) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get(),
kResponseSize - 1);
if (GetParam() == SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
} else {
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
}
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
EXPECT_FALSE(test_helper->response_body());
}
......@@ -1279,17 +1183,11 @@ TEST_P(SimpleURLLoaderTest, BigResponseBodyWithSizeAboveLimitPartialResponse) {
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get(),
kMaxResponseSize);
if (GetParam() == SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_FALSE(test_helper->response_body());
} else {
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(kMaxResponseSize, 'a'),
*test_helper->response_body());
EXPECT_EQ(kMaxResponseSize, test_helper->response_body()->length());
}
EXPECT_EQ(net::ERR_INSUFFICIENT_RESOURCES,
test_helper->simple_url_loader()->NetError());
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(kMaxResponseSize, 'a'), *test_helper->response_body());
EXPECT_EQ(kMaxResponseSize, test_helper->response_body()->length());
}
TEST_P(SimpleURLLoaderTest, NetErrorBeforeHeaders) {
......@@ -1339,11 +1237,8 @@ TEST_P(SimpleURLLoaderTest, NetErrorAfterHeadersWithPartialResults) {
EXPECT_EQ(net::ERR_CONTENT_DECODING_FAILED,
test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, TruncatedBody) {
......@@ -1366,11 +1261,8 @@ TEST_P(SimpleURLLoaderTest, TruncatedBodyWithPartialResults) {
EXPECT_EQ(net::ERR_CONTENT_LENGTH_MISMATCH,
test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kTruncatedBody, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kTruncatedBody, *test_helper->response_body());
}
// Test case where NetworkService is destroyed before headers are received (and
......@@ -1399,11 +1291,8 @@ TEST_P(SimpleURLLoaderTest, UploadShortString) {
"text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, UploadLongString) {
......@@ -1414,11 +1303,8 @@ TEST_P(SimpleURLLoaderTest, UploadLongString) {
"text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(long_string, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(long_string, *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, UploadEmptyString) {
......@@ -1427,25 +1313,19 @@ TEST_P(SimpleURLLoaderTest, UploadEmptyString) {
test_helper->simple_url_loader()->AttachStringForUpload("", "text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
// Also make sure the correct method was sent, with the right content-type.
test_helper = CreateHelperForURL(test_server_.GetURL("/echoall"), "POST");
test_helper->simple_url_loader()->AttachStringForUpload("", "text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("PUT /"));
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("PUT /"));
}
TEST_P(SimpleURLLoaderTest, UploadShortStringWithRetry) {
......@@ -1457,11 +1337,8 @@ TEST_P(SimpleURLLoaderTest, UploadShortStringWithRetry) {
1, SimpleURLLoader::RETRY_ON_5XX);
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(kShortUploadBody, *test_helper->response_body());
if (GetParam() == SimpleLoaderTestHelper::DownloadType::AS_STREAM)
EXPECT_EQ(1, test_helper->download_as_stream_retries());
......@@ -1477,11 +1354,8 @@ TEST_P(SimpleURLLoaderTest, UploadLongStringWithRetry) {
1, SimpleURLLoader::RETRY_ON_5XX);
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(long_string, *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(long_string, *test_helper->response_body());
if (GetParam() == SimpleLoaderTestHelper::DownloadType::AS_STREAM)
EXPECT_EQ(1, test_helper->download_as_stream_retries());
......@@ -1494,11 +1368,8 @@ TEST_P(SimpleURLLoaderTest, UploadFile) {
"text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
// Also make sure the correct method was sent, with the right content-type.
test_helper = CreateHelperForURL(test_server_.GetURL("/echoall"), "POST");
......@@ -1506,14 +1377,11 @@ TEST_P(SimpleURLLoaderTest, UploadFile) {
"text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("PUT /"));
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("PUT /"));
}
TEST_P(SimpleURLLoaderTest, UploadFileWithPut) {
......@@ -1523,11 +1391,8 @@ TEST_P(SimpleURLLoaderTest, UploadFileWithPut) {
"text/plain");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
// Also make sure the correct method was sent, with the right content-type.
test_helper = CreateHelperForURL(test_server_.GetURL("/echoall"), "PUT");
......@@ -1535,16 +1400,13 @@ TEST_P(SimpleURLLoaderTest, UploadFileWithPut) {
"text/salted");
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/salted"));
EXPECT_EQ(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("PUT /"));
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_NE(std::string::npos,
test_helper->response_body()->find("Content-Type: text/salted"));
EXPECT_EQ(std::string::npos,
test_helper->response_body()->find("Content-Type: text/plain"));
EXPECT_EQ(std::string::npos, test_helper->response_body()->find("POST /"));
EXPECT_NE(std::string::npos, test_helper->response_body()->find("PUT /"));
}
TEST_P(SimpleURLLoaderTest, UploadFileWithRetry) {
......@@ -1556,11 +1418,8 @@ TEST_P(SimpleURLLoaderTest, UploadFileWithRetry) {
1, SimpleURLLoader::RETRY_ON_5XX);
test_helper->StartSimpleLoaderAndWait(url_loader_factory_.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(GetTestFileContents(), *test_helper->response_body());
if (GetParam() == SimpleLoaderTestHelper::DownloadType::AS_STREAM)
EXPECT_EQ(1, test_helper->download_as_stream_retries());
......@@ -2115,21 +1974,16 @@ TEST_P(SimpleURLLoaderTest, CloseClientPipeOrder) {
}
if (!allow_partial_results) {
EXPECT_FALSE(test_helper->response_body());
} else if (GetParam() !=
SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
} else {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(bytes_received, 'a'),
*test_helper->response_body());
}
} else {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() !=
SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(bytes_received, 'a'),
*test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(std::string(bytes_received, 'a'),
*test_helper->response_body());
}
}
}
......@@ -2165,11 +2019,8 @@ TEST_P(SimpleURLLoaderTest, SuccessAndCloseClientPipeBeforeBodyComplete) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("a", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("a", *test_helper->response_body());
}
// Make sure the close client pipe message doesn't cause any issues.
......@@ -2185,11 +2036,8 @@ TEST_P(SimpleURLLoaderTest, SuccessAndCloseClientPipeAfterBodyComplete) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("a", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("a", *test_helper->response_body());
}
TEST_P(SimpleURLLoaderTest, DoubleReceivedResponse) {
......@@ -2363,11 +2211,8 @@ TEST_P(SimpleURLLoaderTest, RetryOn5xx) {
if (test_case.expect_success) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(1u, test_helper->response_body()->size());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(1u, test_helper->response_body()->size());
} else {
EXPECT_EQ(501, test_helper->GetResponseCode());
EXPECT_FALSE(test_helper->response_body());
......@@ -2431,8 +2276,7 @@ TEST_P(SimpleURLLoaderTest, RetryAfterRedirect) {
loader_factory.RunTest(test_helper.get());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY)
EXPECT_TRUE(test_helper->response_body());
EXPECT_TRUE(test_helper->response_body());
EXPECT_EQ(2, num_redirects);
EXPECT_EQ(2u, loader_factory.requested_urls().size());
......@@ -2535,11 +2379,8 @@ TEST_P(SimpleURLLoaderTest, RetryOnNetworkChange) {
if (test_case.expect_success) {
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
EXPECT_EQ(200, test_helper->GetResponseCode());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(1u, test_helper->response_body()->size());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ(1u, test_helper->response_body()->size());
} else {
EXPECT_EQ(net::ERR_NETWORK_CHANGED,
test_helper->simple_url_loader()->NetError());
......@@ -2626,10 +2467,8 @@ TEST_P(SimpleURLLoaderTest, UploadLongStringStartReadTwice) {
loader_factory.RunTest(test_helper.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
// Test the case where DataPipeGetter::Read is called a second time, after only
......@@ -2655,11 +2494,8 @@ TEST_P(SimpleURLLoaderTest,
loader_factory.RunTest(test_helper.get());
EXPECT_EQ(net::OK, test_helper->simple_url_loader()->NetError());
if (GetParam() != SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY) {
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
ASSERT_TRUE(test_helper->response_body());
EXPECT_EQ("", *test_helper->response_body());
}
// Test for GetFinalURL.
......@@ -2693,7 +2529,6 @@ INSTANTIATE_TEST_CASE_P(
testing::Values(SimpleLoaderTestHelper::DownloadType::TO_STRING,
SimpleLoaderTestHelper::DownloadType::TO_FILE,
SimpleLoaderTestHelper::DownloadType::TO_TEMP_FILE,
SimpleLoaderTestHelper::DownloadType::HEADERS_ONLY,
SimpleLoaderTestHelper::DownloadType::AS_STREAM));
class SimpleURLLoaderFileTest : public SimpleURLLoaderTestBase,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment