| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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.h" | 5 #include "net/proxy/proxy_script_fetcher.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
| 10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 86 &text, &callback); | 86 &text, &callback); |
| 87 EXPECT_EQ(ERR_IO_PENDING, result); | 87 EXPECT_EQ(ERR_IO_PENDING, result); |
| 88 EXPECT_EQ(OK, callback.WaitForResult()); | 88 EXPECT_EQ(OK, callback.WaitForResult()); |
| 89 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 89 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
| 90 } | 90 } |
| 91 } | 91 } |
| 92 | 92 |
| 93 // Note that all mime types are allowed for PAC file, to be consistent | 93 // Note that all mime types are allowed for PAC file, to be consistent |
| 94 // with other browsers. | 94 // with other browsers. |
| 95 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { | 95 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { |
| 96 scoped_refptr<HTTPTestServer> server = | 96 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 97 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 98 ASSERT_TRUE(NULL != server.get()); | 97 ASSERT_TRUE(NULL != server.get()); |
| 99 scoped_refptr<URLRequestContext> context = new RequestContext; | 98 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 100 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 99 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 101 ProxyScriptFetcher::Create(context)); | 100 ProxyScriptFetcher::Create(context)); |
| 102 | 101 |
| 103 { // Fetch a PAC with mime type "text/plain" | 102 { // Fetch a PAC with mime type "text/plain" |
| 104 GURL url = server->TestServerPage("files/pac.txt"); | 103 GURL url = server->TestServerPage("files/pac.txt"); |
| 105 string16 text; | 104 string16 text; |
| 106 TestCompletionCallback callback; | 105 TestCompletionCallback callback; |
| 107 int result = pac_fetcher->Fetch(url, &text, &callback); | 106 int result = pac_fetcher->Fetch(url, &text, &callback); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 123 string16 text; | 122 string16 text; |
| 124 TestCompletionCallback callback; | 123 TestCompletionCallback callback; |
| 125 int result = pac_fetcher->Fetch(url, &text, &callback); | 124 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 126 EXPECT_EQ(ERR_IO_PENDING, result); | 125 EXPECT_EQ(ERR_IO_PENDING, result); |
| 127 EXPECT_EQ(OK, callback.WaitForResult()); | 126 EXPECT_EQ(OK, callback.WaitForResult()); |
| 128 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 127 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 129 } | 128 } |
| 130 } | 129 } |
| 131 | 130 |
| 132 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { | 131 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { |
| 133 scoped_refptr<HTTPTestServer> server = | 132 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 134 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 135 ASSERT_TRUE(NULL != server.get()); | 133 ASSERT_TRUE(NULL != server.get()); |
| 136 scoped_refptr<URLRequestContext> context = new RequestContext; | 134 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 137 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 135 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 138 ProxyScriptFetcher::Create(context)); | 136 ProxyScriptFetcher::Create(context)); |
| 139 | 137 |
| 140 { // Fetch a PAC which gives a 500 -- FAIL | 138 { // Fetch a PAC which gives a 500 -- FAIL |
| 141 GURL url = server->TestServerPage("files/500.pac"); | 139 GURL url = server->TestServerPage("files/500.pac"); |
| 142 string16 text; | 140 string16 text; |
| 143 TestCompletionCallback callback; | 141 TestCompletionCallback callback; |
| 144 int result = pac_fetcher->Fetch(url, &text, &callback); | 142 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 145 EXPECT_EQ(ERR_IO_PENDING, result); | 143 EXPECT_EQ(ERR_IO_PENDING, result); |
| 146 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 144 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
| 147 EXPECT_TRUE(text.empty()); | 145 EXPECT_TRUE(text.empty()); |
| 148 } | 146 } |
| 149 { // Fetch a PAC which gives a 404 -- FAIL | 147 { // Fetch a PAC which gives a 404 -- FAIL |
| 150 GURL url = server->TestServerPage("files/404.pac"); | 148 GURL url = server->TestServerPage("files/404.pac"); |
| 151 string16 text; | 149 string16 text; |
| 152 TestCompletionCallback callback; | 150 TestCompletionCallback callback; |
| 153 int result = pac_fetcher->Fetch(url, &text, &callback); | 151 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 154 EXPECT_EQ(ERR_IO_PENDING, result); | 152 EXPECT_EQ(ERR_IO_PENDING, result); |
| 155 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 153 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
| 156 EXPECT_TRUE(text.empty()); | 154 EXPECT_TRUE(text.empty()); |
| 157 } | 155 } |
| 158 } | 156 } |
| 159 | 157 |
| 160 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { | 158 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { |
| 161 scoped_refptr<HTTPTestServer> server = | 159 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 162 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 163 ASSERT_TRUE(NULL != server.get()); | 160 ASSERT_TRUE(NULL != server.get()); |
| 164 scoped_refptr<URLRequestContext> context = new RequestContext; | 161 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 165 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 162 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 166 ProxyScriptFetcher::Create(context)); | 163 ProxyScriptFetcher::Create(context)); |
| 167 | 164 |
| 168 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 165 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
| 169 // have no effect. | 166 // have no effect. |
| 170 GURL url = server->TestServerPage("files/downloadable.pac"); | 167 GURL url = server->TestServerPage("files/downloadable.pac"); |
| 171 string16 text; | 168 string16 text; |
| 172 TestCompletionCallback callback; | 169 TestCompletionCallback callback; |
| 173 int result = pac_fetcher->Fetch(url, &text, &callback); | 170 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 174 EXPECT_EQ(ERR_IO_PENDING, result); | 171 EXPECT_EQ(ERR_IO_PENDING, result); |
| 175 EXPECT_EQ(OK, callback.WaitForResult()); | 172 EXPECT_EQ(OK, callback.WaitForResult()); |
| 176 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); | 173 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); |
| 177 } | 174 } |
| 178 | 175 |
| 179 TEST_F(ProxyScriptFetcherTest, NoCache) { | 176 TEST_F(ProxyScriptFetcherTest, NoCache) { |
| 180 scoped_refptr<HTTPTestServer> server = | 177 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 181 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 182 ASSERT_TRUE(NULL != server.get()); | 178 ASSERT_TRUE(NULL != server.get()); |
| 183 scoped_refptr<URLRequestContext> context = new RequestContext; | 179 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 184 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 180 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 185 ProxyScriptFetcher::Create(context)); | 181 ProxyScriptFetcher::Create(context)); |
| 186 | 182 |
| 187 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. | 183 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. |
| 188 GURL url = server->TestServerPage("files/cacheable_1hr.pac"); | 184 GURL url = server->TestServerPage("files/cacheable_1hr.pac"); |
| 189 { | 185 { |
| 190 string16 text; | 186 string16 text; |
| 191 TestCompletionCallback callback; | 187 TestCompletionCallback callback; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 205 { | 201 { |
| 206 string16 text; | 202 string16 text; |
| 207 TestCompletionCallback callback; | 203 TestCompletionCallback callback; |
| 208 int result = pac_fetcher->Fetch(url, &text, &callback); | 204 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 209 EXPECT_EQ(ERR_IO_PENDING, result); | 205 EXPECT_EQ(ERR_IO_PENDING, result); |
| 210 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); | 206 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); |
| 211 } | 207 } |
| 212 } | 208 } |
| 213 | 209 |
| 214 TEST_F(ProxyScriptFetcherTest, TooLarge) { | 210 TEST_F(ProxyScriptFetcherTest, TooLarge) { |
| 215 scoped_refptr<HTTPTestServer> server = | 211 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 216 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 217 ASSERT_TRUE(NULL != server.get()); | 212 ASSERT_TRUE(NULL != server.get()); |
| 218 scoped_refptr<URLRequestContext> context = new RequestContext; | 213 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 219 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 214 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 220 ProxyScriptFetcher::Create(context)); | 215 ProxyScriptFetcher::Create(context)); |
| 221 | 216 |
| 222 // Set the maximum response size to 50 bytes. | 217 // Set the maximum response size to 50 bytes. |
| 223 int prev_size = ProxyScriptFetcher::SetSizeConstraintForUnittest(50); | 218 int prev_size = ProxyScriptFetcher::SetSizeConstraintForUnittest(50); |
| 224 | 219 |
| 225 // These two URLs are the same file, but are http:// vs file:// | 220 // These two URLs are the same file, but are http:// vs file:// |
| 226 GURL urls[] = { | 221 GURL urls[] = { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 248 string16 text; | 243 string16 text; |
| 249 TestCompletionCallback callback; | 244 TestCompletionCallback callback; |
| 250 int result = pac_fetcher->Fetch(url, &text, &callback); | 245 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 251 EXPECT_EQ(ERR_IO_PENDING, result); | 246 EXPECT_EQ(ERR_IO_PENDING, result); |
| 252 EXPECT_EQ(OK, callback.WaitForResult()); | 247 EXPECT_EQ(OK, callback.WaitForResult()); |
| 253 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 248 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 254 } | 249 } |
| 255 } | 250 } |
| 256 | 251 |
| 257 TEST_F(ProxyScriptFetcherTest, Hang) { | 252 TEST_F(ProxyScriptFetcherTest, Hang) { |
| 258 scoped_refptr<HTTPTestServer> server = | 253 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 259 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 260 ASSERT_TRUE(NULL != server.get()); | 254 ASSERT_TRUE(NULL != server.get()); |
| 261 scoped_refptr<URLRequestContext> context = new RequestContext; | 255 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 262 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 256 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 263 ProxyScriptFetcher::Create(context)); | 257 ProxyScriptFetcher::Create(context)); |
| 264 | 258 |
| 265 // Set the timeout period to 0.5 seconds. | 259 // Set the timeout period to 0.5 seconds. |
| 266 int prev_timeout = | 260 int prev_timeout = |
| 267 ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); | 261 ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); |
| 268 | 262 |
| 269 // Try fetching a URL which takes 1.2 seconds. We should abort the request | 263 // Try fetching a URL which takes 1.2 seconds. We should abort the request |
| (...skipping 18 matching lines...) Expand all Loading... |
| 288 EXPECT_EQ(ERR_IO_PENDING, result); | 282 EXPECT_EQ(ERR_IO_PENDING, result); |
| 289 EXPECT_EQ(OK, callback.WaitForResult()); | 283 EXPECT_EQ(OK, callback.WaitForResult()); |
| 290 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 284 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 291 } | 285 } |
| 292 } | 286 } |
| 293 | 287 |
| 294 // The ProxyScriptFetcher should decode any content-codings | 288 // The ProxyScriptFetcher should decode any content-codings |
| 295 // (like gzip, bzip, etc.), and apply any charset conversions to yield | 289 // (like gzip, bzip, etc.), and apply any charset conversions to yield |
| 296 // UTF8. | 290 // UTF8. |
| 297 TEST_F(ProxyScriptFetcherTest, Encodings) { | 291 TEST_F(ProxyScriptFetcherTest, Encodings) { |
| 298 scoped_refptr<HTTPTestServer> server = | 292 scoped_refptr<HTTPTestServer> server(HTTPTestServer::CreateServer(kDocRoot)); |
| 299 HTTPTestServer::CreateServer(kDocRoot, NULL); | |
| 300 ASSERT_TRUE(NULL != server.get()); | 293 ASSERT_TRUE(NULL != server.get()); |
| 301 scoped_refptr<URLRequestContext> context = new RequestContext; | 294 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 302 scoped_ptr<ProxyScriptFetcher> pac_fetcher( | 295 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 303 ProxyScriptFetcher::Create(context)); | 296 ProxyScriptFetcher::Create(context)); |
| 304 | 297 |
| 305 // Test a response that is gzip-encoded -- should get inflated. | 298 // Test a response that is gzip-encoded -- should get inflated. |
| 306 { | 299 { |
| 307 GURL url = server->TestServerPage("files/gzipped_pac"); | 300 GURL url = server->TestServerPage("files/gzipped_pac"); |
| 308 string16 text; | 301 string16 text; |
| 309 TestCompletionCallback callback; | 302 TestCompletionCallback callback; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 320 string16 text; | 313 string16 text; |
| 321 TestCompletionCallback callback; | 314 TestCompletionCallback callback; |
| 322 int result = pac_fetcher->Fetch(url, &text, &callback); | 315 int result = pac_fetcher->Fetch(url, &text, &callback); |
| 323 EXPECT_EQ(ERR_IO_PENDING, result); | 316 EXPECT_EQ(ERR_IO_PENDING, result); |
| 324 EXPECT_EQ(OK, callback.WaitForResult()); | 317 EXPECT_EQ(OK, callback.WaitForResult()); |
| 325 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); | 318 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); |
| 326 } | 319 } |
| 327 } | 320 } |
| 328 | 321 |
| 329 } // namespace net | 322 } // namespace net |
| OLD | NEW |