| OLD | NEW |
| 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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" |
| 11 #include "net/base/ssl_config_service_defaults.h" | 11 #include "net/base/ssl_config_service_defaults.h" |
| 12 #include "net/base/test_completion_callback.h" |
| 12 #include "net/disk_cache/disk_cache.h" | 13 #include "net/disk_cache/disk_cache.h" |
| 13 #include "net/http/http_cache.h" | 14 #include "net/http/http_cache.h" |
| 14 #include "net/url_request/url_request_unittest.h" | 15 #include "net/url_request/url_request_unittest.h" |
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 16 #include "testing/platform_test.h" | 17 #include "testing/platform_test.h" |
| 17 | 18 |
| 18 // TODO(eroman): | 19 // TODO(eroman): |
| 19 // - Test canceling an outstanding request. | 20 // - Test canceling an outstanding request. |
| 20 // - Test deleting ProxyScriptFetcher while a request is in progress. | 21 // - Test deleting ProxyScriptFetcher while a request is in progress. |
| 21 | 22 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 38 http_transaction_factory_ = | 39 http_transaction_factory_ = |
| 39 new net::HttpCache(net::HttpNetworkLayer::CreateFactory( | 40 new net::HttpCache(net::HttpNetworkLayer::CreateFactory( |
| 40 host_resolver_, proxy_service_, ssl_config_service_), | 41 host_resolver_, proxy_service_, ssl_config_service_), |
| 41 disk_cache::CreateInMemoryCacheBackend(0)); | 42 disk_cache::CreateInMemoryCacheBackend(0)); |
| 42 } | 43 } |
| 43 ~RequestContext() { | 44 ~RequestContext() { |
| 44 delete http_transaction_factory_; | 45 delete http_transaction_factory_; |
| 45 } | 46 } |
| 46 }; | 47 }; |
| 47 | 48 |
| 48 // Helper for doing synch fetches. This object lives in SynchFetcher's | |
| 49 // |io_thread_| and communicates with SynchFetcher though (|result|, |event|). | |
| 50 class SynchFetcherThreadHelper { | |
| 51 public: | |
| 52 SynchFetcherThreadHelper(base::WaitableEvent* event, FetchResult* result) | |
| 53 : event_(event), | |
| 54 fetch_result_(result), | |
| 55 url_request_context_(NULL), | |
| 56 fetcher_(NULL), | |
| 57 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 58 callback_(this, &SynchFetcherThreadHelper::OnFetchCompletion)) { | |
| 59 url_request_context_ = new RequestContext; | |
| 60 fetcher_.reset(net::ProxyScriptFetcher::Create(url_request_context_.get())); | |
| 61 } | |
| 62 | |
| 63 // Starts fetching the script at |url|. Upon completion |event_| will be | |
| 64 // signalled, and the bytes read will have been written to |fetch_result_|. | |
| 65 void Start(const GURL& url) { | |
| 66 int rv = fetcher_->Fetch(url, &fetch_result_->bytes, &callback_); | |
| 67 EXPECT_EQ(net::ERR_IO_PENDING, rv); | |
| 68 } | |
| 69 | |
| 70 void OnFetchCompletion(int result) { | |
| 71 fetch_result_->code = result; | |
| 72 event_->Signal(); | |
| 73 } | |
| 74 | |
| 75 private: | |
| 76 base::WaitableEvent* event_; | |
| 77 FetchResult* fetch_result_; | |
| 78 | |
| 79 scoped_refptr<URLRequestContext> url_request_context_; | |
| 80 | |
| 81 scoped_ptr<net::ProxyScriptFetcher> fetcher_; | |
| 82 net::CompletionCallbackImpl<SynchFetcherThreadHelper> callback_; | |
| 83 }; | |
| 84 | |
| 85 // Helper that wraps ProxyScriptFetcher::Fetch() with a synchronous interface. | |
| 86 // It executes Fetch() on a helper thread (IO_Thread). | |
| 87 class SynchFetcher { | |
| 88 public: | |
| 89 SynchFetcher() | |
| 90 : event_(false, false), | |
| 91 io_thread_("IO_Thread"), | |
| 92 thread_helper_(NULL) { | |
| 93 // Start an IO thread. | |
| 94 base::Thread::Options options; | |
| 95 options.message_loop_type = MessageLoop::TYPE_IO; | |
| 96 io_thread_.StartWithOptions(options); | |
| 97 | |
| 98 // Initialize the state in |io_thread_|. | |
| 99 io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 100 this, &SynchFetcher::Init)); | |
| 101 Wait(); | |
| 102 } | |
| 103 | |
| 104 ~SynchFetcher() { | |
| 105 // Tear down the state in |io_thread_|. | |
| 106 io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 107 this, &SynchFetcher::Cleanup)); | |
| 108 Wait(); | |
| 109 } | |
| 110 | |
| 111 // Synchronously fetch the url. | |
| 112 FetchResult Fetch(const GURL& url) { | |
| 113 io_thread_.message_loop()->PostTask(FROM_HERE, NewRunnableMethod( | |
| 114 this, &SynchFetcher::AsynchFetch, url)); | |
| 115 Wait(); | |
| 116 return fetch_result_; | |
| 117 } | |
| 118 | |
| 119 private: | |
| 120 // [Runs on |io_thread_|] Allocates the URLRequestContext and the | |
| 121 // ProxyScriptFetcher, which live inside |thread_helper_|. | |
| 122 void Init() { | |
| 123 thread_helper_ = new SynchFetcherThreadHelper(&event_, &fetch_result_); | |
| 124 event_.Signal(); | |
| 125 } | |
| 126 | |
| 127 // [Runs on |io_thread_|] Signals |event_| on completion. | |
| 128 void AsynchFetch(const GURL& url) { | |
| 129 thread_helper_->Start(url); | |
| 130 } | |
| 131 | |
| 132 // [Runs on |io_thread_|] Signals |event_| on cleanup completion. | |
| 133 void Cleanup() { | |
| 134 delete thread_helper_; | |
| 135 thread_helper_ = NULL; | |
| 136 MessageLoop::current()->RunAllPending(); | |
| 137 event_.Signal(); | |
| 138 } | |
| 139 | |
| 140 void Wait() { | |
| 141 event_.Wait(); | |
| 142 event_.Reset(); | |
| 143 } | |
| 144 | |
| 145 base::WaitableEvent event_; | |
| 146 base::Thread io_thread_; | |
| 147 FetchResult fetch_result_; | |
| 148 // Holds all the state that lives on the IO thread, for easy cleanup. | |
| 149 SynchFetcherThreadHelper* thread_helper_; | |
| 150 }; | |
| 151 | |
| 152 // Template specialization so SynchFetcher does not have to be refcounted. | |
| 153 template<> | |
| 154 void RunnableMethodTraits<SynchFetcher>::RetainCallee(SynchFetcher* remover) {} | |
| 155 template<> | |
| 156 void RunnableMethodTraits<SynchFetcher>::ReleaseCallee(SynchFetcher* remover) {} | |
| 157 | |
| 158 // Required to be in net namespace by FRIEND_TEST. | 49 // Required to be in net namespace by FRIEND_TEST. |
| 159 namespace net { | 50 namespace net { |
| 160 | 51 |
| 161 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest. | 52 // Get a file:// url relative to net/data/proxy/proxy_script_fetcher_unittest. |
| 162 GURL GetTestFileUrl(const std::string& relpath) { | 53 GURL GetTestFileUrl(const std::string& relpath) { |
| 163 FilePath path; | 54 FilePath path; |
| 164 PathService::Get(base::DIR_SOURCE_ROOT, &path); | 55 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 165 path = path.AppendASCII("net"); | 56 path = path.AppendASCII("net"); |
| 166 path = path.AppendASCII("data"); | 57 path = path.AppendASCII("data"); |
| 167 path = path.AppendASCII("proxy_script_fetcher_unittest"); | 58 path = path.AppendASCII("proxy_script_fetcher_unittest"); |
| 168 GURL base_url = net::FilePathToFileURL(path); | 59 GURL base_url = FilePathToFileURL(path); |
| 169 return GURL(base_url.spec() + "/" + relpath); | 60 return GURL(base_url.spec() + "/" + relpath); |
| 170 } | 61 } |
| 171 | 62 |
| 172 typedef PlatformTest ProxyScriptFetcherTest; | 63 typedef PlatformTest ProxyScriptFetcherTest; |
| 173 | 64 |
| 174 TEST_F(ProxyScriptFetcherTest, FileUrl) { | 65 TEST_F(ProxyScriptFetcherTest, FileUrl) { |
| 175 SynchFetcher pac_fetcher; | 66 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 67 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 68 ProxyScriptFetcher::Create(context)); |
| 176 | 69 |
| 177 { // Fetch a non-existent file. | 70 { // Fetch a non-existent file. |
| 178 FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist")); | 71 std::string bytes; |
| 179 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, result.code); | 72 TestCompletionCallback callback; |
| 180 EXPECT_TRUE(result.bytes.empty()); | 73 int result = pac_fetcher->Fetch(GetTestFileUrl("does-not-exist"), |
| 74 &bytes, &callback); |
| 75 EXPECT_EQ(ERR_IO_PENDING, result); |
| 76 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); |
| 77 EXPECT_TRUE(bytes.empty()); |
| 181 } | 78 } |
| 182 { // Fetch a file that exists. | 79 { // Fetch a file that exists. |
| 183 FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt")); | 80 std::string bytes; |
| 184 EXPECT_EQ(net::OK, result.code); | 81 TestCompletionCallback callback; |
| 185 EXPECT_EQ("-pac.txt-\n", result.bytes); | 82 int result = pac_fetcher->Fetch(GetTestFileUrl("pac.txt"), |
| 83 &bytes, &callback); |
| 84 EXPECT_EQ(ERR_IO_PENDING, result); |
| 85 EXPECT_EQ(OK, callback.WaitForResult()); |
| 86 EXPECT_EQ("-pac.txt-\n", bytes); |
| 186 } | 87 } |
| 187 } | 88 } |
| 188 | 89 |
| 189 // Note that all mime types are allowed for PAC file, to be consistent | 90 // Note that all mime types are allowed for PAC file, to be consistent |
| 190 // with other browsers. | 91 // with other browsers. |
| 191 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { | 92 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { |
| 192 scoped_refptr<HTTPTestServer> server = | 93 scoped_refptr<HTTPTestServer> server = |
| 193 HTTPTestServer::CreateServer(kDocRoot, NULL); | 94 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 194 ASSERT_TRUE(NULL != server.get()); | 95 ASSERT_TRUE(NULL != server.get()); |
| 195 SynchFetcher pac_fetcher; | 96 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 97 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 98 ProxyScriptFetcher::Create(context)); |
| 196 | 99 |
| 197 { // Fetch a PAC with mime type "text/plain" | 100 { // Fetch a PAC with mime type "text/plain" |
| 198 GURL url = server->TestServerPage("files/pac.txt"); | 101 GURL url = server->TestServerPage("files/pac.txt"); |
| 199 FetchResult result = pac_fetcher.Fetch(url); | 102 std::string bytes; |
| 200 EXPECT_EQ(net::OK, result.code); | 103 TestCompletionCallback callback; |
| 201 EXPECT_EQ("-pac.txt-\n", result.bytes); | 104 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 105 EXPECT_EQ(ERR_IO_PENDING, result); |
| 106 EXPECT_EQ(OK, callback.WaitForResult()); |
| 107 EXPECT_EQ("-pac.txt-\n", bytes); |
| 202 } | 108 } |
| 203 { // Fetch a PAC with mime type "text/html" | 109 { // Fetch a PAC with mime type "text/html" |
| 204 GURL url = server->TestServerPage("files/pac.html"); | 110 GURL url = server->TestServerPage("files/pac.html"); |
| 205 FetchResult result = pac_fetcher.Fetch(url); | 111 std::string bytes; |
| 206 EXPECT_EQ(net::OK, result.code); | 112 TestCompletionCallback callback; |
| 207 EXPECT_EQ("-pac.html-\n", result.bytes); | 113 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 114 EXPECT_EQ(ERR_IO_PENDING, result); |
| 115 EXPECT_EQ(OK, callback.WaitForResult()); |
| 116 EXPECT_EQ("-pac.html-\n", bytes); |
| 208 } | 117 } |
| 209 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" | 118 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" |
| 210 GURL url = server->TestServerPage("files/pac.nsproxy"); | 119 GURL url = server->TestServerPage("files/pac.nsproxy"); |
| 211 FetchResult result = pac_fetcher.Fetch(url); | 120 std::string bytes; |
| 212 EXPECT_EQ(net::OK, result.code); | 121 TestCompletionCallback callback; |
| 213 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 122 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 123 EXPECT_EQ(ERR_IO_PENDING, result); |
| 124 EXPECT_EQ(OK, callback.WaitForResult()); |
| 125 EXPECT_EQ("-pac.nsproxy-\n", bytes); |
| 214 } | 126 } |
| 215 } | 127 } |
| 216 | 128 |
| 217 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { | 129 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { |
| 218 scoped_refptr<HTTPTestServer> server = | 130 scoped_refptr<HTTPTestServer> server = |
| 219 HTTPTestServer::CreateServer(kDocRoot, NULL); | 131 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 220 ASSERT_TRUE(NULL != server.get()); | 132 ASSERT_TRUE(NULL != server.get()); |
| 221 SynchFetcher pac_fetcher; | 133 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 134 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 135 ProxyScriptFetcher::Create(context)); |
| 222 | 136 |
| 223 { // Fetch a PAC which gives a 500 -- FAIL | 137 { // Fetch a PAC which gives a 500 -- FAIL |
| 224 GURL url = server->TestServerPage("files/500.pac"); | 138 GURL url = server->TestServerPage("files/500.pac"); |
| 225 FetchResult result = pac_fetcher.Fetch(url); | 139 std::string bytes; |
| 226 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); | 140 TestCompletionCallback callback; |
| 227 EXPECT_TRUE(result.bytes.empty()); | 141 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 142 EXPECT_EQ(ERR_IO_PENDING, result); |
| 143 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
| 144 EXPECT_TRUE(bytes.empty()); |
| 228 } | 145 } |
| 229 { // Fetch a PAC which gives a 404 -- FAIL | 146 { // Fetch a PAC which gives a 404 -- FAIL |
| 230 GURL url = server->TestServerPage("files/404.pac"); | 147 GURL url = server->TestServerPage("files/404.pac"); |
| 231 FetchResult result = pac_fetcher.Fetch(url); | 148 std::string bytes; |
| 232 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); | 149 TestCompletionCallback callback; |
| 233 EXPECT_TRUE(result.bytes.empty()); | 150 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 151 EXPECT_EQ(ERR_IO_PENDING, result); |
| 152 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); |
| 153 EXPECT_TRUE(bytes.empty()); |
| 234 } | 154 } |
| 235 } | 155 } |
| 236 | 156 |
| 237 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { | 157 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { |
| 238 scoped_refptr<HTTPTestServer> server = | 158 scoped_refptr<HTTPTestServer> server = |
| 239 HTTPTestServer::CreateServer(kDocRoot, NULL); | 159 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 240 ASSERT_TRUE(NULL != server.get()); | 160 ASSERT_TRUE(NULL != server.get()); |
| 241 SynchFetcher pac_fetcher; | 161 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 162 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 163 ProxyScriptFetcher::Create(context)); |
| 242 | 164 |
| 243 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 165 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
| 244 // have no effect. | 166 // have no effect. |
| 245 GURL url = server->TestServerPage("files/downloadable.pac"); | 167 GURL url = server->TestServerPage("files/downloadable.pac"); |
| 246 FetchResult result = pac_fetcher.Fetch(url); | 168 std::string bytes; |
| 247 EXPECT_EQ(net::OK, result.code); | 169 TestCompletionCallback callback; |
| 248 EXPECT_EQ("-downloadable.pac-\n", result.bytes); | 170 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 171 EXPECT_EQ(ERR_IO_PENDING, result); |
| 172 EXPECT_EQ(OK, callback.WaitForResult()); |
| 173 EXPECT_EQ("-downloadable.pac-\n", bytes); |
| 249 } | 174 } |
| 250 | 175 |
| 251 TEST_F(ProxyScriptFetcherTest, NoCache) { | 176 TEST_F(ProxyScriptFetcherTest, NoCache) { |
| 252 scoped_refptr<HTTPTestServer> server = | 177 scoped_refptr<HTTPTestServer> server = |
| 253 HTTPTestServer::CreateServer(kDocRoot, NULL); | 178 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 254 ASSERT_TRUE(NULL != server.get()); | 179 ASSERT_TRUE(NULL != server.get()); |
| 255 SynchFetcher pac_fetcher; | 180 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 181 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 182 ProxyScriptFetcher::Create(context)); |
| 256 | 183 |
| 257 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. | 184 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. |
| 258 GURL url = server->TestServerPage("files/cacheable_1hr.pac"); | 185 GURL url = server->TestServerPage("files/cacheable_1hr.pac"); |
| 259 FetchResult result = pac_fetcher.Fetch(url); | 186 { |
| 260 EXPECT_EQ(net::OK, result.code); | 187 std::string bytes; |
| 261 EXPECT_EQ("-cacheable_1hr.pac-\n", result.bytes); | 188 TestCompletionCallback callback; |
| 189 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 190 EXPECT_EQ(ERR_IO_PENDING, result); |
| 191 EXPECT_EQ(OK, callback.WaitForResult()); |
| 192 EXPECT_EQ("-cacheable_1hr.pac-\n", bytes); |
| 193 } |
| 262 | 194 |
| 263 // Now kill the HTTP server. | 195 // Now kill the HTTP server. |
| 264 server->SendQuit(); | 196 server->SendQuit(); |
| 265 EXPECT_TRUE(server->WaitToFinish(20000)); | 197 EXPECT_TRUE(server->WaitToFinish(20000)); |
| 266 server = NULL; | 198 server = NULL; |
| 267 | 199 |
| 268 // Try to fetch the file again -- if should fail, since the server is not | 200 // Try to fetch the file again -- if should fail, since the server is not |
| 269 // running anymore. (If it were instead being loaded from cache, we would | 201 // running anymore. (If it were instead being loaded from cache, we would |
| 270 // get a success. | 202 // get a success. |
| 271 result = pac_fetcher.Fetch(url); | 203 { |
| 272 EXPECT_EQ(net::ERR_CONNECTION_REFUSED, result.code); | 204 std::string bytes; |
| 205 TestCompletionCallback callback; |
| 206 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 207 EXPECT_EQ(ERR_IO_PENDING, result); |
| 208 EXPECT_EQ(ERR_CONNECTION_REFUSED, callback.WaitForResult()); |
| 209 } |
| 273 } | 210 } |
| 274 | 211 |
| 275 TEST_F(ProxyScriptFetcherTest, TooLarge) { | 212 TEST_F(ProxyScriptFetcherTest, TooLarge) { |
| 276 scoped_refptr<HTTPTestServer> server = | 213 scoped_refptr<HTTPTestServer> server = |
| 277 HTTPTestServer::CreateServer(kDocRoot, NULL); | 214 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 278 ASSERT_TRUE(NULL != server.get()); | 215 ASSERT_TRUE(NULL != server.get()); |
| 279 SynchFetcher pac_fetcher; | 216 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 217 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 218 ProxyScriptFetcher::Create(context)); |
| 280 | 219 |
| 281 // Set the maximum response size to 50 bytes. | 220 // Set the maximum response size to 50 bytes. |
| 282 int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50); | 221 int prev_size = ProxyScriptFetcher::SetSizeConstraintForUnittest(50); |
| 283 | 222 |
| 284 // These two URLs are the same file, but are http:// vs file:// | 223 // These two URLs are the same file, but are http:// vs file:// |
| 285 GURL urls[] = { | 224 GURL urls[] = { |
| 286 server->TestServerPage("files/large-pac.nsproxy"), | 225 server->TestServerPage("files/large-pac.nsproxy"), |
| 287 GetTestFileUrl("large-pac.nsproxy") | 226 GetTestFileUrl("large-pac.nsproxy") |
| 288 }; | 227 }; |
| 289 | 228 |
| 290 // Try fetching URLs that are 101 bytes large. We should abort the request | 229 // Try fetching URLs that are 101 bytes large. We should abort the request |
| 291 // after 50 bytes have been read, and fail with a too large error. | 230 // after 50 bytes have been read, and fail with a too large error. |
| 292 for (size_t i = 0; i < arraysize(urls); ++i) { | 231 for (size_t i = 0; i < arraysize(urls); ++i) { |
| 293 const GURL& url = urls[i]; | 232 const GURL& url = urls[i]; |
| 294 FetchResult result = pac_fetcher.Fetch(url); | 233 std::string bytes; |
| 295 EXPECT_EQ(net::ERR_FILE_TOO_BIG, result.code); | 234 TestCompletionCallback callback; |
| 296 EXPECT_TRUE(result.bytes.empty()); | 235 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 236 EXPECT_EQ(ERR_IO_PENDING, result); |
| 237 EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult()); |
| 238 EXPECT_TRUE(bytes.empty()); |
| 297 } | 239 } |
| 298 | 240 |
| 299 // Restore the original size bound. | 241 // Restore the original size bound. |
| 300 net::ProxyScriptFetcher::SetSizeConstraintForUnittest(prev_size); | 242 ProxyScriptFetcher::SetSizeConstraintForUnittest(prev_size); |
| 301 | 243 |
| 302 { // Make sure we can still fetch regular URLs. | 244 { // Make sure we can still fetch regular URLs. |
| 303 GURL url = server->TestServerPage("files/pac.nsproxy"); | 245 GURL url = server->TestServerPage("files/pac.nsproxy"); |
| 304 FetchResult result = pac_fetcher.Fetch(url); | 246 std::string bytes; |
| 305 EXPECT_EQ(net::OK, result.code); | 247 TestCompletionCallback callback; |
| 306 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 248 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 249 EXPECT_EQ(ERR_IO_PENDING, result); |
| 250 EXPECT_EQ(OK, callback.WaitForResult()); |
| 251 EXPECT_EQ("-pac.nsproxy-\n", bytes); |
| 307 } | 252 } |
| 308 } | 253 } |
| 309 | 254 |
| 310 TEST_F(ProxyScriptFetcherTest, Hang) { | 255 TEST_F(ProxyScriptFetcherTest, Hang) { |
| 311 scoped_refptr<HTTPTestServer> server = | 256 scoped_refptr<HTTPTestServer> server = |
| 312 HTTPTestServer::CreateServer(kDocRoot, NULL); | 257 HTTPTestServer::CreateServer(kDocRoot, NULL); |
| 313 ASSERT_TRUE(NULL != server.get()); | 258 ASSERT_TRUE(NULL != server.get()); |
| 314 SynchFetcher pac_fetcher; | 259 scoped_refptr<URLRequestContext> context = new RequestContext; |
| 260 scoped_ptr<ProxyScriptFetcher> pac_fetcher( |
| 261 ProxyScriptFetcher::Create(context)); |
| 315 | 262 |
| 316 // Set the timeout period to 0.5 seconds. | 263 // Set the timeout period to 0.5 seconds. |
| 317 int prev_timeout = | 264 int prev_timeout = |
| 318 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); | 265 ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); |
| 319 | 266 |
| 320 // Try fetching a URL which takes 1.2 seconds. We should abort the request | 267 // Try fetching a URL which takes 1.2 seconds. We should abort the request |
| 321 // after 500 ms, and fail with a timeout error. | 268 // after 500 ms, and fail with a timeout error. |
| 322 { GURL url = server->TestServerPage("slow/proxy.pac?1.2"); | 269 { GURL url = server->TestServerPage("slow/proxy.pac?1.2"); |
| 323 FetchResult result = pac_fetcher.Fetch(url); | 270 std::string bytes; |
| 324 EXPECT_EQ(net::ERR_TIMED_OUT, result.code); | 271 TestCompletionCallback callback; |
| 325 EXPECT_TRUE(result.bytes.empty()); | 272 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 273 EXPECT_EQ(ERR_IO_PENDING, result); |
| 274 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult()); |
| 275 EXPECT_TRUE(bytes.empty()); |
| 326 } | 276 } |
| 327 | 277 |
| 328 // Restore the original timeout period. | 278 // Restore the original timeout period. |
| 329 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(prev_timeout); | 279 ProxyScriptFetcher::SetTimeoutConstraintForUnittest(prev_timeout); |
| 330 | 280 |
| 331 { // Make sure we can still fetch regular URLs. | 281 { // Make sure we can still fetch regular URLs. |
| 332 GURL url = server->TestServerPage("files/pac.nsproxy"); | 282 GURL url = server->TestServerPage("files/pac.nsproxy"); |
| 333 FetchResult result = pac_fetcher.Fetch(url); | 283 std::string bytes; |
| 334 EXPECT_EQ(net::OK, result.code); | 284 TestCompletionCallback callback; |
| 335 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 285 int result = pac_fetcher->Fetch(url, &bytes, &callback); |
| 286 EXPECT_EQ(ERR_IO_PENDING, result); |
| 287 EXPECT_EQ(OK, callback.WaitForResult()); |
| 288 EXPECT_EQ("-pac.nsproxy-\n", bytes); |
| 336 } | 289 } |
| 337 } | 290 } |
| 338 | 291 |
| 339 } // namespace net | 292 } // namespace net |
| OLD | NEW |