Commit 2522425b authored by mmenke's avatar mmenke Committed by Commit bot

Update MimeSniffingResourceHandler tests to use MockResourceLoader.

This will make it easier to land an upcoming ResourceHandler refactor,
and also gives us some more ASSERTs shared with other tests. Also add
checking of the error code the request is canceled with.

Also prevent next ResourceHandler in the chain from being called
re-entrantly

BUG=659317

Review-Url: https://codereview.chromium.org/2626663002
Cr-Commit-Position: refs/heads/master@{#443360}
parent e8247131
...@@ -245,8 +245,11 @@ void MimeSniffingResourceHandler::Resume() { ...@@ -245,8 +245,11 @@ void MimeSniffingResourceHandler::Resume() {
} }
// Otherwise proceed with the replay of the response. If it is successful, // Otherwise proceed with the replay of the response. If it is successful,
// it will resume the request. // it will resume the request. Posted as a task to avoid re-entrancy into
AdvanceState(); // the calling class.
base::ThreadTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::Bind(&MimeSniffingResourceHandler::AdvanceState,
weak_ptr_factory_.GetWeakPtr()));
} }
void MimeSniffingResourceHandler::Cancel() { void MimeSniffingResourceHandler::Cancel() {
......
...@@ -35,6 +35,11 @@ MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) { ...@@ -35,6 +35,11 @@ MockResourceLoader::Status MockResourceLoader::OnWillStart(const GURL& url) {
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
// In the case of double-cancels, keep the old error code.
// TODO(mmenke): Once there are no more double cancel cases, remove this
// code.
if (status_ != Status::CANCELED)
error_code_ = net::ERR_ABORTED;
status_ = Status::CANCELED; status_ = Status::CANCELED;
} else if (defer) { } else if (defer) {
status_ = Status::CALLBACK_PENDING; status_ = Status::CALLBACK_PENDING;
...@@ -61,6 +66,9 @@ MockResourceLoader::Status MockResourceLoader::OnRequestRedirected( ...@@ -61,6 +66,9 @@ MockResourceLoader::Status MockResourceLoader::OnRequestRedirected(
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
// In the case of double-cancels, keep the old error code.
if (status_ != Status::CANCELED)
error_code_ = net::ERR_ABORTED;
status_ = Status::CANCELED; status_ = Status::CANCELED;
} else if (defer) { } else if (defer) {
status_ = Status::CALLBACK_PENDING; status_ = Status::CALLBACK_PENDING;
...@@ -85,6 +93,9 @@ MockResourceLoader::Status MockResourceLoader::OnResponseStarted( ...@@ -85,6 +93,9 @@ MockResourceLoader::Status MockResourceLoader::OnResponseStarted(
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
// In the case of double-cancels, keep the old error code.
if (status_ != Status::CANCELED)
error_code_ = net::ERR_ABORTED;
status_ = Status::CANCELED; status_ = Status::CANCELED;
} else if (defer) { } else if (defer) {
status_ = Status::CALLBACK_PENDING; status_ = Status::CALLBACK_PENDING;
...@@ -108,6 +119,9 @@ MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) { ...@@ -108,6 +119,9 @@ MockResourceLoader::Status MockResourceLoader::OnWillRead(int min_size) {
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
// In the case of double-cancels, keep the old error code.
if (status_ != Status::CANCELED)
error_code_ = net::ERR_ABORTED;
EXPECT_EQ(0, io_buffer_size_); EXPECT_EQ(0, io_buffer_size_);
EXPECT_FALSE(io_buffer_); EXPECT_FALSE(io_buffer_);
status_ = Status::CANCELED; status_ = Status::CANCELED;
...@@ -138,6 +152,9 @@ MockResourceLoader::Status MockResourceLoader::OnReadCompleted( ...@@ -138,6 +152,9 @@ MockResourceLoader::Status MockResourceLoader::OnReadCompleted(
EXPECT_TRUE(status_ == Status::CALLING_HANDLER || EXPECT_TRUE(status_ == Status::CALLING_HANDLER ||
(result == false && status_ == Status::CANCELED)); (result == false && status_ == Status::CANCELED));
if (!result) { if (!result) {
// In the case of double-cancels, keep the old error code.
if (status_ != Status::CANCELED)
error_code_ = net::ERR_ABORTED;
status_ = Status::CANCELED; status_ = Status::CANCELED;
} else if (defer) { } else if (defer) {
status_ = Status::CALLBACK_PENDING; status_ = Status::CALLBACK_PENDING;
......
...@@ -19,6 +19,10 @@ class CONTENT_EXPORT ResourceController { ...@@ -19,6 +19,10 @@ class CONTENT_EXPORT ResourceController {
virtual void Cancel() = 0; virtual void Cancel() = 0;
virtual void CancelAndIgnore() = 0; virtual void CancelAndIgnore() = 0;
virtual void CancelWithError(int error_code) = 0; virtual void CancelWithError(int error_code) = 0;
// Resumes the request. May only be called if the request was previously
// deferred. Guaranteed not to call back into the ResourceHandler, or destroy
// it, synchronously.
virtual void Resume() = 0; virtual void Resume() = 0;
protected: protected:
......
...@@ -132,11 +132,14 @@ bool TestResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf, ...@@ -132,11 +132,14 @@ bool TestResourceHandler::OnWillRead(scoped_refptr<net::IOBuffer>* buf,
++on_will_read_called_; ++on_will_read_called_;
*buf = buffer_; if (!on_will_read_result_) {
*buf_size = buffer_size_;
memset(buffer_->data(), '\0', buffer_size_);
if (!on_will_read_result_)
canceled_ = true; canceled_ = true;
} else {
*buf = buffer_;
*buf_size = buffer_size_;
memset(buffer_->data(), '\0', buffer_size_);
}
return on_will_read_result_; return on_will_read_result_;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment