| Index: third_party/WebKit/Source/modules/fetch/FormDataBytesConsumerTest.cpp
|
| diff --git a/third_party/WebKit/Source/modules/fetch/FormDataBytesConsumerTest.cpp b/third_party/WebKit/Source/modules/fetch/FormDataBytesConsumerTest.cpp
|
| index 78ad560a959ef824e4c7dd62dca0591a0b1d2403..18f6ae51f78e92db267751f36f52af3d8696b141 100644
|
| --- a/third_party/WebKit/Source/modules/fetch/FormDataBytesConsumerTest.cpp
|
| +++ b/third_party/WebKit/Source/modules/fetch/FormDataBytesConsumerTest.cpp
|
| @@ -28,6 +28,23 @@ using ::testing::InSequence;
|
| using ::testing::Return;
|
| using Checkpoint = ::testing::StrictMock<::testing::MockFunction<void(int)>>;
|
| using MockBytesConsumer = BytesConsumerTestUtil::MockBytesConsumer;
|
| +using ReplayingBytesConsumer = BytesConsumerTestUtil::ReplayingBytesConsumer;
|
| +using Command = BytesConsumerTestUtil::Command;
|
| +
|
| +BytesConsumer* createStubBytesConsumer(ExecutionContext* executionContext,
|
| + PassRefPtr<BlobDataHandle> handle) {
|
| + auto* consumer = new ReplayingBytesConsumer(executionContext);
|
| + consumer->add(Command(Command::Data, "type = "));
|
| + consumer->add(Command(Command::Data, handle->type().utf8().data()));
|
| + consumer->add(Command(Command::Done));
|
| + return consumer;
|
| +}
|
| +
|
| +BytesConsumer* returnMockBytesConsumer(MockBytesConsumer* bytesConsumer,
|
| + ExecutionContext*,
|
| + PassRefPtr<BlobDataHandle>) {
|
| + return bytesConsumer;
|
| +}
|
|
|
| String toString(const Vector<char>& v) {
|
| return String(v.data(), v.size());
|
| @@ -41,6 +58,7 @@ PassRefPtr<EncodedFormData> complexFormData() {
|
| data->appendFileSystemURLRange(KURL(KURL(), "file:///foo/bar/baz"), 6, 7, 8);
|
| std::unique_ptr<BlobData> blobData = BlobData::create();
|
| blobData->appendText("hello", false);
|
| + blobData->setContentType("text/plain");
|
| auto size = blobData->length();
|
| RefPtr<BlobDataHandle> blobDataHandle =
|
| BlobDataHandle::create(std::move(blobData), size);
|
| @@ -130,27 +148,19 @@ TEST_F(FormDataBytesConsumerTest, TwoPhaseReadFromSimpleFormData) {
|
|
|
| TEST_F(FormDataBytesConsumerTest, TwoPhaseReadFromComplexFormData) {
|
| RefPtr<EncodedFormData> data = complexFormData();
|
| - MockBytesConsumer* underlying = MockBytesConsumer::create();
|
| - BytesConsumer* consumer =
|
| - FormDataBytesConsumer::createForTesting(getDocument(), data, underlying);
|
| - Checkpoint checkpoint;
|
| + BytesConsumer* consumer = FormDataBytesConsumer::createForTesting(
|
| + getDocument(), data, WTF::bind(createStubBytesConsumer));
|
|
|
| - const char* buffer = nullptr;
|
| - size_t available = 0;
|
| + auto result = (new BytesConsumerTestUtil::TwoPhaseReader(consumer))->run();
|
|
|
| - InSequence s;
|
| - EXPECT_CALL(checkpoint, Call(1));
|
| - EXPECT_CALL(*underlying, beginRead(&buffer, &available))
|
| - .WillOnce(Return(Result::Ok));
|
| - EXPECT_CALL(checkpoint, Call(2));
|
| - EXPECT_CALL(*underlying, endRead(0)).WillOnce(Return(Result::Ok));
|
| - EXPECT_CALL(checkpoint, Call(3));
|
| + ASSERT_EQ(Result::Done, result.first);
|
| + String expected =
|
| + "foo"
|
| + "type = application/octet-stream"
|
| + "type = application/octet-stream"
|
| + "type = text/plain";
|
|
|
| - checkpoint.Call(1);
|
| - ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available));
|
| - checkpoint.Call(2);
|
| - EXPECT_EQ(Result::Ok, consumer->endRead(0));
|
| - checkpoint.Call(3);
|
| + EXPECT_EQ(expected, toString(result.second));
|
| }
|
|
|
| TEST_F(FormDataBytesConsumerTest, EndReadCanReturnDone) {
|
| @@ -221,10 +231,12 @@ TEST_F(FormDataBytesConsumerTest, DrainAsBlobDataHandleFromComplexFormData) {
|
|
|
| BytesConsumer* consumer =
|
| new FormDataBytesConsumer(getDocument(), inputFormData);
|
| - RefPtr<BlobDataHandle> blobDataHandle = consumer->drainAsBlobDataHandle();
|
| - ASSERT_TRUE(blobDataHandle);
|
| + EXPECT_EQ(nullptr, consumer->drainAsBlobDataHandle());
|
| + EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting,
|
| + consumer->getPublicState());
|
|
|
| - EXPECT_FALSE(consumer->drainAsFormData());
|
| + EXPECT_TRUE(consumer->drainAsFormData());
|
| + EXPECT_EQ(BytesConsumer::PublicState::Closed, consumer->getPublicState());
|
| const char* buffer = nullptr;
|
| size_t available = 0;
|
| EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available));
|
| @@ -281,7 +293,9 @@ TEST_F(FormDataBytesConsumerTest, DrainAsFormDataFromComplexFormData) {
|
| BytesConsumer* consumer =
|
| new FormDataBytesConsumer(getDocument(), inputFormData);
|
| EXPECT_EQ(inputFormData, consumer->drainAsFormData());
|
| + EXPECT_EQ(BytesConsumer::PublicState::Closed, consumer->getPublicState());
|
| EXPECT_FALSE(consumer->drainAsBlobDataHandle());
|
| + EXPECT_EQ(BytesConsumer::PublicState::Closed, consumer->getPublicState());
|
| const char* buffer = nullptr;
|
| size_t available = 0;
|
| EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available));
|
| @@ -303,80 +317,72 @@ TEST_F(FormDataBytesConsumerTest, BeginReadAffectsDraining) {
|
| }
|
|
|
| TEST_F(FormDataBytesConsumerTest, BeginReadAffectsDrainingWithComplexFormData) {
|
| - MockBytesConsumer* underlying = MockBytesConsumer::create();
|
| BytesConsumer* consumer = FormDataBytesConsumer::createForTesting(
|
| - getDocument(), complexFormData(), underlying);
|
| + getDocument(), complexFormData(), WTF::bind(createStubBytesConsumer));
|
|
|
| const char* buffer = nullptr;
|
| size_t available = 0;
|
| - Checkpoint checkpoint;
|
|
|
| - InSequence s;
|
| - EXPECT_CALL(checkpoint, Call(1));
|
| - EXPECT_CALL(*underlying, beginRead(&buffer, &available))
|
| - .WillOnce(Return(Result::Ok));
|
| - EXPECT_CALL(*underlying, endRead(0)).WillOnce(Return(Result::Ok));
|
| - EXPECT_CALL(checkpoint, Call(2));
|
| - // drainAsFormData should not be called here.
|
| - EXPECT_CALL(checkpoint, Call(3));
|
| - EXPECT_CALL(*underlying, drainAsBlobDataHandle(_));
|
| - EXPECT_CALL(checkpoint, Call(4));
|
| - // |consumer| delegates the getPublicState call to |underlying|.
|
| - EXPECT_CALL(*underlying, getPublicState())
|
| - .WillOnce(Return(BytesConsumer::PublicState::ReadableOrWaiting));
|
| - EXPECT_CALL(checkpoint, Call(5));
|
| -
|
| - checkpoint.Call(1);
|
| ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available));
|
| ASSERT_EQ(Result::Ok, consumer->endRead(0));
|
| - checkpoint.Call(2);
|
| EXPECT_FALSE(consumer->drainAsFormData());
|
| - checkpoint.Call(3);
|
| EXPECT_FALSE(consumer->drainAsBlobDataHandle());
|
| - checkpoint.Call(4);
|
| EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting,
|
| consumer->getPublicState());
|
| - checkpoint.Call(5);
|
| }
|
|
|
| -TEST_F(FormDataBytesConsumerTest, SetClientWithComplexFormData) {
|
| +// The consumer is cancelled before creating a ByteConsumer for a blob.
|
| +TEST_F(FormDataBytesConsumerTest, CancelWithComplexFormData1) {
|
| RefPtr<EncodedFormData> inputFormData = complexFormData();
|
|
|
| MockBytesConsumer* underlying = MockBytesConsumer::create();
|
| BytesConsumer* consumer = FormDataBytesConsumer::createForTesting(
|
| - getDocument(), inputFormData, underlying);
|
| + getDocument(), inputFormData,
|
| + WTF::bind(returnMockBytesConsumer, wrapPersistent(underlying)));
|
| Checkpoint checkpoint;
|
|
|
| InSequence s;
|
| EXPECT_CALL(checkpoint, Call(1));
|
| - EXPECT_CALL(*underlying, setClient(_));
|
| EXPECT_CALL(checkpoint, Call(2));
|
| - EXPECT_CALL(*underlying, clearClient());
|
| - EXPECT_CALL(checkpoint, Call(3));
|
|
|
| checkpoint.Call(1);
|
| - consumer->setClient(new NoopClient());
|
| + consumer->cancel();
|
| checkpoint.Call(2);
|
| - consumer->clearClient();
|
| - checkpoint.Call(3);
|
| }
|
|
|
| -TEST_F(FormDataBytesConsumerTest, CancelWithComplexFormData) {
|
| +// The consumer is cancelled after creating a ByteConsumer for a blob.
|
| +TEST_F(FormDataBytesConsumerTest, CancelWithComplexFormData2) {
|
| RefPtr<EncodedFormData> inputFormData = complexFormData();
|
|
|
| MockBytesConsumer* underlying = MockBytesConsumer::create();
|
| BytesConsumer* consumer = FormDataBytesConsumer::createForTesting(
|
| - getDocument(), inputFormData, underlying);
|
| + getDocument(), inputFormData,
|
| + WTF::bind(returnMockBytesConsumer, wrapPersistent(underlying)));
|
| Checkpoint checkpoint;
|
|
|
| + const char* buffer = nullptr;
|
| + size_t available = 0;
|
| +
|
| InSequence s;
|
| EXPECT_CALL(checkpoint, Call(1));
|
| - EXPECT_CALL(*underlying, cancel());
|
| EXPECT_CALL(checkpoint, Call(2));
|
| + EXPECT_CALL(*underlying, beginRead(&buffer, &available))
|
| + .WillOnce(Return(Result::ShouldWait));
|
| + EXPECT_CALL(checkpoint, Call(3));
|
| + EXPECT_CALL(*underlying, cancel());
|
| + EXPECT_CALL(checkpoint, Call(4));
|
|
|
| checkpoint.Call(1);
|
| - consumer->cancel();
|
| + ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available));
|
| + ASSERT_EQ(3u, available);
|
| + ASSERT_EQ(Result::Ok, consumer->endRead(available));
|
| +
|
| checkpoint.Call(2);
|
| + ASSERT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available));
|
| +
|
| + checkpoint.Call(3);
|
| + consumer->cancel();
|
| + checkpoint.Call(4);
|
| }
|
|
|
| } // namespace
|
|
|