OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-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 "base/thread.h" | 5 #include "base/thread.h" |
6 #include "base/time.h" | 6 #include "base/time.h" |
7 #include "base/timer.h" | 7 #include "base/timer.h" |
| 8 #include "chrome/browser/chrome_thread.h" |
8 #include "chrome/browser/net/url_fetcher.h" | 9 #include "chrome/browser/net/url_fetcher.h" |
9 #include "chrome/browser/net/url_fetcher_protect.h" | 10 #include "chrome/browser/net/url_fetcher_protect.h" |
10 #include "chrome/browser/net/url_request_context_getter.h" | 11 #include "chrome/browser/net/url_request_context_getter.h" |
11 #include "chrome/common/chrome_plugin_lib.h" | 12 #include "chrome/common/chrome_plugin_lib.h" |
12 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
13 #include "net/socket/ssl_test_util.h" | 14 #include "net/socket/ssl_test_util.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 | 17 |
17 using base::Time; | 18 using base::Time; |
18 using base::TimeDelta; | 19 using base::TimeDelta; |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 const wchar_t kDocRoot[] = L"chrome/test/data"; | 23 const wchar_t kDocRoot[] = L"chrome/test/data"; |
23 | 24 |
24 class TestURLRequestContextGetter : public URLRequestContextGetter { | 25 class TestURLRequestContextGetter : public URLRequestContextGetter { |
25 public: | 26 public: |
26 virtual URLRequestContext* GetURLRequestContext() { | 27 virtual URLRequestContext* GetURLRequestContext() { |
27 if (!context_) | 28 if (!context_) |
28 context_ = new TestURLRequestContext(); | 29 context_ = new TestURLRequestContext(); |
29 return context_; | 30 return context_; |
30 } | 31 } |
31 private: | 32 private: |
32 scoped_refptr<URLRequestContext> context_; | 33 scoped_refptr<URLRequestContext> context_; |
33 }; | 34 }; |
34 | 35 |
35 class URLFetcherTest : public testing::Test, public URLFetcher::Delegate { | 36 class URLFetcherTest : public testing::Test, public URLFetcher::Delegate { |
36 public: | 37 public: |
37 URLFetcherTest() : fetcher_(NULL) { } | 38 URLFetcherTest() |
| 39 : io_thread_(ChromeThread::IO, &io_loop_), |
| 40 fetcher_(NULL) { } |
38 | 41 |
39 // Creates a URLFetcher, using the program's main thread to do IO. | 42 // Creates a URLFetcher, using the program's main thread to do IO. |
40 virtual void CreateFetcher(const GURL& url); | 43 virtual void CreateFetcher(const GURL& url); |
41 | 44 |
42 // URLFetcher::Delegate | 45 // URLFetcher::Delegate |
43 virtual void OnURLFetchComplete(const URLFetcher* source, | 46 virtual void OnURLFetchComplete(const URLFetcher* source, |
44 const GURL& url, | 47 const GURL& url, |
45 const URLRequestStatus& status, | 48 const URLRequestStatus& status, |
46 int response_code, | 49 int response_code, |
47 const ResponseCookies& cookies, | 50 const ResponseCookies& cookies, |
48 const std::string& data); | 51 const std::string& data); |
49 | 52 |
50 protected: | 53 protected: |
51 virtual void SetUp() { | 54 virtual void SetUp() { |
52 testing::Test::SetUp(); | 55 testing::Test::SetUp(); |
53 | 56 |
54 // Ensure that any plugin operations done by other tests are cleaned up. | 57 // Ensure that any plugin operations done by other tests are cleaned up. |
55 ChromePluginLib::UnloadAllPlugins(); | 58 ChromePluginLib::UnloadAllPlugins(); |
56 } | 59 } |
57 | 60 |
58 // URLFetcher is designed to run on the main UI thread, but in our tests | 61 // URLFetcher is designed to run on the main UI thread, but in our tests |
59 // we assume that the current thread is the IO thread where the URLFetcher | 62 // we assume that the current thread is the IO thread where the URLFetcher |
60 // dispatches its requests to. When we wish to simulate being used from | 63 // dispatches its requests to. When we wish to simulate being used from |
61 // a UI thread, we dispatch a worker thread to do so. | 64 // a UI thread, we dispatch a worker thread to do so. |
62 MessageLoopForIO io_loop_; | 65 MessageLoopForIO io_loop_; |
| 66 ChromeThread io_thread_; |
63 | 67 |
64 URLFetcher* fetcher_; | 68 URLFetcher* fetcher_; |
65 }; | 69 }; |
66 | 70 |
67 // Version of URLFetcherTest that does a POST instead | 71 // Version of URLFetcherTest that does a POST instead |
68 class URLFetcherPostTest : public URLFetcherTest { | 72 class URLFetcherPostTest : public URLFetcherTest { |
69 public: | 73 public: |
70 virtual void CreateFetcher(const GURL& url); | 74 virtual void CreateFetcher(const GURL& url); |
71 | 75 |
72 // URLFetcher::Delegate | 76 // URLFetcher::Delegate |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 }; | 194 }; |
191 | 195 |
192 private: | 196 private: |
193 URLFetcherTest* test_; | 197 URLFetcherTest* test_; |
194 GURL url_; | 198 GURL url_; |
195 }; | 199 }; |
196 | 200 |
197 void URLFetcherTest::CreateFetcher(const GURL& url) { | 201 void URLFetcherTest::CreateFetcher(const GURL& url) { |
198 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 202 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
199 fetcher_->set_request_context(new TestURLRequestContextGetter()); | 203 fetcher_->set_request_context(new TestURLRequestContextGetter()); |
200 fetcher_->set_io_loop(&io_loop_); | |
201 fetcher_->Start(); | 204 fetcher_->Start(); |
202 } | 205 } |
203 | 206 |
204 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, | 207 void URLFetcherTest::OnURLFetchComplete(const URLFetcher* source, |
205 const GURL& url, | 208 const GURL& url, |
206 const URLRequestStatus& status, | 209 const URLRequestStatus& status, |
207 int response_code, | 210 int response_code, |
208 const ResponseCookies& cookies, | 211 const ResponseCookies& cookies, |
209 const std::string& data) { | 212 const std::string& data) { |
210 EXPECT_TRUE(status.is_success()); | 213 EXPECT_TRUE(status.is_success()); |
211 EXPECT_EQ(200, response_code); // HTTP OK | 214 EXPECT_EQ(200, response_code); // HTTP OK |
212 EXPECT_FALSE(data.empty()); | 215 EXPECT_FALSE(data.empty()); |
213 | 216 |
214 delete fetcher_; // Have to delete this here and not in the destructor, | 217 delete fetcher_; // Have to delete this here and not in the destructor, |
215 // because the destructor won't necessarily run on the | 218 // because the destructor won't necessarily run on the |
216 // same thread that CreateFetcher() did. | 219 // same thread that CreateFetcher() did. |
217 | 220 |
218 io_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 221 ChromeThread::PostTask( |
219 // If MessageLoop::current() != io_loop_, it will be shut down when the | 222 ChromeThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
220 // main loop returns and this thread subsequently goes out of scope. | 223 // If the current message loop is not the IO loop, it will be shut down when |
| 224 // the main loop returns and this thread subsequently goes out of scope. |
221 } | 225 } |
222 | 226 |
223 void URLFetcherPostTest::CreateFetcher(const GURL& url) { | 227 void URLFetcherPostTest::CreateFetcher(const GURL& url) { |
224 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); | 228 fetcher_ = new URLFetcher(url, URLFetcher::POST, this); |
225 fetcher_->set_request_context(new TestURLRequestContextGetter()); | 229 fetcher_->set_request_context(new TestURLRequestContextGetter()); |
226 fetcher_->set_io_loop(&io_loop_); | |
227 fetcher_->set_upload_data("application/x-www-form-urlencoded", | 230 fetcher_->set_upload_data("application/x-www-form-urlencoded", |
228 "bobsyeruncle"); | 231 "bobsyeruncle"); |
229 fetcher_->Start(); | 232 fetcher_->Start(); |
230 } | 233 } |
231 | 234 |
232 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source, | 235 void URLFetcherPostTest::OnURLFetchComplete(const URLFetcher* source, |
233 const GURL& url, | 236 const GURL& url, |
234 const URLRequestStatus& status, | 237 const URLRequestStatus& status, |
235 int response_code, | 238 int response_code, |
236 const ResponseCookies& cookies, | 239 const ResponseCookies& cookies, |
(...skipping 14 matching lines...) Expand all Loading... |
251 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", | 254 EXPECT_TRUE(source->response_headers()->GetNormalizedHeader("cache-control", |
252 &header)); | 255 &header)); |
253 EXPECT_EQ("private", header); | 256 EXPECT_EQ("private", header); |
254 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, | 257 URLFetcherTest::OnURLFetchComplete(source, url, status, response_code, |
255 cookies, data); | 258 cookies, data); |
256 } | 259 } |
257 | 260 |
258 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { | 261 void URLFetcherProtectTest::CreateFetcher(const GURL& url) { |
259 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 262 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
260 fetcher_->set_request_context(new TestURLRequestContextGetter()); | 263 fetcher_->set_request_context(new TestURLRequestContextGetter()); |
261 fetcher_->set_io_loop(&io_loop_); | |
262 start_time_ = Time::Now(); | 264 start_time_ = Time::Now(); |
263 fetcher_->Start(); | 265 fetcher_->Start(); |
264 } | 266 } |
265 | 267 |
266 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source, | 268 void URLFetcherProtectTest::OnURLFetchComplete(const URLFetcher* source, |
267 const GURL& url, | 269 const GURL& url, |
268 const URLRequestStatus& status, | 270 const URLRequestStatus& status, |
269 int response_code, | 271 int response_code, |
270 const ResponseCookies& cookies, | 272 const ResponseCookies& cookies, |
271 const std::string& data) { | 273 const std::string& data) { |
272 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 274 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
273 if (response_code >= 500) { | 275 if (response_code >= 500) { |
274 // Now running ServerUnavailable test. | 276 // Now running ServerUnavailable test. |
275 // It takes more than 1 second to finish all 11 requests. | 277 // It takes more than 1 second to finish all 11 requests. |
276 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 278 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
277 EXPECT_TRUE(status.is_success()); | 279 EXPECT_TRUE(status.is_success()); |
278 EXPECT_FALSE(data.empty()); | 280 EXPECT_FALSE(data.empty()); |
279 delete fetcher_; | 281 delete fetcher_; |
280 io_loop_.Quit(); | 282 ChromeThread::PostTask( |
| 283 ChromeThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
281 } else { | 284 } else { |
282 // Now running Overload test. | 285 // Now running Overload test. |
283 static int count = 0; | 286 static int count = 0; |
284 count++; | 287 count++; |
285 if (count < 20) { | 288 if (count < 20) { |
286 fetcher_->Start(); | 289 fetcher_->Start(); |
287 } else { | 290 } else { |
288 // We have already sent 20 requests continuously. And we expect that | 291 // We have already sent 20 requests continuously. And we expect that |
289 // it takes more than 1 second due to the overload pretection settings. | 292 // it takes more than 1 second due to the overload pretection settings. |
290 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 293 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
(...skipping 25 matching lines...) Expand all Loading... |
316 // This part is different from URLFetcherTest::OnURLFetchComplete | 319 // This part is different from URLFetcherTest::OnURLFetchComplete |
317 // because this test expects the request to be cancelled. | 320 // because this test expects the request to be cancelled. |
318 EXPECT_EQ(URLRequestStatus::CANCELED, status.status()); | 321 EXPECT_EQ(URLRequestStatus::CANCELED, status.status()); |
319 EXPECT_EQ(net::ERR_ABORTED, status.os_error()); | 322 EXPECT_EQ(net::ERR_ABORTED, status.os_error()); |
320 EXPECT_EQ(-1, response_code); | 323 EXPECT_EQ(-1, response_code); |
321 EXPECT_TRUE(cookies.empty()); | 324 EXPECT_TRUE(cookies.empty()); |
322 EXPECT_TRUE(data.empty()); | 325 EXPECT_TRUE(data.empty()); |
323 | 326 |
324 // The rest is the same as URLFetcherTest::OnURLFetchComplete. | 327 // The rest is the same as URLFetcherTest::OnURLFetchComplete. |
325 delete fetcher_; | 328 delete fetcher_; |
326 io_loop_.Quit(); | 329 ChromeThread::PostTask( |
| 330 ChromeThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
327 } | 331 } |
328 | 332 |
329 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { | 333 void URLFetcherCancelTest::CreateFetcher(const GURL& url) { |
330 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); | 334 fetcher_ = new URLFetcher(url, URLFetcher::GET, this); |
331 fetcher_->set_request_context( | 335 fetcher_->set_request_context( |
332 new CancelTestURLRequestContextGetter(&context_released_)); | 336 new CancelTestURLRequestContextGetter(&context_released_)); |
333 fetcher_->set_io_loop(&io_loop_); | |
334 fetcher_->Start(); | 337 fetcher_->Start(); |
335 // Make sure we give the IO thread a chance to run. | 338 // Make sure we give the IO thread a chance to run. |
336 timer_.Start(TimeDelta::FromMilliseconds(300), this, | 339 timer_.Start(TimeDelta::FromMilliseconds(300), this, |
337 &URLFetcherCancelTest::CancelRequest); | 340 &URLFetcherCancelTest::CancelRequest); |
338 } | 341 } |
339 | 342 |
340 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source, | 343 void URLFetcherCancelTest::OnURLFetchComplete(const URLFetcher* source, |
341 const GURL& url, | 344 const GURL& url, |
342 const URLRequestStatus& status, | 345 const URLRequestStatus& status, |
343 int response_code, | 346 int response_code, |
344 const ResponseCookies& cookies, | 347 const ResponseCookies& cookies, |
345 const std::string& data) { | 348 const std::string& data) { |
346 // We should have cancelled the request before completion. | 349 // We should have cancelled the request before completion. |
347 ADD_FAILURE(); | 350 ADD_FAILURE(); |
348 delete fetcher_; | 351 delete fetcher_; |
349 io_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 352 ChromeThread::PostTask( |
| 353 ChromeThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
350 } | 354 } |
351 | 355 |
352 void URLFetcherCancelTest::CancelRequest() { | 356 void URLFetcherCancelTest::CancelRequest() { |
353 delete fetcher_; | 357 delete fetcher_; |
354 timer_.Stop(); | 358 timer_.Stop(); |
355 // Make sure we give the IO thread a chance to run. | 359 // Make sure we give the IO thread a chance to run. |
356 timer_.Start(TimeDelta::FromMilliseconds(300), this, | 360 timer_.Start(TimeDelta::FromMilliseconds(300), this, |
357 &URLFetcherCancelTest::TestContextReleased); | 361 &URLFetcherCancelTest::TestContextReleased); |
358 } | 362 } |
359 | 363 |
360 void URLFetcherCancelTest::TestContextReleased() { | 364 void URLFetcherCancelTest::TestContextReleased() { |
361 EXPECT_TRUE(context_released_); | 365 EXPECT_TRUE(context_released_); |
362 timer_.Stop(); | 366 timer_.Stop(); |
363 io_loop_.PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 367 ChromeThread::PostTask( |
| 368 ChromeThread::IO, FROM_HERE, new MessageLoop::QuitTask()); |
364 } | 369 } |
365 | 370 |
366 TEST_F(URLFetcherTest, SameThreadsTest) { | 371 TEST_F(URLFetcherTest, SameThreadsTest) { |
367 // Create the fetcher on the main thread. Since IO will happen on the main | 372 // Create the fetcher on the main thread. Since IO will happen on the main |
368 // thread, this will test URLFetcher's ability to do everything on one | 373 // thread, this will test URLFetcher's ability to do everything on one |
369 // thread. | 374 // thread. |
370 scoped_refptr<HTTPTestServer> server = | 375 scoped_refptr<HTTPTestServer> server = |
371 HTTPTestServer::CreateServer(kDocRoot, NULL); | 376 HTTPTestServer::CreateServer(kDocRoot, NULL); |
372 ASSERT_TRUE(NULL != server.get()); | 377 ASSERT_TRUE(NULL != server.get()); |
373 | 378 |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // message loop will be shut down automatically as the thread goes out of | 490 // message loop will be shut down automatically as the thread goes out of |
486 // scope. | 491 // scope. |
487 base::Thread t("URLFetcher test thread"); | 492 base::Thread t("URLFetcher test thread"); |
488 ASSERT_TRUE(t.Start()); | 493 ASSERT_TRUE(t.Start()); |
489 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); | 494 t.message_loop()->PostTask(FROM_HERE, new FetcherWrapperTask(this, url)); |
490 | 495 |
491 MessageLoop::current()->Run(); | 496 MessageLoop::current()->Run(); |
492 } | 497 } |
493 | 498 |
494 } // namespace. | 499 } // namespace. |
OLD | NEW |