Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(452)

Side by Side Diff: components/update_client/test/url_request_post_interceptor.h

Issue 808773005: Move most of the component updater artifacts to update_client. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #ifndef COMPONENTS_UPDATE_CLIENT_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
6 #define COMPONENTS_UPDATE_CLIENT_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
7
8 #include <stdint.h>
9 #include <map>
10 #include <queue>
11 #include <string>
12 #include <utility>
13 #include <vector>
14
15 #include "base/macros.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/synchronization/lock.h"
18 #include "url/gurl.h"
19
20 namespace base {
21 class FilePath;
22 class SequencedTaskRunner;
23 }
24
25 namespace net {
26 class URLRequest;
27 }
28
29 namespace update_client {
30
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() {}
43 };
44
45 // Returns the url that is intercepted.
46 GURL GetUrl() const;
47
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);
59
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;
63
64 // Returns how many requests in total have been captured by the interceptor.
65 int GetCount() const;
66
67 // Returns all requests that have been intercepted, matched or not.
68 std::vector<std::string> GetRequests() const;
69
70 // Returns all requests as a string for debugging purposes.
71 std::string GetRequestsAsString() const;
72
73 // Resets the state of the interceptor so that new expectations can be set.
74 void Reset();
75
76 class Delegate;
77
78 private:
79 friend class URLRequestPostInterceptorFactory;
80
81 static const int kResponseCode200 = 200;
82
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;
88 };
89 typedef std::pair<const RequestMatcher*, ExpectationResponse> Expectation;
90
91 URLRequestPostInterceptor(
92 const GURL& url,
93 const scoped_refptr<base::SequencedTaskRunner>& io_task_runner);
94 ~URLRequestPostInterceptor();
95
96 void ClearExpectations();
97
98 const GURL url_;
99 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
100
101 mutable base::Lock interceptor_lock_;
102 mutable int hit_count_;
103 mutable std::vector<std::string> requests_;
104 mutable std::queue<Expectation> expectations_;
105
106 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor);
107 };
108
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();
116
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);
121
122 private:
123 const std::string scheme_;
124 const std::string hostname_;
125 scoped_refptr<base::SequencedTaskRunner> io_task_runner_;
126
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_;
131
132 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory);
133 };
134
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();
141
142 // Creates an interceptor for the url path defined by POST_INTERCEPT_PATH.
143 URLRequestPostInterceptor* CreateInterceptor();
144
145 // Creates an interceptor for the given url path.
146 URLRequestPostInterceptor* CreateInterceptorForPath(const char* url_path);
147
148 private:
149 DISALLOW_COPY_AND_ASSIGN(InterceptorFactory);
150 };
151
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;
156
157 private:
158 const std::string expected_;
159
160 DISALLOW_COPY_AND_ASSIGN(PartialMatch);
161 };
162
163 } // namespace update_client
164
165 #endif // COMPONENTS_UPDATE_CLIENT_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698