OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "net/proxy/proxy_service.h" | 5 #include "net/proxy/proxy_service.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "base/string_util.h" | 11 #include "base/string_util.h" |
12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
13 #include "googleurl/src/gurl.h" | 13 #include "googleurl/src/gurl.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 #include "net/base/net_log.h" | 15 #include "net/base/net_log.h" |
16 #include "net/base/net_log_unittest.h" | 16 #include "net/base/net_log_unittest.h" |
17 #include "net/base/test_completion_callback.h" | 17 #include "net/base/test_completion_callback.h" |
18 #include "net/proxy/mock_proxy_resolver.h" | 18 #include "net/proxy/mock_proxy_resolver.h" |
| 19 #include "net/proxy/mock_proxy_script_fetcher.h" |
19 #include "net/proxy/proxy_config_service.h" | 20 #include "net/proxy/proxy_config_service.h" |
20 #include "net/proxy/proxy_resolver.h" | 21 #include "net/proxy/proxy_resolver.h" |
21 #include "net/proxy/proxy_script_fetcher.h" | 22 #include "net/proxy/proxy_script_fetcher.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
23 | 24 |
24 // TODO(eroman): Write a test which exercises | 25 // TODO(eroman): Write a test which exercises |
25 // ProxyService::SuspendAllPendingRequests(). | 26 // ProxyService::SuspendAllPendingRequests(). |
26 namespace net { | 27 namespace net { |
27 namespace { | 28 namespace { |
28 | 29 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 } | 61 } |
61 | 62 |
62 private: | 63 private: |
63 ConfigAvailability availability_; | 64 ConfigAvailability availability_; |
64 ProxyConfig config_; | 65 ProxyConfig config_; |
65 ObserverList<Observer, true> observers_; | 66 ObserverList<Observer, true> observers_; |
66 }; | 67 }; |
67 | 68 |
68 } // namespace | 69 } // namespace |
69 | 70 |
70 // A mock ProxyScriptFetcher. No result will be returned to the fetch client | |
71 // until we call NotifyFetchCompletion() to set the results. | |
72 class MockProxyScriptFetcher : public ProxyScriptFetcher { | |
73 public: | |
74 MockProxyScriptFetcher() | |
75 : pending_request_callback_(NULL), pending_request_text_(NULL) { | |
76 } | |
77 | |
78 // ProxyScriptFetcher implementation. | |
79 virtual int Fetch(const GURL& url, | |
80 string16* text, | |
81 CompletionCallback* callback) { | |
82 DCHECK(!has_pending_request()); | |
83 | |
84 // Save the caller's information, and have them wait. | |
85 pending_request_url_ = url; | |
86 pending_request_callback_ = callback; | |
87 pending_request_text_ = text; | |
88 return ERR_IO_PENDING; | |
89 } | |
90 | |
91 void NotifyFetchCompletion(int result, const std::string& ascii_text) { | |
92 DCHECK(has_pending_request()); | |
93 *pending_request_text_ = ASCIIToUTF16(ascii_text); | |
94 CompletionCallback* callback = pending_request_callback_; | |
95 pending_request_callback_ = NULL; | |
96 callback->Run(result); | |
97 } | |
98 | |
99 virtual void Cancel() {} | |
100 | |
101 virtual URLRequestContext* GetRequestContext() { return NULL; } | |
102 | |
103 const GURL& pending_request_url() const { | |
104 return pending_request_url_; | |
105 } | |
106 | |
107 bool has_pending_request() const { | |
108 return pending_request_callback_ != NULL; | |
109 } | |
110 | |
111 private: | |
112 GURL pending_request_url_; | |
113 CompletionCallback* pending_request_callback_; | |
114 string16* pending_request_text_; | |
115 }; | |
116 | |
117 TEST(ProxyServiceTest, Direct) { | 71 TEST(ProxyServiceTest, Direct) { |
118 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; | 72 MockAsyncProxyResolver* resolver = new MockAsyncProxyResolver; |
119 scoped_refptr<ProxyService> service( | 73 scoped_refptr<ProxyService> service( |
120 new ProxyService(new MockProxyConfigService( | 74 new ProxyService(new MockProxyConfigService( |
121 ProxyConfig::CreateDirect()), resolver, NULL)); | 75 ProxyConfig::CreateDirect()), resolver, NULL)); |
122 | 76 |
123 GURL url("http://www.google.com/"); | 77 GURL url("http://www.google.com/"); |
124 | 78 |
125 ProxyInfo info; | 79 ProxyInfo info; |
126 TestCompletionCallback callback; | 80 TestCompletionCallback callback; |
(...skipping 1567 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1694 log.GetEntries(&entries); | 1648 log.GetEntries(&entries); |
1695 | 1649 |
1696 EXPECT_TRUE(LogContainsEntryWithType(entries, 0, | 1650 EXPECT_TRUE(LogContainsEntryWithType(entries, 0, |
1697 NetLog::TYPE_PROXY_CONFIG_CHANGED)); | 1651 NetLog::TYPE_PROXY_CONFIG_CHANGED)); |
1698 ASSERT_EQ(13u, entries.size()); | 1652 ASSERT_EQ(13u, entries.size()); |
1699 for (size_t i = 1; i < entries.size(); ++i) | 1653 for (size_t i = 1; i < entries.size(); ++i) |
1700 EXPECT_NE(NetLog::TYPE_PROXY_CONFIG_CHANGED, entries[i].type); | 1654 EXPECT_NE(NetLog::TYPE_PROXY_CONFIG_CHANGED, entries[i].type); |
1701 } | 1655 } |
1702 | 1656 |
1703 } // namespace net | 1657 } // namespace net |
OLD | NEW |