OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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_file_dir_job.h" | 5 #include "net/url_request/url_request_file_dir_job.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 TestURLRequestContext context_; | 102 TestURLRequestContext context_; |
103 TestDirectoryURLRequestDelegate delegate_; | 103 TestDirectoryURLRequestDelegate delegate_; |
104 scoped_refptr<IOBuffer> buffer_; | 104 scoped_refptr<IOBuffer> buffer_; |
105 }; | 105 }; |
106 | 106 |
107 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { | 107 TEST_F(URLRequestFileDirTest, ListCompletionOnNoPending) { |
108 base::ScopedTempDir directory; | 108 base::ScopedTempDir directory; |
109 // It is necessary to pass an existing directory to UrlRequest object, | 109 // It is necessary to pass an existing directory to UrlRequest object, |
110 // but it will be deleted for testing purpose after request is started. | 110 // but it will be deleted for testing purpose after request is started. |
111 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 111 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
112 TestJobFactory factory(directory.path()); | 112 TestJobFactory factory(directory.GetPath()); |
113 context_.set_job_factory(&factory); | 113 context_.set_job_factory(&factory); |
114 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 114 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
115 FilePathToFileURL( | 115 FilePathToFileURL( |
116 directory.path().AppendASCII("this_path_does_not_exist")), | 116 directory.GetPath().AppendASCII("this_path_does_not_exist")), |
117 DEFAULT_PRIORITY, &delegate_)); | 117 DEFAULT_PRIORITY, &delegate_)); |
118 | 118 |
119 request->Start(); | 119 request->Start(); |
120 ASSERT_TRUE(directory.Delete()); | 120 ASSERT_TRUE(directory.Delete()); |
121 | 121 |
122 // Since the DirectoryLister is running on the network thread, this | 122 // Since the DirectoryLister is running on the network thread, this |
123 // will spin the message loop until the read error is returned to the | 123 // will spin the message loop until the read error is returned to the |
124 // URLRequestFileDirJob. | 124 // URLRequestFileDirJob. |
125 base::RunLoop().RunUntilIdle(); | 125 base::RunLoop().RunUntilIdle(); |
126 ASSERT_TRUE(delegate_.got_response_started()); | 126 ASSERT_TRUE(delegate_.got_response_started()); |
127 | 127 |
128 int bytes_read = 0; | 128 int bytes_read = 0; |
129 EXPECT_FALSE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); | 129 EXPECT_FALSE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); |
130 | 130 |
131 // The URLRequestFileDirJobShould return the cached read error synchronously. | 131 // The URLRequestFileDirJobShould return the cached read error synchronously. |
132 // If it's not returned synchronously, the code path this is intended to test | 132 // If it's not returned synchronously, the code path this is intended to test |
133 // was not executed. | 133 // was not executed. |
134 EXPECT_THAT(request->status().ToNetError(), IsError(ERR_FILE_NOT_FOUND)); | 134 EXPECT_THAT(request->status().ToNetError(), IsError(ERR_FILE_NOT_FOUND)); |
135 } | 135 } |
136 | 136 |
137 // Test the case where reading the response completes synchronously. | 137 // Test the case where reading the response completes synchronously. |
138 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { | 138 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileSync) { |
139 base::ScopedTempDir directory; | 139 base::ScopedTempDir directory; |
140 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 140 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
141 base::FilePath path; | 141 base::FilePath path; |
142 base::CreateTemporaryFileInDir(directory.path(), &path); | 142 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
143 | 143 |
144 TestJobFactory factory(directory.path()); | 144 TestJobFactory factory(directory.GetPath()); |
145 context_.set_job_factory(&factory); | 145 context_.set_job_factory(&factory); |
146 | 146 |
147 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 147 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
148 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); | 148 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate_)); |
149 request->Start(); | 149 request->Start(); |
150 EXPECT_TRUE(request->is_pending()); | 150 EXPECT_TRUE(request->is_pending()); |
151 | 151 |
152 // Since the DirectoryLister is running on the network thread, this will spin | 152 // Since the DirectoryLister is running on the network thread, this will spin |
153 // the message loop until the URLRequetsFileDirJob has received the | 153 // the message loop until the URLRequetsFileDirJob has received the |
154 // entire directory listing and cached it. | 154 // entire directory listing and cached it. |
155 base::RunLoop().RunUntilIdle(); | 155 base::RunLoop().RunUntilIdle(); |
156 | 156 |
157 int bytes_read = 0; | 157 int bytes_read = 0; |
158 // This will complete synchronously, since the URLRequetsFileDirJob had | 158 // This will complete synchronously, since the URLRequetsFileDirJob had |
159 // directory listing cached in memory. | 159 // directory listing cached in memory. |
160 EXPECT_TRUE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); | 160 EXPECT_TRUE(request->Read(buffer_.get(), kBufferSize, &bytes_read)); |
161 | 161 |
162 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); | 162 EXPECT_EQ(URLRequestStatus::SUCCESS, request->status().status()); |
163 | 163 |
164 ASSERT_GT(bytes_read, 0); | 164 ASSERT_GT(bytes_read, 0); |
165 ASSERT_LE(bytes_read, kBufferSize); | 165 ASSERT_LE(bytes_read, kBufferSize); |
166 std::string data(buffer_->data(), bytes_read); | 166 std::string data(buffer_->data(), bytes_read); |
167 EXPECT_TRUE(data.find(directory.path().BaseName().MaybeAsASCII()) != | 167 EXPECT_TRUE(data.find(directory.GetPath().BaseName().MaybeAsASCII()) != |
168 std::string::npos); | 168 std::string::npos); |
169 EXPECT_TRUE(data.find(path.BaseName().MaybeAsASCII()) != std::string::npos); | 169 EXPECT_TRUE(data.find(path.BaseName().MaybeAsASCII()) != std::string::npos); |
170 } | 170 } |
171 | 171 |
172 // Test the case where reading the response completes asynchronously. | 172 // Test the case where reading the response completes asynchronously. |
173 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileAsync) { | 173 TEST_F(URLRequestFileDirTest, DirectoryWithASingleFileAsync) { |
174 base::ScopedTempDir directory; | 174 base::ScopedTempDir directory; |
175 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 175 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
176 base::FilePath path; | 176 base::FilePath path; |
177 base::CreateTemporaryFileInDir(directory.path(), &path); | 177 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
178 | 178 |
179 TestJobFactory factory(directory.path()); | 179 TestJobFactory factory(directory.GetPath()); |
180 context_.set_job_factory(&factory); | 180 context_.set_job_factory(&factory); |
181 | 181 |
182 TestDelegate delegate; | 182 TestDelegate delegate; |
183 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 183 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
184 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); | 184 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); |
185 request->Start(); | 185 request->Start(); |
186 EXPECT_TRUE(request->is_pending()); | 186 EXPECT_TRUE(request->is_pending()); |
187 | 187 |
188 base::RunLoop().Run(); | 188 base::RunLoop().Run(); |
189 | 189 |
190 ASSERT_GT(delegate.bytes_received(), 0); | 190 ASSERT_GT(delegate.bytes_received(), 0); |
191 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 191 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
192 EXPECT_TRUE(delegate.data_received().find( | 192 EXPECT_TRUE(delegate.data_received().find( |
193 directory.path().BaseName().MaybeAsASCII()) != | 193 directory.GetPath().BaseName().MaybeAsASCII()) != |
194 std::string::npos); | 194 std::string::npos); |
195 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != | 195 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != |
196 std::string::npos); | 196 std::string::npos); |
197 } | 197 } |
198 | 198 |
199 TEST_F(URLRequestFileDirTest, DirectoryWithAFileAndSubdirectory) { | 199 TEST_F(URLRequestFileDirTest, DirectoryWithAFileAndSubdirectory) { |
200 base::ScopedTempDir directory; | 200 base::ScopedTempDir directory; |
201 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 201 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
202 | 202 |
203 base::FilePath sub_dir; | 203 base::FilePath sub_dir; |
204 CreateTemporaryDirInDir(directory.path(), | 204 CreateTemporaryDirInDir(directory.GetPath(), |
205 FILE_PATH_LITERAL("CreateNewSubDirectoryInDirectory"), | 205 FILE_PATH_LITERAL("CreateNewSubDirectoryInDirectory"), |
206 &sub_dir); | 206 &sub_dir); |
207 | 207 |
208 base::FilePath path; | 208 base::FilePath path; |
209 base::CreateTemporaryFileInDir(directory.path(), &path); | 209 base::CreateTemporaryFileInDir(directory.GetPath(), &path); |
210 | 210 |
211 TestJobFactory factory(directory.path()); | 211 TestJobFactory factory(directory.GetPath()); |
212 context_.set_job_factory(&factory); | 212 context_.set_job_factory(&factory); |
213 | 213 |
214 TestDelegate delegate; | 214 TestDelegate delegate; |
215 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 215 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
216 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); | 216 FilePathToFileURL(path), DEFAULT_PRIORITY, &delegate)); |
217 request->Start(); | 217 request->Start(); |
218 EXPECT_TRUE(request->is_pending()); | 218 EXPECT_TRUE(request->is_pending()); |
219 | 219 |
220 base::RunLoop().Run(); | 220 base::RunLoop().Run(); |
221 | 221 |
222 ASSERT_GT(delegate.bytes_received(), 0); | 222 ASSERT_GT(delegate.bytes_received(), 0); |
223 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 223 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
224 EXPECT_TRUE(delegate.data_received().find( | 224 EXPECT_TRUE(delegate.data_received().find( |
225 directory.path().BaseName().MaybeAsASCII()) != | 225 directory.GetPath().BaseName().MaybeAsASCII()) != |
226 std::string::npos); | 226 std::string::npos); |
227 EXPECT_TRUE(delegate.data_received().find( | 227 EXPECT_TRUE(delegate.data_received().find( |
228 sub_dir.BaseName().MaybeAsASCII()) != std::string::npos); | 228 sub_dir.BaseName().MaybeAsASCII()) != std::string::npos); |
229 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != | 229 EXPECT_TRUE(delegate.data_received().find(path.BaseName().MaybeAsASCII()) != |
230 std::string::npos); | 230 std::string::npos); |
231 } | 231 } |
232 | 232 |
233 TEST_F(URLRequestFileDirTest, EmptyDirectory) { | 233 TEST_F(URLRequestFileDirTest, EmptyDirectory) { |
234 base::ScopedTempDir directory; | 234 base::ScopedTempDir directory; |
235 ASSERT_TRUE(directory.CreateUniqueTempDir()); | 235 ASSERT_TRUE(directory.CreateUniqueTempDir()); |
236 | 236 |
237 TestJobFactory factory(directory.path()); | 237 TestJobFactory factory(directory.GetPath()); |
238 context_.set_job_factory(&factory); | 238 context_.set_job_factory(&factory); |
239 | 239 |
240 TestDelegate delegate; | 240 TestDelegate delegate; |
241 std::unique_ptr<URLRequest> request(context_.CreateRequest( | 241 std::unique_ptr<URLRequest> request(context_.CreateRequest( |
242 FilePathToFileURL(directory.path()), DEFAULT_PRIORITY, &delegate)); | 242 FilePathToFileURL(directory.GetPath()), DEFAULT_PRIORITY, &delegate)); |
243 request->Start(); | 243 request->Start(); |
244 EXPECT_TRUE(request->is_pending()); | 244 EXPECT_TRUE(request->is_pending()); |
245 | 245 |
246 base::RunLoop().Run(); | 246 base::RunLoop().Run(); |
247 | 247 |
248 ASSERT_GT(delegate.bytes_received(), 0); | 248 ASSERT_GT(delegate.bytes_received(), 0); |
249 ASSERT_LE(delegate.bytes_received(), kBufferSize); | 249 ASSERT_LE(delegate.bytes_received(), kBufferSize); |
250 EXPECT_TRUE(delegate.data_received().find( | 250 EXPECT_TRUE(delegate.data_received().find( |
251 directory.path().BaseName().MaybeAsASCII()) != | 251 directory.GetPath().BaseName().MaybeAsASCII()) != |
252 std::string::npos); | 252 std::string::npos); |
253 } | 253 } |
254 | 254 |
255 } // namespace | 255 } // namespace |
256 | 256 |
257 } // namespace net | 257 } // namespace net |
OLD | NEW |