Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(98)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/FetchDataLoaderTest.cpp

Issue 2292763002: [Fetch API] Implement Request.formData and Response.formData. (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 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 "modules/fetch/FetchDataLoader.h" 5 #include "modules/fetch/FetchDataLoader.h"
6 6
7 #include "core/fileapi/Blob.h"
8 #include "core/html/FormData.h"
7 #include "modules/fetch/BytesConsumerForDataConsumerHandle.h" 9 #include "modules/fetch/BytesConsumerForDataConsumerHandle.h"
8 #include "modules/fetch/BytesConsumerTestUtil.h" 10 #include "modules/fetch/BytesConsumerTestUtil.h"
9 #include "testing/gmock/include/gmock/gmock.h" 11 #include "testing/gmock/include/gmock/gmock.h"
10 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
11 #include <memory> 13 #include <memory>
12 14
13 namespace blink { 15 namespace blink {
14 16
15 namespace { 17 namespace {
16 18
17 using ::testing::ByMove; 19 using ::testing::ByMove;
18 using ::testing::InSequence; 20 using ::testing::InSequence;
19 using ::testing::Return; 21 using ::testing::Return;
20 using ::testing::DoAll; 22 using ::testing::DoAll;
21 using ::testing::StrictMock; 23 using ::testing::StrictMock;
22 using ::testing::_; 24 using ::testing::_;
23 using ::testing::SaveArg; 25 using ::testing::SaveArg;
24 using ::testing::SetArgPointee; 26 using ::testing::SetArgPointee;
25 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>; 27 using Checkpoint = StrictMock<::testing::MockFunction<void(int)>>;
26 using MockFetchDataLoaderClient = 28 using MockFetchDataLoaderClient =
27 BytesConsumerTestUtil::MockFetchDataLoaderClient; 29 BytesConsumerTestUtil::MockFetchDataLoaderClient;
28 using MockBytesConsumer = BytesConsumerTestUtil::MockBytesConsumer; 30 using MockBytesConsumer = BytesConsumerTestUtil::MockBytesConsumer;
29 using Result = BytesConsumer::Result; 31 using Result = BytesConsumer::Result;
30 32
31 constexpr char kQuickBrownFox[] = "Quick brown fox"; 33 constexpr char kQuickBrownFox[] = "Quick brown fox";
32 constexpr size_t kQuickBrownFoxLength = 15; 34 constexpr size_t kQuickBrownFoxLength = 15;
33 constexpr size_t kQuickBrownFoxLengthWithTerminatingNull = 16; 35 constexpr size_t kQuickBrownFoxLengthWithTerminatingNull = 16;
36 constexpr char kQuickBrownFoxFormData[] =
37 "--boundary\r\n"
38 "Content-Disposition: form-data; name=blob; filename=blob\r\n"
39 "Content-Type: text/plain; charset=iso-8859-1\r\n"
40 "\r\n"
41 "Quick brown fox\r\n"
42 "--boundary\r\n"
43 "Content-Disposition: form-data; name=blob\xC2\xA0without\xC2\xA0type; "
44 "filename=\"blob\xC2\xA0without\xC2\xA0type.txt\"\r\n"
45 "\r\n"
46 "Quick brown fox\r\n"
47 "--boundary\r\n"
48 "Content-Disposition: form-data; name=string\r\n"
49 "\r\n"
50 "Quick brown fox\r\n"
51 "--boundary\r\n"
52 "Content-Disposition: form-data; name=string-with-type\r\n"
53 "Content-Type: text/plain; charset=invalid\r\n"
54 "\r\n"
55 "Quick brown fox\r\n"
56 "--boundary--\r\n";
57 constexpr size_t kQuickBrownFoxFormDataLength =
58 WTF_ARRAY_LENGTH(kQuickBrownFoxFormData) - 1u;
34 59
35 TEST(FetchDataLoaderTest, LoadAsBlob) { 60 TEST(FetchDataLoaderTest, LoadAsBlob) {
36 Checkpoint checkpoint; 61 Checkpoint checkpoint;
37 BytesConsumer::Client* client = nullptr; 62 BytesConsumer::Client* client = nullptr;
38 MockBytesConsumer* consumer = MockBytesConsumer::create(); 63 MockBytesConsumer* consumer = MockBytesConsumer::create();
39 64
40 FetchDataLoader* fetchDataLoader = 65 FetchDataLoader* fetchDataLoader =
41 FetchDataLoader::createLoaderAsBlobHandle("text/test"); 66 FetchDataLoader::createLoaderAsBlobHandle("text/test");
42 MockFetchDataLoaderClient* fetchDataLoaderClient = 67 MockFetchDataLoaderClient* fetchDataLoaderClient =
43 MockFetchDataLoaderClient::create(); 68 MockFetchDataLoaderClient::create();
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
343 EXPECT_CALL(*consumer, cancel()); 368 EXPECT_CALL(*consumer, cancel());
344 EXPECT_CALL(checkpoint, Call(3)); 369 EXPECT_CALL(checkpoint, Call(3));
345 370
346 checkpoint.Call(1); 371 checkpoint.Call(1);
347 fetchDataLoader->start(consumer, fetchDataLoaderClient); 372 fetchDataLoader->start(consumer, fetchDataLoaderClient);
348 checkpoint.Call(2); 373 checkpoint.Call(2);
349 fetchDataLoader->cancel(); 374 fetchDataLoader->cancel();
350 checkpoint.Call(3); 375 checkpoint.Call(3);
351 } 376 }
352 377
378 TEST(FetchDataLoaderTest, LoadAsFormData) {
379 Checkpoint checkpoint;
380 BytesConsumer::Client* client = nullptr;
381 MockBytesConsumer* consumer = MockBytesConsumer::create();
382
383 FetchDataLoader* fetchDataLoader =
384 FetchDataLoader::createLoaderAsFormData("boundary");
385 MockFetchDataLoaderClient* fetchDataLoaderClient =
386 MockFetchDataLoaderClient::create();
387 FormData* formData = nullptr;
388
389 InSequence s;
390 EXPECT_CALL(checkpoint, Call(1));
391 EXPECT_CALL(*consumer, setClient(_)).WillOnce(SaveArg<0>(&client));
392 EXPECT_CALL(*consumer, beginRead(_, _))
393 .WillOnce(DoAll(SetArgPointee<0>(nullptr), SetArgPointee<1>(0),
394 Return(Result::ShouldWait)));
395 EXPECT_CALL(checkpoint, Call(2));
396 EXPECT_CALL(*consumer, beginRead(_, _))
397 .WillOnce(DoAll(SetArgPointee<0>(kQuickBrownFoxFormData),
398 SetArgPointee<1>(kQuickBrownFoxFormDataLength),
399 Return(Result::Ok)));
400 EXPECT_CALL(*consumer, endRead(kQuickBrownFoxFormDataLength))
401 .WillOnce(Return(Result::Ok));
402 EXPECT_CALL(*consumer, beginRead(_, _)).WillOnce(Return(Result::Done));
403 EXPECT_CALL(*fetchDataLoaderClient, didFetchDataLoadedFormDataMock(_))
404 .WillOnce(SaveArg<0>(&formData));
405 EXPECT_CALL(checkpoint, Call(3));
406 EXPECT_CALL(*consumer, cancel());
407 EXPECT_CALL(checkpoint, Call(4));
408
409 checkpoint.Call(1);
410 fetchDataLoader->start(consumer, fetchDataLoaderClient);
411 checkpoint.Call(2);
412 ASSERT_TRUE(client);
413 client->onStateChange();
414 checkpoint.Call(3);
415 fetchDataLoader->cancel();
416 checkpoint.Call(4);
417
418 ASSERT_TRUE(formData);
419 ASSERT_EQ(4u, formData->entries().size());
420
421 EXPECT_EQ("blob", formData->entries()[0]->name());
422 EXPECT_EQ("blob", formData->entries()[0]->filename());
423 ASSERT_TRUE(formData->entries()[0]->isFile());
424 EXPECT_EQ(kQuickBrownFoxLength, formData->entries()[0]->blob()->size());
425 EXPECT_EQ("text/plain; charset=iso-8859-1",
426 formData->entries()[0]->blob()->type());
427
428 EXPECT_EQ("blob\xC2\xA0without\xC2\xA0type", formData->entries()[1]->name());
429 EXPECT_EQ("blob\xC2\xA0without\xC2\xA0type.txt",
430 formData->entries()[1]->filename().utf8());
431 ASSERT_TRUE(formData->entries()[1]->isFile());
432 EXPECT_EQ(kQuickBrownFoxLength, formData->entries()[1]->blob()->size());
433 EXPECT_EQ("text/plain", formData->entries()[1]->blob()->type());
434
435 EXPECT_EQ("string", formData->entries()[2]->name());
436 EXPECT_TRUE(formData->entries()[2]->filename().isNull());
437 ASSERT_TRUE(formData->entries()[2]->isString());
438 EXPECT_EQ(kQuickBrownFox, formData->entries()[2]->value());
439
440 EXPECT_EQ("string-with-type", formData->entries()[3]->name());
441 EXPECT_TRUE(formData->entries()[3]->filename().isNull());
442 ASSERT_TRUE(formData->entries()[3]->isString());
443 EXPECT_EQ(kQuickBrownFox, formData->entries()[3]->value());
444 }
445
446 TEST(FetchDataLoaderTest, LoadAsFormDataPartialInput) {
447 Checkpoint checkpoint;
448 BytesConsumer::Client* client = nullptr;
449 MockBytesConsumer* consumer = MockBytesConsumer::create();
450
451 FetchDataLoader* fetchDataLoader =
452 FetchDataLoader::createLoaderAsFormData("boundary");
453 MockFetchDataLoaderClient* fetchDataLoaderClient =
454 MockFetchDataLoaderClient::create();
455
456 InSequence s;
457 EXPECT_CALL(checkpoint, Call(1));
458 EXPECT_CALL(*consumer, setClient(_)).WillOnce(SaveArg<0>(&client));
459 EXPECT_CALL(*consumer, beginRead(_, _))
460 .WillOnce(DoAll(SetArgPointee<0>(nullptr), SetArgPointee<1>(0),
461 Return(Result::ShouldWait)));
462 EXPECT_CALL(checkpoint, Call(2));
463 EXPECT_CALL(*consumer, beginRead(_, _))
464 .WillOnce(DoAll(SetArgPointee<0>(kQuickBrownFoxFormData),
465 SetArgPointee<1>(kQuickBrownFoxFormDataLength - 3u),
466 Return(Result::Ok)));
467 EXPECT_CALL(*consumer, endRead(kQuickBrownFoxFormDataLength - 3u))
468 .WillOnce(Return(Result::Ok));
469 EXPECT_CALL(*consumer, beginRead(_, _)).WillOnce(Return(Result::Done));
470 EXPECT_CALL(*fetchDataLoaderClient, didFetchDataLoadFailed());
471 EXPECT_CALL(checkpoint, Call(3));
472 EXPECT_CALL(*consumer, cancel());
473 EXPECT_CALL(checkpoint, Call(4));
474
475 checkpoint.Call(1);
476 fetchDataLoader->start(consumer, fetchDataLoaderClient);
477 checkpoint.Call(2);
478 ASSERT_TRUE(client);
479 client->onStateChange();
480 checkpoint.Call(3);
481 fetchDataLoader->cancel();
482 checkpoint.Call(4);
483 }
484
485 TEST(FetchDataLoaderTest, LoadAsFormDataFailed) {
486 Checkpoint checkpoint;
487 BytesConsumer::Client* client = nullptr;
488 MockBytesConsumer* consumer = MockBytesConsumer::create();
489
490 FetchDataLoader* fetchDataLoader =
491 FetchDataLoader::createLoaderAsFormData("boundary");
492 MockFetchDataLoaderClient* fetchDataLoaderClient =
493 MockFetchDataLoaderClient::create();
494
495 InSequence s;
496 EXPECT_CALL(checkpoint, Call(1));
497 EXPECT_CALL(*consumer, setClient(_)).WillOnce(SaveArg<0>(&client));
498 EXPECT_CALL(*consumer, beginRead(_, _))
499 .WillOnce(DoAll(SetArgPointee<0>(nullptr), SetArgPointee<1>(0),
500 Return(Result::ShouldWait)));
501 EXPECT_CALL(checkpoint, Call(2));
502 EXPECT_CALL(*consumer, beginRead(_, _))
503 .WillOnce(DoAll(SetArgPointee<0>(kQuickBrownFoxFormData),
504 SetArgPointee<1>(kQuickBrownFoxFormDataLength),
505 Return(Result::Ok)));
506 EXPECT_CALL(*consumer, endRead(kQuickBrownFoxFormDataLength))
507 .WillOnce(Return(Result::Ok));
508 EXPECT_CALL(*consumer, beginRead(_, _)).WillOnce(Return(Result::Error));
509 EXPECT_CALL(*fetchDataLoaderClient, didFetchDataLoadFailed());
510 EXPECT_CALL(checkpoint, Call(3));
511 EXPECT_CALL(*consumer, cancel());
512 EXPECT_CALL(checkpoint, Call(4));
513
514 checkpoint.Call(1);
515 fetchDataLoader->start(consumer, fetchDataLoaderClient);
516 checkpoint.Call(2);
517 ASSERT_TRUE(client);
518 client->onStateChange();
519 checkpoint.Call(3);
520 fetchDataLoader->cancel();
521 checkpoint.Call(4);
522 }
523
524 TEST(FetchDataLoaderTest, LoadAsFormDataCancel) {
525 Checkpoint checkpoint;
526 BytesConsumer::Client* client = nullptr;
527 MockBytesConsumer* consumer = MockBytesConsumer::create();
528
529 FetchDataLoader* fetchDataLoader =
530 FetchDataLoader::createLoaderAsFormData("boundary");
531 MockFetchDataLoaderClient* fetchDataLoaderClient =
532 MockFetchDataLoaderClient::create();
533
534 InSequence s;
535 EXPECT_CALL(checkpoint, Call(1));
536 EXPECT_CALL(*consumer, setClient(_)).WillOnce(SaveArg<0>(&client));
537 EXPECT_CALL(*consumer, beginRead(_, _))
538 .WillOnce(DoAll(SetArgPointee<0>(nullptr), SetArgPointee<1>(0),
539 Return(Result::ShouldWait)));
540 EXPECT_CALL(checkpoint, Call(2));
541 EXPECT_CALL(*consumer, cancel());
542 EXPECT_CALL(checkpoint, Call(3));
543
544 checkpoint.Call(1);
545 fetchDataLoader->start(consumer, fetchDataLoaderClient);
546 checkpoint.Call(2);
547 fetchDataLoader->cancel();
548 checkpoint.Call(3);
549 }
550
353 TEST(FetchDataLoaderTest, LoadAsString) { 551 TEST(FetchDataLoaderTest, LoadAsString) {
354 Checkpoint checkpoint; 552 Checkpoint checkpoint;
355 BytesConsumer::Client* client = nullptr; 553 BytesConsumer::Client* client = nullptr;
356 MockBytesConsumer* consumer = MockBytesConsumer::create(); 554 MockBytesConsumer* consumer = MockBytesConsumer::create();
357 555
358 FetchDataLoader* fetchDataLoader = FetchDataLoader::createLoaderAsString(); 556 FetchDataLoader* fetchDataLoader = FetchDataLoader::createLoaderAsString();
359 MockFetchDataLoaderClient* fetchDataLoaderClient = 557 MockFetchDataLoaderClient* fetchDataLoaderClient =
360 MockFetchDataLoaderClient::create(); 558 MockFetchDataLoaderClient::create();
361 559
362 InSequence s; 560 InSequence s;
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 checkpoint.Call(1); 687 checkpoint.Call(1);
490 fetchDataLoader->start(consumer, fetchDataLoaderClient); 688 fetchDataLoader->start(consumer, fetchDataLoaderClient);
491 checkpoint.Call(2); 689 checkpoint.Call(2);
492 fetchDataLoader->cancel(); 690 fetchDataLoader->cancel();
493 checkpoint.Call(3); 691 checkpoint.Call(3);
494 } 692 }
495 693
496 } // namespace 694 } // namespace
497 695
498 } // namespace blink 696 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698