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_IMPL_H_ | |
6 #define IOS_WEB_PUBLIC_TEST_RESPONSE_PROVIDERS_HTML_RESPONSE_PROVIDER_IMPL_H_ | |
7 | |
8 #include <map> | |
9 #include <string> | |
10 | |
11 #include "base/memory/ref_counted.h" | |
12 #import "ios/web/public/test/response_providers/data_response_provider.h" | |
13 #import "ios/web/public/test/response_providers/response_provider.h" | |
14 #include "net/http/http_response_headers.h" | |
15 #include "net/http/http_status_code.h" | |
16 #include "url/gurl.h" | |
17 | |
18 // This class encapsulates the logic needed to map a request URL to a response. | |
19 // The mapping -- between URL to response -- is maintained internally, use | |
20 // |CanHandleRequest| to check if a request can be handled and use | |
21 // |GetResponseHeadersAndBody| to actually handle the request. | |
22 class HtmlResponseProviderImpl { | |
23 public: | |
24 // Encapsulates the body and headers that make up a response. | |
25 struct Response { | |
26 Response(const std::string& body, | |
27 const scoped_refptr<net::HttpResponseHeaders>& headers); | |
28 Response(const Response&); | |
29 Response(); | |
30 ~Response(); | |
31 | |
32 std::string body; | |
33 scoped_refptr<net::HttpResponseHeaders> headers; | |
34 }; | |
35 // Constructs an HtmlResponseProviderImpl that does not respond to any | |
36 // request. | |
37 HtmlResponseProviderImpl(); | |
38 // Constructs an HtmlResponseProviderImpl that generates a simple string | |
39 // response to a URL based on the mapping present in |responses|. | |
40 explicit HtmlResponseProviderImpl( | |
41 const std::map<GURL, std::string>& responses); | |
42 // Constructs an HtmlResponseProvider that generates a simple string response | |
43 // to a URL with set cookie in the headers based on the mapping present in | |
44 // |responses|. | |
45 explicit HtmlResponseProviderImpl( | |
46 const std::map<GURL, std::pair<std::string, std::string>>& responses); | |
47 // Constructs an HtmlResponseProviderImpl that generates a response to a URL | |
48 // based on the mapping present in |responses|. | |
49 explicit HtmlResponseProviderImpl(const std::map<GURL, Response>& responses); | |
50 | |
51 virtual ~HtmlResponseProviderImpl(); | |
52 | |
53 // Creates a reponse based on a redirect to |destination_url|. | |
54 static HtmlResponseProviderImpl::Response GetRedirectResponse( | |
55 const GURL& destination_url, | |
56 const net::HttpStatusCode& http_status); | |
57 | |
58 // Creates a response with |net::HTTP_OK|. | |
59 static HtmlResponseProviderImpl::Response GetSimpleResponse( | |
60 const std::string& body); | |
61 | |
62 // Returns true if the request can be handled. | |
63 bool CanHandleRequest(const web::ResponseProvider::Request& request); | |
64 // Returns the headers and the response body. | |
65 void GetResponseHeadersAndBody( | |
66 const web::ResponseProvider::Request& request, | |
67 scoped_refptr<net::HttpResponseHeaders>* headers, | |
68 std::string* response_body); | |
69 | |
70 private: | |
71 const std::map<GURL, Response> responses_; | |
72 }; | |
73 | |
74 #endif // IOS_WEB_PUBLIC_TEST_RESPONSE_PROVIDERS_HTML_RESPONSE_PROVIDER_IMPL_H_ | |
OLD | NEW |