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

Side by Side 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, 10 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <memory>
6
7 #include "base/memory/ptr_util.h"
8 #include "base/run_loop.h"
9 #include "content/browser/download/download_item_impl.h"
10 #include "content/browser/download/download_request_core.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/download_url_parameters.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "net/url_request/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16
17 namespace content {
18
19 class DownloadRequestCoreTest : public testing::Test {
20 public:
21 DownloadRequestCoreTest() = default;
22 ~DownloadRequestCoreTest() override = default;
23
24 std::unique_ptr<DownloadUrlParameters> BuildDownloadParameters(
25 const std::string& url) const {
26 GURL gurl(url);
27 return base::MakeUnique<DownloadUrlParameters>(
28 gurl, request_context_getter_.get());
29 }
30
31 void CheckRequestHeaders(const std::string& expected_range_header) const {
32 DCHECK(url_request_.get());
33 std::string range_header;
34 url_request_->extra_request_headers().GetHeader(
35 net::HttpRequestHeaders::kRange, &range_header);
36 EXPECT_EQ(expected_range_header, range_header);
37 }
38
39 void CreateRequestOnIOThread(DownloadUrlParameters* params) {
40 run_loop_.reset(new base::RunLoop());
41
42 // Run on Browser IO thread since there is a thread check in
43 // DownloadRequestCore.
44 BrowserThread::PostTaskAndReplyWithResult(
45 BrowserThread::IO, FROM_HERE,
46 base::Bind(&DownloadRequestCore::CreateRequestOnIOThread,
47 DownloadItem::kInvalidId, params),
48 base::Bind(&DownloadRequestCoreTest::OnRequestCreated,
49 base::Unretained(this)));
50
51 // RunLoop blocks the runtime util the URLRequest is created and reply back
52 // to the main test thread.
53 run_loop_->Run();
54 }
55
56 void OnRequestCreated(std::unique_ptr<net::URLRequest> url_request) {
57 url_request_ = std::move(url_request);
58 DCHECK(url_request_.get());
59 run_loop_->Quit();
60 }
61
62 void SetUp() override {
63 request_context_getter_ = new net::TestURLRequestContextGetter(
64 content::BrowserThread::GetTaskRunnerForThread(
65 content::BrowserThread::UI));
66 }
67
68 void TearDown() override {
69 // URLRequest must be released before |request_context_getter_| gets
70 // destroyed.
71 url_request_.reset();
72 }
73
74 std::unique_ptr<base::RunLoop> run_loop_;
75 std::unique_ptr<net::URLRequest> url_request_;
76
77 // Used to test functions run on particular browser thread.
78 content::TestBrowserThreadBundle browser_threads_;
79 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
80 };
81
82 // Ensure "Range" header is built correctly for normal download.
83 TEST_F(DownloadRequestCoreTest, BuildRangeRequest) {
84 std::unique_ptr<DownloadUrlParameters> params =
85 BuildDownloadParameters("something.com");
86
87 EXPECT_EQ(DownloadSaveInfo::kLengthUnknown, params->length());
88
89 // Non-range request.
90 CreateRequestOnIOThread(params.get());
91 CheckRequestHeaders("");
92 url_request_.reset();
93
94 // Range request with header "Range:bytes=50-99".
95 params->set_etag("123");
96 params->set_length(50);
97 params->set_offset(50);
98 CreateRequestOnIOThread(params.get());
99 CheckRequestHeaders("bytes=50-99");
100 url_request_.reset();
101 }
102
103 // Ensure "Range" header is built correctly for download resumption.
104 // Notice download resumption requires strong validator(i.e. etag or
105 // last-modified).
106 TEST_F(DownloadRequestCoreTest, BuildRangeRequestWithoutLength) {
107 std::unique_ptr<DownloadUrlParameters> params =
108 BuildDownloadParameters("something.com");
109 params->set_etag("123");
110 params->set_offset(50);
111 CreateRequestOnIOThread(params.get());
112 CheckRequestHeaders("bytes=50-");
113 url_request_.reset();
114 }
115
116 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698