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 CancelURLRequestDelegate : public URLRequest::Delegate { | 68 class CancelURLRequestDelegate : public TestDelegate { |
davidben
2015/08/05 18:56:39
Any reason this CancelURLRequestDelegate calls Rea
mmenke
2015/08/05 20:00:24
Carelessness on my part, while iterating to get th
| |
69 public: | 69 public: |
70 CancelURLRequestDelegate() | 70 CancelURLRequestDelegate() |
71 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} | 71 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} |
72 | 72 |
73 void OnResponseStarted(URLRequest* request) override { | 73 void OnResponseStarted(URLRequest* request) override { |
74 int bytes_read = 0; | 74 int bytes_read = 0; |
75 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); | 75 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); |
76 EXPECT_TRUE(request->status().is_io_pending()); | 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 } | |
82 | 85 |
83 void WaitUntilHeadersReceived() const { run_loop_->Run(); } | 86 void WaitUntilHeadersReceived() const { run_loop_->Run(); } |
84 | 87 |
85 private: | 88 private: |
86 static const int kBufferSize = 4096; | 89 static const int kBufferSize = 4096; |
87 scoped_refptr<IOBuffer> buf_; | 90 scoped_refptr<IOBuffer> buf_; |
88 scoped_ptr<base::RunLoop> run_loop_; | 91 scoped_ptr<base::RunLoop> run_loop_; |
89 }; | 92 }; |
90 | 93 |
91 class SimpleJobProtocolHandler : | 94 class SimpleJobProtocolHandler : |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
201 } | 204 } |
202 | 205 |
203 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { | 206 TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) { |
204 request_ = | 207 request_ = |
205 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); | 208 context_.CreateRequest(GURL("data:empty"), DEFAULT_PRIORITY, &delegate_); |
206 StartRequest(nullptr); | 209 StartRequest(nullptr); |
207 ASSERT_TRUE(request_->status().is_success()); | 210 ASSERT_TRUE(request_->status().is_success()); |
208 EXPECT_EQ("", delegate_.data_received()); | 211 EXPECT_EQ("", delegate_.data_received()); |
209 } | 212 } |
210 | 213 |
211 TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) { | 214 TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) { |
215 request_ = | |
216 context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_); | |
217 request_->Start(); | |
218 request_->Cancel(); | |
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) { | |
212 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( | 226 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( |
213 new CancelURLRequestDelegate()); | 227 new CancelURLRequestDelegate()); |
214 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 228 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
215 cancel_delegate.get()); | 229 cancel_delegate.get()); |
216 request_->Start(); | 230 request_->Start(); |
217 cancel_delegate->WaitUntilHeadersReceived(); | 231 cancel_delegate->WaitUntilHeadersReceived(); |
218 | 232 |
219 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 233 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
220 // callbacks which are invoked in ReadRawData have completed safely. | 234 // callbacks which are invoked in ReadRawData have completed safely. |
221 base::RunLoop run_loop; | 235 base::RunLoop run_loop; |
222 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 236 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
223 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 237 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
224 run_loop.Run(); | 238 run_loop.Run(); |
239 | |
240 EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status()); | |
225 } | 241 } |
226 | 242 |
227 } // namespace net | 243 } // namespace net |
OLD | NEW |