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 COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 5 #ifndef COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
6 #define COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 6 #define COMPONENTS_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> |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 virtual bool Match(const std::string& actual) const = 0; | 59 virtual bool Match(const std::string& actual) const = 0; |
60 virtual ~RequestMatcher() {} | 60 virtual ~RequestMatcher() {} |
61 }; | 61 }; |
62 | 62 |
63 // Returns the url that is intercepted. | 63 // Returns the url that is intercepted. |
64 GURL GetUrl() const; | 64 GURL GetUrl() const; |
65 | 65 |
66 // Sets an expection for the body of the POST request and optionally, | 66 // Sets an expection for the body of the POST request and optionally, |
67 // provides a canned response identified by a |file_path| to be returned when | 67 // provides a canned response identified by a |file_path| to be returned when |
68 // the expectation is met. If no |file_path| is provided, then an empty | 68 // the expectation is met. If no |file_path| is provided, then an empty |
69 // response body is served. This class takes ownership of the | 69 // response body is served. If |response_code| is provided, then an empty |
70 // |request_matcher| object. Returns |true| if the expectation was set. | 70 // response body with that response code is returned. |
| 71 // Returns |true| if the expectation was set. This class takes ownership of |
| 72 // the |request_matcher| object. |
71 bool ExpectRequest(class RequestMatcher* request_matcher); | 73 bool ExpectRequest(class RequestMatcher* request_matcher); |
| 74 bool ExpectRequest(class RequestMatcher* request_matcher, int response_code); |
72 bool ExpectRequest(class RequestMatcher* request_matcher, | 75 bool ExpectRequest(class RequestMatcher* request_matcher, |
73 const base::FilePath& filepath); | 76 const base::FilePath& filepath); |
74 | 77 |
75 // Returns how many requests have been intercepted and matched by | 78 // Returns how many requests have been intercepted and matched by |
76 // an expectation. One expectation can only be matched by one request. | 79 // an expectation. One expectation can only be matched by one request. |
77 int GetHitCount() const; | 80 int GetHitCount() const; |
78 | 81 |
79 // Returns how many requests in total have been captured by the interceptor. | 82 // Returns how many requests in total have been captured by the interceptor. |
80 int GetCount() const; | 83 int GetCount() const; |
81 | 84 |
82 // Returns all requests that have been intercepted, matched or not. | 85 // Returns all requests that have been intercepted, matched or not. |
83 std::vector<std::string> GetRequests() const; | 86 std::vector<std::string> GetRequests() const; |
84 | 87 |
85 // Returns all requests as a string for debugging purposes. | 88 // Returns all requests as a string for debugging purposes. |
86 std::string GetRequestsAsString() const; | 89 std::string GetRequestsAsString() const; |
87 | 90 |
88 // Resets the state of the interceptor so that new expectations can be set. | 91 // Resets the state of the interceptor so that new expectations can be set. |
89 void Reset(); | 92 void Reset(); |
90 | 93 |
91 class Delegate; | 94 class Delegate; |
92 | 95 |
93 private: | 96 private: |
94 friend class URLRequestPostInterceptorFactory; | 97 friend class URLRequestPostInterceptorFactory; |
95 typedef std::pair<const RequestMatcher*, std::string> Expectation; | 98 |
| 99 static const int kResponseCode200 = 200; |
| 100 |
| 101 struct ExpectationResponse { |
| 102 ExpectationResponse(int code, const std::string& body) |
| 103 : response_code(code), response_body(body) {} |
| 104 const int response_code; |
| 105 const std::string response_body; |
| 106 }; |
| 107 typedef std::pair<const RequestMatcher*, ExpectationResponse> Expectation; |
96 | 108 |
97 URLRequestPostInterceptor( | 109 URLRequestPostInterceptor( |
98 const GURL& url, | 110 const GURL& url, |
99 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); | 111 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); |
100 ~URLRequestPostInterceptor(); | 112 ~URLRequestPostInterceptor(); |
101 | 113 |
102 void ClearExpectations(); | 114 void ClearExpectations(); |
| 115 |
103 const GURL url_; | 116 const GURL url_; |
104 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | 117 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; |
105 | 118 |
106 mutable base::Lock interceptor_lock_; | 119 mutable base::Lock interceptor_lock_; |
107 mutable int hit_count_; | 120 mutable int hit_count_; |
108 mutable std::vector<std::string> requests_; | 121 mutable std::vector<std::string> requests_; |
109 mutable std::queue<Expectation> expectations_; | 122 mutable std::queue<Expectation> expectations_; |
110 | 123 |
111 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); | 124 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); |
112 }; | 125 }; |
(...skipping 24 matching lines...) Expand all Loading... |
137 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory); | 150 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory); |
138 }; | 151 }; |
139 | 152 |
140 // Intercepts HTTP POST requests sent to "localhost2". | 153 // Intercepts HTTP POST requests sent to "localhost2". |
141 class InterceptorFactory : public URLRequestPostInterceptorFactory { | 154 class InterceptorFactory : public URLRequestPostInterceptorFactory { |
142 public: | 155 public: |
143 explicit InterceptorFactory( | 156 explicit InterceptorFactory( |
144 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); | 157 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner); |
145 ~InterceptorFactory(); | 158 ~InterceptorFactory(); |
146 | 159 |
| 160 // Creates an interceptor for the url path defined by POST_INTERCEPT_PATH. |
147 URLRequestPostInterceptor* CreateInterceptor(); | 161 URLRequestPostInterceptor* CreateInterceptor(); |
148 | 162 |
| 163 // Creates an interceptor for the given url path. |
| 164 URLRequestPostInterceptor* CreateInterceptorForPath(const char* url_path); |
| 165 |
149 private: | 166 private: |
150 DISALLOW_COPY_AND_ASSIGN(InterceptorFactory); | 167 DISALLOW_COPY_AND_ASSIGN(InterceptorFactory); |
151 }; | 168 }; |
152 | 169 |
153 class PartialMatch : public URLRequestPostInterceptor::RequestMatcher { | 170 class PartialMatch : public URLRequestPostInterceptor::RequestMatcher { |
154 public: | 171 public: |
155 explicit PartialMatch(const std::string& expected) : expected_(expected) {} | 172 explicit PartialMatch(const std::string& expected) : expected_(expected) {} |
156 virtual bool Match(const std::string& actual) const OVERRIDE; | 173 virtual bool Match(const std::string& actual) const OVERRIDE; |
157 | 174 |
158 private: | 175 private: |
159 const std::string expected_; | 176 const std::string expected_; |
160 | 177 |
161 DISALLOW_COPY_AND_ASSIGN(PartialMatch); | 178 DISALLOW_COPY_AND_ASSIGN(PartialMatch); |
162 }; | 179 }; |
163 | 180 |
164 } // namespace component_updater | 181 } // namespace component_updater |
165 | 182 |
166 #endif // COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 183 #endif // COMPONENTS_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
OLD | NEW |