Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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. | |
| 4 | |
| 5 #ifndef IOS_WEB_PUBLIC_TEST_RESPONSE_PROVIDERS_HTML_RESPONSE_PROVIDER_H_ | |
| 6 #define IOS_WEB_PUBLIC_TEST_RESPONSE_PROVIDERS_HTML_RESPONSE_PROVIDER_H_ | |
|
baxley
2016/04/15 23:57:48
This file is being moved upstream so it can be use
| |
| 7 | |
| 8 #include <map> | |
| 9 #include <string> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "ios/web/public/test/response_providers/data_response_provider.h" | |
| 13 #include "ios/web/public/test/response_providers/response_provider.h" | |
| 14 #include "net/http/http_request_headers.h" | |
| 15 #include "net/http/http_response_headers.h" | |
| 16 #include "net/http/http_status_code.h" | |
| 17 #include "url/gurl.h" | |
| 18 | |
| 19 namespace net { | |
| 20 class HttpResponseHeaders; | |
| 21 } | |
| 22 | |
| 23 // This class encapsulates the logic needed to map a request URL to a response. | |
| 24 // The mapping -- between URL to response -- is maintained internally, use | |
| 25 // |CanHandleRequest| to check if a request can be handled and use | |
| 26 // |GetResponseHeadersAndBody| to actually handle the request. | |
| 27 class HtmlResponseProviderImpl { | |
| 28 public: | |
| 29 // Encapsulates the body and headers that make up a response. | |
| 30 struct Response { | |
| 31 Response(const std::string& body, | |
| 32 const scoped_refptr<net::HttpResponseHeaders>& headers); | |
| 33 Response(); | |
| 34 ~Response(); | |
| 35 | |
| 36 std::string body; | |
| 37 scoped_refptr<net::HttpResponseHeaders> headers; | |
| 38 }; | |
| 39 // Constructs an HtmlResponseProviderImpl that does not respond to any | |
| 40 // request. | |
| 41 HtmlResponseProviderImpl(); | |
| 42 // Constructs an HtmlResponseProviderImpl that generates a simple string | |
| 43 // response to a URL based on the mapping present in |responses|. | |
| 44 explicit HtmlResponseProviderImpl( | |
| 45 const std::map<GURL, std::string>& responses); | |
| 46 // Constructs an HtmlResponseProviderImpl that generates a response to a URL | |
| 47 // based on the mapping present in |responses|. | |
| 48 explicit HtmlResponseProviderImpl(const std::map<GURL, Response>& responses); | |
| 49 | |
| 50 virtual ~HtmlResponseProviderImpl(); | |
| 51 | |
| 52 // Creates a reponse based on a redirect to |destination_url|. | |
| 53 static HtmlResponseProviderImpl::Response GetRedirectResponse( | |
| 54 const GURL& destination_url, | |
| 55 const net::HttpStatusCode& http_status); | |
| 56 | |
| 57 // Creates a response with |net::HTTP_OK|. | |
| 58 static HtmlResponseProviderImpl::Response GetSimpleResponse( | |
| 59 const std::string& body); | |
| 60 | |
| 61 // Returns true if the request can be handled. | |
| 62 bool CanHandleRequest(const web::ResponseProvider::Request& request); | |
| 63 // Returns the headers and the response body. | |
| 64 void GetResponseHeadersAndBody( | |
| 65 const web::ResponseProvider::Request& request, | |
| 66 scoped_refptr<net::HttpResponseHeaders>* headers, | |
| 67 std::string* response_body); | |
| 68 | |
| 69 private: | |
| 70 const std::map<GURL, Response> responses_; | |
| 71 }; | |
| 72 | |
| 73 // A web::DataResponseProvider that maps URLs to requests. | |
| 74 // Note: This works with the web::test::HttpServer and hence works on UIWebView | |
| 75 // and WKWebView. | |
| 76 class HtmlResponseProvider : public web::DataResponseProvider { | |
| 77 public: | |
| 78 // Constructs an HtmlResponseProvider that does not respond to any request. | |
| 79 HtmlResponseProvider(); | |
| 80 // Constructs an HtmlResponseProvider that generates a simple string response | |
| 81 // to a URL based on the mapping present in |responses|. | |
| 82 explicit HtmlResponseProvider(const std::map<GURL, std::string>& responses); | |
| 83 // Constructs an HtmlResponseProvider that generates a response to a URL based | |
| 84 // on the mapping present in |responses|. | |
| 85 explicit HtmlResponseProvider( | |
| 86 const std::map<GURL, HtmlResponseProviderImpl::Response>& responses); | |
| 87 | |
| 88 ~HtmlResponseProvider() override; | |
| 89 | |
| 90 // web::ResponseProvider implementation. | |
| 91 bool CanHandleRequest(const Request& request) override; | |
| 92 // web::DataResponseProvider implementation. | |
| 93 void GetResponseHeadersAndBody( | |
| 94 const Request& request, | |
| 95 scoped_refptr<net::HttpResponseHeaders>* headers, | |
| 96 std::string* response_body) override; | |
| 97 | |
| 98 private: | |
| 99 std::unique_ptr<HtmlResponseProviderImpl> response_provider_impl_; | |
| 100 }; | |
| 101 | |
| 102 #endif // IOS_WEB_PUBLIC_TEST_RESPONSE_PROVIDERS_HTML_RESPONSE_PROVIDER_H_ | |
| OLD | NEW |