This source file includes following definitions.
- TEST
#include "base/basictypes.h"
#include "net/base/data_url.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
namespace {
struct ParseTestData {
const char* url;
bool is_valid;
const char* mime_type;
const char* charset;
const char* data;
};
}
TEST(DataURLTest, Parse) {
const ParseTestData tests[] = {
{ "data:",
false,
"",
"",
"" },
{ "data:,",
true,
"text/plain",
"US-ASCII",
"" },
{ "data:;base64,",
true,
"text/plain",
"US-ASCII",
"" },
{ "data:;charset=,test",
true,
"text/plain",
"US-ASCII",
"test" },
{ "data:TeXt/HtMl,<b>x</b>",
true,
"text/html",
"US-ASCII",
"<b>x</b>" },
{ "data:,foo",
true,
"text/plain",
"US-ASCII",
"foo" },
{ "data:;base64,aGVsbG8gd29ybGQ=",
true,
"text/plain",
"US-ASCII",
"hello world" },
{ "data:foo/bar;baz=1;charset=kk,boo",
true,
"foo/bar",
"kk",
"boo" },
{ "data:foo/bar;charset=kk;baz=1,boo",
true,
"foo/bar",
"kk",
"boo" },
{ "data:text/html,%3Chtml%3E%3Cbody%3E%3Cb%3Ehello%20world"
"%3C%2Fb%3E%3C%2Fbody%3E%3C%2Fhtml%3E",
true,
"text/html",
"US-ASCII",
"<html><body><b>hello world</b></body></html>" },
{ "data:text/html,<html><body><b>hello world</b></body></html>",
true,
"text/html",
"US-ASCII",
"<html><body><b>hello world</b></body></html>" },
{ "data:%2Cblah",
false,
"",
"",
"" },
{ "data:;base64,aGVs_-_-",
false,
"",
"",
"" },
{ "data:image/fractal,a b c d e f g",
true,
"image/fractal",
"US-ASCII",
"abcdefg" },
{ "data:;base64,aGVs bG8gd2 9ybGQ=",
true,
"text/plain",
"US-ASCII",
"hello world" },
{ "data:;base64,aGVs bG8gd2 \n9ybGQ=",
true,
"text/plain",
"US-ASCII",
"hello world" },
{ "data:text/javascript;base64,%20ZD%20Qg%0D%0APS%20An%20Zm91cic%0D%0A%207"
"%20",
true,
"text/javascript",
"US-ASCII",
"d4 = 'four';" },
{ "data:img/png,A B %20 %0A C",
true,
"img/png",
"US-ASCII",
"AB \nC" },
{ "data:text/plain;charset=utf-8;base64,SGVsbMO2",
true,
"text/plain",
"utf-8",
"Hell\xC3\xB6" },
{ "data:;base64,aGVsbG8gd29ybGQ",
true,
"text/plain",
"US-ASCII",
"hello world" },
{ "data:;base64,aGVsbG8gd29yb",
false,
"",
"",
"" },
};
for (size_t i = 0; i < arraysize(tests); ++i) {
std::string mime_type;
std::string charset;
std::string data;
bool ok =
net::DataURL::Parse(GURL(tests[i].url), &mime_type, &charset, &data);
EXPECT_EQ(ok, tests[i].is_valid);
if (tests[i].is_valid) {
EXPECT_EQ(tests[i].mime_type, mime_type);
EXPECT_EQ(tests[i].charset, charset);
EXPECT_EQ(tests[i].data, data);
}
}
}