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 CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
13 #include "base/basictypes.h"
14 #include "base/synchronization/lock.h"
25 namespace component_updater {
27 // Intercepts requests to a file path, counts them, and captures the body of
28 // the requests. Optionally, for each request, it can return a canned response
29 // from a given file. The class maintains a queue of expectations, and returns
30 // one and only one response for each request that matches and it is
32 class URLRequestPostInterceptor {
34 // Allows a generic string maching interface when setting up expectations.
35 class RequestMatcher {
37 virtual bool Match(const std::string& actual) const = 0;
38 virtual ~RequestMatcher() {}
41 // Returns the url that is intercepted.
44 // Sets an expection for the body of the POST request and optionally,
45 // provides a canned response identified by a |file_path| to be returned when
46 // the expectation is met. If no |file_path| is provided, then an empty
47 // response body is served. This class takes ownership of the
48 // |request_matcher| object. Returns |true| if the expectation was set.
49 bool ExpectRequest(class RequestMatcher* request_matcher);
50 bool ExpectRequest(class RequestMatcher* request_matcher,
51 const base::FilePath& filepath);
53 // Returns how many requests have been intercepted and matched by
54 // an expectation. One expectation can only be matched by one request.
55 int GetHitCount() const;
57 // Returns how many requests in total have been captured by the interceptor.
60 // Returns all requests that have been intercepted, matched or not.
61 std::vector<std::string> GetRequests() const;
63 // Returns all requests as a string for debugging purposes.
64 std::string GetRequestsAsString() const;
66 // Resets the state of the interceptor so that new expectations can be set.
72 friend class URLRequestPostInterceptorFactory;
73 typedef std::pair<const RequestMatcher*, std::string> Expectation;
75 explicit URLRequestPostInterceptor(const GURL& url);
76 ~URLRequestPostInterceptor();
78 void ClearExpectations();
81 mutable base::Lock interceptor_lock_;
82 mutable int hit_count_;
83 mutable std::vector<std::string> requests_;
84 mutable std::queue<Expectation> expectations_;
86 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor);
89 class URLRequestPostInterceptorFactory {
91 URLRequestPostInterceptorFactory(const std::string& scheme,
92 const std::string& hostname);
93 ~URLRequestPostInterceptorFactory();
95 // Creates an interceptor object for the specified url path. Returns NULL
96 // in case of errors or a valid interceptor object otherwise. The caller
97 // does not own the returned object.
98 URLRequestPostInterceptor* CreateInterceptor(const base::FilePath& filepath);
101 const std::string scheme_;
102 const std::string hostname_;
104 // After creation, |delegate_| lives on the IO thread and it is owned by
105 // a URLRequestFilter after registration. A task to unregister it and
106 // implicitly destroy it is posted from ~URLRequestPostInterceptorFactory().
107 URLRequestPostInterceptor::Delegate* delegate_;
109 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory);
112 } // namespace component_updater
114 #endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_