OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_simple_job.h" | 5 #include "net/url_request/url_request_simple_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
| 8 #include <utility> |
8 | 9 |
9 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
10 #include "base/macros.h" | 11 #include "base/macros.h" |
11 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 14 #include "base/sequenced_task_runner.h" |
| 15 #include "base/strings/string_piece.h" |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/test/sequenced_worker_pool_owner.h" | 17 #include "base/threading/thread.h" |
15 #include "base/threading/worker_pool.h" | |
16 #include "net/base/request_priority.h" | 18 #include "net/base/request_priority.h" |
17 #include "net/test/gtest_util.h" | 19 #include "net/test/gtest_util.h" |
18 #include "net/url_request/url_request_job.h" | 20 #include "net/url_request/url_request_job.h" |
19 #include "net/url_request/url_request_job_factory.h" | 21 #include "net/url_request/url_request_job_factory.h" |
20 #include "net/url_request/url_request_job_factory_impl.h" | 22 #include "net/url_request/url_request_job_factory_impl.h" |
21 #include "net/url_request/url_request_test_util.h" | 23 #include "net/url_request/url_request_test_util.h" |
22 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
24 | 26 |
25 using net::test::IsError; | 27 using net::test::IsError; |
(...skipping 10 matching lines...) Expand all Loading... |
36 kRangeFirstPosition < kRangeLastPosition && | 38 kRangeFirstPosition < kRangeLastPosition && |
37 kRangeLastPosition < | 39 kRangeLastPosition < |
38 static_cast<int>(arraysize(kTestData) - 1), | 40 static_cast<int>(arraysize(kTestData) - 1), |
39 "invalid range"); | 41 "invalid range"); |
40 | 42 |
41 class MockSimpleJob : public URLRequestSimpleJob { | 43 class MockSimpleJob : public URLRequestSimpleJob { |
42 public: | 44 public: |
43 MockSimpleJob(URLRequest* request, | 45 MockSimpleJob(URLRequest* request, |
44 NetworkDelegate* network_delegate, | 46 NetworkDelegate* network_delegate, |
45 scoped_refptr<base::TaskRunner> task_runner, | 47 scoped_refptr<base::TaskRunner> task_runner, |
46 std::string data) | 48 base::StringPiece data) |
47 : URLRequestSimpleJob(request, network_delegate), | 49 : URLRequestSimpleJob(request, network_delegate), |
48 data_(data), | 50 data_(data.as_string()), |
49 task_runner_(task_runner) {} | 51 task_runner_(std::move(task_runner)) {} |
50 | 52 |
51 protected: | 53 protected: |
52 // URLRequestSimpleJob implementation: | 54 // URLRequestSimpleJob implementation: |
53 int GetData(std::string* mime_type, | 55 int GetData(std::string* mime_type, |
54 std::string* charset, | 56 std::string* charset, |
55 std::string* data, | 57 std::string* data, |
56 const CompletionCallback& callback) const override { | 58 const CompletionCallback& callback) const override { |
57 mime_type->assign("text/plain"); | 59 mime_type->assign("text/plain"); |
58 charset->assign("US-ASCII"); | 60 charset->assign("US-ASCII"); |
59 data->assign(data_); | 61 data->assign(data_); |
60 return OK; | 62 return OK; |
61 } | 63 } |
62 | 64 |
63 base::TaskRunner* GetTaskRunner() const override { | 65 base::TaskRunner* GetTaskRunner() const override { |
64 return task_runner_.get(); | 66 return task_runner_.get(); |
65 } | 67 } |
66 | 68 |
67 private: | 69 private: |
68 ~MockSimpleJob() override {} | 70 ~MockSimpleJob() override {} |
69 | 71 |
70 const std::string data_; | 72 const std::string data_; |
71 | 73 |
72 scoped_refptr<base::TaskRunner> task_runner_; | 74 const scoped_refptr<base::TaskRunner> task_runner_; |
73 | 75 |
74 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); | 76 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); |
75 }; | 77 }; |
76 | 78 |
77 class CancelAfterFirstReadURLRequestDelegate : public TestDelegate { | 79 class CancelAfterFirstReadURLRequestDelegate : public TestDelegate { |
78 public: | 80 public: |
79 CancelAfterFirstReadURLRequestDelegate() : run_loop_(new base::RunLoop) {} | 81 CancelAfterFirstReadURLRequestDelegate() : run_loop_(new base::RunLoop) {} |
80 | 82 |
81 ~CancelAfterFirstReadURLRequestDelegate() override {} | 83 ~CancelAfterFirstReadURLRequestDelegate() override {} |
82 | 84 |
(...skipping 15 matching lines...) Expand all Loading... |
98 private: | 100 private: |
99 std::unique_ptr<base::RunLoop> run_loop_; | 101 std::unique_ptr<base::RunLoop> run_loop_; |
100 | 102 |
101 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); | 103 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); |
102 }; | 104 }; |
103 | 105 |
104 class SimpleJobProtocolHandler : | 106 class SimpleJobProtocolHandler : |
105 public URLRequestJobFactory::ProtocolHandler { | 107 public URLRequestJobFactory::ProtocolHandler { |
106 public: | 108 public: |
107 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) | 109 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) |
108 : task_runner_(task_runner) {} | 110 : task_runner_(std::move(task_runner)) {} |
109 URLRequestJob* MaybeCreateJob( | 111 URLRequestJob* MaybeCreateJob( |
110 URLRequest* request, | 112 URLRequest* request, |
111 NetworkDelegate* network_delegate) const override { | 113 NetworkDelegate* network_delegate) const override { |
112 if (request->url().spec() == "data:empty") | 114 if (request->url().spec() == "data:empty") |
113 return new MockSimpleJob(request, network_delegate, task_runner_, ""); | 115 return new MockSimpleJob(request, network_delegate, task_runner_, ""); |
114 return new MockSimpleJob(request, network_delegate, task_runner_, | 116 return new MockSimpleJob(request, network_delegate, task_runner_, |
115 kTestData); | 117 kTestData); |
116 } | 118 } |
117 | 119 |
118 ~SimpleJobProtocolHandler() override {} | 120 ~SimpleJobProtocolHandler() override {} |
119 | 121 |
120 private: | 122 private: |
121 scoped_refptr<base::TaskRunner> task_runner_; | 123 const scoped_refptr<base::TaskRunner> task_runner_; |
| 124 |
| 125 DISALLOW_COPY_AND_ASSIGN(SimpleJobProtocolHandler); |
122 }; | 126 }; |
123 | 127 |
124 class URLRequestSimpleJobTest : public ::testing::Test { | 128 class URLRequestSimpleJobTest : public ::testing::Test { |
125 public: | 129 public: |
126 URLRequestSimpleJobTest() | 130 URLRequestSimpleJobTest() |
127 : worker_pool_owner_(1, "URLRequestSimpleJobTest"), | 131 : worker_thread_("URLRequestSimpleJobTest"), context_(true) { |
128 task_runner_(worker_pool_owner_.pool() | 132 EXPECT_TRUE(worker_thread_.Start()); |
129 ->GetSequencedTaskRunnerWithShutdownBehavior( | 133 |
130 worker_pool_owner_.pool() | |
131 ->GetSequenceToken(), | |
132 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | |
133 context_(true) { | |
134 job_factory_.SetProtocolHandler( | 134 job_factory_.SetProtocolHandler( |
135 "data", base::MakeUnique<SimpleJobProtocolHandler>(task_runner_)); | 135 "data", base::MakeUnique<SimpleJobProtocolHandler>(task_runner())); |
136 context_.set_job_factory(&job_factory_); | 136 context_.set_job_factory(&job_factory_); |
137 context_.Init(); | 137 context_.Init(); |
138 | 138 |
139 request_ = | 139 request_ = |
140 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); | 140 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); |
141 } | 141 } |
142 | 142 |
143 void StartRequest(const HttpRequestHeaders* headers) { | 143 void StartRequest(const HttpRequestHeaders* headers) { |
144 if (headers) | 144 if (headers) |
145 request_->SetExtraRequestHeaders(*headers); | 145 request_->SetExtraRequestHeaders(*headers); |
146 request_->Start(); | 146 request_->Start(); |
147 | 147 |
148 EXPECT_TRUE(request_->is_pending()); | 148 EXPECT_TRUE(request_->is_pending()); |
149 base::RunLoop().Run(); | 149 base::RunLoop().Run(); |
150 EXPECT_FALSE(request_->is_pending()); | 150 EXPECT_FALSE(request_->is_pending()); |
151 } | 151 } |
152 | 152 |
| 153 scoped_refptr<base::SequencedTaskRunner> task_runner() { |
| 154 return worker_thread_.task_runner(); |
| 155 } |
| 156 |
153 protected: | 157 protected: |
154 base::SequencedWorkerPoolOwner worker_pool_owner_; | 158 base::Thread worker_thread_; |
155 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
156 TestURLRequestContext context_; | 159 TestURLRequestContext context_; |
157 URLRequestJobFactoryImpl job_factory_; | 160 URLRequestJobFactoryImpl job_factory_; |
158 TestDelegate delegate_; | 161 TestDelegate delegate_; |
159 std::unique_ptr<URLRequest> request_; | 162 std::unique_ptr<URLRequest> request_; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(URLRequestSimpleJobTest); |
160 }; | 165 }; |
161 | 166 |
162 } // namespace | 167 } // namespace |
163 | 168 |
164 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { | 169 TEST_F(URLRequestSimpleJobTest, SimpleRequest) { |
165 StartRequest(NULL); | 170 StartRequest(NULL); |
166 ASSERT_TRUE(request_->status().is_success()); | 171 ASSERT_TRUE(request_->status().is_success()); |
167 EXPECT_EQ(kTestData, delegate_.data_received()); | 172 EXPECT_EQ(kTestData, delegate_.data_received()); |
168 } | 173 } |
169 | 174 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | 238 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { |
234 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | 239 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
235 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 240 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
236 &cancel_delegate); | 241 &cancel_delegate); |
237 request_->Start(); | 242 request_->Start(); |
238 cancel_delegate.WaitUntilHeadersReceived(); | 243 cancel_delegate.WaitUntilHeadersReceived(); |
239 | 244 |
240 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 245 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
241 // callbacks which are invoked in ReadRawData have completed safely. | 246 // callbacks which are invoked in ReadRawData have completed safely. |
242 base::RunLoop run_loop; | 247 base::RunLoop run_loop; |
243 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 248 EXPECT_TRUE(task_runner()->PostTaskAndReply( |
244 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 249 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
245 run_loop.Run(); | 250 run_loop.Run(); |
246 | 251 |
247 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | 252 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); |
248 EXPECT_EQ(1, cancel_delegate.response_started_count()); | 253 EXPECT_EQ(1, cancel_delegate.response_started_count()); |
249 EXPECT_EQ("", cancel_delegate.data_received()); | 254 EXPECT_EQ("", cancel_delegate.data_received()); |
250 // Destroy the request so it doesn't outlive its delegate. | 255 // Destroy the request so it doesn't outlive its delegate. |
251 request_.reset(); | 256 request_.reset(); |
252 } | 257 } |
253 | 258 |
254 } // namespace net | 259 } // namespace net |
OLD | NEW |