1 // Copyright 2013 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 "content/test/weburl_loader_mock.h"
7 #include "base/logging.h"
8 #include "content/test/weburl_loader_mock_factory.h"
9 #include "third_party/WebKit/public/platform/WebData.h"
10 #include "third_party/WebKit/public/platform/WebURLError.h"
11 #include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
12 #include "third_party/WebKit/public/platform/WebUnitTestSupport.h"
14 WebURLLoaderMock::WebURLLoaderMock(WebURLLoaderMockFactory
* factory
,
15 blink::WebURLLoader
* default_loader
)
18 default_loader_(default_loader
),
19 using_default_loader_(false),
24 WebURLLoaderMock::~WebURLLoaderMock() {
27 void WebURLLoaderMock::ServeAsynchronousRequest(
28 blink::WebURLLoaderTestDelegate
* delegate
,
29 const blink::WebURLResponse
& response
,
30 const blink::WebData
& data
,
31 const blink::WebURLError
& error
) {
32 DCHECK(!using_default_loader_
);
36 // didReceiveResponse() and didReceiveData() might end up getting ::cancel()
37 // to be called which will make the ResourceLoader to delete |this|.
38 base::WeakPtr
<WebURLLoaderMock
> self(weak_factory_
.GetWeakPtr());
40 client_
->didReceiveResponse(this, response
);
45 client_
->didFail(this, error
);
49 delegate
->didReceiveData(client_
, this, data
.data(), data
.size(),
52 client_
->didReceiveData(this, data
.data(), data
.size(), data
.size());
58 client_
->didFinishLoading(this, 0, data
.size());
61 blink::WebURLRequest
WebURLLoaderMock::ServeRedirect(
62 const blink::WebURLResponse
& redirectResponse
) {
63 blink::WebURLRequest newRequest
;
64 newRequest
.initialize();
65 newRequest
.setRequestContext(blink::WebURLRequest::RequestContextInternal
);
66 GURL
redirectURL(redirectResponse
.httpHeaderField("Location"));
67 newRequest
.setURL(redirectURL
);
68 client_
->willSendRequest(this, newRequest
, redirectResponse
);
72 void WebURLLoaderMock::loadSynchronously(const blink::WebURLRequest
& request
,
73 blink::WebURLResponse
& response
,
74 blink::WebURLError
& error
,
75 blink::WebData
& data
) {
76 if (factory_
->IsMockedURL(request
.url())) {
77 factory_
->LoadSynchronously(request
, &response
, &error
, &data
);
80 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
81 << "loadSynchronously shouldn't be falling back: "
82 << request
.url().spec().data();
83 using_default_loader_
= true;
84 default_loader_
->loadSynchronously(request
, response
, error
, data
);
87 void WebURLLoaderMock::loadAsynchronously(const blink::WebURLRequest
& request
,
88 blink::WebURLLoaderClient
* client
) {
89 if (factory_
->IsMockedURL(request
.url())) {
91 factory_
->LoadAsynchronouly(request
, this);
94 DCHECK(static_cast<const GURL
&>(request
.url()).SchemeIs("data"))
95 << "loadAsynchronously shouldn't be falling back: "
96 << request
.url().spec().data();
97 using_default_loader_
= true;
98 default_loader_
->loadAsynchronously(request
, client
);
101 void WebURLLoaderMock::cancel() {
102 if (using_default_loader_
) {
103 default_loader_
->cancel();
107 factory_
->CancelLoad(this);
110 void WebURLLoaderMock::setDefersLoading(bool deferred
) {
111 is_deferred_
= deferred
;
112 if (using_default_loader_
) {
113 default_loader_
->setDefersLoading(deferred
);