Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1142)

Unified Diff: net/url_request/url_request_simple_job_unittest.cc

Issue 1281943002: Reland https://codereview.chromium.org/1271593002/ which was reverted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « net/url_request/url_request_simple_job.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: net/url_request/url_request_simple_job_unittest.cc
diff --git a/net/url_request/url_request_simple_job_unittest.cc b/net/url_request/url_request_simple_job_unittest.cc
index 439995a3b8ebdc0121e81462025d83d9d1181c79..abccbe050c329b02636d8e6f5d52c7fac89bb86e 100644
--- a/net/url_request/url_request_simple_job_unittest.cc
+++ b/net/url_request/url_request_simple_job_unittest.cc
@@ -65,27 +65,30 @@ class MockSimpleJob : public URLRequestSimpleJob {
DISALLOW_COPY_AND_ASSIGN(MockSimpleJob);
};
-class CancelURLRequestDelegate : public URLRequest::Delegate {
+class CancelAfterFirstReadURLRequestDelegate : public TestDelegate {
public:
- CancelURLRequestDelegate()
- : buf_(new IOBuffer(kBufferSize)), run_loop_(new base::RunLoop) {}
+ CancelAfterFirstReadURLRequestDelegate() : run_loop_(new base::RunLoop) {}
+
+ ~CancelAfterFirstReadURLRequestDelegate() override {}
void OnResponseStarted(URLRequest* request) override {
- int bytes_read = 0;
- EXPECT_FALSE(request->Read(buf_.get(), kBufferSize, &bytes_read));
- EXPECT_TRUE(request->status().is_io_pending());
+ // net::TestDelegate will start the first read.
+ TestDelegate::OnResponseStarted(request);
request->Cancel();
run_loop_->Quit();
}
- void OnReadCompleted(URLRequest* request, int bytes_read) override {}
+ void OnReadCompleted(URLRequest* request, int bytes_read) override {
+ // Read should have been cancelled.
+ EXPECT_EQ(-1, bytes_read);
+ }
void WaitUntilHeadersReceived() const { run_loop_->Run(); }
private:
- static const int kBufferSize = 4096;
- scoped_refptr<IOBuffer> buf_;
scoped_ptr<base::RunLoop> run_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(CancelAfterFirstReadURLRequestDelegate);
};
class SimpleJobProtocolHandler :
@@ -208,13 +211,23 @@ TEST_F(URLRequestSimpleJobTest, EmptyDataRequest) {
EXPECT_EQ("", delegate_.data_received());
}
-TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) {
- scoped_ptr<CancelURLRequestDelegate> cancel_delegate(
- new CancelURLRequestDelegate());
+TEST_F(URLRequestSimpleJobTest, CancelBeforeResponseStarts) {
+ request_ =
+ context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY, &delegate_);
+ request_->Start();
+ request_->Cancel();
+
+ base::RunLoop().RunUntilIdle();
+ EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status());
+ EXPECT_EQ(1, delegate_.response_started_count());
+}
+
+TEST_F(URLRequestSimpleJobTest, CancelAfterFirstReadStarted) {
+ CancelAfterFirstReadURLRequestDelegate cancel_delegate;
request_ = context_.CreateRequest(GURL("data:cancel"), DEFAULT_PRIORITY,
- cancel_delegate.get());
+ &cancel_delegate);
request_->Start();
- cancel_delegate->WaitUntilHeadersReceived();
+ cancel_delegate.WaitUntilHeadersReceived();
// Feed a dummy task to the SequencedTaskRunner to make sure that the
// callbacks which are invoked in ReadRawData have completed safely.
@@ -222,6 +235,12 @@ TEST_F(URLRequestSimpleJobTest, CancelAfterFirstRead) {
EXPECT_TRUE(task_runner_->PostTaskAndReply(
FROM_HERE, base::Bind(&base::DoNothing), run_loop.QuitClosure()));
run_loop.Run();
+
+ EXPECT_EQ(URLRequestStatus::CANCELED, request_->status().status());
+ EXPECT_EQ(1, cancel_delegate.response_started_count());
+ EXPECT_EQ("", cancel_delegate.data_received());
+ // Destroy the request so it doesn't outlive its delegate.
+ request_.reset();
}
} // namespace net
« no previous file with comments | « net/url_request/url_request_simple_job.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698