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/bind_helpers.h" | 5 #include "base/bind_helpers.h" |
6 #include "base/memory/scoped_ptr.h" | 6 #include "base/memory/scoped_ptr.h" |
7 #include "base/run_loop.h" | 7 #include "base/run_loop.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/threading/sequenced_worker_pool.h" | 9 #include "base/threading/sequenced_worker_pool.h" |
10 #include "base/threading/worker_pool.h" | 10 #include "base/threading/worker_pool.h" |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 private: | 58 private: |
59 ~MockSimpleJob() override {} | 59 ~MockSimpleJob() override {} |
60 | 60 |
61 const std::string data_; | 61 const std::string data_; |
62 | 62 |
63 scoped_refptr<base::TaskRunner> task_runner_; | 63 scoped_refptr<base::TaskRunner> task_runner_; |
64 | 64 |
65 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); | 65 DISALLOW_COPY_AND_ASSIGN(MockSimpleJob); |
66 }; | 66 }; |
67 | 67 |
68 class CancelAfterFirstReadURLRequestDelegate : public TestDelegate { | 68 class CancelURLRequestDelegate : public URLRequest::Delegate { |
69 public: | 69 public: |
70 CancelAfterFirstReadURLRequestDelegate() : run_loop_(new base::RunLoop) {} | 70 CancelURLRequestDelegate() |
71 | 71 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} |
72 ~CancelAfterFirstReadURLRequestDelegate() override {} | |
73 | 72 |
74 void OnResponseStarted(URLRequest* request) override { | 73 void OnResponseStarted(URLRequest* request) override { |
75 // net::TestDelegate will start the first read. | 74 int bytes_read = 0; |
76 TestDelegate::OnResponseStarted(request); | 75 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); |
| 76 EXPECT_TRUE(request->status().is_io_pending()); |
77 request->Cancel(); | 77 request->Cancel(); |
78 run_loop_->Quit(); | 78 run_loop_->Quit(); |
79 } | 79 } |
80 | 80 |
81 void OnReadCompleted(URLRequest* request, int bytes_read) override { | 81 void OnReadCompleted(URLRequest* request, int bytes_read) override {} |
82 // Read should have been cancelled. | |
83 EXPECT_EQ(-1, bytes_read); | |
84 } | |
85 | 82 |
86 void WaitUntilHeadersReceived() const { run_loop_->Run(); } | 83 void WaitUntilHeadersReceived() const { run_loop_->Run(); } |
87 | 84 |
88 private: | 85 private: |
| 86 static const int kBufferSize = 4096; |
| 87 scoped_refptr<IOBuffer> buf_; |
89 scoped_ptr<base::RunLoop> run_loop_; | 88 scoped_ptr<base::RunLoop> run_loop_; |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); | |
92 }; | 89 }; |
93 | 90 |
94 class SimpleJobProtocolHandler : | 91 class SimpleJobProtocolHandler : |
95 public URLRequestJobFactory::ProtocolHandler { | 92 public URLRequestJobFactory::ProtocolHandler { |
96 public: | 93 public: |
97 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) | 94 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) |
98 : task_runner_(task_runner) {} | 95 : task_runner_(task_runner) {} |
99 URLRequestJob* MaybeCreateJob( | 96 URLRequestJob* MaybeCreateJob( |
100 URLRequest* request, | 97 URLRequest* request, |
101 NetworkDelegate* network_delegate) const override { | 98 NetworkDelegate* network_delegate) const override { |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 201 } |
205 | 202 |
206 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 203 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
207 request_ = | 204 request_ = |
208 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); | 205 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); |
209 StartRequest(nullptr); | 206 StartRequest(nullptr); |
210 ASSERT_TRUE(request_->status().is_success()); | 207 ASSERT_TRUE(request_->status().is_success()); |
211 EXPECT_EQ("", delegate_.data_received()); | 208 EXPECT_EQ("", delegate_.data_received()); |
212 } | 209 } |
213 | 210 |
214 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { | 211 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) { |
215 request_ = | 212 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( |
216 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); | 213 new CancelURLRequestDelegate()); |
| 214 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
| 215 cancel_delegate.get()); |
217 request_->Start(); | 216 request_->Start(); |
218 request_->Cancel(); | 217 cancel_delegate->WaitUntilHeadersReceived(); |
219 | |
220 base::RunLoop().RunUntilIdle(); | |
221 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | |
222 EXPECT_EQ(1, delegate_.response_started_count()); | |
223 } | |
224 | |
225 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) { | |
226 CancelAfterFirstReadURLRequestDelegate cancel_delegate; | |
227 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | |
228 &cancel_delegate); | |
229 request_->Start(); | |
230 cancel_delegate.WaitUntilHeadersReceived(); | |
231 | 218 |
232 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 219 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
233 // callbacks which are invoked in ReadRawData have completed safely. | 220 // callbacks which are invoked in ReadRawData have completed safely. |
234 base::RunLoop run_loop; | 221 base::RunLoop run_loop; |
235 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 222 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
236 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 223 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
237 run_loop.Run(); | 224 run_loop.Run(); |
238 | |
239 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | |
240 EXPECT_EQ(1, cancel_delegate.response_started_count()); | |
241 EXPECT_EQ("", cancel_delegate.data_received()); | |
242 // Destroy the request so it doesn't outlive its delegate. | |
243 request_.reset(); | |
244 } | 225 } |
245 | 226 |
246 } // namespace net | 227 } // namespace net |
OLD | NEW |