OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/http/http_content_disposition.h" | 5 #include "net/http/http_content_disposition.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
(...skipping 494 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
505 }; | 505 }; |
506 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 506 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
507 HttpContentDisposition header(tests[i].header, std::string()); | 507 HttpContentDisposition header(tests[i].header, std::string()); |
508 EXPECT_EQ(tests[i].expected_type, header.type()) | 508 EXPECT_EQ(tests[i].expected_type, header.type()) |
509 << "Failed on input: " << tests[i].header; | 509 << "Failed on input: " << tests[i].header; |
510 EXPECT_EQ(tests[i].expected_filename, UTF8ToWide(header.filename())) | 510 EXPECT_EQ(tests[i].expected_filename, UTF8ToWide(header.filename())) |
511 << "Failed on input: " << tests[i].header; | 511 << "Failed on input: " << tests[i].header; |
512 } | 512 } |
513 } | 513 } |
514 | 514 |
| 515 TEST(HttpContentDispositionTest, ParseResult) { |
| 516 enum ParseResultFlags { |
| 517 INVALID = 0, |
| 518 HAS_DISPOSITION_TYPE = 1 << 0, |
| 519 HAS_UNKNOWN_DISPOSITION_TYPE = 1 << 1, |
| 520 HAS_NAME = 1 << 2, |
| 521 HAS_FILENAME = 1 << 3, |
| 522 HAS_EXT_FILENAME = 1 << 4, |
| 523 HAS_NON_ASCII_STRINGS = 1 << 5, |
| 524 HAS_PERCENT_ENCODED_STRINGS = 1 << 6, |
| 525 HAS_RFC2047_ENCODED_STRINGS = 1 << 7 |
| 526 }; |
| 527 |
| 528 const struct ParseResultTestCase { |
| 529 const char* header; |
| 530 int expected_flags; |
| 531 } kTestCases[] = { |
| 532 // Basic feature tests |
| 533 { "", INVALID }, |
| 534 { "example=x", |
| 535 INVALID }, |
| 536 { "filename=x", |
| 537 HAS_FILENAME}, |
| 538 { "example; filename=x", |
| 539 HAS_DISPOSITION_TYPE | HAS_UNKNOWN_DISPOSITION_TYPE | |
| 540 HAS_FILENAME}, |
| 541 { "attachment; filename=x", |
| 542 HAS_DISPOSITION_TYPE | HAS_FILENAME }, |
| 543 { "attachment; filename=x; name=y", |
| 544 HAS_DISPOSITION_TYPE | HAS_FILENAME | HAS_NAME }, |
| 545 { "attachment; name=y; filename*=utf-8''foo; name=x", |
| 546 HAS_DISPOSITION_TYPE | HAS_EXT_FILENAME | HAS_NAME }, |
| 547 |
| 548 // Feature tests for 'filename' attribute. |
| 549 { "filename=foo\xcc\x88", |
| 550 HAS_FILENAME | HAS_NON_ASCII_STRINGS }, |
| 551 { "filename=foo%cc%88", |
| 552 HAS_FILENAME | HAS_PERCENT_ENCODED_STRINGS }, |
| 553 { "filename==?utf-8?Q?foo?=", |
| 554 HAS_FILENAME | HAS_RFC2047_ENCODED_STRINGS }, |
| 555 { "filename=\"=?utf-8?Q?foo?=\"", |
| 556 HAS_FILENAME | HAS_RFC2047_ENCODED_STRINGS }, |
| 557 { "filename==?utf-8?Q?foo?", INVALID }, |
| 558 { "name=foo\xcc\x88", |
| 559 HAS_NAME }, |
| 560 |
| 561 // Shouldn't set |has_non_ascii_strings| based on 'name' attribute. |
| 562 { "filename=x; name=foo\xcc\x88", |
| 563 HAS_FILENAME | HAS_NAME }, |
| 564 { "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?=", |
| 565 HAS_FILENAME | HAS_NON_ASCII_STRINGS | HAS_PERCENT_ENCODED_STRINGS | |
| 566 HAS_RFC2047_ENCODED_STRINGS }, |
| 567 |
| 568 // If 'filename' attribute is invalid, should set any flags based on it. |
| 569 { "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?", |
| 570 INVALID }, |
| 571 { "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?; name=x", |
| 572 HAS_NAME }, |
| 573 }; |
| 574 |
| 575 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) { |
| 576 const ParseResultTestCase& test_case = kTestCases[i]; |
| 577 HttpContentDisposition content_disposition(test_case.header, "utf-8"); |
| 578 const HttpContentDisposition::ParseResult& result = |
| 579 content_disposition.parse_result(); |
| 580 |
| 581 SCOPED_TRACE(testing::Message() << "Test case " << i |
| 582 << " with header " << test_case.header); |
| 583 |
| 584 EXPECT_EQ(!!(test_case.expected_flags & HAS_DISPOSITION_TYPE), |
| 585 result.has_disposition_type); |
| 586 EXPECT_EQ(!!(test_case.expected_flags & HAS_UNKNOWN_DISPOSITION_TYPE), |
| 587 result.has_unknown_disposition_type); |
| 588 EXPECT_EQ(!!(test_case.expected_flags & HAS_NAME), |
| 589 result.has_name); |
| 590 EXPECT_EQ(!!(test_case.expected_flags & HAS_FILENAME), |
| 591 result.has_filename); |
| 592 EXPECT_EQ(!!(test_case.expected_flags & HAS_EXT_FILENAME), |
| 593 result.has_ext_filename); |
| 594 EXPECT_EQ(!!(test_case.expected_flags & HAS_NON_ASCII_STRINGS), |
| 595 result.has_non_ascii_strings); |
| 596 EXPECT_EQ(!!(test_case.expected_flags & HAS_PERCENT_ENCODED_STRINGS), |
| 597 result.has_percent_encoded_strings); |
| 598 EXPECT_EQ(!!(test_case.expected_flags & HAS_RFC2047_ENCODED_STRINGS), |
| 599 result.has_rfc2047_encoded_strings); |
| 600 } |
| 601 } |
| 602 |
515 } // namespace net | 603 } // namespace net |
OLD | NEW |