Index: Source/web/tests/sim/SimRequest.cpp |
diff --git a/Source/web/tests/sim/SimRequest.cpp b/Source/web/tests/sim/SimRequest.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..84e11e74d1cca77e6596af32c8f1848bb3ae095b |
--- /dev/null |
+++ b/Source/web/tests/sim/SimRequest.cpp |
@@ -0,0 +1,78 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "config.h" |
+#include "web/tests/sim/SimRequest.h" |
+ |
+#include "platform/weborigin/KURL.h" |
+#include "public/platform/Platform.h" |
+#include "public/platform/WebURLLoaderClient.h" |
+#include "public/platform/WebUnitTestSupport.h" |
+#include "web/tests/sim/SimNetwork.h" |
+ |
+namespace blink { |
+ |
+SimRequest::SimRequest(String url, String mimeType) |
+ : m_url(url) |
+ , m_loader(nullptr) |
+ , m_client(nullptr) |
+ , m_totalEncodedDataLength(0) |
+ , m_isReady(false) |
+{ |
+ KURL fullUrl(ParsedURLString, url); |
+ WebURLResponse response(fullUrl); |
+ response.setMIMEType(mimeType); |
+ response.setHTTPStatusCode(200); |
+ Platform::current()->unitTestSupport()->registerMockedURL(fullUrl, response, ""); |
+ SimNetwork::current().addRequest(*this); |
+} |
+ |
+void SimRequest::didReceiveResponse(WebURLLoaderClient* client, WebURLLoader* loader, const WebURLResponse& response) |
+{ |
+ m_client = client; |
+ m_loader = loader; |
+ m_response = response; |
+ m_isReady = true; |
+} |
+ |
+void SimRequest::didFail(const WebURLError& error) |
+{ |
+ m_error = error; |
+} |
+ |
+void SimRequest::start() |
+{ |
+ SimNetwork::current().servePendingRequests(); |
+ ASSERT(m_isReady); |
+ m_client->didReceiveResponse(m_loader, m_response); |
+} |
+ |
+void SimRequest::write(const String& data) |
+{ |
+ ASSERT(m_isReady && !m_error.reason); |
+ m_totalEncodedDataLength += data.length(); |
+ m_client->didReceiveData(m_loader, data.utf8().data(), data.length(), data.length()); |
+} |
+ |
+void SimRequest::finish() |
+{ |
+ ASSERT(m_isReady); |
+ if (m_error.reason) { |
+ m_client->didFail(m_loader, m_error); |
+ } else { |
+ // TODO(esprehn): Is claiming a request time of 0 okay for tests? |
+ m_client->didFinishLoading(m_loader, 0, m_totalEncodedDataLength); |
+ } |
+ reset(); |
+} |
+ |
+void SimRequest::reset() |
+{ |
+ m_isReady = false; |
+ m_client = nullptr; |
+ m_loader = nullptr; |
+ SimNetwork::current().removeRequest(*this); |
+} |
+ |
+} // namespace blink |