blob: 619f8c9e26740dd1c9ba1bf0274535945e7a1985 [file] [log] [blame]
<!DOCTYPE html>
<html>
<head>
<script src="../../http/tests/inspector/resources/inspector-test.js"></script>
<script>
function test()
{
let suite = InspectorTest.createSyncSuite("MIMETypeUtilities");
suite.addTestCase({
name: "parseMIMEType",
test() {
function test(fullMimeType, expected) {
InspectorTest.newline();
InspectorTest.log(`Test ${JSON.stringify(fullMimeType)}`);
let {
type: expectedType,
boundary: expectedBoundary,
encoding: expectedEncoding,
} = expected;
let {
type: actualType,
boundary: actualBoundary,
encoding: actualEncoding,
} = parseMIMEType(fullMimeType);
InspectorTest.expectEqual(actualType, expectedType, `type should be: ${JSON.stringify(expectedType)}`);
InspectorTest.expectEqual(actualBoundary, expectedBoundary, `boundary should be: ${JSON.stringify(expectedBoundary)}`);
InspectorTest.expectEqual(actualEncoding, expectedEncoding, `encoding should be: ${JSON.stringify(expectedEncoding)}`);
}
test(null, {
type: null,
boundary: null,
encoding: null,
});
test("a", {
type: "a",
boundary: null,
encoding: null,
});
test("mime", {
type: "mime",
boundary: null,
encoding: null,
});
test("mime-dash", {
type: "mime-dash",
boundary: null,
encoding: null,
});
test("mime/slash", {
type: "mime/slash",
boundary: null,
encoding: null,
});
test("mime+plus", {
type: "mime+plus",
boundary: null,
encoding: null,
});
test("mime/slash+plus", {
type: "mime/slash+plus",
boundary: null,
encoding: null,
});
test("mime/slash ;", {
type: "mime/slash",
boundary: null,
encoding: null,
});
test("mime ; semicolon", {
type: "mime",
boundary: null,
encoding: null,
});
test("mime/generic ; a = b", {
type: "mime/generic",
boundary: null,
encoding: null,
});
test("mime/generic ; boundary = charset", {
type: "mime/generic",
boundary: "charset",
encoding: null,
});
test("mime/generic ; charset = boundary", {
type: "mime/generic",
boundary: null,
encoding: "boundary",
});
test("mime/generic ; boundary = foo ; charset = bar", {
type: "mime/generic",
boundary: "foo",
encoding: "bar",
});
return true;
}
});
suite.addTestCase({
name: "fileExtensionForFilename",
test() {
InspectorTest.expectNull(WI.fileExtensionForFilename(null), `File extension for null filename should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("test"), null, `File extension for filename without a period should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("test."), null, `File extension for filename ending in a period should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForFilename("foo.xyz"), "xyz", `File extension for "foo.xyz" should be "xyz".`);
InspectorTest.expectEqual(WI.fileExtensionForFilename("image.png"), "png", `File extension for "image.png" should be "png".`);
InspectorTest.expectEqual(WI.fileExtensionForFilename("image.gif"), "gif", `File extension for "image.png" should be "gif".`);
InspectorTest.expectEqual(WI.fileExtensionForFilename("script.js"), "js", `File extension for "script.js" should be "js".`);
InspectorTest.expectEqual(WI.fileExtensionForFilename("script.min.js"), "js", `File extension for "script.min.js" should be "js".`);
return true;
}
});
suite.addTestCase({
name: "fileExtensionForURL",
test() {
InspectorTest.expectNull(WI.fileExtensionForURL(null), `File extension for null URL should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("invalid-url"), null, `File extension for invalid URL should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com"), null, `File extension for URL without last path component should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/"), null, `File extension for URL without last path component should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/test"), null, `File extension for URL with last path component without a period should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/foo.bar/test"), null, `File extension for URL with last path component without a period should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/test."), null, `File extension for URL with last path component ending in a period should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/foo.xyz"), "xyz", `File extension for "foo.xyz" should be "xyz".`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/image.png"), "png", `File extension for "image.png" should be "png".`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/image.gif"), "gif", `File extension for "image.png" should be "gif".`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/script.js"), "js", `File extension for "script.js" should be "js".`);
InspectorTest.expectEqual(WI.fileExtensionForURL("https://example.com/script.min.js"), "js", `File extension for "script.min.js" should be "js".`);
return true;
}
});
suite.addTestCase({
name: "fileExtensionForMIMEType",
test() {
InspectorTest.expectEqual(WI.fileExtensionForMIMEType(null), null, `File extension for null mime type should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("invalid-mimetype"), null, `File extension for invalid mime type should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("application/unknown"), null, `File extension for unknown mime type should be null.`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("image/jpeg"), "jpg", `File extension for "image/jpeg" should be "jpg".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("image/png"), "png", `File extension for "image/png" should be "png".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("image/gif"), "gif", `File extension for "image/gif" should be "gif".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("text/javascript"), "js", `File extension for "text/javascript" should be "js".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("application/json"), "json", `File extension for "application/json" should be "json".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("application/vnd.api+json"), "json", `File extension for "application/vnd.api+json" should be "json".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("application/xhtml+xml"), "xhtml", `File extension for "application/xhtml+xml" should be "xhtml".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("image/svg+xml"), "svg", `File extension for "image/svg+xml" should be "svg".`);
InspectorTest.expectEqual(WI.fileExtensionForMIMEType("text/foo+xml"), "xml", `File extension for "text/foo+xml" should be "xml".`);
return true;
}
});
suite.addTestCase({
name: "shouldTreatMIMETypeAsText",
test() {
function expectText(mimeType) {
InspectorTest.expectTrue(WI.shouldTreatMIMETypeAsText(mimeType), `"${mimeType}" should be treated as text.`);
}
function expectNotText(mimeType) {
InspectorTest.expectFalse(WI.shouldTreatMIMETypeAsText(mimeType), `"${mimeType}" should not be treated as text.`);
}
InspectorTest.expectFalse(WI.shouldTreatMIMETypeAsText(null), `null mime type should not be treated as text.`);
expectNotText("application/unknown");
expectText("text/plain");
expectText("text/javascript");
expectText("application/json");
expectText("application/vnd.api+json");
expectText("application/vnd.apple.mpegurl");
expectText("image/svg+xml");
expectText("text/x-coffeescript");
expectText("application/xml");
expectNotText("image/jpeg");
expectNotText("image/png");
expectNotText("image/gif");
expectNotText("font/woff");
expectNotText("video/mpeg");
expectNotText("audio/ogg");
expectNotText("application/pdf");
return true;
}
})
suite.runTestCasesAndFinish();
}
</script>
</head>
<body onLoad="runTest()">
</body>
</html>