OLD | NEW |
---|---|
1 // Copyright (c) 2010 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_script_fetcher_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/file_path.h" | 9 #include "base/file_path.h" |
10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
11 #include "base/path_service.h" | 11 #include "base/path_service.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
25 | 25 |
26 const FilePath::CharType kDocRoot[] = | 26 const FilePath::CharType kDocRoot[] = |
27 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); | 27 FILE_PATH_LITERAL("net/data/proxy_script_fetcher_unittest"); |
28 | 28 |
29 struct FetchResult { | 29 struct FetchResult { |
30 int code; | 30 int code; |
31 string16 text; | 31 string16 text; |
32 }; | 32 }; |
33 | 33 |
34 // A non-mock URL request which can access http:// and file:// urls. | 34 // A non-mock URL request which can access http:// and file:// urls. |
35 class RequestContext : public URLRequestContext { | 35 class RequestContext : public net::URLRequestContext { |
36 public: | 36 public: |
37 RequestContext() { | 37 RequestContext() { |
38 net::ProxyConfig no_proxy; | 38 net::ProxyConfig no_proxy; |
39 host_resolver_ = | 39 host_resolver_ = |
40 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, | 40 net::CreateSystemHostResolver(net::HostResolver::kDefaultParallelism, |
41 NULL, NULL); | 41 NULL, NULL); |
42 cert_verifier_ = new net::CertVerifier; | 42 cert_verifier_ = new net::CertVerifier; |
43 proxy_service_ = net::ProxyService::CreateFixed(no_proxy); | 43 proxy_service_ = net::ProxyService::CreateFixed(no_proxy); |
44 ssl_config_service_ = new net::SSLConfigServiceDefaults; | 44 ssl_config_service_ = new net::SSLConfigServiceDefaults; |
45 | 45 |
46 http_transaction_factory_ = new net::HttpCache( | 46 http_transaction_factory_ = new net::HttpCache( |
47 net::HttpNetworkLayer::CreateFactory(host_resolver_, cert_verifier_, | 47 net::HttpNetworkLayer::CreateFactory(host_resolver_, cert_verifier_, |
48 NULL, NULL, NULL, proxy_service_, ssl_config_service_, NULL, NULL, | 48 NULL, NULL, NULL, proxy_service_, ssl_config_service_, NULL, NULL, |
49 NULL), | 49 NULL), |
50 NULL, | 50 NULL, |
51 net::HttpCache::DefaultBackend::InMemory(0)); | 51 net::HttpCache::DefaultBackend::InMemory(0)); |
52 } | 52 } |
53 | 53 |
54 private: | 54 private: |
55 ~RequestContext() { | 55 ~RequestContext() { |
56 delete http_transaction_factory_; | 56 delete http_transaction_factory_; |
57 delete cert_verifier_; | 57 delete cert_verifier_; |
58 delete host_resolver_; | 58 delete host_resolver_; |
59 } | 59 } |
60 }; | 60 }; |
61 | 61 |
62 // Required to be in net namespace by FRIEND_TEST. | 62 // Required to be in net namespace by FRIEND_TEST. |
63 namespace net { | 63 namespace net { |
wtc
2011/01/15 17:54:23
The 'net' namespace starts here. Undo the changes
| |
64 | 64 |
65 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest. | 65 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest. |
66 GURL GetTestFileUrl(const std::string& relpath) { | 66 GURL GetTestFileUrl(const std::string& relpath) { |
67 FilePath path; | 67 FilePath path; |
68 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 68 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
69 path = path.AppendASCII("net"); | 69 path = path.AppendASCII("net"); |
70 path = path.AppendASCII("data"); | 70 path = path.AppendASCII("data"); |
71 path = path.AppendASCII("proxy_script_fetcher_unittest"); | 71 path = path.AppendASCII("proxy_script_fetcher_unittest"); |
72 GURL base_url = FilePathToFileURL(path); | 72 GURL base_url = FilePathToFileURL(path); |
73 return GURL(base_url.spec() + "/" + relpath); | 73 return GURL(base_url.spec() + "/" + relpath); |
74 } | 74 } |
75 | 75 |
76 class ProxyScriptFetcherImplTest : public PlatformTest { | 76 class ProxyScriptFetcherImplTest : public PlatformTest { |
77 public: | 77 public: |
78 ProxyScriptFetcherImplTest() | 78 ProxyScriptFetcherImplTest() |
79 : test_server_(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)) { | 79 : test_server_(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)) { |
80 } | 80 } |
81 | 81 |
82 static void SetUpTestCase() { | 82 static void SetUpTestCase() { |
83 net::URLRequest::AllowFileAccess(); | 83 net::URLRequest::AllowFileAccess(); |
84 } | 84 } |
85 | 85 |
86 protected: | 86 protected: |
87 net::TestServer test_server_; | 87 net::TestServer test_server_; |
88 }; | 88 }; |
89 | 89 |
90 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { | 90 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { |
91 scoped_refptr<URLRequestContext> context(new RequestContext); | 91 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
92 ProxyScriptFetcherImpl pac_fetcher(context); | 92 ProxyScriptFetcherImpl pac_fetcher(context); |
93 | 93 |
94 { // Fetch a non-existent file. | 94 { // Fetch a non-existent file. |
95 string16 text; | 95 string16 text; |
96 TestCompletionCallback callback; | 96 TestCompletionCallback callback; |
97 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), | 97 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), |
98 &text, &callback); | 98 &text, &callback); |
99 EXPECT_EQ(ERR_IO_PENDING, result); | 99 EXPECT_EQ(ERR_IO_PENDING, result); |
100 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); | 100 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); |
101 EXPECT_TRUE(text.empty()); | 101 EXPECT_TRUE(text.empty()); |
102 } | 102 } |
103 { // Fetch a file that exists. | 103 { // Fetch a file that exists. |
104 string16 text; | 104 string16 text; |
105 TestCompletionCallback callback; | 105 TestCompletionCallback callback; |
106 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), | 106 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), |
107 &text, &callback); | 107 &text, &callback); |
108 EXPECT_EQ(ERR_IO_PENDING, result); | 108 EXPECT_EQ(ERR_IO_PENDING, result); |
109 EXPECT_EQ(OK, callback.WaitForResult()); | 109 EXPECT_EQ(OK, callback.WaitForResult()); |
110 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 110 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
111 } | 111 } |
112 } | 112 } |
113 | 113 |
114 // Note that all mime types are allowed for PAC file, to be consistent | 114 // Note that all mime types are allowed for PAC file, to be consistent |
115 // with other browsers. | 115 // with other browsers. |
116 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { | 116 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { |
117 ASSERT_TRUE(test_server_.Start()); | 117 ASSERT_TRUE(test_server_.Start()); |
118 | 118 |
119 scoped_refptr<URLRequestContext> context(new RequestContext); | 119 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
120 ProxyScriptFetcherImpl pac_fetcher(context); | 120 ProxyScriptFetcherImpl pac_fetcher(context); |
121 | 121 |
122 { // Fetch a PAC with mime type "text/plain" | 122 { // Fetch a PAC with mime type "text/plain" |
123 GURL url(test_server_.GetURL("files/pac.txt")); | 123 GURL url(test_server_.GetURL("files/pac.txt")); |
124 string16 text; | 124 string16 text; |
125 TestCompletionCallback callback; | 125 TestCompletionCallback callback; |
126 int result = pac_fetcher.Fetch(url, &text, &callback); | 126 int result = pac_fetcher.Fetch(url, &text, &callback); |
127 EXPECT_EQ(ERR_IO_PENDING, result); | 127 EXPECT_EQ(ERR_IO_PENDING, result); |
128 EXPECT_EQ(OK, callback.WaitForResult()); | 128 EXPECT_EQ(OK, callback.WaitForResult()); |
129 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 129 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
(...skipping 14 matching lines...) Expand all Loading... | |
144 int result = pac_fetcher.Fetch(url, &text, &callback); | 144 int result = pac_fetcher.Fetch(url, &text, &callback); |
145 EXPECT_EQ(ERR_IO_PENDING, result); | 145 EXPECT_EQ(ERR_IO_PENDING, result); |
146 EXPECT_EQ(OK, callback.WaitForResult()); | 146 EXPECT_EQ(OK, callback.WaitForResult()); |
147 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 147 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
148 } | 148 } |
149 } | 149 } |
150 | 150 |
151 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) { | 151 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) { |
152 ASSERT_TRUE(test_server_.Start()); | 152 ASSERT_TRUE(test_server_.Start()); |
153 | 153 |
154 scoped_refptr<URLRequestContext> context(new RequestContext); | 154 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
155 ProxyScriptFetcherImpl pac_fetcher(context); | 155 ProxyScriptFetcherImpl pac_fetcher(context); |
156 | 156 |
157 { // Fetch a PAC which gives a 500 -- FAIL | 157 { // Fetch a PAC which gives a 500 -- FAIL |
158 GURL url(test_server_.GetURL("files/500.pac")); | 158 GURL url(test_server_.GetURL("files/500.pac")); |
159 string16 text; | 159 string16 text; |
160 TestCompletionCallback callback; | 160 TestCompletionCallback callback; |
161 int result = pac_fetcher.Fetch(url, &text, &callback); | 161 int result = pac_fetcher.Fetch(url, &text, &callback); |
162 EXPECT_EQ(ERR_IO_PENDING, result); | 162 EXPECT_EQ(ERR_IO_PENDING, result); |
163 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 163 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
164 EXPECT_TRUE(text.empty()); | 164 EXPECT_TRUE(text.empty()); |
165 } | 165 } |
166 { // Fetch a PAC which gives a 404 -- FAIL | 166 { // Fetch a PAC which gives a 404 -- FAIL |
167 GURL url(test_server_.GetURL("files/404.pac")); | 167 GURL url(test_server_.GetURL("files/404.pac")); |
168 string16 text; | 168 string16 text; |
169 TestCompletionCallback callback; | 169 TestCompletionCallback callback; |
170 int result = pac_fetcher.Fetch(url, &text, &callback); | 170 int result = pac_fetcher.Fetch(url, &text, &callback); |
171 EXPECT_EQ(ERR_IO_PENDING, result); | 171 EXPECT_EQ(ERR_IO_PENDING, result); |
172 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 172 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
173 EXPECT_TRUE(text.empty()); | 173 EXPECT_TRUE(text.empty()); |
174 } | 174 } |
175 } | 175 } |
176 | 176 |
177 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) { | 177 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) { |
178 ASSERT_TRUE(test_server_.Start()); | 178 ASSERT_TRUE(test_server_.Start()); |
179 | 179 |
180 scoped_refptr<URLRequestContext> context(new RequestContext); | 180 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
181 ProxyScriptFetcherImpl pac_fetcher(context); | 181 ProxyScriptFetcherImpl pac_fetcher(context); |
182 | 182 |
183 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 183 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
184 // have no effect. | 184 // have no effect. |
185 GURL url(test_server_.GetURL("files/downloadable.pac")); | 185 GURL url(test_server_.GetURL("files/downloadable.pac")); |
186 string16 text; | 186 string16 text; |
187 TestCompletionCallback callback; | 187 TestCompletionCallback callback; |
188 int result = pac_fetcher.Fetch(url, &text, &callback); | 188 int result = pac_fetcher.Fetch(url, &text, &callback); |
189 EXPECT_EQ(ERR_IO_PENDING, result); | 189 EXPECT_EQ(ERR_IO_PENDING, result); |
190 EXPECT_EQ(OK, callback.WaitForResult()); | 190 EXPECT_EQ(OK, callback.WaitForResult()); |
191 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); | 191 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); |
192 } | 192 } |
193 | 193 |
194 TEST_F(ProxyScriptFetcherImplTest, NoCache) { | 194 TEST_F(ProxyScriptFetcherImplTest, NoCache) { |
195 ASSERT_TRUE(test_server_.Start()); | 195 ASSERT_TRUE(test_server_.Start()); |
196 | 196 |
197 scoped_refptr<URLRequestContext> context(new RequestContext); | 197 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
198 ProxyScriptFetcherImpl pac_fetcher(context); | 198 ProxyScriptFetcherImpl pac_fetcher(context); |
199 | 199 |
200 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. | 200 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. |
201 GURL url(test_server_.GetURL("files/cacheable_1hr.pac")); | 201 GURL url(test_server_.GetURL("files/cacheable_1hr.pac")); |
202 { | 202 { |
203 string16 text; | 203 string16 text; |
204 TestCompletionCallback callback; | 204 TestCompletionCallback callback; |
205 int result = pac_fetcher.Fetch(url, &text, &callback); | 205 int result = pac_fetcher.Fetch(url, &text, &callback); |
206 EXPECT_EQ(ERR_IO_PENDING, result); | 206 EXPECT_EQ(ERR_IO_PENDING, result); |
207 EXPECT_EQ(OK, callback.WaitForResult()); | 207 EXPECT_EQ(OK, callback.WaitForResult()); |
(...skipping 11 matching lines...) Expand all Loading... | |
219 TestCompletionCallback callback; | 219 TestCompletionCallback callback; |
220 int result = pac_fetcher.Fetch(url, &text, &callback); | 220 int result = pac_fetcher.Fetch(url, &text, &callback); |
221 EXPECT_EQ(ERR_IO_PENDING, result); | 221 EXPECT_EQ(ERR_IO_PENDING, result); |
222 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); | 222 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); |
223 } | 223 } |
224 } | 224 } |
225 | 225 |
226 TEST_F(ProxyScriptFetcherImplTest, TooLarge) { | 226 TEST_F(ProxyScriptFetcherImplTest, TooLarge) { |
227 ASSERT_TRUE(test_server_.Start()); | 227 ASSERT_TRUE(test_server_.Start()); |
228 | 228 |
229 scoped_refptr<URLRequestContext> context(new RequestContext); | 229 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
230 ProxyScriptFetcherImpl pac_fetcher(context); | 230 ProxyScriptFetcherImpl pac_fetcher(context); |
231 | 231 |
232 // Set the maximum response size to 50 bytes. | 232 // Set the maximum response size to 50 bytes. |
233 int prev_size = pac_fetcher.SetSizeConstraint(50); | 233 int prev_size = pac_fetcher.SetSizeConstraint(50); |
234 | 234 |
235 // These two URLs are the same file, but are http:// vs file:// | 235 // These two URLs are the same file, but are http:// vs file:// |
236 GURL urls[] = { | 236 GURL urls[] = { |
237 test_server_.GetURL("files/large-pac.nsproxy"), | 237 test_server_.GetURL("files/large-pac.nsproxy"), |
238 GetTestFileUrl("large-pac.nsproxy") | 238 GetTestFileUrl("large-pac.nsproxy") |
239 }; | 239 }; |
(...skipping 20 matching lines...) Expand all Loading... | |
260 int result = pac_fetcher.Fetch(url, &text, &callback); | 260 int result = pac_fetcher.Fetch(url, &text, &callback); |
261 EXPECT_EQ(ERR_IO_PENDING, result); | 261 EXPECT_EQ(ERR_IO_PENDING, result); |
262 EXPECT_EQ(OK, callback.WaitForResult()); | 262 EXPECT_EQ(OK, callback.WaitForResult()); |
263 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 263 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
264 } | 264 } |
265 } | 265 } |
266 | 266 |
267 TEST_F(ProxyScriptFetcherImplTest, Hang) { | 267 TEST_F(ProxyScriptFetcherImplTest, Hang) { |
268 ASSERT_TRUE(test_server_.Start()); | 268 ASSERT_TRUE(test_server_.Start()); |
269 | 269 |
270 scoped_refptr<URLRequestContext> context(new RequestContext); | 270 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
271 ProxyScriptFetcherImpl pac_fetcher(context); | 271 ProxyScriptFetcherImpl pac_fetcher(context); |
272 | 272 |
273 // Set the timeout period to 0.5 seconds. | 273 // Set the timeout period to 0.5 seconds. |
274 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint( | 274 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint( |
275 base::TimeDelta::FromMilliseconds(500)); | 275 base::TimeDelta::FromMilliseconds(500)); |
276 | 276 |
277 // Try fetching a URL which takes 1.2 seconds. We should abort the request | 277 // Try fetching a URL which takes 1.2 seconds. We should abort the request |
278 // after 500 ms, and fail with a timeout error. | 278 // after 500 ms, and fail with a timeout error. |
279 { GURL url(test_server_.GetURL("slow/proxy.pac?1.2")); | 279 { GURL url(test_server_.GetURL("slow/proxy.pac?1.2")); |
280 string16 text; | 280 string16 text; |
(...skipping 17 matching lines...) Expand all Loading... | |
298 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 298 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
299 } | 299 } |
300 } | 300 } |
301 | 301 |
302 // The ProxyScriptFetcher should decode any content-codings | 302 // The ProxyScriptFetcher should decode any content-codings |
303 // (like gzip, bzip, etc.), and apply any charset conversions to yield | 303 // (like gzip, bzip, etc.), and apply any charset conversions to yield |
304 // UTF8. | 304 // UTF8. |
305 TEST_F(ProxyScriptFetcherImplTest, Encodings) { | 305 TEST_F(ProxyScriptFetcherImplTest, Encodings) { |
306 ASSERT_TRUE(test_server_.Start()); | 306 ASSERT_TRUE(test_server_.Start()); |
307 | 307 |
308 scoped_refptr<URLRequestContext> context(new RequestContext); | 308 scoped_refptr<net::URLRequestContext> context(new RequestContext); |
309 ProxyScriptFetcherImpl pac_fetcher(context); | 309 ProxyScriptFetcherImpl pac_fetcher(context); |
310 | 310 |
311 // Test a response that is gzip-encoded -- should get inflated. | 311 // Test a response that is gzip-encoded -- should get inflated. |
312 { | 312 { |
313 GURL url(test_server_.GetURL("files/gzipped_pac")); | 313 GURL url(test_server_.GetURL("files/gzipped_pac")); |
314 string16 text; | 314 string16 text; |
315 TestCompletionCallback callback; | 315 TestCompletionCallback callback; |
316 int result = pac_fetcher.Fetch(url, &text, &callback); | 316 int result = pac_fetcher.Fetch(url, &text, &callback); |
317 EXPECT_EQ(ERR_IO_PENDING, result); | 317 EXPECT_EQ(ERR_IO_PENDING, result); |
318 EXPECT_EQ(OK, callback.WaitForResult()); | 318 EXPECT_EQ(OK, callback.WaitForResult()); |
319 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text); | 319 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text); |
320 } | 320 } |
321 | 321 |
322 // Test a response that was served as UTF-16 (BE). It should | 322 // Test a response that was served as UTF-16 (BE). It should |
323 // be converted to UTF8. | 323 // be converted to UTF8. |
324 { | 324 { |
325 GURL url(test_server_.GetURL("files/utf16be_pac")); | 325 GURL url(test_server_.GetURL("files/utf16be_pac")); |
326 string16 text; | 326 string16 text; |
327 TestCompletionCallback callback; | 327 TestCompletionCallback callback; |
328 int result = pac_fetcher.Fetch(url, &text, &callback); | 328 int result = pac_fetcher.Fetch(url, &text, &callback); |
329 EXPECT_EQ(ERR_IO_PENDING, result); | 329 EXPECT_EQ(ERR_IO_PENDING, result); |
330 EXPECT_EQ(OK, callback.WaitForResult()); | 330 EXPECT_EQ(OK, callback.WaitForResult()); |
331 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); | 331 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); |
332 } | 332 } |
333 } | 333 } |
334 | 334 |
335 } // namespace net | 335 } // namespace net |
OLD | NEW |