| // 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. |
| |
| #ifndef THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_SIM_SIM_REQUEST_H_ |
| #define THIRD_PARTY_BLINK_RENDERER_CORE_TESTING_SIM_SIM_REQUEST_H_ |
| |
| #include "base/optional.h" |
| #include "third_party/blink/public/platform/web_url_error.h" |
| #include "third_party/blink/public/platform/web_url_response.h" |
| #include "third_party/blink/renderer/platform/weborigin/kurl.h" |
| #include "third_party/blink/renderer/platform/wtf/text/wtf_string.h" |
| #include "third_party/blink/renderer/platform/wtf/vector.h" |
| |
| namespace blink { |
| |
| class SimNetwork; |
| class WebURLLoaderClient; |
| |
| // Simulates a single request for a resource from the server. Requires a |
| // SimNetwork to have been created first. Use the Write(), Finish() and |
| // Complete() methods to simulate the response from the server. |
| // Note that all requests must be finished. |
| class SimRequestBase { |
| public: |
| // Write a chunk of the response body. |
| void Write(const String& data); |
| void Write(const Vector<char>& data); |
| |
| // Finish the response, this is as if the server closed the connection. |
| void Finish(); |
| |
| // Shorthand to complete a request (start/write/finish) sequence in order. |
| void Complete(const String& data = String()); |
| void Complete(const Vector<char>& data); |
| |
| protected: |
| SimRequestBase(String url, String mime_type, bool start_immediately); |
| ~SimRequestBase(); |
| |
| void StartInternal(); |
| void ServePending(); |
| |
| private: |
| friend class SimNetwork; |
| |
| void Reset(); |
| |
| // Used by SimNetwork. |
| void DidReceiveResponse(WebURLLoaderClient*, const WebURLResponse&); |
| void DidFail(const WebURLError&); |
| |
| KURL url_; |
| String mime_type_; |
| bool start_immediately_; |
| bool started_; |
| WebURLResponse response_; |
| base::Optional<WebURLError> error_; |
| WebURLLoaderClient* client_; |
| unsigned total_encoded_data_length_; |
| }; |
| |
| // This request can be used as a main resource request for navigation. |
| // It does not allow starting asynchronously, because that's not how |
| // navigations work in reality. |
| // TODO(dgozman): rename this to SimNavigationRequest or something. |
| class SimRequest final : public SimRequestBase { |
| public: |
| SimRequest(String url, String mime_type); |
| ~SimRequest(); |
| }; |
| |
| // This request can be started asynchronously, suited for simulating |
| // delayed load of subresources. |
| class SimSubresourceRequest final : public SimRequestBase { |
| public: |
| SimSubresourceRequest(String url, String mime_type); |
| ~SimSubresourceRequest(); |
| |
| // Starts the response from the server, this is as if the headers and 200 OK |
| // reply had been received but no response body yet. |
| void Start(); |
| }; |
| |
| } // namespace blink |
| |
| #endif |