| 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/Platform.h" | 8 #include "public/platform/Platform.h" |
| 9 #include "public/platform/WebURL.h" | 9 #include "public/platform/WebURL.h" |
| 10 #include "public/platform/WebURLResponse.h" | 10 #include "public/platform/WebURLResponse.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 { | 22 { |
| 23 if (data.isEmpty()) | 23 if (data.isEmpty()) |
| 24 return String(""); | 24 return String(""); |
| 25 return String(data.data(), data.size()); | 25 return String(data.data(), data.size()); |
| 26 } | 26 } |
| 27 | 27 |
| 28 class MockClient final : public NoBaseWillBeGarbageCollectedFinalized<MockClient
>, public MultipartImageResourceParser::Client { | 28 class MockClient final : public NoBaseWillBeGarbageCollectedFinalized<MockClient
>, public MultipartImageResourceParser::Client { |
| 29 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockClient); | 29 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(MockClient); |
| 30 | 30 |
| 31 public: | 31 public: |
| 32 void onePartInMultipartReceived(const ResourceResponse& response) override | 32 void onePartInMultipartReceived(const ResourceResponse& response, bool isFir
stPart) override |
| 33 { | 33 { |
| 34 if (isFirstPart != m_responses.isEmpty()) |
| 35 FAIL() << "m_responses.size() = " << m_responses.size() << ", isFirs
tPart = " << isFirstPart; |
| 36 |
| 34 m_responses.append(response); | 37 m_responses.append(response); |
| 35 m_data.append(Vector<char>()); | 38 m_data.append(Vector<char>()); |
| 36 } | 39 } |
| 37 void multipartDataReceived(const char* bytes, size_t size) override | 40 void multipartDataReceived(const char* bytes, size_t size) override |
| 38 { | 41 { |
| 39 m_data.last().append(bytes, size); | 42 m_data.last().append(bytes, size); |
| 40 } | 43 } |
| 41 | 44 |
| 42 Vector<ResourceResponse> m_responses; | 45 Vector<ResourceResponse> m_responses; |
| 43 Vector<Vector<char>> m_data; | 46 Vector<Vector<char>> m_data; |
| (...skipping 391 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 435 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); | 438 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); |
| 436 | 439 |
| 437 const char data[] = "--bound\r\n\r\n--bound\r\n\r\nfoofoo--bound--"; | 440 const char data[] = "--bound\r\n\r\n--bound\r\n\r\nfoofoo--bound--"; |
| 438 parser->appendData(data, strlen(data)); | 441 parser->appendData(data, strlen(data)); |
| 439 ASSERT_EQ(2u, client->m_responses.size()); | 442 ASSERT_EQ(2u, client->m_responses.size()); |
| 440 ASSERT_EQ(2u, client->m_data.size()); | 443 ASSERT_EQ(2u, client->m_data.size()); |
| 441 EXPECT_EQ("", toString(client->m_data[0])); | 444 EXPECT_EQ("", toString(client->m_data[0])); |
| 442 EXPECT_EQ("foofoo", toString(client->m_data[1])); | 445 EXPECT_EQ("foofoo", toString(client->m_data[1])); |
| 443 } | 446 } |
| 444 | 447 |
| 445 TEST(MultipartResponseTest, MultipartPayloadSet) | 448 TEST(MultipartResponseTest, IsFirstPartSet) |
| 446 { | 449 { |
| 447 ResourceResponse response; | 450 ResourceResponse response; |
| 448 response.setMimeType("multipart/x-mixed-replace"); | 451 response.setMimeType("multipart/x-mixed-replace"); |
| 449 MockClient* client = new MockClient; | 452 MockClient* client = new MockClient; |
| 450 Vector<char> boundary; | 453 Vector<char> boundary; |
| 451 boundary.append("bound", 5); | 454 boundary.append("bound", 5); |
| 452 | 455 |
| 453 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); | 456 MultipartImageResourceParser* parser = new MultipartImageResourceParser(resp
onse, boundary, client); |
| 454 | 457 |
| 458 // isFirstPart is checked at MockClient::didReceiveResponse. |
| 455 const char data[] = | 459 const char data[] = |
| 456 "--bound\n" | 460 "--bound\n" |
| 457 "Content-type: text/plain\n\n" | 461 "Content-type: text/plain\n\n" |
| 458 "response data\n" | 462 "response data\n" |
| 459 "--bound\n"; | 463 "--bound\n"; |
| 460 parser->appendData(data, strlen(data)); | 464 parser->appendData(data, strlen(data)); |
| 461 ASSERT_EQ(1u, client->m_responses.size()); | 465 ASSERT_EQ(1u, client->m_responses.size()); |
| 462 ASSERT_EQ(1u, client->m_data.size()); | 466 ASSERT_EQ(1u, client->m_data.size()); |
| 463 EXPECT_EQ("response data", toString(client->m_data[0])); | 467 EXPECT_EQ("response data", toString(client->m_data[0])); |
| 464 EXPECT_FALSE(client->m_responses[0].isMultipartPayload()); | |
| 465 | 468 |
| 466 const char data2[] = | 469 const char data2[] = |
| 467 "Content-type: text/plain\n\n" | 470 "Content-type: text/plain\n\n" |
| 468 "response data2\n" | 471 "response data2\n" |
| 469 "--bound\n"; | 472 "--bound\n"; |
| 470 parser->appendData(data2, strlen(data2)); | 473 parser->appendData(data2, strlen(data2)); |
| 471 ASSERT_EQ(2u, client->m_responses.size()); | 474 ASSERT_EQ(2u, client->m_responses.size()); |
| 472 ASSERT_EQ(2u, client->m_data.size()); | 475 ASSERT_EQ(2u, client->m_data.size()); |
| 473 EXPECT_EQ("response data2", toString(client->m_data[1])); | 476 EXPECT_EQ("response data2", toString(client->m_data[1])); |
| 474 EXPECT_TRUE(client->m_responses[1].isMultipartPayload()); | |
| 475 } | 477 } |
| 476 | 478 |
| 477 } // namespace | 479 } // namespace |
| 478 | 480 |
| 479 } // namespace blink | 481 } // namespace blink |
| OLD | NEW |