Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / content / browser / fileapi / mock_url_request_delegate.cc
blob97e09f0df6f6285601f55bf3f98337f6399302d4
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
3 // found in the LICENSE file.
5 #include "mock_url_request_delegate.h"
7 #include "base/run_loop.h"
8 #include "net/base/io_buffer.h"
9 #include "testing/gtest/include/gtest/gtest.h"
11 namespace {
12 const int kBufferSize = 1024;
15 namespace content {
17 MockURLRequestDelegate::MockURLRequestDelegate()
18 : io_buffer_(new net::IOBuffer(kBufferSize)) {
21 MockURLRequestDelegate::~MockURLRequestDelegate() {
24 void MockURLRequestDelegate::OnResponseStarted(net::URLRequest* request) {
25 if (request->status().is_success()) {
26 EXPECT_TRUE(request->response_headers());
27 ReadSome(request);
28 } else {
29 RequestComplete();
33 void MockURLRequestDelegate::OnReadCompleted(net::URLRequest* request,
34 int bytes_read) {
35 if (bytes_read > 0)
36 ReceiveData(request, bytes_read);
37 else
38 RequestComplete();
41 void MockURLRequestDelegate::ReadSome(net::URLRequest* request) {
42 if (!request->is_pending()) {
43 RequestComplete();
44 return;
47 int bytes_read = 0;
48 if (!request->Read(io_buffer_.get(), kBufferSize, &bytes_read)) {
49 if (!request->status().is_io_pending())
50 RequestComplete();
51 return;
54 ReceiveData(request, bytes_read);
57 void MockURLRequestDelegate::ReceiveData(net::URLRequest* request,
58 int bytes_read) {
59 if (bytes_read) {
60 response_data_.append(io_buffer_->data(),
61 static_cast<size_t>(bytes_read));
62 ReadSome(request);
63 } else {
64 RequestComplete();
68 void MockURLRequestDelegate::RequestComplete() {
69 base::MessageLoop::current()->Quit();
72 } // namespace