OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/message_loop/message_loop.h" | 6 #include "base/message_loop/message_loop.h" |
7 #include "base/path_service.h" | 7 #include "base/path_service.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/threading/thread_task_runner_handle.h" | 9 #include "base/threading/thread_task_runner_handle.h" |
10 #include "content/browser/loader/test_url_loader_client.h" | 10 #include "content/browser/loader/test_url_loader_client.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
62 public: | 62 public: |
63 URLLoaderImplTest() {} | 63 URLLoaderImplTest() {} |
64 ~URLLoaderImplTest() override {} | 64 ~URLLoaderImplTest() override {} |
65 | 65 |
66 void SetUp() override { | 66 void SetUp() override { |
67 test_server_.ServeFilesFromSourceDirectory( | 67 test_server_.ServeFilesFromSourceDirectory( |
68 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); | 68 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
69 ASSERT_TRUE(test_server_.Start()); | 69 ASSERT_TRUE(test_server_.Start()); |
70 } | 70 } |
71 | 71 |
| 72 void Load(const GURL& url, |
| 73 TestURLLoaderClient* client, |
| 74 uint32_t options = 0) { |
| 75 mojom::URLLoaderAssociatedPtr loader; |
| 76 |
| 77 ResourceRequest request = |
| 78 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, url); |
| 79 |
| 80 URLLoaderImpl loader_impl(context(), mojo::MakeIsolatedRequest(&loader), |
| 81 options, request, client->CreateInterfacePtr()); |
| 82 |
| 83 client->RunUntilComplete(); |
| 84 } |
| 85 |
72 void LoadAndCompareFile(const std::string& path) { | 86 void LoadAndCompareFile(const std::string& path) { |
73 TestURLLoaderClient client; | 87 TestURLLoaderClient client; |
74 mojom::URLLoaderAssociatedPtr loader; | 88 GURL url = test_server()->GetURL(std::string("/") + path); |
75 | 89 Load(url, &client); |
76 ResourceRequest request = | |
77 CreateResourceRequest("GET", RESOURCE_TYPE_MAIN_FRAME, | |
78 test_server()->GetURL(std::string("/") + path)); | |
79 | |
80 URLLoaderImpl loader_impl(context(), mojo::MakeIsolatedRequest(&loader), | |
81 request, client.CreateInterfacePtr()); | |
82 | |
83 client.RunUntilComplete(); | |
84 | 90 |
85 base::FilePath file; | 91 base::FilePath file; |
86 PathService::Get(content::DIR_TEST_DATA, &file); | 92 PathService::Get(content::DIR_TEST_DATA, &file); |
87 file = file.AppendASCII(path); | 93 file = file.AppendASCII(path); |
88 | 94 |
89 std::string file_contents; | 95 std::string file_contents; |
90 if (!base::ReadFileToString(file, &file_contents)) { | 96 if (!base::ReadFileToString(file, &file_contents)) { |
91 ADD_FAILURE() << "File not found: " << file.value(); | 97 ADD_FAILURE() << "File not found: " << file.value(); |
92 return; | 98 return; |
93 } | 99 } |
94 | 100 |
95 std::string data = | 101 std::string data = |
96 ReadData(client.response_body().value(), file_contents.size()); | 102 ReadData(client.response_body().value(), file_contents.size()); |
97 CHECK_EQ(data, file_contents); | 103 CHECK_EQ(data, file_contents); |
98 } | 104 } |
99 | 105 |
100 net::EmbeddedTestServer* test_server() { return &test_server_; } | 106 net::EmbeddedTestServer* test_server() { return &test_server_; } |
101 NetworkContext* context() { return &context_; } | 107 NetworkContext* context() { return &context_; } |
102 | 108 |
103 private: | 109 private: |
104 base::MessageLoopForIO message_loop_; | 110 base::MessageLoopForIO message_loop_; |
105 net::EmbeddedTestServer test_server_; | 111 net::EmbeddedTestServer test_server_; |
106 NetworkContext context_; | 112 NetworkContext context_; |
107 }; | 113 }; |
108 | 114 |
109 TEST_F(URLLoaderImplTest, Basic) { | 115 TEST_F(URLLoaderImplTest, Basic) { |
110 LoadAndCompareFile("simple_page.html"); | 116 LoadAndCompareFile("simple_page.html"); |
111 } | 117 } |
112 | 118 |
| 119 TEST_F(URLLoaderImplTest, BasicSSL) { |
| 120 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 121 https_server.ServeFilesFromSourceDirectory( |
| 122 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
| 123 ASSERT_TRUE(https_server.Start()); |
| 124 |
| 125 TestURLLoaderClient client; |
| 126 GURL url = https_server.GetURL("/simple_page.html"); |
| 127 Load(url, &client, mojom::kURLLoadOptionSendSSLInfo); |
| 128 ASSERT_TRUE(!!client.ssl_info()); |
| 129 ASSERT_TRUE(!!client.ssl_info()->cert); |
| 130 |
| 131 ASSERT_TRUE( |
| 132 https_server.GetCertificate()->Equals(client.ssl_info()->cert.get())); |
| 133 } |
| 134 |
| 135 TEST_F(URLLoaderImplTest, SSLSentOnlyWhenRequested) { |
| 136 net::EmbeddedTestServer https_server(net::EmbeddedTestServer::TYPE_HTTPS); |
| 137 https_server.ServeFilesFromSourceDirectory( |
| 138 base::FilePath(FILE_PATH_LITERAL("content/test/data"))); |
| 139 ASSERT_TRUE(https_server.Start()); |
| 140 |
| 141 TestURLLoaderClient client; |
| 142 GURL url = https_server.GetURL("/simple_page.html"); |
| 143 Load(url, &client, 0); |
| 144 ASSERT_FALSE(!!client.ssl_info()); |
| 145 } |
| 146 |
113 } // namespace content | 147 } // namespace content |
OLD | NEW |