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/profiler/scoped_tracker.h"
8 #include "base/run_loop.h"
9 #include "net/base/io_buffer.h"
10 #include "testing/gtest/include/gtest/gtest.h"
13 const int kBufferSize
= 1024;
18 MockURLRequestDelegate::MockURLRequestDelegate()
19 : io_buffer_(new net::IOBuffer(kBufferSize
)) {
22 MockURLRequestDelegate::~MockURLRequestDelegate() {
25 void MockURLRequestDelegate::OnResponseStarted(net::URLRequest
* request
) {
26 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
27 tracked_objects::ScopedTracker
tracking_profile(
28 FROM_HERE_WITH_EXPLICIT_FUNCTION(
29 "422516 MockURLRequestDelegate::OnResponseStarted"));
31 if (request
->status().is_success()) {
32 EXPECT_TRUE(request
->response_headers());
39 void MockURLRequestDelegate::OnReadCompleted(net::URLRequest
* request
,
41 // TODO(vadimt): Remove ScopedTracker below once crbug.com/422516 is fixed.
42 tracked_objects::ScopedTracker
tracking_profile(
43 FROM_HERE_WITH_EXPLICIT_FUNCTION(
44 "422516 MockURLRequestDelegate::OnReadCompleted"));
47 ReceiveData(request
, bytes_read
);
52 void MockURLRequestDelegate::ReadSome(net::URLRequest
* request
) {
53 if (!request
->is_pending()) {
59 if (!request
->Read(io_buffer_
.get(), kBufferSize
, &bytes_read
)) {
60 if (!request
->status().is_io_pending())
65 ReceiveData(request
, bytes_read
);
68 void MockURLRequestDelegate::ReceiveData(net::URLRequest
* request
,
71 response_data_
.append(io_buffer_
->data(),
72 static_cast<size_t>(bytes_read
));
79 void MockURLRequestDelegate::RequestComplete() {
80 base::MessageLoop::current()->Quit();