Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / sim / SimRequest.cpp
blob228853340ad41460eb3a86301274e71c789c48f7
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.
5 #include "config.h"
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"
14 namespace blink {
16 SimRequest::SimRequest(String url, String mimeType)
17 : m_url(url)
18 , m_loader(nullptr)
19 , m_client(nullptr)
20 , m_totalEncodedDataLength(0)
21 , m_isReady(false)
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()
33 ASSERT(!m_isReady);
36 void SimRequest::didReceiveResponse(WebURLLoaderClient* client, WebURLLoader* loader, const WebURLResponse& response)
38 m_client = client;
39 m_loader = loader;
40 m_response = response;
41 m_isReady = true;
44 void SimRequest::didFail(const WebURLError& error)
46 m_error = error;
49 void SimRequest::start()
51 SimNetwork::current().servePendingRequests();
52 ASSERT(m_isReady);
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()
65 ASSERT(m_isReady);
66 if (m_error.reason) {
67 m_client->didFail(m_loader, m_error);
68 } else {
69 // TODO(esprehn): Is claiming a request time of 0 okay for tests?
70 m_client->didFinishLoading(m_loader, 0, m_totalEncodedDataLength);
72 reset();
75 void SimRequest::reset()
77 m_isReady = false;
78 m_client = nullptr;
79 m_loader = nullptr;
80 SimNetwork::current().removeRequest(*this);
83 } // namespace blink