| 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 "content/common/net/url_fetcher.h" | 5 #include "content/common/net/url_fetcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop_proxy.h" | 8 #include "base/message_loop_proxy.h" |
| 9 #include "base/synchronization/waitable_event.h" | 9 #include "base/synchronization/waitable_event.h" |
| 10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 URLFetcherTest() : fetcher_(NULL) { } | 64 URLFetcherTest() : fetcher_(NULL) { } |
| 65 | 65 |
| 66 static int GetNumFetcherCores() { | 66 static int GetNumFetcherCores() { |
| 67 return URLFetcher::GetNumFetcherCores(); | 67 return URLFetcher::GetNumFetcherCores(); |
| 68 } | 68 } |
| 69 | 69 |
| 70 // Creates a URLFetcher, using the program's main thread to do IO. | 70 // Creates a URLFetcher, using the program's main thread to do IO. |
| 71 virtual void CreateFetcher(const GURL& url); | 71 virtual void CreateFetcher(const GURL& url); |
| 72 | 72 |
| 73 // content::URLFetcherDelegate | 73 // content::URLFetcherDelegate |
| 74 virtual void OnURLFetchComplete(const URLFetcher* source); | 74 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 75 | 75 |
| 76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { | 76 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy() { |
| 77 return io_message_loop_proxy_; | 77 return io_message_loop_proxy_; |
| 78 } | 78 } |
| 79 | 79 |
| 80 protected: | 80 protected: |
| 81 virtual void SetUp() { | 81 virtual void SetUp() { |
| 82 testing::Test::SetUp(); | 82 testing::Test::SetUp(); |
| 83 | 83 |
| 84 io_message_loop_proxy_ = base::MessageLoopProxy::current(); | 84 io_message_loop_proxy_ = base::MessageLoopProxy::current(); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 100 // dispatches its requests to. When we wish to simulate being used from | 100 // dispatches its requests to. When we wish to simulate being used from |
| 101 // a UI thread, we dispatch a worker thread to do so. | 101 // a UI thread, we dispatch a worker thread to do so. |
| 102 MessageLoopForIO io_loop_; | 102 MessageLoopForIO io_loop_; |
| 103 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 103 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 104 | 104 |
| 105 URLFetcher* fetcher_; | 105 URLFetcher* fetcher_; |
| 106 }; | 106 }; |
| 107 | 107 |
| 108 void URLFetcherTest::CreateFetcher(const GURL& url) { | 108 void URLFetcherTest::CreateFetcher(const GURL& url) { |
| 109 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 109 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 110 fetcher_->set_request_context(new TestURLRequestContextGetter( | 110 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 111 io_message_loop_proxy())); | 111 io_message_loop_proxy())); |
| 112 fetcher_->Start(); | 112 fetcher_->Start(); |
| 113 } | 113 } |
| 114 | 114 |
| 115 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source) { | 115 void URLFetcherTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| 116 EXPECT_TRUE(source->status().is_success()); | 116 EXPECT_TRUE(source->GetStatus().is_success()); |
| 117 EXPECT_EQ(200, source->response_code()); // HTTP OK | 117 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| 118 | 118 |
| 119 std::string data; | 119 std::string data; |
| 120 EXPECT_TRUE(source->GetResponseAsString(&data)); | 120 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 121 EXPECT_FALSE(data.empty()); | 121 EXPECT_FALSE(data.empty()); |
| 122 | 122 |
| 123 delete fetcher_; // Have to delete this here and not in the destructor, | 123 delete fetcher_; // Have to delete this here and not in the destructor, |
| 124 // because the destructor won't necessarily run on the | 124 // because the destructor won't necessarily run on the |
| 125 // same thread that CreateFetcher() did. | 125 // same thread that CreateFetcher() did. |
| 126 | 126 |
| 127 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 127 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 128 // If the current message loop is not the IO loop, it will be shut down when | 128 // If the current message loop is not the IO loop, it will be shut down when |
| 129 // the main loop returns and this thread subsequently goes out of scope. | 129 // the main loop returns and this thread subsequently goes out of scope. |
| 130 } | 130 } |
| 131 | 131 |
| 132 namespace { | 132 namespace { |
| 133 | 133 |
| 134 // Version of URLFetcherTest that does a POST instead | 134 // Version of URLFetcherTest that does a POST instead |
| 135 class URLFetcherPostTest : public URLFetcherTest { | 135 class URLFetcherPostTest : public URLFetcherTest { |
| 136 public: | 136 public: |
| 137 virtual void CreateFetcher(const GURL& url); | 137 virtual void CreateFetcher(const GURL& url); |
| 138 | 138 |
| 139 // content::URLFetcherDelegate | 139 // content::URLFetcherDelegate |
| 140 virtual void OnURLFetchComplete(const URLFetcher* source); | 140 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 141 }; | 141 }; |
| 142 | 142 |
| 143 // Version of URLFetcherTest that tests headers. | 143 // Version of URLFetcherTest that tests headers. |
| 144 class URLFetcherHeadersTest : public URLFetcherTest { | 144 class URLFetcherHeadersTest : public URLFetcherTest { |
| 145 public: | 145 public: |
| 146 // content::URLFetcherDelegate | 146 // content::URLFetcherDelegate |
| 147 virtual void OnURLFetchComplete(const URLFetcher* source); | 147 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 148 }; | 148 }; |
| 149 | 149 |
| 150 // Version of URLFetcherTest that tests SocketAddress. | 150 // Version of URLFetcherTest that tests SocketAddress. |
| 151 class URLFetcherSocketAddressTest : public URLFetcherTest { | 151 class URLFetcherSocketAddressTest : public URLFetcherTest { |
| 152 public: | 152 public: |
| 153 // content::URLFetcherDelegate | 153 // content::URLFetcherDelegate |
| 154 virtual void OnURLFetchComplete(const URLFetcher* source); | 154 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 155 protected: | 155 protected: |
| 156 std::string expected_host_; | 156 std::string expected_host_; |
| 157 uint16 expected_port_; | 157 uint16 expected_port_; |
| 158 }; | 158 }; |
| 159 | 159 |
| 160 // Version of URLFetcherTest that tests overload protection. | 160 // Version of URLFetcherTest that tests overload protection. |
| 161 class URLFetcherProtectTest : public URLFetcherTest { | 161 class URLFetcherProtectTest : public URLFetcherTest { |
| 162 public: | 162 public: |
| 163 virtual void CreateFetcher(const GURL& url); | 163 virtual void CreateFetcher(const GURL& url); |
| 164 // content::URLFetcherDelegate | 164 // content::URLFetcherDelegate |
| 165 virtual void OnURLFetchComplete(const URLFetcher* source); | 165 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 166 private: | 166 private: |
| 167 Time start_time_; | 167 Time start_time_; |
| 168 }; | 168 }; |
| 169 | 169 |
| 170 // Version of URLFetcherTest that tests overload protection, when responses | 170 // Version of URLFetcherTest that tests overload protection, when responses |
| 171 // passed through. | 171 // passed through. |
| 172 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { | 172 class URLFetcherProtectTestPassedThrough : public URLFetcherTest { |
| 173 public: | 173 public: |
| 174 virtual void CreateFetcher(const GURL& url); | 174 virtual void CreateFetcher(const GURL& url); |
| 175 // content::URLFetcherDelegate | 175 // content::URLFetcherDelegate |
| 176 virtual void OnURLFetchComplete(const URLFetcher* source); | 176 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 177 private: | 177 private: |
| 178 Time start_time_; | 178 Time start_time_; |
| 179 }; | 179 }; |
| 180 | 180 |
| 181 // Version of URLFetcherTest that tests bad HTTPS requests. | 181 // Version of URLFetcherTest that tests bad HTTPS requests. |
| 182 class URLFetcherBadHTTPSTest : public URLFetcherTest { | 182 class URLFetcherBadHTTPSTest : public URLFetcherTest { |
| 183 public: | 183 public: |
| 184 URLFetcherBadHTTPSTest(); | 184 URLFetcherBadHTTPSTest(); |
| 185 | 185 |
| 186 // content::URLFetcherDelegate | 186 // content::URLFetcherDelegate |
| 187 virtual void OnURLFetchComplete(const URLFetcher* source); | 187 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 188 | 188 |
| 189 private: | 189 private: |
| 190 FilePath cert_dir_; | 190 FilePath cert_dir_; |
| 191 }; | 191 }; |
| 192 | 192 |
| 193 // Version of URLFetcherTest that tests request cancellation on shutdown. | 193 // Version of URLFetcherTest that tests request cancellation on shutdown. |
| 194 class URLFetcherCancelTest : public URLFetcherTest { | 194 class URLFetcherCancelTest : public URLFetcherTest { |
| 195 public: | 195 public: |
| 196 virtual void CreateFetcher(const GURL& url); | 196 virtual void CreateFetcher(const GURL& url); |
| 197 // content::URLFetcherDelegate | 197 // content::URLFetcherDelegate |
| 198 virtual void OnURLFetchComplete(const URLFetcher* source); | 198 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 199 | 199 |
| 200 void CancelRequest(); | 200 void CancelRequest(); |
| 201 }; | 201 }; |
| 202 | 202 |
| 203 // Version of TestURLRequestContext that posts a Quit task to the IO | 203 // Version of TestURLRequestContext that posts a Quit task to the IO |
| 204 // thread once it is deleted. | 204 // thread once it is deleted. |
| 205 class CancelTestURLRequestContext : public TestURLRequestContext { | 205 class CancelTestURLRequestContext : public TestURLRequestContext { |
| 206 virtual ~CancelTestURLRequestContext() { | 206 virtual ~CancelTestURLRequestContext() { |
| 207 // The d'tor should execute in the IO thread. Post the quit task to the | 207 // The d'tor should execute in the IO thread. Post the quit task to the |
| 208 // current thread. | 208 // current thread. |
| (...skipping 27 matching lines...) Expand all Loading... |
| 236 | 236 |
| 237 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; | 237 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; |
| 238 base::WaitableEvent context_created_; | 238 base::WaitableEvent context_created_; |
| 239 scoped_refptr<net::URLRequestContext> context_; | 239 scoped_refptr<net::URLRequestContext> context_; |
| 240 }; | 240 }; |
| 241 | 241 |
| 242 // Version of URLFetcherTest that tests retying the same request twice. | 242 // Version of URLFetcherTest that tests retying the same request twice. |
| 243 class URLFetcherMultipleAttemptTest : public URLFetcherTest { | 243 class URLFetcherMultipleAttemptTest : public URLFetcherTest { |
| 244 public: | 244 public: |
| 245 // content::URLFetcherDelegate | 245 // content::URLFetcherDelegate |
| 246 virtual void OnURLFetchComplete(const URLFetcher* source); | 246 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 247 private: | 247 private: |
| 248 std::string data_; | 248 std::string data_; |
| 249 }; | 249 }; |
| 250 | 250 |
| 251 class URLFetcherTempFileTest : public URLFetcherTest { | 251 class URLFetcherTempFileTest : public URLFetcherTest { |
| 252 public: | 252 public: |
| 253 URLFetcherTempFileTest() | 253 URLFetcherTempFileTest() |
| 254 : take_ownership_of_temp_file_(false) { | 254 : take_ownership_of_temp_file_(false) { |
| 255 } | 255 } |
| 256 | 256 |
| 257 // content::URLFetcherDelegate | 257 // content::URLFetcherDelegate |
| 258 virtual void OnURLFetchComplete(const URLFetcher* source); | 258 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 259 | 259 |
| 260 virtual void CreateFetcher(const GURL& url); | 260 virtual void CreateFetcher(const GURL& url); |
| 261 | 261 |
| 262 protected: | 262 protected: |
| 263 FilePath expected_file_; | 263 FilePath expected_file_; |
| 264 FilePath temp_file_; | 264 FilePath temp_file_; |
| 265 | 265 |
| 266 // Set by the test. Used in OnURLFetchComplete() to decide if | 266 // Set by the test. Used in OnURLFetchComplete() to decide if |
| 267 // the URLFetcher should own the temp file, so that we can test | 267 // the URLFetcher should own the temp file, so that we can test |
| 268 // disowning prevents the file from being deleted. | 268 // disowning prevents the file from being deleted. |
| 269 bool take_ownership_of_temp_file_; | 269 bool take_ownership_of_temp_file_; |
| 270 }; | 270 }; |
| 271 | 271 |
| 272 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { | 272 void URLFetcherTempFileTest::CreateFetcher(const GURL& url) { |
| 273 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 273 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 274 fetcher_->set_request_context(new TestURLRequestContextGetter( | 274 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 275 io_message_loop_proxy())); | 275 io_message_loop_proxy())); |
| 276 | 276 |
| 277 // Use the IO message loop to do the file operations in this test. | 277 // Use the IO message loop to do the file operations in this test. |
| 278 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); | 278 fetcher_->SaveResponseToTemporaryFile(io_message_loop_proxy()); |
| 279 fetcher_->Start(); | 279 fetcher_->Start(); |
| 280 } | 280 } |
| 281 | 281 |
| 282 TEST_F(URLFetcherTempFileTest, SmallGet) { | 282 TEST_F(URLFetcherTempFileTest, SmallGet) { |
| 283 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); | 283 net::TestServer test_server(net::TestServer::TYPE_HTTP, FilePath(kDocRoot)); |
| 284 ASSERT_TRUE(test_server.Start()); | 284 ASSERT_TRUE(test_server.Start()); |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 339 test_->CreateFetcher(url_); | 339 test_->CreateFetcher(url_); |
| 340 } | 340 } |
| 341 | 341 |
| 342 private: | 342 private: |
| 343 URLFetcherTest* test_; | 343 URLFetcherTest* test_; |
| 344 GURL url_; | 344 GURL url_; |
| 345 }; | 345 }; |
| 346 | 346 |
| 347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 347 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
| 348 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); | 348 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); |
| 349 fetcher_->set_request_context(new TestURLRequestContextGetter( | 349 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 350 io_message_loop_proxy())); | 350 io_message_loop_proxy())); |
| 351 fetcher_->set_upload_data("application/x-www-form-urlencoded", | 351 fetcher_->SetUploadData("application/x-www-form-urlencoded", |
| 352 "bobsyeruncle"); | 352 "bobsyeruncle"); |
| 353 fetcher_->Start(); | 353 fetcher_->Start(); |
| 354 } | 354 } |
| 355 | 355 |
| 356 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source) { | 356 void URLFetcherPostTest::OnURLFetchComplete(const content::URLFetcher* source) { |
| 357 std::string data; | 357 std::string data; |
| 358 EXPECT_TRUE(source->GetResponseAsString(&data)); | 358 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 359 EXPECT_EQ(std::string("bobsyeruncle"), data); | 359 EXPECT_EQ(std::string("bobsyeruncle"), data); |
| 360 URLFetcherTest::OnURLFetchComplete(source); | 360 URLFetcherTest::OnURLFetchComplete(source); |
| 361 } | 361 } |
| 362 | 362 |
| 363 void URLFetcherHeadersTest::OnURLFetchComplete(const URLFetcher* source) { | 363 void URLFetcherHeadersTest::OnURLFetchComplete(const content::URLFetcher* source
) { |
| 364 std::string header; | 364 std::string header; |
| 365 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", | 365 EXPECT_TRUE(source->GetResponseHeaders()->GetNormalizedHeader("cache-control", |
| 366 &header)); | 366 &header)); |
| 367 EXPECT_EQ("private", header); | 367 EXPECT_EQ("private", header); |
| 368 URLFetcherTest::OnURLFetchComplete(source); | 368 URLFetcherTest::OnURLFetchComplete(source); |
| 369 } | 369 } |
| 370 | 370 |
| 371 void URLFetcherSocketAddressTest::OnURLFetchComplete(const URLFetcher* source) { | 371 void URLFetcherSocketAddressTest::OnURLFetchComplete( |
| 372 EXPECT_EQ("127.0.0.1", source->socket_address().host()); | 372 const content::URLFetcher* source) { |
| 373 EXPECT_EQ(expected_port_, source->socket_address().port()); | 373 EXPECT_EQ("127.0.0.1", source->GetSocketAddress().host()); |
| 374 EXPECT_EQ(expected_port_, source->GetSocketAddress().port()); |
| 374 URLFetcherTest::OnURLFetchComplete(source); | 375 URLFetcherTest::OnURLFetchComplete(source); |
| 375 } | 376 } |
| 376 | 377 |
| 377 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 378 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
| 378 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 379 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 379 fetcher_->set_request_context(new TestURLRequestContextGetter( | 380 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 380 io_message_loop_proxy())); | 381 io_message_loop_proxy())); |
| 381 start_time_ = Time::Now(); | 382 start_time_ = Time::Now(); |
| 382 fetcher_->set_max_retries(11); | 383 fetcher_->SetMaxRetries(11); |
| 383 fetcher_->Start(); | 384 fetcher_->Start(); |
| 384 } | 385 } |
| 385 | 386 |
| 386 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source) { | 387 void URLFetcherProtectTest::OnURLFetchComplete( |
| 388 const content::URLFetcher* source) { |
| 387 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 389 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
| 388 if (source->response_code() >= 500) { | 390 if (source->GetResponseCode() >= 500) { |
| 389 // Now running ServerUnavailable test. | 391 // Now running ServerUnavailable test. |
| 390 // It takes more than 1 second to finish all 11 requests. | 392 // It takes more than 1 second to finish all 11 requests. |
| 391 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 393 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
| 392 EXPECT_TRUE(source->status().is_success()); | 394 EXPECT_TRUE(source->GetStatus().is_success()); |
| 393 std::string data; | 395 std::string data; |
| 394 EXPECT_TRUE(source->GetResponseAsString(&data)); | 396 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 395 EXPECT_FALSE(data.empty()); | 397 EXPECT_FALSE(data.empty()); |
| 396 delete fetcher_; | 398 delete fetcher_; |
| 397 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 399 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 398 } else { | 400 } else { |
| 399 // Now running Overload test. | 401 // Now running Overload test. |
| 400 static int count = 0; | 402 static int count = 0; |
| 401 count++; | 403 count++; |
| 402 if (count < 20) { | 404 if (count < 20) { |
| 403 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( | 405 fetcher_->StartWithRequestContextGetter(new TestURLRequestContextGetter( |
| 404 io_message_loop_proxy())); | 406 io_message_loop_proxy())); |
| 405 } else { | 407 } else { |
| 406 // We have already sent 20 requests continuously. And we expect that | 408 // We have already sent 20 requests continuously. And we expect that |
| 407 // it takes more than 1 second due to the overload protection settings. | 409 // it takes more than 1 second due to the overload protection settings. |
| 408 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 410 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
| 409 URLFetcherTest::OnURLFetchComplete(source); | 411 URLFetcherTest::OnURLFetchComplete(source); |
| 410 } | 412 } |
| 411 } | 413 } |
| 412 } | 414 } |
| 413 | 415 |
| 414 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { | 416 void URLFetcherProtectTestPassedThrough::CreateFetcher(const GURL& url) { |
| 415 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 417 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 416 fetcher_->set_request_context(new TestURLRequestContextGetter( | 418 fetcher_->SetRequestContext(new TestURLRequestContextGetter( |
| 417 io_message_loop_proxy())); | 419 io_message_loop_proxy())); |
| 418 fetcher_->set_automatically_retry_on_5xx(false); | 420 fetcher_->SetAutomaticallyRetryOn5xx(false); |
| 419 start_time_ = Time::Now(); | 421 start_time_ = Time::Now(); |
| 420 fetcher_->set_max_retries(11); | 422 fetcher_->SetMaxRetries(11); |
| 421 fetcher_->Start(); | 423 fetcher_->Start(); |
| 422 } | 424 } |
| 423 | 425 |
| 424 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( | 426 void URLFetcherProtectTestPassedThrough::OnURLFetchComplete( |
| 425 const URLFetcher* source) { | 427 const content::URLFetcher* source) { |
| 426 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); | 428 const TimeDelta one_minute = TimeDelta::FromMilliseconds(60000); |
| 427 if (source->response_code() >= 500) { | 429 if (source->GetResponseCode() >= 500) { |
| 428 // Now running ServerUnavailable test. | 430 // Now running ServerUnavailable test. |
| 429 // It should get here on the first attempt, so almost immediately and | 431 // It should get here on the first attempt, so almost immediately and |
| 430 // *not* to attempt to execute all 11 requests (2.5 minutes). | 432 // *not* to attempt to execute all 11 requests (2.5 minutes). |
| 431 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); | 433 EXPECT_TRUE(Time::Now() - start_time_ < one_minute); |
| 432 EXPECT_TRUE(source->status().is_success()); | 434 EXPECT_TRUE(source->GetStatus().is_success()); |
| 433 // Check that suggested back off time is bigger than 0. | 435 // Check that suggested back off time is bigger than 0. |
| 434 EXPECT_GT(fetcher_->backoff_delay().InMicroseconds(), 0); | 436 EXPECT_GT(fetcher_->GetBackoffDelay().InMicroseconds(), 0); |
| 435 std::string data; | 437 std::string data; |
| 436 EXPECT_TRUE(source->GetResponseAsString(&data)); | 438 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 437 EXPECT_FALSE(data.empty()); | 439 EXPECT_FALSE(data.empty()); |
| 438 } else { | 440 } else { |
| 439 // We should not get here! | 441 // We should not get here! |
| 440 ADD_FAILURE(); | 442 ADD_FAILURE(); |
| 441 } | 443 } |
| 442 | 444 |
| 443 delete fetcher_; | 445 delete fetcher_; |
| 444 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 446 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 445 } | 447 } |
| 446 | 448 |
| 447 | 449 |
| 448 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { | 450 URLFetcherBadHTTPSTest::URLFetcherBadHTTPSTest() { |
| 449 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); | 451 PathService::Get(base::DIR_SOURCE_ROOT, &cert_dir_); |
| 450 cert_dir_ = cert_dir_.AppendASCII("chrome"); | 452 cert_dir_ = cert_dir_.AppendASCII("chrome"); |
| 451 cert_dir_ = cert_dir_.AppendASCII("test"); | 453 cert_dir_ = cert_dir_.AppendASCII("test"); |
| 452 cert_dir_ = cert_dir_.AppendASCII("data"); | 454 cert_dir_ = cert_dir_.AppendASCII("data"); |
| 453 cert_dir_ = cert_dir_.AppendASCII("ssl"); | 455 cert_dir_ = cert_dir_.AppendASCII("ssl"); |
| 454 cert_dir_ = cert_dir_.AppendASCII("certificates"); | 456 cert_dir_ = cert_dir_.AppendASCII("certificates"); |
| 455 } | 457 } |
| 456 | 458 |
| 457 // The "server certificate expired" error should result in automatic | 459 // The "server certificate expired" error should result in automatic |
| 458 // cancellation of the request by | 460 // cancellation of the request by |
| 459 // net::URLRequest::Delegate::OnSSLCertificateError. | 461 // net::URLRequest::Delegate::OnSSLCertificateError. |
| 460 void URLFetcherBadHTTPSTest::OnURLFetchComplete(const URLFetcher* source) { | 462 void URLFetcherBadHTTPSTest::OnURLFetchComplete( |
| 463 const content::URLFetcher* source) { |
| 461 // This part is different from URLFetcherTest::OnURLFetchComplete | 464 // This part is different from URLFetcherTest::OnURLFetchComplete |
| 462 // because this test expects the request to be cancelled. | 465 // because this test expects the request to be cancelled. |
| 463 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->status().status()); | 466 EXPECT_EQ(net::URLRequestStatus::CANCELED, source->GetStatus().status()); |
| 464 EXPECT_EQ(net::ERR_ABORTED, source->status().error()); | 467 EXPECT_EQ(net::ERR_ABORTED, source->GetStatus().error()); |
| 465 EXPECT_EQ(-1, source->response_code()); | 468 EXPECT_EQ(-1, source->GetResponseCode()); |
| 466 EXPECT_TRUE(source->cookies().empty()); | 469 EXPECT_TRUE(source->GetCookies().empty()); |
| 467 std::string data; | 470 std::string data; |
| 468 EXPECT_TRUE(source->GetResponseAsString(&data)); | 471 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 469 EXPECT_TRUE(data.empty()); | 472 EXPECT_TRUE(data.empty()); |
| 470 | 473 |
| 471 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 474 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
| 472 delete fetcher_; | 475 delete fetcher_; |
| 473 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 476 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 474 } | 477 } |
| 475 | 478 |
| 476 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 479 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
| 477 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 480 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
| 478 CancelTestURLRequestContextGetter* context_getter = | 481 CancelTestURLRequestContextGetter* context_getter = |
| 479 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); | 482 new CancelTestURLRequestContextGetter(io_message_loop_proxy()); |
| 480 fetcher_->set_request_context(context_getter); | 483 fetcher_->SetRequestContext(context_getter); |
| 481 fetcher_->set_max_retries(2); | 484 fetcher_->SetMaxRetries(2); |
| 482 fetcher_->Start(); | 485 fetcher_->Start(); |
| 483 // We need to wait for the creation of the net::URLRequestContext, since we | 486 // We need to wait for the creation of the net::URLRequestContext, since we |
| 484 // rely on it being destroyed as a signal to end the test. | 487 // rely on it being destroyed as a signal to end the test. |
| 485 context_getter->WaitForContextCreation(); | 488 context_getter->WaitForContextCreation(); |
| 486 CancelRequest(); | 489 CancelRequest(); |
| 487 } | 490 } |
| 488 | 491 |
| 489 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source) { | 492 void URLFetcherCancelTest::OnURLFetchComplete( |
| 493 const content::URLFetcher* source) { |
| 490 // We should have cancelled the request before completion. | 494 // We should have cancelled the request before completion. |
| 491 ADD_FAILURE(); | 495 ADD_FAILURE(); |
| 492 delete fetcher_; | 496 delete fetcher_; |
| 493 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 497 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 494 } | 498 } |
| 495 | 499 |
| 496 void URLFetcherCancelTest::CancelRequest() { | 500 void URLFetcherCancelTest::CancelRequest() { |
| 497 delete fetcher_; | 501 delete fetcher_; |
| 498 // The URLFetcher's test context will post a Quit task once it is | 502 // The URLFetcher's test context will post a Quit task once it is |
| 499 // deleted. So if this test simply hangs, it means cancellation | 503 // deleted. So if this test simply hangs, it means cancellation |
| 500 // did not work. | 504 // did not work. |
| 501 } | 505 } |
| 502 | 506 |
| 503 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( | 507 void URLFetcherMultipleAttemptTest::OnURLFetchComplete( |
| 504 const URLFetcher* source) { | 508 const content::URLFetcher* source) { |
| 505 EXPECT_TRUE(source->status().is_success()); | 509 EXPECT_TRUE(source->GetStatus().is_success()); |
| 506 EXPECT_EQ(200, source->response_code()); // HTTP OK | 510 EXPECT_EQ(200, source->GetResponseCode()); // HTTP OK |
| 507 std::string data; | 511 std::string data; |
| 508 EXPECT_TRUE(source->GetResponseAsString(&data)); | 512 EXPECT_TRUE(source->GetResponseAsString(&data)); |
| 509 EXPECT_FALSE(data.empty()); | 513 EXPECT_FALSE(data.empty()); |
| 510 if (!data.empty() && data_.empty()) { | 514 if (!data.empty() && data_.empty()) { |
| 511 data_ = data; | 515 data_ = data; |
| 512 fetcher_->StartWithRequestContextGetter( | 516 fetcher_->StartWithRequestContextGetter( |
| 513 new TestURLRequestContextGetter(io_message_loop_proxy())); | 517 new TestURLRequestContextGetter(io_message_loop_proxy())); |
| 514 } else { | 518 } else { |
| 515 EXPECT_EQ(data, data_); | 519 EXPECT_EQ(data, data_); |
| 516 delete fetcher_; // Have to delete this here and not in the destructor, | 520 delete fetcher_; // Have to delete this here and not in the destructor, |
| 517 // because the destructor won't necessarily run on the | 521 // because the destructor won't necessarily run on the |
| 518 // same thread that CreateFetcher() did. | 522 // same thread that CreateFetcher() did. |
| 519 | 523 |
| 520 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 524 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 521 // If the current message loop is not the IO loop, it will be shut down when | 525 // If the current message loop is not the IO loop, it will be shut down when |
| 522 // the main loop returns and this thread subsequently goes out of scope. | 526 // the main loop returns and this thread subsequently goes out of scope. |
| 523 } | 527 } |
| 524 } | 528 } |
| 525 | 529 |
| 526 void URLFetcherTempFileTest::OnURLFetchComplete(const URLFetcher* source) { | 530 void URLFetcherTempFileTest::OnURLFetchComplete( |
| 527 EXPECT_TRUE(source->status().is_success()); | 531 const content::URLFetcher* source) { |
| 528 EXPECT_EQ(source->response_code(), 200); | 532 EXPECT_TRUE(source->GetStatus().is_success()); |
| 533 EXPECT_EQ(source->GetResponseCode(), 200); |
| 529 | 534 |
| 530 EXPECT_TRUE(source->GetResponseAsFilePath( | 535 EXPECT_TRUE(source->GetResponseAsFilePath( |
| 531 take_ownership_of_temp_file_, &temp_file_)); | 536 take_ownership_of_temp_file_, &temp_file_)); |
| 532 | 537 |
| 533 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); | 538 EXPECT_TRUE(file_util::ContentsEqual(expected_file_, temp_file_)); |
| 534 | 539 |
| 535 delete fetcher_; | 540 delete fetcher_; |
| 536 | 541 |
| 537 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 542 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 538 } | 543 } |
| (...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 787 FROM_HERE, | 792 FROM_HERE, |
| 788 base::Bind(&CancelAllOnIO), | 793 base::Bind(&CancelAllOnIO), |
| 789 base::Bind(&MessageLoop::Quit, | 794 base::Bind(&MessageLoop::Quit, |
| 790 base::Unretained(MessageLoop::current()))); | 795 base::Unretained(MessageLoop::current()))); |
| 791 MessageLoop::current()->Run(); | 796 MessageLoop::current()->Run(); |
| 792 EXPECT_EQ(0, GetNumFetcherCores()); | 797 EXPECT_EQ(0, GetNumFetcherCores()); |
| 793 delete fetcher_; | 798 delete fetcher_; |
| 794 } | 799 } |
| 795 | 800 |
| 796 } // namespace. | 801 } // namespace. |
| OLD | NEW |