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/SimRequest.h"
8 #include "platform/weborigin/KURL.h"
9 #include "public/platform/Platform.h"
10 #include "public/platform/WebURLLoaderClient.h"
11 #include "public/platform/WebUnitTestSupport.h"
12 #include "web/tests/sim/SimNetwork.h"
16 SimRequest::SimRequest(String url
, String mimeType
)
20 , m_totalEncodedDataLength(0)
23 KURL
fullUrl(ParsedURLString
, url
);
24 WebURLResponse
response(fullUrl
);
25 response
.setMIMEType(mimeType
);
26 response
.setHTTPStatusCode(200);
27 Platform::current()->unitTestSupport()->registerMockedURL(fullUrl
, response
, "");
28 SimNetwork::current().addRequest(*this);
31 SimRequest::~SimRequest()
36 void SimRequest::didReceiveResponse(WebURLLoaderClient
* client
, WebURLLoader
* loader
, const WebURLResponse
& response
)
40 m_response
= response
;
44 void SimRequest::didFail(const WebURLError
& error
)
49 void SimRequest::start()
51 SimNetwork::current().servePendingRequests();
53 m_client
->didReceiveResponse(m_loader
, m_response
);
56 void SimRequest::write(const String
& data
)
58 ASSERT(m_isReady
&& !m_error
.reason
);
59 m_totalEncodedDataLength
+= data
.length();
60 m_client
->didReceiveData(m_loader
, data
.utf8().data(), data
.length(), data
.length());
63 void SimRequest::finish()
67 m_client
->didFail(m_loader
, m_error
);
69 // TODO(esprehn): Is claiming a request time of 0 okay for tests?
70 m_client
->didFinishLoading(m_loader
, 0, m_totalEncodedDataLength
);
75 void SimRequest::reset()
80 SimNetwork::current().removeRequest(*this);