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 "base/memory/scoped_ptr.h" | 5 #include "base/memory/scoped_ptr.h" |
6 #include "base/run_loop.h" | 6 #include "base/run_loop.h" |
7 #include "base/strings/stringprintf.h" | 7 #include "base/strings/stringprintf.h" |
8 #include "base/threading/sequenced_worker_pool.h" | 8 #include "base/threading/sequenced_worker_pool.h" |
9 #include "base/threading/worker_pool.h" | 9 #include "base/threading/worker_pool.h" |
10 #include "net/base/request_priority.h" | 10 #include "net/base/request_priority.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 new base::SequencedWorkerPool(1, "URLRequestSimpleJobTest")), | 118 new base::SequencedWorkerPool(1, "URLRequestSimpleJobTest")), |
119 task_runner_(worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( | 119 task_runner_(worker_pool_->GetSequencedTaskRunnerWithShutdownBehavior( |
120 worker_pool_->GetSequenceToken(), | 120 worker_pool_->GetSequenceToken(), |
121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), | 121 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)), |
122 context_(true) { | 122 context_(true) { |
123 job_factory_.SetProtocolHandler("data", | 123 job_factory_.SetProtocolHandler("data", |
124 new SimpleJobProtocolHandler(task_runner_)); | 124 new SimpleJobProtocolHandler(task_runner_)); |
125 context_.set_job_factory(&job_factory_); | 125 context_.set_job_factory(&job_factory_); |
126 context_.Init(); | 126 context_.Init(); |
127 | 127 |
128 request_ = context_.CreateRequest( | 128 request_ = |
129 GURL("data:test"), DEFAULT_PRIORITY, &delegate_, NULL); | 129 context_.CreateRequest(GURL("data:test"), DEFAULT_PRIORITY, &delegate_); |
130 } | 130 } |
131 | 131 |
132 ~URLRequestSimpleJobTest() override { worker_pool_->Shutdown(); } | 132 ~URLRequestSimpleJobTest() override { worker_pool_->Shutdown(); } |
133 | 133 |
134 void StartRequest(const HttpRequestHeaders* headers) { | 134 void StartRequest(const HttpRequestHeaders* headers) { |
135 if (headers) | 135 if (headers) |
136 request_->SetExtraRequestHeaders(*headers); | 136 request_->SetExtraRequestHeaders(*headers); |
137 request_->Start(); | 137 request_->Start(); |
138 | 138 |
139 EXPECT_TRUE(request_->is_pending()); | 139 EXPECT_TRUE(request_->is_pending()); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
197 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); | 197 "bytes=%d-%d", kRangeLastPosition, kRangeFirstPosition); |
198 headers.SetHeader(HttpRequestHeaders::kRange, range); | 198 headers.SetHeader(HttpRequestHeaders::kRange, range); |
199 | 199 |
200 StartRequest(&headers); | 200 StartRequest(&headers); |
201 | 201 |
202 ASSERT_TRUE(request_->status().is_success()); | 202 ASSERT_TRUE(request_->status().is_success()); |
203 EXPECT_EQ(kTestData, delegate_.data_received()); | 203 EXPECT_EQ(kTestData, delegate_.data_received()); |
204 } | 204 } |
205 | 205 |
206 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 206 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
207 request_ = context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, | 207 request_ = |
208 &delegate_, NULL); | 208 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); |
209 StartRequest(nullptr); | 209 StartRequest(nullptr); |
210 ASSERT_TRUE(request_->status().is_success()); | 210 ASSERT_TRUE(request_->status().is_success()); |
211 EXPECT_EQ("", delegate_.data_received()); | 211 EXPECT_EQ("", delegate_.data_received()); |
212 } | 212 } |
213 | 213 |
214 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) { | 214 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) { |
215 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( | 215 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( |
216 new CancelURLRequestDelegate()); | 216 new CancelURLRequestDelegate()); |
217 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 217 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
218 cancel_delegate.get(), NULL); | 218 cancel_delegate.get()); |
219 request_->Start(); | 219 request_->Start(); |
220 cancel_delegate->WaitUntilHeadersReceived(); | 220 cancel_delegate->WaitUntilHeadersReceived(); |
221 | 221 |
222 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 222 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
223 // callbacks which are invoked in ReadRawData have completed safely. | 223 // callbacks which are invoked in ReadRawData have completed safely. |
224 base::RunLoop run_loop; | 224 base::RunLoop run_loop; |
225 EXPECT_TRUE(task_runner_->PostTaskAndReply(FROM_HERE, base::Bind(&DoNothing), | 225 EXPECT_TRUE(task_runner_->PostTaskAndReply(FROM_HERE, base::Bind(&DoNothing), |
226 run_loop.QuitClosure())); | 226 run_loop.QuitClosure())); |
227 run_loop.Run(); | 227 run_loop.Run(); |
228 } | 228 } |
229 | 229 |
230 } // namespace net | 230 } // namespace net |
OLD | NEW |