OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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/url_request/url_request_unittest.h" | 5 #include "net/url_request/url_request_unittest.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 | 8 |
9 #if defined(OS_WIN) | 9 #if defined(OS_WIN) |
10 #include <windows.h> | 10 #include <windows.h> |
11 #include <shlobj.h> | 11 #include <shlobj.h> |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
76 bool ContainsString(const std::string& haystack, const char* needle) { | 76 bool ContainsString(const std::string& haystack, const char* needle) { |
77 std::string::const_iterator it = | 77 std::string::const_iterator it = |
78 std::search(haystack.begin(), | 78 std::search(haystack.begin(), |
79 haystack.end(), | 79 haystack.end(), |
80 needle, | 80 needle, |
81 needle + strlen(needle), | 81 needle + strlen(needle), |
82 CaseInsensitiveCompare<char>()); | 82 CaseInsensitiveCompare<char>()); |
83 return it != haystack.end(); | 83 return it != haystack.end(); |
84 } | 84 } |
85 | 85 |
| 86 void FillBuffer(char* buffer, size_t len) { |
| 87 static bool called = false; |
| 88 if (!called) { |
| 89 called = true; |
| 90 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 91 srand(seed); |
| 92 } |
| 93 |
| 94 for (size_t i = 0; i < len; i++) { |
| 95 buffer[i] = static_cast<char>(rand()); |
| 96 if (!buffer[i]) |
| 97 buffer[i] = 'g'; |
| 98 } |
| 99 } |
| 100 |
86 } // namespace | 101 } // namespace |
87 | 102 |
88 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f | 103 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
89 class URLRequestTest : public PlatformTest { | 104 class URLRequestTest : public PlatformTest { |
90 }; | 105 }; |
91 | 106 |
92 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { | 107 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { |
93 // In this unit test, we're using the HTTPTestServer as a proxy server and | 108 // In this unit test, we're using the HTTPTestServer as a proxy server and |
94 // issuing a CONNECT request with the magic host name "www.redirect.com". | 109 // issuing a CONNECT request with the magic host name "www.redirect.com". |
95 // The HTTPTestServer will return a 302 response, which we should not | 110 // The HTTPTestServer will return a 302 response, which we should not |
(...skipping 510 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
606 EXPECT_TRUE(!r.is_pending()); | 621 EXPECT_TRUE(!r.is_pending()); |
607 EXPECT_EQ(1, d.response_started_count()); | 622 EXPECT_EQ(1, d.response_started_count()); |
608 EXPECT_FALSE(d.received_data_before_response()); | 623 EXPECT_FALSE(d.received_data_before_response()); |
609 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); | 624 EXPECT_EQ(d.bytes_received(), static_cast<int>(file_size)); |
610 } | 625 } |
611 #ifndef NDEBUG | 626 #ifndef NDEBUG |
612 DCHECK_EQ(url_request_metrics.object_count, 0); | 627 DCHECK_EQ(url_request_metrics.object_count, 0); |
613 #endif | 628 #endif |
614 } | 629 } |
615 | 630 |
| 631 TEST_F(URLRequestTest, FileTestFullSpecifiedRange) { |
| 632 const size_t buffer_size = 4000; |
| 633 scoped_array<char> buffer(new char[buffer_size]); |
| 634 FillBuffer(buffer.get(), buffer_size); |
| 635 |
| 636 FilePath temp_path; |
| 637 EXPECT_TRUE(file_util::CreateTemporaryFileName(&temp_path)); |
| 638 GURL temp_url = net::FilePathToFileURL(temp_path); |
| 639 file_util::WriteFile(temp_path, buffer.get(), buffer_size); |
| 640 |
| 641 int64 file_size; |
| 642 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 643 |
| 644 const size_t first_byte_position = 500; |
| 645 const size_t last_byte_position = buffer_size - first_byte_position; |
| 646 const size_t content_length = last_byte_position - first_byte_position + 1; |
| 647 std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 648 buffer.get() + last_byte_position + 1); |
| 649 |
| 650 TestDelegate d; |
| 651 { |
| 652 TestURLRequest r(temp_url, &d); |
| 653 |
| 654 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=%d-%d\n", |
| 655 first_byte_position, |
| 656 last_byte_position)); |
| 657 r.Start(); |
| 658 EXPECT_TRUE(r.is_pending()); |
| 659 |
| 660 MessageLoop::current()->Run(); |
| 661 EXPECT_TRUE(!r.is_pending()); |
| 662 EXPECT_EQ(1, d.response_started_count()); |
| 663 EXPECT_FALSE(d.received_data_before_response()); |
| 664 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 665 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 666 EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 667 } |
| 668 |
| 669 EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 670 #ifndef NDEBUG |
| 671 DCHECK_EQ(url_request_metrics.object_count, 0); |
| 672 #endif |
| 673 } |
| 674 |
| 675 TEST_F(URLRequestTest, FileTestHalfSpecifiedRange) { |
| 676 const size_t buffer_size = 4000; |
| 677 scoped_array<char> buffer(new char[buffer_size]); |
| 678 FillBuffer(buffer.get(), buffer_size); |
| 679 |
| 680 FilePath temp_path; |
| 681 EXPECT_TRUE(file_util::CreateTemporaryFileName(&temp_path)); |
| 682 GURL temp_url = net::FilePathToFileURL(temp_path); |
| 683 file_util::WriteFile(temp_path, buffer.get(), buffer_size); |
| 684 |
| 685 int64 file_size; |
| 686 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 687 |
| 688 const size_t first_byte_position = 500; |
| 689 const size_t last_byte_position = buffer_size - 1; |
| 690 const size_t content_length = last_byte_position - first_byte_position + 1; |
| 691 std::string partial_buffer_string(buffer.get() + first_byte_position, |
| 692 buffer.get() + last_byte_position + 1); |
| 693 |
| 694 TestDelegate d; |
| 695 { |
| 696 TestURLRequest r(temp_url, &d); |
| 697 |
| 698 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=%d-\n", |
| 699 first_byte_position)); |
| 700 r.Start(); |
| 701 EXPECT_TRUE(r.is_pending()); |
| 702 |
| 703 MessageLoop::current()->Run(); |
| 704 EXPECT_TRUE(!r.is_pending()); |
| 705 EXPECT_EQ(1, d.response_started_count()); |
| 706 EXPECT_FALSE(d.received_data_before_response()); |
| 707 EXPECT_EQ(static_cast<int>(content_length), d.bytes_received()); |
| 708 // Don't use EXPECT_EQ, it will print out a lot of garbage if check failed. |
| 709 EXPECT_TRUE(partial_buffer_string == d.data_received()); |
| 710 } |
| 711 |
| 712 EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 713 #ifndef NDEBUG |
| 714 DCHECK_EQ(url_request_metrics.object_count, 0); |
| 715 #endif |
| 716 } |
| 717 |
| 718 TEST_F(URLRequestTest, FileTestMultipleRanges) { |
| 719 const size_t buffer_size = 400000; |
| 720 scoped_array<char> buffer(new char[buffer_size]); |
| 721 FillBuffer(buffer.get(), buffer_size); |
| 722 |
| 723 FilePath temp_path; |
| 724 EXPECT_TRUE(file_util::CreateTemporaryFileName(&temp_path)); |
| 725 GURL temp_url = net::FilePathToFileURL(temp_path); |
| 726 file_util::WriteFile(temp_path, buffer.get(), buffer_size); |
| 727 |
| 728 int64 file_size; |
| 729 EXPECT_TRUE(file_util::GetFileSize(temp_path, &file_size)); |
| 730 |
| 731 TestDelegate d; |
| 732 { |
| 733 TestURLRequest r(temp_url, &d); |
| 734 |
| 735 r.SetExtraRequestHeaders(StringPrintf("Range: bytes=0-0,10-200,200-300\n")); |
| 736 r.Start(); |
| 737 EXPECT_TRUE(r.is_pending()); |
| 738 |
| 739 MessageLoop::current()->Run(); |
| 740 EXPECT_TRUE(d.request_failed()); |
| 741 } |
| 742 |
| 743 EXPECT_TRUE(file_util::Delete(temp_path, false)); |
| 744 #ifndef NDEBUG |
| 745 DCHECK_EQ(url_request_metrics.object_count, 0); |
| 746 #endif |
| 747 } |
| 748 |
616 TEST_F(URLRequestTest, InvalidUrlTest) { | 749 TEST_F(URLRequestTest, InvalidUrlTest) { |
617 TestDelegate d; | 750 TestDelegate d; |
618 { | 751 { |
619 TestURLRequest r(GURL("invalid url"), &d); | 752 TestURLRequest r(GURL("invalid url"), &d); |
620 | 753 |
621 r.Start(); | 754 r.Start(); |
622 EXPECT_TRUE(r.is_pending()); | 755 EXPECT_TRUE(r.is_pending()); |
623 | 756 |
624 MessageLoop::current()->Run(); | 757 MessageLoop::current()->Run(); |
625 EXPECT_TRUE(d.request_failed()); | 758 EXPECT_TRUE(d.request_failed()); |
(...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1662 | 1795 |
1663 int64 file_size = 0; | 1796 int64 file_size = 0; |
1664 file_util::GetFileSize(app_path, &file_size); | 1797 file_util::GetFileSize(app_path, &file_size); |
1665 | 1798 |
1666 EXPECT_TRUE(!r.is_pending()); | 1799 EXPECT_TRUE(!r.is_pending()); |
1667 EXPECT_EQ(1, d.response_started_count()); | 1800 EXPECT_EQ(1, d.response_started_count()); |
1668 EXPECT_FALSE(d.received_data_before_response()); | 1801 EXPECT_FALSE(d.received_data_before_response()); |
1669 EXPECT_EQ(d.bytes_received(), 0); | 1802 EXPECT_EQ(d.bytes_received(), 0); |
1670 } | 1803 } |
1671 } | 1804 } |
OLD | NEW |