| 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 CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 5 #ifndef CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
| 6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 6 #define CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
| 7 | 7 |
| 8 #include <string> |
| 9 #include <vector> |
| 8 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 9 #include "net/url_request/url_request_job_factory.h" | 11 |
| 10 #include "net/url_request/url_request_simple_job.h" | 12 namespace base { |
| 13 class FilePath; |
| 14 } |
| 11 | 15 |
| 12 namespace net { | 16 namespace net { |
| 13 class NetworkDelegate; | |
| 14 class URLRequest; | 17 class URLRequest; |
| 15 } | 18 } |
| 16 | 19 |
| 17 class RequestCounter { | 20 // Intercepts POST requests, counts them, and captures the body of the requests. |
| 18 public: | 21 // Optionally, for each request, it can return a canned response from a |
| 19 virtual void Trial(net::URLRequest* request) = 0; | 22 // given file. The class maintains a queue of paths to serve responses from, |
| 20 }; | 23 // and returns one and only one response for each request that it intercepts. |
| 21 | |
| 22 class URLRequestPostInterceptor { | 24 class URLRequestPostInterceptor { |
| 23 public: | 25 public: |
| 24 explicit URLRequestPostInterceptor(RequestCounter* counter); | 26 URLRequestPostInterceptor(const std::string& scheme, |
| 25 virtual ~URLRequestPostInterceptor(); | 27 const std::string& hostname); |
| 28 ~URLRequestPostInterceptor(); |
| 29 |
| 30 // Returns how many requests have been intercepted. |
| 31 int GetHitCount() const; |
| 32 |
| 33 // Returns the requests that have been intercepted. |
| 34 std::vector<std::string> GetRequests() const; |
| 35 |
| 36 // Returns all requests as a string for debugging purposes. |
| 37 std::string GetRequestsAsString() const; |
| 38 |
| 39 // Adds a response to its queue of responses to serve. |
| 40 // TODO(sorin): implement this. |
| 41 void AddResponse(const base::FilePath& path); |
| 26 | 42 |
| 27 private: | 43 private: |
| 28 class Delegate; | 44 class Delegate; |
| 29 | 45 |
| 30 // After creation, |delegate_| lives on the IO thread, and a task to delete it | 46 const std::string scheme_; |
| 31 // is posted from ~URLRequestPostInterceptor(). | 47 const std::string hostname_; |
| 48 |
| 49 // After creation, |delegate_| lives on the IO thread and it is owned by |
| 50 // a URLRequestFilter after registration. A task to unregister it and |
| 51 // implicitly destroy it is posted from ~URLRequestPostInterceptor(). |
| 32 Delegate* delegate_; | 52 Delegate* delegate_; |
| 33 | 53 |
| 34 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); | 54 DISALLOW_COPY_AND_ASSIGN(URLRequestPostInterceptor); |
| 35 }; | 55 }; |
| 36 | 56 |
| 37 class URLRequestPostInterceptor::Delegate | |
| 38 : public net::URLRequestJobFactory::ProtocolHandler { | |
| 39 public: | |
| 40 explicit Delegate(RequestCounter* counter); | |
| 41 virtual ~Delegate() {} | |
| 42 | |
| 43 void Register(); | |
| 44 | |
| 45 void Unregister(); | |
| 46 | |
| 47 private: | |
| 48 RequestCounter* counter_; | |
| 49 | |
| 50 virtual net::URLRequestJob* MaybeCreateJob( | |
| 51 net::URLRequest* request, | |
| 52 net::NetworkDelegate* network_delegate) const OVERRIDE; | |
| 53 }; | |
| 54 | |
| 55 class URLRequestPingMockJob : public net::URLRequestSimpleJob { | |
| 56 public: | |
| 57 URLRequestPingMockJob(net::URLRequest* request, | |
| 58 net::NetworkDelegate* network_delegate); | |
| 59 | |
| 60 protected: | |
| 61 virtual int GetData(std::string* mime_type, | |
| 62 std::string* charset, | |
| 63 std::string* data, | |
| 64 const net::CompletionCallback& callback) const OVERRIDE; | |
| 65 | |
| 66 private: | |
| 67 virtual ~URLRequestPingMockJob() {} | |
| 68 | |
| 69 DISALLOW_COPY_AND_ASSIGN(URLRequestPingMockJob); | |
| 70 }; | |
| 71 | |
| 72 #endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ | 57 #endif // CHROME_BROWSER_COMPONENT_UPDATER_TEST_URL_REQUEST_POST_INTERCEPTOR_H_ |
| OLD | NEW |