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 "core/fetch/MultipartImageResourceParser.h" | 5 #include "core/fetch/MultipartImageResourceParser.h" |
6 | 6 |
7 #include "platform/network/ResourceResponse.h" | 7 #include "platform/network/ResourceResponse.h" |
8 #include "public/platform/WebURL.h" | 8 #include "public/platform/WebURL.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
10 | 10 |
(...skipping 12 matching lines...) Expand all Loading... |
23 return String(data.data(), data.size()); | 23 return String(data.data(), data.size()); |
24 } | 24 } |
25 | 25 |
26 class MultipartResponseTest : public testing::Test { | 26 class MultipartResponseTest : public testing::Test { |
27 }; | 27 }; |
28 | 28 |
29 class MockClient final : public NoBaseWillBeGarbageCollectedFinalized<MockClient
>, public MultipartImageResourceParser::Client { | 29 class MockClient final : public NoBaseWillBeGarbageCollectedFinalized<MockClient
>, public MultipartImageResourceParser::Client { |
30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockClient); | 30 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockClient); |
31 | 31 |
32 public: | 32 public: |
33 void didReceiveResponse(const ResourceResponse& response) override | 33 void didReceiveResponse(const ResourceResponse& response, bool isFirstPart)
override |
34 { | 34 { |
| 35 if (isFirstPart != m_responses.isEmpty()) |
| 36 FAIL() << "m_responses.size() = " << m_responses.size() << ", isFirs
tPart = " << isFirstPart; |
| 37 |
35 m_responses.append(response); | 38 m_responses.append(response); |
36 m_data.append(Vector<char>()); | 39 m_data.append(Vector<char>()); |
37 } | 40 } |
38 void didReceiveData(const char* bytes, size_t size) override | 41 void didReceiveData(const char* bytes, size_t size) override |
39 { | 42 { |
40 m_data.last().append(bytes, size); | 43 m_data.last().append(bytes, size); |
41 } | 44 } |
42 | 45 |
43 Vector<ResourceResponse> m_responses; | 46 Vector<ResourceResponse> m_responses; |
44 Vector<Vector<char>> m_data; | 47 Vector<Vector<char>> m_data; |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); | 378 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); |
376 | 379 |
377 const char data[] = "--bound\r\n\r\n--bound\r\n\r\nfoofoo--bound--"; | 380 const char data[] = "--bound\r\n\r\n--bound\r\n\r\nfoofoo--bound--"; |
378 parser->addData(data, strlen(data)); | 381 parser->addData(data, strlen(data)); |
379 ASSERT_EQ(2u, client->m_responses.size()); | 382 ASSERT_EQ(2u, client->m_responses.size()); |
380 ASSERT_EQ(2u, client->m_data.size()); | 383 ASSERT_EQ(2u, client->m_data.size()); |
381 EXPECT_EQ("", toString(client->m_data[0])); | 384 EXPECT_EQ("", toString(client->m_data[0])); |
382 EXPECT_EQ("foofoo", toString(client->m_data[1])); | 385 EXPECT_EQ("foofoo", toString(client->m_data[1])); |
383 } | 386 } |
384 | 387 |
385 TEST(MultipartResponseTest, MultipartPayloadSet) | 388 TEST(MultipartResponseTest, IsFirstPartSet) |
386 { | 389 { |
387 ResourceResponse response; | 390 ResourceResponse response; |
388 response.setMimeType("multipart/x-mixed-replace"); | 391 response.setMimeType("multipart/x-mixed-replace"); |
389 MockClient* client = new MockClient; | 392 MockClient* client = new MockClient; |
390 Vector<char> boundary; | 393 Vector<char> boundary; |
391 boundary.append("bound", 5); | 394 boundary.append("bound", 5); |
392 | 395 |
393 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); | 396 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); |
394 | 397 |
| 398 // isFirstPart is checked at MockClient::didReceiveResponse. |
395 const char data[] = | 399 const char data[] = |
396 "--bound\n" | 400 "--bound\n" |
397 "Content-type: text/plain\n\n" | 401 "Content-type: text/plain\n\n" |
398 "response data\n" | 402 "response data\n" |
399 "--bound\n"; | 403 "--bound\n"; |
400 parser->addData(data, strlen(data)); | 404 parser->addData(data, strlen(data)); |
401 ASSERT_EQ(1u, client->m_responses.size()); | 405 ASSERT_EQ(1u, client->m_responses.size()); |
402 ASSERT_EQ(1u, client->m_data.size()); | 406 ASSERT_EQ(1u, client->m_data.size()); |
403 EXPECT_EQ("response data", toString(client->m_data[0])); | 407 EXPECT_EQ("response data", toString(client->m_data[0])); |
404 EXPECT_FALSE(client->m_responses[0].isMultipartPayload()); | |
405 | 408 |
406 const char data2[] = | 409 const char data2[] = |
407 "Content-type: text/plain\n\n" | 410 "Content-type: text/plain\n\n" |
408 "response data2\n" | 411 "response data2\n" |
409 "--bound\n"; | 412 "--bound\n"; |
410 parser->addData(data2, strlen(data2)); | 413 parser->addData(data2, strlen(data2)); |
411 ASSERT_EQ(2u, client->m_responses.size()); | 414 ASSERT_EQ(2u, client->m_responses.size()); |
412 ASSERT_EQ(2u, client->m_data.size()); | 415 ASSERT_EQ(2u, client->m_data.size()); |
413 EXPECT_EQ("response data2", toString(client->m_data[1])); | 416 EXPECT_EQ("response data2", toString(client->m_data[1])); |
414 EXPECT_TRUE(client->m_responses[1].isMultipartPayload()); | |
415 } | 417 } |
416 | 418 |
417 } // namespace | 419 } // namespace |
418 | 420 |
419 } // namespace blink | 421 } // namespace blink |
OLD | NEW |