Fix "Googlers" typo.
[chromium-blink-merge.git] / components / update_client / url_request_post_interceptor.h
blob15672bcf6f52562cbd6ebfe3f3f5e762d263a43f
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
3 // found in the LICENSE file.
5 #ifndef COMPONENTS_UPDATE_CLIENT_URL_REQUEST_POST_INTERCEPTOR_H_
6 #define COMPONENTS_UPDATE_CLIENT_URL_REQUEST_POST_INTERCEPTOR_H_
8 #include <stdint.h>
9 #include <map>
10 #include <queue>
11 #include <string>
12 #include <utility>
13 #include <vector>
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/lock.h"
18 #include "url/gurl.h"
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
25 namespace net {
26 class URLRequest;
29 namespace update_client {
31 // Intercepts requests to a file path, counts them, and captures the body of
32 // the requests. Optionally, for each request, it can return a canned response
33 // from a given file. The class maintains a queue of expectations, and returns
34 // one and only one response for each request that matches and it is
35 // intercepted.
36 class URLRequestPostInterceptor {
37 public:
38 // Allows a generic string maching interface when setting up expectations.
39 class RequestMatcher {
40 public:
41 virtual bool Match(const std::string& actual) const = 0;
42 virtual ~RequestMatcher() {}
45 // Returns the url that is intercepted.
46 GURL GetUrl() const;
48 // Sets an expection for the body of the POST request and optionally,
49 // provides a canned response identified by a |file_path| to be returned when
50 // the expectation is met. If no |file_path| is provided, then an empty
51 // response body is served. If |response_code| is provided, then an empty
52 // response body with that response code is returned.
53 // Returns |true| if the expectation was set. This class takes ownership of
54 // the |request_matcher| object.
55 bool ExpectRequest(class RequestMatcher* request_matcher);
56 bool ExpectRequest(class RequestMatcher* request_matcher, int response_code);
57 bool ExpectRequest(class RequestMatcher* request_matcher,
58 const base::FilePath& filepath);
60 // Returns how many requests have been intercepted and matched by
61 // an expectation. One expectation can only be matched by one request.
62 int GetHitCount() const;
64 // Returns how many requests in total have been captured by the interceptor.
65 int GetCount() const;
67 // Returns all requests that have been intercepted, matched or not.
68 std::vector<std::string> GetRequests() const;
70 // Returns all requests as a string for debugging purposes.
71 std::string GetRequestsAsString() const;
73 // Resets the state of the interceptor so that new expectations can be set.
74 void Reset();
76 class Delegate;
78 private:
79 friend class URLRequestPostInterceptorFactory;
81 static const int kResponseCode200 = 200;
83 struct ExpectationResponse {
84 ExpectationResponse(int code, const std::string& body)
85 : response_code(code), response_body(body) {}
86 const int response_code;
87 const std::string response_body;
89 typedef std::pair<const RequestMatcher*, ExpectationResponse> Expectation;
91 URLRequestPostInterceptor(
92 const GURL& url,
93 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
94 ~URLRequestPostInterceptor();
96 void ClearExpectations();
98 const GURL url_;
99 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
101 mutable base::Lock interceptor_lock_;
102 mutable int hit_count_;
103 mutable std::vector<std::string> requests_;
104 mutable std::queue<Expectation> expectations_;
106 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor);
109 class URLRequestPostInterceptorFactory {
110 public:
111 URLRequestPostInterceptorFactory(
112 const std::string& scheme,
113 const std::string& hostname,
114 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
115 ~URLRequestPostInterceptorFactory();
117 // Creates an interceptor object for the specified url path. Returns NULL
118 // in case of errors or a valid interceptor object otherwise. The caller
119 // does not own the returned object.
120 URLRequestPostInterceptor* CreateInterceptor(const base::FilePath& filepath);
122 private:
123 const std::string scheme_;
124 const std::string hostname_;
125 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
127 // After creation, |delegate_| lives on the IO thread and it is owned by
128 // a URLRequestFilter after registration. A task to unregister it and
129 // implicitly destroy it is posted from ~URLRequestPostInterceptorFactory().
130 URLRequestPostInterceptor::Delegate* delegate_;
132 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory);
135 // Intercepts HTTP POST requests sent to "localhost2".
136 class InterceptorFactory : public URLRequestPostInterceptorFactory {
137 public:
138 explicit InterceptorFactory(
139 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
140 ~InterceptorFactory();
142 // Creates an interceptor for the url path defined by POST_INTERCEPT_PATH.
143 URLRequestPostInterceptor* CreateInterceptor();
145 // Creates an interceptor for the given url path.
146 URLRequestPostInterceptor* CreateInterceptorForPath(const char* url_path);
148 private:
149 DISALLOW_COPY_AND_ASSIGN(InterceptorFactory);
152 class PartialMatch : public URLRequestPostInterceptor::RequestMatcher {
153 public:
154 explicit PartialMatch(const std::string& expected) : expected_(expected) {}
155 bool Match(const std::string& actual) const override;
157 private:
158 const std::string expected_;
160 DISALLOW_COPY_AND_ASSIGN(PartialMatch);
163 } // namespace update_client
165 #endif // COMPONENTS_UPDATE_CLIENT_URL_REQUEST_POST_INTERCEPTOR_H_