OLD | NEW |
1 // Copyright (c) 2009 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium OS 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 <unistd.h> | 5 #include <unistd.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 namespace { | 154 namespace { |
155 class HttpFetcherTestDelegate : public HttpFetcherDelegate { | 155 class HttpFetcherTestDelegate : public HttpFetcherDelegate { |
156 public: | 156 public: |
157 virtual void ReceivedBytes(HttpFetcher* fetcher, | 157 virtual void ReceivedBytes(HttpFetcher* fetcher, |
158 const char* bytes, int length) { | 158 const char* bytes, int length) { |
159 char str[length + 1]; | 159 char str[length + 1]; |
160 memset(str, 0, length + 1); | 160 memset(str, 0, length + 1); |
161 memcpy(str, bytes, length); | 161 memcpy(str, bytes, length); |
162 } | 162 } |
163 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { | 163 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { |
| 164 EXPECT_EQ(200, fetcher->http_response_code()); |
164 g_main_loop_quit(loop_); | 165 g_main_loop_quit(loop_); |
165 } | 166 } |
166 GMainLoop* loop_; | 167 GMainLoop* loop_; |
167 }; | 168 }; |
168 | 169 |
169 struct StartTransferArgs { | 170 struct StartTransferArgs { |
170 HttpFetcher *http_fetcher; | 171 HttpFetcher *http_fetcher; |
171 string url; | 172 string url; |
172 }; | 173 }; |
173 | 174 |
(...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
325 ASSERT_TRUE(server.started_); | 326 ASSERT_TRUE(server.started_); |
326 GSource* timeout_source_; | 327 GSource* timeout_source_; |
327 timeout_source_ = g_timeout_source_new(0); // ms | 328 timeout_source_ = g_timeout_source_new(0); // ms |
328 g_source_set_callback(timeout_source_, AbortingTimeoutCallback, &delegate, | 329 g_source_set_callback(timeout_source_, AbortingTimeoutCallback, &delegate, |
329 NULL); | 330 NULL); |
330 g_source_attach(timeout_source_, NULL); | 331 g_source_attach(timeout_source_, NULL); |
331 fetcher->BeginTransfer(this->BigUrl()); | 332 fetcher->BeginTransfer(this->BigUrl()); |
332 | 333 |
333 g_main_loop_run(loop); | 334 g_main_loop_run(loop); |
334 g_source_destroy(timeout_source_); | 335 g_source_destroy(timeout_source_); |
| 336 EXPECT_EQ(0, fetcher->http_response_code()); |
335 } | 337 } |
336 g_main_loop_unref(loop); | 338 g_main_loop_unref(loop); |
337 } | 339 } |
338 | 340 |
339 namespace { | 341 namespace { |
340 class FlakyHttpFetcherTestDelegate : public HttpFetcherDelegate { | 342 class FlakyHttpFetcherTestDelegate : public HttpFetcherDelegate { |
341 public: | 343 public: |
342 virtual void ReceivedBytes(HttpFetcher* fetcher, | 344 virtual void ReceivedBytes(HttpFetcher* fetcher, |
343 const char* bytes, int length) { | 345 const char* bytes, int length) { |
344 data.append(bytes, length); | 346 data.append(bytes, length); |
345 } | 347 } |
346 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { | 348 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { |
347 EXPECT_TRUE(successful); | 349 EXPECT_TRUE(successful); |
| 350 EXPECT_EQ(206, fetcher->http_response_code()); |
348 g_main_loop_quit(loop_); | 351 g_main_loop_quit(loop_); |
349 } | 352 } |
350 string data; | 353 string data; |
351 GMainLoop* loop_; | 354 GMainLoop* loop_; |
352 }; | 355 }; |
353 } // namespace {} | 356 } // namespace {} |
354 | 357 |
355 TYPED_TEST(HttpFetcherTest, FlakyTest) { | 358 TYPED_TEST(HttpFetcherTest, FlakyTest) { |
356 if (this->IsMock()) | 359 if (this->IsMock()) |
357 return; | 360 return; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
391 const char* bytes, int length) { | 394 const char* bytes, int length) { |
392 if (server_) { | 395 if (server_) { |
393 LOG(INFO) << "Stopping server"; | 396 LOG(INFO) << "Stopping server"; |
394 delete server_; | 397 delete server_; |
395 LOG(INFO) << "server stopped"; | 398 LOG(INFO) << "server stopped"; |
396 server_ = NULL; | 399 server_ = NULL; |
397 } | 400 } |
398 } | 401 } |
399 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { | 402 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { |
400 EXPECT_FALSE(successful); | 403 EXPECT_FALSE(successful); |
| 404 EXPECT_EQ(0, fetcher->http_response_code()); |
401 g_main_loop_quit(loop_); | 405 g_main_loop_quit(loop_); |
402 } | 406 } |
403 GMainLoop* loop_; | 407 GMainLoop* loop_; |
404 PythonHttpServer* server_; | 408 PythonHttpServer* server_; |
405 }; | 409 }; |
406 } // namespace {} | 410 } // namespace {} |
407 | 411 |
408 | 412 |
409 TYPED_TEST(HttpFetcherTest, FailureTest) { | 413 TYPED_TEST(HttpFetcherTest, FailureTest) { |
410 if (this->IsMock()) | 414 if (this->IsMock()) |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
459 class RedirectHttpFetcherTestDelegate : public HttpFetcherDelegate { | 463 class RedirectHttpFetcherTestDelegate : public HttpFetcherDelegate { |
460 public: | 464 public: |
461 RedirectHttpFetcherTestDelegate(bool expected_successful) | 465 RedirectHttpFetcherTestDelegate(bool expected_successful) |
462 : expected_successful_(expected_successful) {} | 466 : expected_successful_(expected_successful) {} |
463 virtual void ReceivedBytes(HttpFetcher* fetcher, | 467 virtual void ReceivedBytes(HttpFetcher* fetcher, |
464 const char* bytes, int length) { | 468 const char* bytes, int length) { |
465 data.append(bytes, length); | 469 data.append(bytes, length); |
466 } | 470 } |
467 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { | 471 virtual void TransferComplete(HttpFetcher* fetcher, bool successful) { |
468 EXPECT_EQ(expected_successful_, successful); | 472 EXPECT_EQ(expected_successful_, successful); |
| 473 if (expected_successful_) |
| 474 EXPECT_EQ(200, fetcher->http_response_code()); |
| 475 else { |
| 476 EXPECT_GE(fetcher->http_response_code(), 301); |
| 477 EXPECT_LE(fetcher->http_response_code(), 307); |
| 478 } |
469 g_main_loop_quit(loop_); | 479 g_main_loop_quit(loop_); |
470 } | 480 } |
471 bool expected_successful_; | 481 bool expected_successful_; |
472 string data; | 482 string data; |
473 GMainLoop* loop_; | 483 GMainLoop* loop_; |
474 }; | 484 }; |
475 | 485 |
476 // RedirectTest takes ownership of |http_fetcher|. | 486 // RedirectTest takes ownership of |http_fetcher|. |
477 void RedirectTest(bool expected_successful, | 487 void RedirectTest(bool expected_successful, |
478 const string& url, | 488 const string& url, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 string url; | 544 string url; |
535 for (int r = 0; r < LibcurlHttpFetcher::kMaxRedirects + 1; r++) { | 545 for (int r = 0; r < LibcurlHttpFetcher::kMaxRedirects + 1; r++) { |
536 url += base::StringPrintf("/redirect/%d", | 546 url += base::StringPrintf("/redirect/%d", |
537 kRedirectCodes[r % arraysize(kRedirectCodes)]); | 547 kRedirectCodes[r % arraysize(kRedirectCodes)]); |
538 } | 548 } |
539 url += "/medium"; | 549 url += "/medium"; |
540 RedirectTest(false, url, this->NewLargeFetcher()); | 550 RedirectTest(false, url, this->NewLargeFetcher()); |
541 } | 551 } |
542 | 552 |
543 } // namespace chromeos_update_engine | 553 } // namespace chromeos_update_engine |
OLD | NEW |