Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Unified Diff: content/browser/download/download_request_core_unittest.cc

Issue 2660783002: Range request support for parallel download in DownloadRequestCore. (Closed)
Patch Set: Strong validator for all range request. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/download/download_request_core_unittest.cc
diff --git a/content/browser/download/download_request_core_unittest.cc b/content/browser/download/download_request_core_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..62fc9374e20fad435d498a8b96be17524111103a
--- /dev/null
+++ b/content/browser/download/download_request_core_unittest.cc
@@ -0,0 +1,116 @@
+// Copyright 2017 The Chromium 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 <memory>
+
+#include "base/memory/ptr_util.h"
+#include "base/run_loop.h"
+#include "content/browser/download/download_item_impl.h"
+#include "content/browser/download/download_request_core.h"
+#include "content/public/browser/browser_thread.h"
+#include "content/public/browser/download_url_parameters.h"
+#include "content/public/test/test_browser_thread_bundle.h"
+#include "net/url_request/url_request_test_util.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace content {
+
+class DownloadRequestCoreTest : public testing::Test {
+ public:
+ DownloadRequestCoreTest() = default;
+ ~DownloadRequestCoreTest() override = default;
+
+ std::unique_ptr<DownloadUrlParameters> BuildDownloadParameters(
+ const std::string& url) const {
+ GURL gurl(url);
+ return base::MakeUnique<DownloadUrlParameters>(
+ gurl, request_context_getter_.get());
+ }
+
+ void CheckRequestHeaders(const std::string& expected_range_header) const {
+ DCHECK(url_request_.get());
+ std::string range_header;
+ url_request_->extra_request_headers().GetHeader(
+ net::HttpRequestHeaders::kRange, &range_header);
+ EXPECT_EQ(expected_range_header, range_header);
+ }
+
+ void CreateRequestOnIOThread(DownloadUrlParameters* params) {
+ run_loop_.reset(new base::RunLoop());
+
+ // Run on Browser IO thread since there is a thread check in
+ // DownloadRequestCore.
+ BrowserThread::PostTaskAndReplyWithResult(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&DownloadRequestCore::CreateRequestOnIOThread,
+ DownloadItem::kInvalidId, params),
+ base::Bind(&DownloadRequestCoreTest::OnRequestCreated,
+ base::Unretained(this)));
+
+ // RunLoop blocks the runtime util the URLRequest is created and reply back
+ // to the main test thread.
+ run_loop_->Run();
+ }
+
+ void OnRequestCreated(std::unique_ptr<net::URLRequest> url_request) {
+ url_request_ = std::move(url_request);
+ DCHECK(url_request_.get());
+ run_loop_->Quit();
+ }
+
+ void SetUp() override {
+ request_context_getter_ = new net::TestURLRequestContextGetter(
+ content::BrowserThread::GetTaskRunnerForThread(
+ content::BrowserThread::UI));
+ }
+
+ void TearDown() override {
+ // URLRequest must be released before |request_context_getter_| gets
+ // destroyed.
+ url_request_.reset();
+ }
+
+ std::unique_ptr<base::RunLoop> run_loop_;
+ std::unique_ptr<net::URLRequest> url_request_;
+
+ // Used to test functions run on particular browser thread.
+ content::TestBrowserThreadBundle browser_threads_;
+ scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
+};
+
+// Ensure "Range" header is built correctly for normal download.
+TEST_F(DownloadRequestCoreTest, BuildRangeRequest) {
+ std::unique_ptr<DownloadUrlParameters> params =
+ BuildDownloadParameters("something.com");
+
+ EXPECT_EQ(DownloadSaveInfo::kLengthUnknown, params->length());
+
+ // Non-range request.
+ CreateRequestOnIOThread(params.get());
+ CheckRequestHeaders("");
+ url_request_.reset();
+
+ // Range request with header "Range:bytes=50-99".
+ params->set_etag("123");
+ params->set_length(50);
+ params->set_offset(50);
+ CreateRequestOnIOThread(params.get());
+ CheckRequestHeaders("bytes=50-99");
+ url_request_.reset();
+}
+
+// Ensure "Range" header is built correctly for download resumption.
+// Notice download resumption requires strong validator(i.e. etag or
+// last-modified).
+TEST_F(DownloadRequestCoreTest, BuildRangeRequestWithoutLength) {
+ std::unique_ptr<DownloadUrlParameters> params =
+ BuildDownloadParameters("something.com");
+ params->set_etag("123");
+ params->set_offset(50);
+ CreateRequestOnIOThread(params.get());
+ CheckRequestHeaders("bytes=50-");
+ url_request_.reset();
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698