| 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 CancelAfterFirstReadURLRequestDelegate : public TestDelegate { |
| 69 public: | 69 public: |
| 70 CancelURLRequestDelegate() | 70 CancelAfterFirstReadURLRequestDelegate() : run_loop_(new base::RunLoop) {} |
| 71 : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {} | 71 |
| 72 ~CancelAfterFirstReadURLRequestDelegate() override {} |
| 72 | 73 |
| 73 void OnResponseStarted(URLRequest* request) override { | 74 void OnResponseStarted(URLRequest* request) override { |
| 74 int bytes_read = 0; | 75 // net::TestDelegate will start the first read. |
| 75 EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read)); | 76 TestDelegate::OnResponseStarted(request); |
| 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; | |
| 87 scoped_refptr<IOBuffer> buf_; | |
| 88 scoped_ptr<base::RunLoop> run_loop_; | 89 scoped_ptr<base::RunLoop> run_loop_; |
| 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate); |
| 89 }; | 92 }; |
| 90 | 93 |
| 91 class SimpleJobProtocolHandler : | 94 class SimpleJobProtocolHandler : |
| 92 public URLRequestJobFactory::ProtocolHandler { | 95 public URLRequestJobFactory::ProtocolHandler { |
| 93 public: | 96 public: |
| 94 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) | 97 SimpleJobProtocolHandler(scoped_refptr<base::TaskRunner> task_runner) |
| 95 : task_runner_(task_runner) {} | 98 : task_runner_(task_runner) {} |
| 96 URLRequestJob* MaybeCreateJob( | 99 URLRequestJob* MaybeCreateJob( |
| 97 URLRequest* request, | 100 URLRequest* request, |
| 98 NetworkDelegate* network_delegate) const override { | 101 NetworkDelegate* network_delegate) const override { |
| (...skipping 102 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) { |
| 212 scoped_ptr<CancelURLRequestDelegate> cancel_delegate( | 215 request_ = |
| 213 new CancelURLRequestDelegate()); | 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) { |
| 226 CancelAfterFirstReadURLRequestDelegate cancel_delegate; |
| 214 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, | 227 request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, |
| 215 cancel_delegate.get()); | 228 &cancel_delegate); |
| 216 request_->Start(); | 229 request_->Start(); |
| 217 cancel_delegate->WaitUntilHeadersReceived(); | 230 cancel_delegate.WaitUntilHeadersReceived(); |
| 218 | 231 |
| 219 // Feed a dummy task to the SequencedTaskRunner to make sure that the | 232 // Feed a dummy task to the SequencedTaskRunner to make sure that the |
| 220 // callbacks which are invoked in ReadRawData have completed safely. | 233 // callbacks which are invoked in ReadRawData have completed safely. |
| 221 base::RunLoop run_loop; | 234 base::RunLoop run_loop; |
| 222 EXPECT_TRUE(task_runner_->PostTaskAndReply( | 235 EXPECT_TRUE(task_runner_->PostTaskAndReply( |
| 223 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); | 236 FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure())); |
| 224 run_loop.Run(); | 237 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(); |
| 225 } | 244 } |
| 226 | 245 |
| 227 } // namespace net | 246 } // namespace net |
| OLD | NEW |