summaryrefslogtreecommitdiffstats
path: root/net/proxy/proxy_service_unittest.cc
diff options
context:
space:
mode:
authorhamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-06 12:11:05 +0000
committerhamaji@chromium.org <hamaji@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-06 12:11:05 +0000
commita692c6f3a59e711bc38fc72fce3c938deec64da0 (patch)
tree0583e80059992b55252390a7891e1651ba1c2ab3 /net/proxy/proxy_service_unittest.cc
parent4bd778d6cd4e108a38caeda9b283f01691fe7ecf (diff)
downloadchromium_src-a692c6f3a59e711bc38fc72fce3c938deec64da0.zip
chromium_src-a692c6f3a59e711bc38fc72fce3c938deec64da0.tar.gz
chromium_src-a692c6f3a59e711bc38fc72fce3c938deec64da0.tar.bz2
Reverting r22591. It seems to be making purify fail.
r22590: http://build.chromium.org/buildbot/waterfall/builders/XP%20Unit%20(purify)/builds/5026 r22591: http://build.chromium.org/buildbot/waterfall/builders/XP%20Unit%20(purify)/builds/5030 TEST=none BUG=none TBR=eroman Review URL: http://codereview.chromium.org/165048 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22603 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/proxy/proxy_service_unittest.cc')
-rw-r--r--net/proxy/proxy_service_unittest.cc151
1 files changed, 150 insertions, 1 deletions
diff --git a/net/proxy/proxy_service_unittest.cc b/net/proxy/proxy_service_unittest.cc
index f4deb67..3b3eb49 100644
--- a/net/proxy/proxy_service_unittest.cc
+++ b/net/proxy/proxy_service_unittest.cc
@@ -9,7 +9,6 @@
#include "googleurl/src/gurl.h"
#include "net/base/net_errors.h"
#include "net/base/test_completion_callback.h"
-#include "net/proxy/mock_proxy_resolver.h"
#include "net/proxy/proxy_config_service.h"
#include "net/proxy/proxy_resolver.h"
#include "net/proxy/proxy_script_fetcher.h"
@@ -37,6 +36,156 @@ class MockProxyConfigService: public ProxyConfigService {
ProxyConfig config;
};
+// Asynchronous mock proxy resolver. All requests complete asynchronously,
+// user must call Request::CompleteNow() on a pending request to signal it.
+class MockAsyncProxyResolverBase : public ProxyResolver {
+ public:
+ class Request : public base::RefCounted<Request> {
+ public:
+ Request(MockAsyncProxyResolverBase* resolver,
+ const GURL& url,
+ ProxyInfo* results,
+ CompletionCallback* callback)
+ : resolver_(resolver),
+ url_(url),
+ results_(results),
+ callback_(callback),
+ origin_loop_(MessageLoop::current()) {
+ }
+
+ const GURL& url() const { return url_; }
+ ProxyInfo* results() const { return results_; }
+ CompletionCallback* callback() const { return callback_; }
+
+ void CompleteNow(int rv) {
+ CompletionCallback* callback = callback_;
+
+ // May delete |this|.
+ resolver_->RemovePendingRequest(this);
+
+ callback->Run(rv);
+ }
+
+ private:
+ MockAsyncProxyResolverBase* resolver_;
+ const GURL url_;
+ ProxyInfo* results_;
+ CompletionCallback* callback_;
+ MessageLoop* origin_loop_;
+ };
+
+ class SetPacScriptRequest {
+ public:
+ SetPacScriptRequest(MockAsyncProxyResolverBase* resolver,
+ const GURL& pac_url,
+ const std::string& pac_bytes,
+ CompletionCallback* callback)
+ : resolver_(resolver),
+ pac_url_(pac_url),
+ pac_bytes_(pac_bytes),
+ callback_(callback),
+ origin_loop_(MessageLoop::current()) {
+ }
+
+ const GURL& pac_url() const { return pac_url_; }
+ const std::string& pac_bytes() const { return pac_bytes_; }
+
+ void CompleteNow(int rv) {
+ CompletionCallback* callback = callback_;
+
+ // Will delete |this|.
+ resolver_->RemovePendingSetPacScriptRequest(this);
+
+ callback->Run(rv);
+ }
+
+ private:
+ MockAsyncProxyResolverBase* resolver_;
+ const GURL pac_url_;
+ const std::string pac_bytes_;
+ CompletionCallback* callback_;
+ MessageLoop* origin_loop_;
+ };
+
+ typedef std::vector<scoped_refptr<Request> > RequestsList;
+
+ // ProxyResolver implementation:
+ virtual int GetProxyForURL(const GURL& url,
+ ProxyInfo* results,
+ CompletionCallback* callback,
+ RequestHandle* request_handle) {
+ scoped_refptr<Request> request = new Request(this, url, results, callback);
+ pending_requests_.push_back(request);
+
+ if (request_handle)
+ *request_handle = reinterpret_cast<RequestHandle>(request.get());
+
+ // Test code completes the request by calling request->CompleteNow().
+ return ERR_IO_PENDING;
+ }
+
+ virtual void CancelRequest(RequestHandle request_handle) {
+ scoped_refptr<Request> request = reinterpret_cast<Request*>(request_handle);
+ cancelled_requests_.push_back(request);
+ RemovePendingRequest(request);
+ }
+
+ virtual int SetPacScript(const GURL& pac_url,
+ const std::string& pac_bytes,
+ CompletionCallback* callback) {
+ EXPECT_EQ(NULL, pending_set_pac_script_request_.get());
+ pending_set_pac_script_request_.reset(
+ new SetPacScriptRequest(this, pac_url, pac_bytes, callback));
+ // Finished when user calls SetPacScriptRequest::CompleteNow().
+ return ERR_IO_PENDING;
+ }
+
+ const RequestsList& pending_requests() const {
+ return pending_requests_;
+ }
+
+ const RequestsList& cancelled_requests() const {
+ return cancelled_requests_;
+ }
+
+ SetPacScriptRequest* pending_set_pac_script_request() const {
+ return pending_set_pac_script_request_.get();
+ }
+
+ void RemovePendingRequest(Request* request) {
+ RequestsList::iterator it = std::find(
+ pending_requests_.begin(), pending_requests_.end(), request);
+ DCHECK(it != pending_requests_.end());
+ pending_requests_.erase(it);
+ }
+
+ void RemovePendingSetPacScriptRequest(SetPacScriptRequest* request) {
+ EXPECT_EQ(request, pending_set_pac_script_request());
+ pending_set_pac_script_request_.reset();
+ }
+
+ protected:
+ explicit MockAsyncProxyResolverBase(bool expects_pac_bytes)
+ : ProxyResolver(expects_pac_bytes) {}
+
+ private:
+ RequestsList pending_requests_;
+ RequestsList cancelled_requests_;
+ scoped_ptr<SetPacScriptRequest> pending_set_pac_script_request_;
+};
+
+class MockAsyncProxyResolver : public MockAsyncProxyResolverBase {
+ public:
+ MockAsyncProxyResolver()
+ : MockAsyncProxyResolverBase(false /*expects_pac_bytes*/) {}
+};
+
+class MockAsyncProxyResolverExpectsBytes : public MockAsyncProxyResolverBase {
+ public:
+ MockAsyncProxyResolverExpectsBytes()
+ : MockAsyncProxyResolverBase(true /*expects_pac_bytes*/) {}
+};
+
} // namespace
// A mock ProxyScriptFetcher. No result will be returned to the fetch client