OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/chromeos/drive/drive_url_request_job.h" | 5 #include "chrome/browser/chromeos/drive/drive_url_request_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/sequenced_task_runner.h" | 10 #include "base/sequenced_task_runner.h" |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 if (test_util::ReadAllData(reader.get(), &content) != net::OK) | 195 if (test_util::ReadAllData(reader.get(), &content) != net::OK) |
196 return false; | 196 return false; |
197 | 197 |
198 if (static_cast<size_t>(entry->file_info().size()) != content.size()) | 198 if (static_cast<size_t>(entry->file_info().size()) != content.size()) |
199 return false; | 199 return false; |
200 | 200 |
201 *out_content = content; | 201 *out_content = content; |
202 return true; | 202 return true; |
203 } | 203 } |
204 | 204 |
205 MessageLoopForIO message_loop_; | 205 base::MessageLoopForIO message_loop_; |
206 content::TestBrowserThread ui_thread_; | 206 content::TestBrowserThread ui_thread_; |
207 content::TestBrowserThread io_thread_; | 207 content::TestBrowserThread io_thread_; |
208 | 208 |
209 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; | 209 scoped_ptr<google_apis::FakeDriveService> fake_drive_service_; |
210 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; | 210 scoped_ptr<test_util::FakeFileSystem> fake_file_system_; |
211 | 211 |
212 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; | 212 scoped_ptr<net::TestNetworkDelegate> test_network_delegate_; |
213 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; | 213 scoped_ptr<TestURLRequestJobFactory> test_url_request_job_factory_; |
214 scoped_ptr<net::URLRequestContext> url_request_context_; | 214 scoped_ptr<net::URLRequestContext> url_request_context_; |
215 scoped_ptr<TestDelegate> test_delegate_; | 215 scoped_ptr<TestDelegate> test_delegate_; |
216 }; | 216 }; |
217 | 217 |
218 TEST_F(DriveURLRequestJobTest, NonGetMethod) { | 218 TEST_F(DriveURLRequestJobTest, NonGetMethod) { |
219 net::URLRequest request( | 219 net::URLRequest request( |
220 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 220 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
221 url_request_context_.get(), test_network_delegate_.get()); | 221 url_request_context_.get(), test_network_delegate_.get()); |
222 request.set_method("POST"); // Set non "GET" method. | 222 request.set_method("POST"); // Set non "GET" method. |
223 request.Start(); | 223 request.Start(); |
224 | 224 |
225 MessageLoop::current()->Run(); | 225 base::MessageLoop::current()->Run(); |
226 | 226 |
227 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 227 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
228 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); | 228 EXPECT_EQ(net::ERR_METHOD_NOT_SUPPORTED, request.status().error()); |
229 } | 229 } |
230 | 230 |
231 TEST_F(DriveURLRequestJobTest, RegularFile) { | 231 TEST_F(DriveURLRequestJobTest, RegularFile) { |
232 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 232 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
233 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 233 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
234 | 234 |
235 // For the first time, the file should be fetched from the server. | 235 // For the first time, the file should be fetched from the server. |
236 { | 236 { |
237 net::URLRequest request( | 237 net::URLRequest request( |
238 kTestUrl, test_delegate_.get(), | 238 kTestUrl, test_delegate_.get(), |
239 url_request_context_.get(), test_network_delegate_.get()); | 239 url_request_context_.get(), test_network_delegate_.get()); |
240 request.Start(); | 240 request.Start(); |
241 | 241 |
242 MessageLoop::current()->Run(); | 242 base::MessageLoop::current()->Run(); |
243 | 243 |
244 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 244 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
245 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" | 245 // It looks weird, but the mime type for the "File 1.txt" is "audio/mpeg" |
246 // on the server. | 246 // on the server. |
247 std::string mime_type; | 247 std::string mime_type; |
248 request.GetMimeType(&mime_type); | 248 request.GetMimeType(&mime_type); |
249 EXPECT_EQ("audio/mpeg", mime_type); | 249 EXPECT_EQ("audio/mpeg", mime_type); |
250 | 250 |
251 // Reading file must be done after |request| runs, otherwise | 251 // Reading file must be done after |request| runs, otherwise |
252 // it'll create a local cache file, and we cannot test correctly. | 252 // it'll create a local cache file, and we cannot test correctly. |
253 std::string expected_data; | 253 std::string expected_data; |
254 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 254 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
255 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 255 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
256 } | 256 } |
257 | 257 |
258 // For the second time, the locally cached file should be used. | 258 // For the second time, the locally cached file should be used. |
259 // The caching emulation is done by FakeFileSystem. | 259 // The caching emulation is done by FakeFileSystem. |
260 { | 260 { |
261 test_delegate_.reset(new TestDelegate); | 261 test_delegate_.reset(new TestDelegate); |
262 net::URLRequest request( | 262 net::URLRequest request( |
263 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 263 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
264 url_request_context_.get(), test_network_delegate_.get()); | 264 url_request_context_.get(), test_network_delegate_.get()); |
265 request.Start(); | 265 request.Start(); |
266 | 266 |
267 MessageLoop::current()->Run(); | 267 base::MessageLoop::current()->Run(); |
268 | 268 |
269 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 269 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
270 std::string mime_type; | 270 std::string mime_type; |
271 request.GetMimeType(&mime_type); | 271 request.GetMimeType(&mime_type); |
272 EXPECT_EQ("audio/mpeg", mime_type); | 272 EXPECT_EQ("audio/mpeg", mime_type); |
273 | 273 |
274 std::string expected_data; | 274 std::string expected_data; |
275 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 275 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
276 EXPECT_EQ(expected_data, test_delegate_->data_received()); | 276 EXPECT_EQ(expected_data, test_delegate_->data_received()); |
277 } | 277 } |
278 } | 278 } |
279 | 279 |
280 TEST_F(DriveURLRequestJobTest, HostedDocument) { | 280 TEST_F(DriveURLRequestJobTest, HostedDocument) { |
281 // Open a gdoc file. | 281 // Open a gdoc file. |
282 test_delegate_->set_quit_on_redirect(true); | 282 test_delegate_->set_quit_on_redirect(true); |
283 net::URLRequest request( | 283 net::URLRequest request( |
284 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), | 284 GURL("drive:drive/root/Document 1 excludeDir-test.gdoc"), |
285 test_delegate_.get(), | 285 test_delegate_.get(), |
286 url_request_context_.get(), test_network_delegate_.get()); | 286 url_request_context_.get(), test_network_delegate_.get()); |
287 request.Start(); | 287 request.Start(); |
288 | 288 |
289 MessageLoop::current()->Run(); | 289 base::MessageLoop::current()->Run(); |
290 | 290 |
291 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 291 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
292 // Make sure that a hosted document triggers redirection. | 292 // Make sure that a hosted document triggers redirection. |
293 EXPECT_TRUE(request.is_redirecting()); | 293 EXPECT_TRUE(request.is_redirecting()); |
294 EXPECT_EQ(GURL("https://3_document_alternate_link"), | 294 EXPECT_EQ(GURL("https://3_document_alternate_link"), |
295 test_delegate_->redirect_url()); | 295 test_delegate_->redirect_url()); |
296 } | 296 } |
297 | 297 |
298 TEST_F(DriveURLRequestJobTest, RootDirectory) { | 298 TEST_F(DriveURLRequestJobTest, RootDirectory) { |
299 net::URLRequest request( | 299 net::URLRequest request( |
300 GURL("drive:drive/root"), test_delegate_.get(), | 300 GURL("drive:drive/root"), test_delegate_.get(), |
301 url_request_context_.get(), test_network_delegate_.get()); | 301 url_request_context_.get(), test_network_delegate_.get()); |
302 request.Start(); | 302 request.Start(); |
303 | 303 |
304 MessageLoop::current()->Run(); | 304 base::MessageLoop::current()->Run(); |
305 | 305 |
306 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 306 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
307 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 307 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
308 } | 308 } |
309 | 309 |
310 TEST_F(DriveURLRequestJobTest, Directory) { | 310 TEST_F(DriveURLRequestJobTest, Directory) { |
311 net::URLRequest request( | 311 net::URLRequest request( |
312 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), | 312 GURL("drive:drive/root/Directory 1"), test_delegate_.get(), |
313 url_request_context_.get(), test_network_delegate_.get()); | 313 url_request_context_.get(), test_network_delegate_.get()); |
314 request.Start(); | 314 request.Start(); |
315 | 315 |
316 MessageLoop::current()->Run(); | 316 base::MessageLoop::current()->Run(); |
317 | 317 |
318 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 318 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
319 EXPECT_EQ(net::ERR_FAILED, request.status().error()); | 319 EXPECT_EQ(net::ERR_FAILED, request.status().error()); |
320 } | 320 } |
321 | 321 |
322 TEST_F(DriveURLRequestJobTest, NonExistingFile) { | 322 TEST_F(DriveURLRequestJobTest, NonExistingFile) { |
323 net::URLRequest request( | 323 net::URLRequest request( |
324 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), | 324 GURL("drive:drive/root/non-existing-file.txt"), test_delegate_.get(), |
325 url_request_context_.get(), test_network_delegate_.get()); | 325 url_request_context_.get(), test_network_delegate_.get()); |
326 request.Start(); | 326 request.Start(); |
327 | 327 |
328 MessageLoop::current()->Run(); | 328 base::MessageLoop::current()->Run(); |
329 | 329 |
330 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 330 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
331 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); | 331 EXPECT_EQ(net::ERR_FILE_NOT_FOUND, request.status().error()); |
332 } | 332 } |
333 | 333 |
334 TEST_F(DriveURLRequestJobTest, WrongFormat) { | 334 TEST_F(DriveURLRequestJobTest, WrongFormat) { |
335 net::URLRequest request( | 335 net::URLRequest request( |
336 GURL("drive:"), test_delegate_.get(), | 336 GURL("drive:"), test_delegate_.get(), |
337 url_request_context_.get(), test_network_delegate_.get()); | 337 url_request_context_.get(), test_network_delegate_.get()); |
338 request.Start(); | 338 request.Start(); |
339 | 339 |
340 MessageLoop::current()->Run(); | 340 base::MessageLoop::current()->Run(); |
341 | 341 |
342 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 342 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
343 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); | 343 EXPECT_EQ(net::ERR_INVALID_URL, request.status().error()); |
344 } | 344 } |
345 | 345 |
346 TEST_F(DriveURLRequestJobTest, Cancel) { | 346 TEST_F(DriveURLRequestJobTest, Cancel) { |
347 net::URLRequest request( | 347 net::URLRequest request( |
348 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), | 348 GURL("drive:drive/root/File 1.txt"), test_delegate_.get(), |
349 url_request_context_.get(), test_network_delegate_.get()); | 349 url_request_context_.get(), test_network_delegate_.get()); |
350 | 350 |
351 // Start the request, and cancel it immediately after it. | 351 // Start the request, and cancel it immediately after it. |
352 request.Start(); | 352 request.Start(); |
353 request.Cancel(); | 353 request.Cancel(); |
354 | 354 |
355 MessageLoop::current()->Run(); | 355 base::MessageLoop::current()->Run(); |
356 | 356 |
357 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); | 357 EXPECT_EQ(net::URLRequestStatus::CANCELED, request.status().status()); |
358 } | 358 } |
359 | 359 |
360 TEST_F(DriveURLRequestJobTest, RangeHeader) { | 360 TEST_F(DriveURLRequestJobTest, RangeHeader) { |
361 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 361 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
362 const base::FilePath kTestFilePath("drive/root/File 1.txt"); | 362 const base::FilePath kTestFilePath("drive/root/File 1.txt"); |
363 | 363 |
364 net::URLRequest request( | 364 net::URLRequest request( |
365 kTestUrl, test_delegate_.get(), | 365 kTestUrl, test_delegate_.get(), |
366 url_request_context_.get(), test_network_delegate_.get()); | 366 url_request_context_.get(), test_network_delegate_.get()); |
367 | 367 |
368 // Set range header. | 368 // Set range header. |
369 request.SetExtraRequestHeaderByName( | 369 request.SetExtraRequestHeaderByName( |
370 "Range", "bytes=3-5", false /* overwrite */); | 370 "Range", "bytes=3-5", false /* overwrite */); |
371 request.Start(); | 371 request.Start(); |
372 | 372 |
373 MessageLoop::current()->Run(); | 373 base::MessageLoop::current()->Run(); |
374 | 374 |
375 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); | 375 EXPECT_EQ(net::URLRequestStatus::SUCCESS, request.status().status()); |
376 | 376 |
377 // Reading file must be done after |request| runs, otherwise | 377 // Reading file must be done after |request| runs, otherwise |
378 // it'll create a local cache file, and we cannot test correctly. | 378 // it'll create a local cache file, and we cannot test correctly. |
379 std::string expected_data; | 379 std::string expected_data; |
380 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); | 380 ASSERT_TRUE(ReadDriveFileSync(kTestFilePath, &expected_data)); |
381 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); | 381 EXPECT_EQ(expected_data.substr(3, 3), test_delegate_->data_received()); |
382 } | 382 } |
383 | 383 |
384 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { | 384 TEST_F(DriveURLRequestJobTest, WrongRangeHeader) { |
385 const GURL kTestUrl("drive:drive/root/File 1.txt"); | 385 const GURL kTestUrl("drive:drive/root/File 1.txt"); |
386 | 386 |
387 net::URLRequest request( | 387 net::URLRequest request( |
388 kTestUrl, test_delegate_.get(), | 388 kTestUrl, test_delegate_.get(), |
389 url_request_context_.get(), test_network_delegate_.get()); | 389 url_request_context_.get(), test_network_delegate_.get()); |
390 | 390 |
391 // Set range header. | 391 // Set range header. |
392 request.SetExtraRequestHeaderByName( | 392 request.SetExtraRequestHeaderByName( |
393 "Range", "Wrong Range Header Value", false /* overwrite */); | 393 "Range", "Wrong Range Header Value", false /* overwrite */); |
394 request.Start(); | 394 request.Start(); |
395 | 395 |
396 MessageLoop::current()->Run(); | 396 base::MessageLoop::current()->Run(); |
397 | 397 |
398 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); | 398 EXPECT_EQ(net::URLRequestStatus::FAILED, request.status().status()); |
399 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); | 399 EXPECT_EQ(net::ERR_REQUEST_RANGE_NOT_SATISFIABLE, request.status().error()); |
400 } | 400 } |
401 | 401 |
402 } // namespace drive | 402 } // namespace drive |
OLD | NEW |