OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <queue> | 9 #include <queue> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 #include <vector> | 12 #include <vector> |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
14 #include "base/path_service.h" | |
15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
16 #include "url/gurl.h" | 15 #include "url/gurl.h" |
17 | 16 |
| 17 namespace base { |
| 18 class FilePath; |
| 19 } |
| 20 |
18 namespace net { | 21 namespace net { |
19 class URLRequest; | 22 class URLRequest; |
20 } | 23 } |
21 | 24 |
22 namespace component_updater { | 25 namespace component_updater { |
23 | 26 |
24 // Intercepts requests to a file path, counts them, and captures the body of | 27 // Intercepts requests to a file path, counts them, and captures the body of |
25 // the requests. Optionally, for each request, it can return a canned response | 28 // the requests. Optionally, for each request, it can return a canned response |
26 // from a given file. The class maintains a queue of expectations, and returns | 29 // from a given file. The class maintains a queue of expectations, and returns |
27 // one and only one response for each request that matches and it is | 30 // one and only one response for each request that matches and it is |
(...skipping 10 matching lines...) Expand all Loading... |
38 // Returns the url that is intercepted. | 41 // Returns the url that is intercepted. |
39 GURL GetUrl() const; | 42 GURL GetUrl() const; |
40 | 43 |
41 // Sets an expection for the body of the POST request and optionally, | 44 // Sets an expection for the body of the POST request and optionally, |
42 // provides a canned response identified by a |file_path| to be returned when | 45 // provides a canned response identified by a |file_path| to be returned when |
43 // the expectation is met. If no |file_path| is provided, then an empty | 46 // the expectation is met. If no |file_path| is provided, then an empty |
44 // response body is served. This class takes ownership of the | 47 // response body is served. This class takes ownership of the |
45 // |request_matcher| object. Returns |true| if the expectation was set. | 48 // |request_matcher| object. Returns |true| if the expectation was set. |
46 bool ExpectRequest(class RequestMatcher* request_matcher); | 49 bool ExpectRequest(class RequestMatcher* request_matcher); |
47 bool ExpectRequest(class RequestMatcher* request_matcher, | 50 bool ExpectRequest(class RequestMatcher* request_matcher, |
48 const std::string& filepath); | 51 const base::FilePath& filepath); |
49 | 52 |
50 // Returns how many requests have been intercepted and matched by | 53 // Returns how many requests have been intercepted and matched by |
51 // an expectation. One expectation can only be matched by one request. | 54 // an expectation. One expectation can only be matched by one request. |
52 int GetHitCount() const; | 55 int GetHitCount() const; |
53 | 56 |
54 // Returns how many requests have been intercepted but not matched by | 57 // Returns how many requests in total have been captured by the interceptor. |
55 // any expectation. | 58 int GetCount() const; |
56 int GetMissCount() const; | |
57 | 59 |
58 // Returns all requests that have been intercepted, matched or not. | 60 // Returns all requests that have been intercepted, matched or not. |
59 std::vector<std::string> GetRequests() const; | 61 std::vector<std::string> GetRequests() const; |
60 | 62 |
61 // Returns all requests as a string for debugging purposes. | 63 // Returns all requests as a string for debugging purposes. |
62 std::string GetRequestsAsString() const; | 64 std::string GetRequestsAsString() const; |
63 | 65 |
| 66 // Resets the state of the interceptor so that new expectations can be set. |
| 67 void Reset(); |
| 68 |
64 class Delegate; | 69 class Delegate; |
65 | 70 |
66 private: | 71 private: |
67 friend class URLRequestPostInterceptorFactory; | 72 friend class URLRequestPostInterceptorFactory; |
68 typedef std::pair<const RequestMatcher*, std::string> Expectation; | 73 typedef std::pair<const RequestMatcher*, std::string> Expectation; |
69 | 74 |
70 explicit URLRequestPostInterceptor(const GURL& url); | 75 explicit URLRequestPostInterceptor(const GURL& url); |
71 ~URLRequestPostInterceptor(); | 76 ~URLRequestPostInterceptor(); |
72 | 77 |
| 78 void ClearExpectations(); |
73 const GURL url_; | 79 const GURL url_; |
74 | 80 |
75 mutable base::Lock interceptor_lock_; | 81 mutable base::Lock interceptor_lock_; |
76 mutable int hit_count_; | 82 mutable int hit_count_; |
77 mutable int miss_count_; | |
78 mutable std::vector<std::string> requests_; | 83 mutable std::vector<std::string> requests_; |
79 mutable std::queue<Expectation> expectations_; | 84 mutable std::queue<Expectation> expectations_; |
80 | 85 |
81 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); | 86 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); |
82 }; | 87 }; |
83 | 88 |
84 class URLRequestPostInterceptorFactory { | 89 class URLRequestPostInterceptorFactory { |
85 public: | 90 public: |
86 URLRequestPostInterceptorFactory(const std::string& scheme, | 91 URLRequestPostInterceptorFactory(const std::string& scheme, |
87 const std::string& hostname); | 92 const std::string& hostname); |
88 ~URLRequestPostInterceptorFactory(); | 93 ~URLRequestPostInterceptorFactory(); |
89 | 94 |
90 // Creates an interceptor object for the specified url path. Returns NULL | 95 // Creates an interceptor object for the specified url path. Returns NULL |
91 // in case of errors or a valid interceptor object otherwise. The caller | 96 // in case of errors or a valid interceptor object otherwise. The caller |
92 // does not own the returned object. | 97 // does not own the returned object. |
93 URLRequestPostInterceptor* CreateInterceptor(const std::string& file_path); | 98 URLRequestPostInterceptor* CreateInterceptor(const base::FilePath& filepath); |
94 | 99 |
95 private: | 100 private: |
96 const std::string scheme_; | 101 const std::string scheme_; |
97 const std::string hostname_; | 102 const std::string hostname_; |
98 | 103 |
99 // After creation, |delegate_| lives on the IO thread and it is owned by | 104 // After creation, |delegate_| lives on the IO thread and it is owned by |
100 // a URLRequestFilter after registration. A task to unregister it and | 105 // a URLRequestFilter after registration. A task to unregister it and |
101 // implicitly destroy it is posted from ~URLRequestPostInterceptorFactory(). | 106 // implicitly destroy it is posted from ~URLRequestPostInterceptorFactory(). |
102 URLRequestPostInterceptor::Delegate* delegate_; | 107 URLRequestPostInterceptor::Delegate* delegate_; |
103 | 108 |
104 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory); | 109 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory); |
105 }; | 110 }; |
106 | 111 |
107 } // namespace component_updater | 112 } // namespace component_updater |
108 | 113 |
109 #endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 114 #endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
OLD | NEW |