| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 "platform/network/ParsedContentType.h" | 5 #include "platform/network/ParsedContentType.h" |
| 6 | 6 |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 8 |
| 9 namespace blink { | 9 namespace blink { |
| 10 | 10 |
| 11 namespace { |
| 12 |
| 13 bool isValid(const String& input) { |
| 14 return ParsedContentType(input).isValid(); |
| 15 } |
| 16 |
| 11 TEST(ParsedContentTypeTest, MimeTypeWithoutCharset) { | 17 TEST(ParsedContentTypeTest, MimeTypeWithoutCharset) { |
| 12 ParsedContentType t("text/plain"); | 18 ParsedContentType t("text/plain"); |
| 13 | 19 |
| 20 EXPECT_TRUE(t.isValid()); |
| 14 EXPECT_EQ("text/plain", t.mimeType()); | 21 EXPECT_EQ("text/plain", t.mimeType()); |
| 15 EXPECT_EQ(String(), t.charset()); | 22 EXPECT_EQ(String(), t.charset()); |
| 16 } | 23 } |
| 17 | 24 |
| 18 TEST(ParsedContentTypeTest, MimeTypeWithCharSet) { | 25 TEST(ParsedContentTypeTest, MimeTypeWithCharSet) { |
| 19 ParsedContentType t(" text/plain ; x=y;charset=utf-8 "); | 26 ParsedContentType t(" text/plain ; x=y;charset=utf-8 "); |
| 20 | 27 |
| 28 EXPECT_TRUE(t.isValid()); |
| 21 EXPECT_EQ("text/plain", t.mimeType()); | 29 EXPECT_EQ("text/plain", t.mimeType()); |
| 22 EXPECT_EQ("utf-8", t.charset()); | 30 EXPECT_EQ("utf-8", t.charset()); |
| 23 } | 31 } |
| 24 | 32 |
| 25 TEST(ParsedContentTypeTest, MimeTypeWithQuotedCharSet) { | 33 TEST(ParsedContentTypeTest, MimeTypeWithQuotedCharSet) { |
| 26 ParsedContentType t("text/plain; \"charset\"=\"x=y;y=z; ;;\""); | 34 ParsedContentType t("text/plain; \"charset\"=\"x=y;y=z; ;;\""); |
| 27 | 35 |
| 36 EXPECT_TRUE(t.isValid()); |
| 28 EXPECT_EQ("text/plain", t.mimeType()); | 37 EXPECT_EQ("text/plain", t.mimeType()); |
| 29 EXPECT_EQ("x=y;y=z; ;;", t.charset()); | 38 EXPECT_EQ("x=y;y=z; ;;", t.charset()); |
| 30 } | 39 } |
| 31 | 40 |
| 32 // TODO(yhirano): Add tests for escaped quotation: it's currently | 41 // TODO(yhirano): Add tests for escaped quotation: it's currently |
| 33 // mis-implemented. | 42 // mis-implemented. |
| 34 | 43 |
| 35 TEST(ParsedContentTypeTest, InvalidMimeTypeWithoutCharset) { | 44 TEST(ParsedContentTypeTest, InvalidMimeTypeWithoutCharset) { |
| 36 ParsedContentType t(" "); | 45 ParsedContentType t(" "); |
| 37 | 46 |
| 47 EXPECT_FALSE(t.isValid()); |
| 38 EXPECT_EQ(String(), t.mimeType()); | 48 EXPECT_EQ(String(), t.mimeType()); |
| 39 EXPECT_EQ(String(), t.charset()); | 49 EXPECT_EQ(String(), t.charset()); |
| 40 } | 50 } |
| 41 | 51 |
| 42 TEST(ParsedContentTypeTest, InvalidMimeTypeWithCharset) { | 52 TEST(ParsedContentTypeTest, InvalidMimeTypeWithCharset) { |
| 43 ParsedContentType t("text/plain; charset;"); | 53 ParsedContentType t("text/plain; charset;"); |
| 44 | 54 |
| 55 EXPECT_FALSE(t.isValid()); |
| 45 EXPECT_EQ("text/plain", t.mimeType()); | 56 EXPECT_EQ("text/plain", t.mimeType()); |
| 46 EXPECT_EQ(String(), t.charset()); | 57 EXPECT_EQ(String(), t.charset()); |
| 47 } | 58 } |
| 48 | 59 |
| 49 TEST(ParsedContentTypeTest, Validity) { | 60 TEST(ParsedContentTypeTest, Validity) { |
| 50 EXPECT_TRUE(isValidContentType("text/plain")); | 61 EXPECT_TRUE(isValid("text/plain")); |
| 51 EXPECT_TRUE(isValidContentType("text/plain; charset=utf-8")); | 62 EXPECT_TRUE(isValid("text/plain; charset=utf-8")); |
| 52 EXPECT_TRUE(isValidContentType(" text/plain ")); | 63 EXPECT_TRUE(isValid(" text/plain ")); |
| 53 EXPECT_TRUE(isValidContentType(" text/plain;charset=utf-8 ")); | 64 EXPECT_TRUE(isValid(" text/plain;charset=utf-8 ")); |
| 54 EXPECT_TRUE(isValidContentType("unknown/unknown")); | 65 EXPECT_TRUE(isValid("unknown/unknown")); |
| 55 EXPECT_TRUE(isValidContentType("unknown/unknown; charset=unknown")); | 66 EXPECT_TRUE(isValid("unknown/unknown; charset=unknown")); |
| 56 EXPECT_TRUE(isValidContentType("x/y;\"z=\\\"q;t\"=\"ttx&r=z;;kd==\"")); | 67 EXPECT_TRUE(isValid("x/y;\"z=\\\"q;t\"=\"ttx&r=z;;kd==\"")); |
| 57 | 68 |
| 58 EXPECT_FALSE(isValidContentType("text/plain\r")); | 69 EXPECT_FALSE(isValid("text/plain\r")); |
| 59 EXPECT_FALSE(isValidContentType("text/plain\n")); | 70 EXPECT_FALSE(isValid("text/plain\n")); |
| 60 EXPECT_FALSE(isValidContentType("")); | 71 EXPECT_FALSE(isValid("")); |
| 61 EXPECT_FALSE(isValidContentType(" ")); | 72 EXPECT_FALSE(isValid(" ")); |
| 62 EXPECT_FALSE(isValidContentType("text/plain;")); | 73 EXPECT_FALSE(isValid("text/plain;")); |
| 63 EXPECT_FALSE(isValidContentType("text/plain; ")); | 74 EXPECT_FALSE(isValid("text/plain; ")); |
| 64 EXPECT_FALSE(isValidContentType("text/plain; charset")); | 75 EXPECT_FALSE(isValid("text/plain; charset")); |
| 65 EXPECT_FALSE(isValidContentType("text/plain; charset;")); | 76 EXPECT_FALSE(isValid("text/plain; charset;")); |
| 66 EXPECT_FALSE(isValidContentType("x/y;\"xx")); | 77 EXPECT_FALSE(isValid("x/y;\"xx")); |
| 67 EXPECT_FALSE(isValidContentType("x/y;\"xx=y")); | 78 EXPECT_FALSE(isValid("x/y;\"xx=y")); |
| 68 | 79 |
| 69 // TODO(yhirano): Add tests for non-tokens. They are currently accepted. | 80 // TODO(yhirano): Add tests for non-tokens. They are currently accepted. |
| 70 } | 81 } |
| 71 | 82 |
| 72 TEST(ParsedContentTypeTest, ParameterName) { | 83 TEST(ParsedContentTypeTest, ParameterName) { |
| 73 String input = "x; y=z; y=u; t=r;s=x;\"Q\"=U;\"T\"=S;\"z u\"=\"q a\""; | 84 String input = "x; y=z; y=u; t=r;s=x;\"Q\"=U;\"T\"=S;\"z u\"=\"q a\""; |
| 74 | 85 |
| 75 ParsedContentType t(input); | 86 ParsedContentType t(input); |
| 76 | 87 |
| 77 EXPECT_EQ(6u, t.parameterCount()); | 88 EXPECT_EQ(6u, t.parameterCount()); |
| 78 EXPECT_EQ(String(), t.parameterValueForName("a")); | 89 EXPECT_EQ(String(), t.parameterValueForName("a")); |
| 79 EXPECT_EQ(String(), t.parameterValueForName("x")); | 90 EXPECT_EQ(String(), t.parameterValueForName("x")); |
| 80 EXPECT_EQ("u", t.parameterValueForName("y")); | 91 EXPECT_EQ("u", t.parameterValueForName("y")); |
| 81 EXPECT_EQ("r", t.parameterValueForName("t")); | 92 EXPECT_EQ("r", t.parameterValueForName("t")); |
| 82 EXPECT_EQ("x", t.parameterValueForName("s")); | 93 EXPECT_EQ("x", t.parameterValueForName("s")); |
| 83 EXPECT_EQ("U", t.parameterValueForName("Q")); | 94 EXPECT_EQ("U", t.parameterValueForName("Q")); |
| 84 EXPECT_EQ("S", t.parameterValueForName("T")); | 95 EXPECT_EQ("S", t.parameterValueForName("T")); |
| 85 EXPECT_EQ("q a", t.parameterValueForName("z u")); | 96 EXPECT_EQ("q a", t.parameterValueForName("z u")); |
| 86 | 97 |
| 87 // TODO(yhirano): Case-sensitivity is mis-implemented. | 98 // TODO(yhirano): Case-sensitivity is mis-implemented. |
| 88 // TODO(yhirano): Add tests for escaped quotations. | 99 // TODO(yhirano): Add tests for escaped quotations. |
| 89 // TODO(yhirano): Leading spaces of a parameter value should be ignored. | 100 // TODO(yhirano): Leading spaces of a parameter value should be ignored. |
| 90 // TODO(yhirano): Trailing spaces of a parameter value should be ignored. | 101 // TODO(yhirano): Trailing spaces of a parameter value should be ignored. |
| 91 } | 102 } |
| 92 | 103 |
| 104 } // namespace |
| 105 |
| 93 } // namespace blink | 106 } // namespace blink |
| OLD | NEW |