1 // Copyright 2015 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.
6 #include "web/tests/sim/SimNetwork.h"
8 #include "public/platform/Platform.h"
9 #include "public/platform/WebURLError.h"
10 #include "public/platform/WebURLLoader.h"
11 #include "public/platform/WebURLLoaderClient.h"
12 #include "public/platform/WebURLResponse.h"
13 #include "public/platform/WebUnitTestSupport.h"
14 #include "web/tests/sim/SimRequest.h"
18 static SimNetwork
* s_network
= nullptr;
20 SimNetwork::SimNetwork()
21 : m_currentRequest(nullptr)
23 Platform::current()->unitTestSupport()->setLoaderDelegate(this);
28 SimNetwork::~SimNetwork()
30 Platform::current()->unitTestSupport()->setLoaderDelegate(nullptr);
31 Platform::current()->unitTestSupport()->unregisterAllMockedURLs();
35 SimNetwork
& SimNetwork::current()
41 void SimNetwork::servePendingRequests()
43 Platform::current()->unitTestSupport()->serveAsynchronousMockedRequests();
46 void SimNetwork::didReceiveResponse(WebURLLoaderClient
* client
, WebURLLoader
* loader
, const WebURLResponse
& response
)
48 auto it
= m_requests
.find(response
.url().string());
49 if (it
== m_requests
.end()) {
50 client
->didReceiveResponse(loader
, response
);
54 m_currentRequest
= it
->value
;
55 m_currentRequest
->didReceiveResponse(client
, loader
, response
);
58 void SimNetwork::didReceiveData(WebURLLoaderClient
* client
, WebURLLoader
* loader
, const char* data
, int dataLength
, int encodedDataLength
)
60 if (!m_currentRequest
)
61 client
->didReceiveData(loader
, data
, dataLength
, encodedDataLength
);
64 void SimNetwork::didFail(WebURLLoaderClient
* client
, WebURLLoader
* loader
, const WebURLError
& error
)
66 if (!m_currentRequest
) {
67 client
->didFail(loader
, error
);
70 m_currentRequest
->didFail(error
);
73 void SimNetwork::didFinishLoading(WebURLLoaderClient
* client
, WebURLLoader
* loader
, double finishTime
, int64_t totalEncodedDataLength
)
75 if (!m_currentRequest
) {
76 client
->didFinishLoading(loader
, finishTime
, totalEncodedDataLength
);
79 m_currentRequest
= nullptr;
82 void SimNetwork::addRequest(SimRequest
& request
)
84 m_requests
.add(request
.url(), &request
);
87 void SimNetwork::removeRequest(SimRequest
& request
)
89 m_requests
.remove(request
.url());