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
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "net/base/data_url.h"
7 #include "testing/gtest/include/gtest/gtest.h"
12 struct ParseTestData
{
15 const char* mime_type
;
22 TEST(DataURLTest
, Parse
) {
23 const ParseTestData tests
[] = {
42 { "data:;charset=,test",
48 { "data:TeXt/HtMl,<b>x</b>",
60 { "data:;base64,aGVsbG8gd29ybGQ=",
66 { "data:foo/bar;baz=1;charset=kk,boo",
72 { "data:foo/bar;charset=kk;baz=1,boo",
78 { "data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world"
79 "%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E",
83 "<html><body><b>hello world</b></body></html>" },
85 { "data:text/html,<html><body><b>hello world</b></body></html>",
89 "<html><body><b>hello world</b></body></html>" },
92 { "data:f(oo/bar;baz=1;charset=kk,boo",
98 // the comma cannot be url-escaped!
105 // invalid base64 content
106 { "data:;base64,aGVs_-_-",
112 // Spaces should be removed from non-text data URLs (we already tested
114 { "data:image/fractal,a b c d e f g",
120 // Spaces should also be removed from anything base-64 encoded
121 { "data:;base64,aGVs bG8gd2 9ybGQ=",
127 // Other whitespace should also be removed from anything base-64 encoded.
128 { "data:;base64,aGVs bG8gd2 \n9ybGQ=",
134 // In base64 encoding, escaped whitespace should be stripped.
135 // (This test was taken from acid3)
137 { "data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207"
144 // Only unescaped whitespace should be stripped in non-base64.
146 { "data:img/png,A B %20 %0A C",
152 { "data:text/plain;charset=utf-8;base64,SGVsbMO2",
158 // Not sufficiently padded.
159 { "data:;base64,aGVsbG8gd29ybGQ",
165 // Bad encoding (truncated).
166 { "data:;base64,aGVsbG8gd29yb",
172 // TODO(darin): add more interesting tests
175 for (size_t i
= 0; i
< arraysize(tests
); ++i
) {
176 std::string mime_type
;
180 net::DataURL::Parse(GURL(tests
[i
].url
), &mime_type
, &charset
, &data
);
181 EXPECT_EQ(ok
, tests
[i
].is_valid
);
182 if (tests
[i
].is_valid
) {
183 EXPECT_EQ(tests
[i
].mime_type
, mime_type
);
184 EXPECT_EQ(tests
[i
].charset
, charset
);
185 EXPECT_EQ(tests
[i
].data
, data
);