OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "build/build_config.h" | 5 #include "build/build_config.h" |
6 | 6 |
7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
8 #include <windows.h> | 8 #include <windows.h> |
9 #include <shlobj.h> | 9 #include <shlobj.h> |
10 #endif | 10 #endif |
(...skipping 12 matching lines...) Expand all Loading... |
23 #include "base/message_loop/message_loop_proxy.h" | 23 #include "base/message_loop/message_loop_proxy.h" |
24 #include "base/path_service.h" | 24 #include "base/path_service.h" |
25 #include "base/run_loop.h" | 25 #include "base/run_loop.h" |
26 #include "base/strings/string_number_conversions.h" | 26 #include "base/strings/string_number_conversions.h" |
27 #include "base/strings/string_piece.h" | 27 #include "base/strings/string_piece.h" |
28 #include "base/strings/string_split.h" | 28 #include "base/strings/string_split.h" |
29 #include "base/strings/string_util.h" | 29 #include "base/strings/string_util.h" |
30 #include "base/strings/stringprintf.h" | 30 #include "base/strings/stringprintf.h" |
31 #include "base/strings/utf_string_conversions.h" | 31 #include "base/strings/utf_string_conversions.h" |
32 #include "net/base/capturing_net_log.h" | 32 #include "net/base/capturing_net_log.h" |
33 #include "net/base/filename_util.h" | |
34 #include "net/base/load_flags.h" | 33 #include "net/base/load_flags.h" |
35 #include "net/base/load_timing_info.h" | 34 #include "net/base/load_timing_info.h" |
36 #include "net/base/load_timing_info_test_util.h" | 35 #include "net/base/load_timing_info_test_util.h" |
37 #include "net/base/net_errors.h" | 36 #include "net/base/net_errors.h" |
38 #include "net/base/net_log.h" | 37 #include "net/base/net_log.h" |
39 #include "net/base/net_log_unittest.h" | 38 #include "net/base/net_log_unittest.h" |
40 #include "net/base/net_module.h" | 39 #include "net/base/net_module.h" |
41 #include "net/base/net_util.h" | 40 #include "net/base/net_util.h" |
42 #include "net/base/request_priority.h" | 41 #include "net/base/request_priority.h" |
43 #include "net/base/test_data_directory.h" | 42 #include "net/base/test_data_directory.h" |
(...skipping 26 matching lines...) Expand all Loading... |
70 #include "net/url_request/url_request.h" | 69 #include "net/url_request/url_request.h" |
71 #include "net/url_request/url_request_http_job.h" | 70 #include "net/url_request/url_request_http_job.h" |
72 #include "net/url_request/url_request_job_factory_impl.h" | 71 #include "net/url_request/url_request_job_factory_impl.h" |
73 #include "net/url_request/url_request_redirect_job.h" | 72 #include "net/url_request/url_request_redirect_job.h" |
74 #include "net/url_request/url_request_test_job.h" | 73 #include "net/url_request/url_request_test_job.h" |
75 #include "net/url_request/url_request_test_util.h" | 74 #include "net/url_request/url_request_test_util.h" |
76 #include "testing/gtest/include/gtest/gtest.h" | 75 #include "testing/gtest/include/gtest/gtest.h" |
77 #include "testing/platform_test.h" | 76 #include "testing/platform_test.h" |
78 | 77 |
79 #if !defined(DISABLE_FILE_SUPPORT) | 78 #if !defined(DISABLE_FILE_SUPPORT) |
| 79 #include "net/base/filename_util.h" |
80 #include "net/url_request/file_protocol_handler.h" | 80 #include "net/url_request/file_protocol_handler.h" |
81 #include "net/url_request/url_request_file_dir_job.h" | 81 #include "net/url_request/url_request_file_dir_job.h" |
82 #endif | 82 #endif |
83 | 83 |
84 #if !defined(DISABLE_FTP_SUPPORT) | 84 #if !defined(DISABLE_FTP_SUPPORT) |
85 #include "net/url_request/ftp_protocol_handler.h" | 85 #include "net/url_request/ftp_protocol_handler.h" |
86 #endif | 86 #endif |
87 | 87 |
88 #if defined(OS_WIN) | 88 #if defined(OS_WIN) |
89 #include "base/win/scoped_com_initializer.h" | 89 #include "base/win/scoped_com_initializer.h" |
(...skipping 600 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
690 EXPECT_FALSE(d.received_data_before_response()); | 690 EXPECT_FALSE(d.received_data_before_response()); |
691 EXPECT_EQ(d.bytes_received(), 911); | 691 EXPECT_EQ(d.bytes_received(), 911); |
692 EXPECT_EQ("", r.GetSocketAddress().host()); | 692 EXPECT_EQ("", r.GetSocketAddress().host()); |
693 EXPECT_EQ(0, r.GetSocketAddress().port()); | 693 EXPECT_EQ(0, r.GetSocketAddress().port()); |
694 | 694 |
695 HttpRequestHeaders headers; | 695 HttpRequestHeaders headers; |
696 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); | 696 EXPECT_FALSE(r.GetFullRequestHeaders(&headers)); |
697 } | 697 } |
698 } | 698 } |
699 | 699 |
| 700 #if !defined(DISABLE_FILE_SUPPORT) |
700 TEST_F(URLRequestTest, FileTest) { | 701 TEST_F(URLRequestTest, FileTest) { |
701 base::FilePath app_path; | 702 base::FilePath app_path; |
702 PathService::Get(base::FILE_EXE, &app_path); | 703 PathService::Get(base::FILE_EXE, &app_path); |
703 GURL app_url = FilePathToFileURL(app_path); | 704 GURL app_url = FilePathToFileURL(app_path); |
704 | 705 |
705 TestDelegate d; | 706 TestDelegate d; |
706 { | 707 { |
707 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); | 708 URLRequest r(app_url, DEFAULT_PRIORITY, &d, &default_context_); |
708 | 709 |
709 r.Start(); | 710 r.Start(); |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
888 network_delegate.set_can_access_files(false); | 889 network_delegate.set_can_access_files(false); |
889 default_context_.set_network_delegate(&network_delegate); | 890 default_context_.set_network_delegate(&network_delegate); |
890 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_); | 891 URLRequest r(test_file_url, DEFAULT_PRIORITY, &d, &default_context_); |
891 r.Start(); | 892 r.Start(); |
892 base::RunLoop().Run(); | 893 base::RunLoop().Run(); |
893 EXPECT_TRUE(d.request_failed()); | 894 EXPECT_TRUE(d.request_failed()); |
894 EXPECT_EQ("", d.data_received()); | 895 EXPECT_EQ("", d.data_received()); |
895 } | 896 } |
896 } | 897 } |
897 | 898 |
898 TEST_F(URLRequestTest, InvalidUrlTest) { | |
899 TestDelegate d; | |
900 { | |
901 URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_); | |
902 | |
903 r.Start(); | |
904 EXPECT_TRUE(r.is_pending()); | |
905 | |
906 base::RunLoop().Run(); | |
907 EXPECT_TRUE(d.request_failed()); | |
908 } | |
909 } | |
910 | |
911 #if defined(OS_WIN) | |
912 TEST_F(URLRequestTest, ResolveShortcutTest) { | |
913 base::FilePath app_path; | |
914 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); | |
915 app_path = app_path.AppendASCII("net"); | |
916 app_path = app_path.AppendASCII("data"); | |
917 app_path = app_path.AppendASCII("url_request_unittest"); | |
918 app_path = app_path.AppendASCII("with-headers.html"); | |
919 | |
920 std::wstring lnk_path = app_path.value() + L".lnk"; | |
921 | |
922 base::win::ScopedCOMInitializer com_initializer; | |
923 | |
924 // Temporarily create a shortcut for test | |
925 { | |
926 base::win::ScopedComPtr<IShellLink> shell; | |
927 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, | |
928 CLSCTX_INPROC_SERVER))); | |
929 base::win::ScopedComPtr<IPersistFile> persist; | |
930 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); | |
931 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); | |
932 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); | |
933 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); | |
934 } | |
935 | |
936 TestDelegate d; | |
937 { | |
938 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), | |
939 DEFAULT_PRIORITY, | |
940 &d, | |
941 &default_context_); | |
942 | |
943 r.Start(); | |
944 EXPECT_TRUE(r.is_pending()); | |
945 | |
946 base::RunLoop().Run(); | |
947 | |
948 WIN32_FILE_ATTRIBUTE_DATA data; | |
949 GetFileAttributesEx(app_path.value().c_str(), | |
950 GetFileExInfoStandard, &data); | |
951 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, | |
952 FILE_SHARE_READ, NULL, OPEN_EXISTING, | |
953 FILE_ATTRIBUTE_NORMAL, NULL); | |
954 EXPECT_NE(INVALID_HANDLE_VALUE, file); | |
955 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); | |
956 DWORD read_size; | |
957 BOOL result; | |
958 result = ReadFile(file, buffer.get(), data.nFileSizeLow, | |
959 &read_size, NULL); | |
960 std::string content(buffer.get(), read_size); | |
961 CloseHandle(file); | |
962 | |
963 EXPECT_TRUE(!r.is_pending()); | |
964 EXPECT_EQ(1, d.received_redirect_count()); | |
965 EXPECT_EQ(content, d.data_received()); | |
966 } | |
967 | |
968 // Clean the shortcut | |
969 DeleteFile(lnk_path.c_str()); | |
970 } | |
971 #endif // defined(OS_WIN) | |
972 | 899 |
973 TEST_F(URLRequestTest, FileDirCancelTest) { | 900 TEST_F(URLRequestTest, FileDirCancelTest) { |
974 // Put in mock resource provider. | 901 // Put in mock resource provider. |
975 NetModule::SetResourceProvider(TestNetResourceProvider); | 902 NetModule::SetResourceProvider(TestNetResourceProvider); |
976 | 903 |
977 TestDelegate d; | 904 TestDelegate d; |
978 { | 905 { |
979 base::FilePath file_path; | 906 base::FilePath file_path; |
980 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); | 907 PathService::Get(base::DIR_SOURCE_ROOT, &file_path); |
981 file_path = file_path.Append(FILE_PATH_LITERAL("net")); | 908 file_path = file_path.Append(FILE_PATH_LITERAL("net")); |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1060 // Don't accept the url "file:///" on windows. See http://crbug.com/1474. | 987 // Don't accept the url "file:///" on windows. See http://crbug.com/1474. |
1061 TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { | 988 TEST_F(URLRequestTest, FileDirRedirectSingleSlash) { |
1062 TestDelegate d; | 989 TestDelegate d; |
1063 URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_); | 990 URLRequest req(GURL("file:///"), DEFAULT_PRIORITY, &d, &default_context_); |
1064 req.Start(); | 991 req.Start(); |
1065 base::RunLoop().Run(); | 992 base::RunLoop().Run(); |
1066 | 993 |
1067 ASSERT_EQ(1, d.received_redirect_count()); | 994 ASSERT_EQ(1, d.received_redirect_count()); |
1068 ASSERT_FALSE(req.status().is_success()); | 995 ASSERT_FALSE(req.status().is_success()); |
1069 } | 996 } |
1070 #endif | 997 #endif // defined(OS_WIN) |
| 998 |
| 999 #endif // !defined(DISABLE_FILE_SUPPORT) |
| 1000 |
| 1001 TEST_F(URLRequestTest, InvalidUrlTest) { |
| 1002 TestDelegate d; |
| 1003 { |
| 1004 URLRequest r(GURL("invalid url"), DEFAULT_PRIORITY, &d, &default_context_); |
| 1005 |
| 1006 r.Start(); |
| 1007 EXPECT_TRUE(r.is_pending()); |
| 1008 |
| 1009 base::RunLoop().Run(); |
| 1010 EXPECT_TRUE(d.request_failed()); |
| 1011 } |
| 1012 } |
| 1013 |
| 1014 #if defined(OS_WIN) |
| 1015 TEST_F(URLRequestTest, ResolveShortcutTest) { |
| 1016 base::FilePath app_path; |
| 1017 PathService::Get(base::DIR_SOURCE_ROOT, &app_path); |
| 1018 app_path = app_path.AppendASCII("net"); |
| 1019 app_path = app_path.AppendASCII("data"); |
| 1020 app_path = app_path.AppendASCII("url_request_unittest"); |
| 1021 app_path = app_path.AppendASCII("with-headers.html"); |
| 1022 |
| 1023 std::wstring lnk_path = app_path.value() + L".lnk"; |
| 1024 |
| 1025 base::win::ScopedCOMInitializer com_initializer; |
| 1026 |
| 1027 // Temporarily create a shortcut for test |
| 1028 { |
| 1029 base::win::ScopedComPtr<IShellLink> shell; |
| 1030 ASSERT_TRUE(SUCCEEDED(shell.CreateInstance(CLSID_ShellLink, NULL, |
| 1031 CLSCTX_INPROC_SERVER))); |
| 1032 base::win::ScopedComPtr<IPersistFile> persist; |
| 1033 ASSERT_TRUE(SUCCEEDED(shell.QueryInterface(persist.Receive()))); |
| 1034 EXPECT_TRUE(SUCCEEDED(shell->SetPath(app_path.value().c_str()))); |
| 1035 EXPECT_TRUE(SUCCEEDED(shell->SetDescription(L"ResolveShortcutTest"))); |
| 1036 EXPECT_TRUE(SUCCEEDED(persist->Save(lnk_path.c_str(), TRUE))); |
| 1037 } |
| 1038 |
| 1039 TestDelegate d; |
| 1040 { |
| 1041 URLRequest r(FilePathToFileURL(base::FilePath(lnk_path)), |
| 1042 DEFAULT_PRIORITY, |
| 1043 &d, |
| 1044 &default_context_); |
| 1045 |
| 1046 r.Start(); |
| 1047 EXPECT_TRUE(r.is_pending()); |
| 1048 |
| 1049 base::RunLoop().Run(); |
| 1050 |
| 1051 WIN32_FILE_ATTRIBUTE_DATA data; |
| 1052 GetFileAttributesEx(app_path.value().c_str(), |
| 1053 GetFileExInfoStandard, &data); |
| 1054 HANDLE file = CreateFile(app_path.value().c_str(), GENERIC_READ, |
| 1055 FILE_SHARE_READ, NULL, OPEN_EXISTING, |
| 1056 FILE_ATTRIBUTE_NORMAL, NULL); |
| 1057 EXPECT_NE(INVALID_HANDLE_VALUE, file); |
| 1058 scoped_ptr<char[]> buffer(new char[data.nFileSizeLow]); |
| 1059 DWORD read_size; |
| 1060 BOOL result; |
| 1061 result = ReadFile(file, buffer.get(), data.nFileSizeLow, |
| 1062 &read_size, NULL); |
| 1063 std::string content(buffer.get(), read_size); |
| 1064 CloseHandle(file); |
| 1065 |
| 1066 EXPECT_TRUE(!r.is_pending()); |
| 1067 EXPECT_EQ(1, d.received_redirect_count()); |
| 1068 EXPECT_EQ(content, d.data_received()); |
| 1069 } |
| 1070 |
| 1071 // Clean the shortcut |
| 1072 DeleteFile(lnk_path.c_str()); |
| 1073 } |
| 1074 #endif // defined(OS_WIN) |
1071 | 1075 |
1072 // Custom URLRequestJobs for use with interceptor tests | 1076 // Custom URLRequestJobs for use with interceptor tests |
1073 class RestartTestJob : public URLRequestTestJob { | 1077 class RestartTestJob : public URLRequestTestJob { |
1074 public: | 1078 public: |
1075 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) | 1079 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
1076 : URLRequestTestJob(request, network_delegate, true) {} | 1080 : URLRequestTestJob(request, network_delegate, true) {} |
1077 protected: | 1081 protected: |
1078 virtual void StartAsync() OVERRIDE { | 1082 virtual void StartAsync() OVERRIDE { |
1079 this->NotifyRestartRequired(); | 1083 this->NotifyRestartRequired(); |
1080 } | 1084 } |
(...skipping 6928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
8009 | 8013 |
8010 EXPECT_FALSE(r.is_pending()); | 8014 EXPECT_FALSE(r.is_pending()); |
8011 EXPECT_EQ(1, d->response_started_count()); | 8015 EXPECT_EQ(1, d->response_started_count()); |
8012 EXPECT_FALSE(d->received_data_before_response()); | 8016 EXPECT_FALSE(d->received_data_before_response()); |
8013 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); | 8017 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); |
8014 } | 8018 } |
8015 } | 8019 } |
8016 #endif // !defined(DISABLE_FTP_SUPPORT) | 8020 #endif // !defined(DISABLE_FTP_SUPPORT) |
8017 | 8021 |
8018 } // namespace net | 8022 } // namespace net |
OLD | NEW |