Home | History | Annotate | Download | only in test
      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 CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
      6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
      7 
      8 #include <map>
      9 #include <queue>
     10 #include <string>
     11 #include <utility>
     12 #include <vector>
     13 #include "base/basictypes.h"
     14 #include "base/synchronization/lock.h"
     15 #include "url/gurl.h"
     16 
     17 namespace base {
     18 class FilePath;
     19 }
     20 
     21 namespace net {
     22 class URLRequest;
     23 }
     24 
     25 namespace component_updater {
     26 
     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
     31 // intercepted.
     32 class URLRequestPostInterceptor {
     33  public:
     34   // Allows a generic string maching interface when setting up expectations.
     35   class RequestMatcher {
     36    public:
     37     virtual bool Match(const std::string& actual) const = 0;
     38     virtual ~RequestMatcher() {}
     39   };
     40 
     41   // Returns the url that is intercepted.
     42   GURL GetUrl() const;
     43 
     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);
     52 
     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;
     56 
     57   // Returns how many requests in total have been captured by the interceptor.
     58   int GetCount() const;
     59 
     60   // Returns all requests that have been intercepted, matched or not.
     61   std::vector<std::string> GetRequests() const;
     62 
     63   // Returns all requests as a string for debugging purposes.
     64   std::string GetRequestsAsString() const;
     65 
     66   // Resets the state of the interceptor so that new expectations can be set.
     67   void Reset();
     68 
     69   class Delegate;
     70 
     71  private:
     72   friend class URLRequestPostInterceptorFactory;
     73   typedef std::pair<const RequestMatcher*, std::string> Expectation;
     74 
     75   explicit URLRequestPostInterceptor(const GURL& url);
     76   ~URLRequestPostInterceptor();
     77 
     78   void ClearExpectations();
     79   const GURL url_;
     80 
     81   mutable base::Lock interceptor_lock_;
     82   mutable int hit_count_;
     83   mutable std::vector<std::string> requests_;
     84   mutable std::queue<Expectation> expectations_;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor);
     87 };
     88 
     89 class URLRequestPostInterceptorFactory {
     90  public:
     91   URLRequestPostInterceptorFactory(const std::string& scheme,
     92                                    const std::string& hostname);
     93   ~URLRequestPostInterceptorFactory();
     94 
     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);
     99 
    100  private:
    101   const std::string scheme_;
    102   const std::string hostname_;
    103 
    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_;
    108 
    109   DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptorFactory);
    110 };
    111 
    112 }  // namespace component_updater
    113 
    114 #endif  // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_
    115