dymokomi/luce-browser-engine
Source of release 0.6.0
0.6.0 / src / web / tests_mime_sniff.lucb
#==============================================================================================
#
# tests_mime_sniff - MIME type parsing and sniffing
#
# DESCRIPTION:
# Port of Tests/LibWeb/TestMimeSniff.cpp (lines 1-499), plus parsing and serialization
# cases pinned to the donor (MimeType::parse, serialized, the classification predicates).
# The C++ HashMap<StringView, Vector<StringView>> tables are arrays of (expected type,
# header) rows; their iteration order does not matter to the checks.
#
#==============================================================================================
# mark: Helpers =================================================================================
## A header and the MIME type sniffing it must give.
struct MimeSniffCase:
var mime_type: str
var header: const u8[]
## `MimeType::create(type, subtype)` from two literals.
func mime_test_create(type_: str, subtype: str) -> MimeType:
return mime_type_create(ak.string_from_str(type_), ak.string_from_str(subtype))
## `MimeType::parse(string).release_value()`.
func mime_test_parse(string: str) -> MimeType:
return mime_type_parse(ak.sv(string)) else trap("MimeType::parse failed")
## `EXPECT_EQ(expected, string)` for a String.
func mime_test_expect(expected: str, string: ak.String):
let view = ak.string_bytes_as_string_view(&string)
if not ak.string_view_equals(&view, expected):
print(f"expected \"{expected}\", got \"{(str)ak.string_view_bytes(&view)}\"")
assert(false)
## A configuration with a sniffing context.
func mime_test_context(context: MimeSniffingContext) -> MimeSniffingConfiguration:
return MimeSniffingConfiguration(sniffing_context = context)
## Ported from TestMimeSniff.cpp:44 set_image_type_mappings.
let mime_test_image_type_mappings: MimeSniffCase[7] = [
MimeSniffCase(mime_type = "image/x-icon", header = b"\x00\x00\x01\x00"),
MimeSniffCase(mime_type = "image/x-icon", header = b"\x00\x00\x02\x00"),
MimeSniffCase(mime_type = "image/bmp", header = b"BM"),
MimeSniffCase(mime_type = "image/gif", header = b"GIF87a"),
MimeSniffCase(mime_type = "image/gif", header = b"GIF89a"),
MimeSniffCase(mime_type = "image/webp", header = b"RIFF\x00\x00\x00\x00WEBPVP"),
MimeSniffCase(mime_type = "image/png", header = b"\x89PNG\x0D\x0A\x1A\x0A"),
]
## The JPEG row of set_image_type_mappings (kept apart so the table above stays one line a row).
let mime_test_jpeg_mapping: MimeSniffCase = MimeSniffCase(mime_type = "image/jpeg", header = b"\xFF\xD8\xFF")
## Ported from TestMimeSniff.cpp:54 set_audio_or_video_type_mappings.
let mime_test_audio_or_video_type_mappings: MimeSniffCase[6] = [
MimeSniffCase(mime_type = "audio/aiff", header = b"FORM\x00\x00\x00\x00\x41IFF"),
MimeSniffCase(mime_type = "audio/mpeg", header = b"ID3"),
MimeSniffCase(mime_type = "application/ogg", header = b"OggS\x00"),
MimeSniffCase(mime_type = "audio/midi", header = b"MThd\x00\x00\x00\x06"),
MimeSniffCase(mime_type = "video/avi", header = b"RIFF\x00\x00\x00\x00\x41\x56\x49\x20"),
MimeSniffCase(mime_type = "audio/wave", header = b"RIFF\x00\x00\x00\x00WAVE"),
]
## Ported from TestMimeSniff.cpp:64 set_text_plain_type_mappings.
let mime_test_text_plain_type_mappings: MimeSniffCase[4] = [
MimeSniffCase(mime_type = "text/plain", header = b"\xFE\xFF\x00\x00"),
MimeSniffCase(mime_type = "text/plain", header = b"\xFF\xFE\x00\x00"),
MimeSniffCase(mime_type = "text/plain", header = b"\xEF\xBB\xBF\x00"),
MimeSniffCase(mime_type = "text/plain", header = b"Hello world!"),
]
## The octet-stream row most tests add.
let mime_test_octet_stream: MimeSniffCase = MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00")
## Checks that sniffing each row's header in `context` gives the row's essence.
func mime_test_expect_essences(cases: const MimeSniffCase[], context: MimeSniffingContext):
for row in cases:
let computed_mime_type = mime_resource_sniff(row.header, mime_test_context(context))
mime_test_expect(row.mime_type, mime_type_essence(&computed_mime_type))
## Checks that sniffing each row's header in `context` serializes as the row's type.
func mime_test_expect_serialized(cases: const MimeSniffCase[], context: MimeSniffingContext):
for row in cases:
let computed_mime_type = mime_resource_sniff(row.header, mime_test_context(context))
mime_test_expect(row.mime_type, mime_type_serialized(&computed_mime_type))
# mark: TestMimeSniff.cpp =======================================================================
test "MimeSniff: determine_computed_mime_type_given_no_sniff_is_set":
let mime_type = mime_test_create("text", "html")
var computed_mime_type = mime_resource_sniff(b"\x00", MimeSniffingConfiguration(supplied_type = mime_type, no_sniff = true))
mime_test_expect("text/html", mime_type_serialized(&computed_mime_type))
# Cover the edge case in the context-specific sniffing algorithm.
computed_mime_type = mime_resource_sniff(b"\x00", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.image,
supplied_type = mime_type,
no_sniff = true,
))
mime_test_expect("text/html", mime_type_serialized(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_given_no_sniff_is_unset":
let supplied_type = mime_test_create("application", "x-this-is-a-test")
let computed_mime_type = mime_resource_sniff(b"\x00", MimeSniffingConfiguration(supplied_type = supplied_type))
mime_test_expect("application/x-this-is-a-test", mime_type_serialized(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_given_xml_mime_type_as_supplied_type":
let xml_mime_type = "application/rss+xml"
let supplied_type = mime_test_parse(xml_mime_type)
let computed_mime_type = mime_resource_sniff(b"\x00", MimeSniffingConfiguration(supplied_type = supplied_type))
mime_test_expect(xml_mime_type, mime_type_serialized(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_given_supplied_type_that_is_an_apache_bug_mime_type":
let apache_bug_mime_types: str[4] = [
"text/plain",
"text/plain; charset=ISO-8859-1",
"text/plain; charset=iso-8859-1",
"text/plain; charset=UTF-8",
]
# Cover all Apache bug MIME types.
for apache_bug_mime_type in apache_bug_mime_types:
let supplied_type = mime_test_parse(apache_bug_mime_type)
let computed_mime_type = mime_resource_sniff(b"Hello world!",
MimeSniffingConfiguration(scheme = ak.sv("http"), supplied_type = supplied_type))
mime_test_expect("text/plain", mime_type_serialized(&computed_mime_type))
# Cover all code paths in "rules for distinguishing if a resource is text or binary".
let supplied_type = mime_test_create("text", "plain")
var cases: MimeSniffCase[5] = ---
cases[0] = mime_test_octet_stream
for i in 0..<4:
cases[i + 1] = mime_test_text_plain_type_mappings[i]
for row in cases:
let computed_mime_type = mime_resource_sniff(row.header,
MimeSniffingConfiguration(scheme = ak.sv("http"), supplied_type = supplied_type))
mime_test_expect(row.mime_type, mime_type_serialized(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_given_xml_or_html_supplied_type":
# With HTML supplied type.
var config = MimeSniffingConfiguration(supplied_type = mime_test_create("text", "html"))
var computed_mime_type = mime_resource_sniff(b"", config)
mime_test_expect("text/html", mime_type_serialized(&computed_mime_type))
# With XML supplied type.
config = MimeSniffingConfiguration(supplied_type = mime_test_create("text", "xml"))
computed_mime_type = mime_resource_sniff(b"", config)
mime_test_expect("text/xml", mime_type_serialized(&computed_mime_type))
## The HTML, XML, PDF and PostScript rows of determine_computed_mime_type_in_both_none_and_browsing_sniffing_context.
let mime_test_scriptable_mappings: MimeSniffCase[21] = [
MimeSniffCase(mime_type = "text/html", header = b"\x09\x09<!DOCTYPE HTML\x20"),
MimeSniffCase(mime_type = "text/html", header = b"\x0A<HTML\x3E"),
MimeSniffCase(mime_type = "text/html", header = b"\x0C<HEAD\x20"),
MimeSniffCase(mime_type = "text/html", header = b"\x0D<SCRIPT>"),
MimeSniffCase(mime_type = "text/html", header = b"\x20<IFRAME>"),
MimeSniffCase(mime_type = "text/html", header = b"<H1>"),
MimeSniffCase(mime_type = "text/html", header = b"<DIV>"),
MimeSniffCase(mime_type = "text/html", header = b"<FONT>"),
MimeSniffCase(mime_type = "text/html", header = b"<TABLE>"),
MimeSniffCase(mime_type = "text/html", header = b"<A>"),
MimeSniffCase(mime_type = "text/html", header = b"<STYLE>"),
MimeSniffCase(mime_type = "text/html", header = b"<TITLE>"),
MimeSniffCase(mime_type = "text/html", header = b"<B>"),
MimeSniffCase(mime_type = "text/html", header = b"<BODY>"),
MimeSniffCase(mime_type = "text/html", header = b"<BR>"),
MimeSniffCase(mime_type = "text/html", header = b"<P>"),
MimeSniffCase(mime_type = "text/html", header = b"<!-->"),
MimeSniffCase(mime_type = "text/xml", header = b"<?xml"),
MimeSniffCase(mime_type = "application/pdf", header = b"%PDF-"),
MimeSniffCase(mime_type = "application/postscript", header = b"%!PS-Adobe-"),
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00"),
]
## The archive rows of determine_computed_mime_type_in_both_none_and_browsing_sniffing_context.
let mime_test_archive_mappings: MimeSniffCase[3] = [
MimeSniffCase(mime_type = "application/x-gzip", header = b"\x1F\x8B\x08"),
MimeSniffCase(mime_type = "application/zip", header = b"PK\x03\x04"),
MimeSniffCase(mime_type = "application/x-rar-compressed", header = b"Rar!\x1A\x07\x00"),
]
test "MimeSniff: determine_computed_mime_type_in_both_none_and_browsing_sniffing_context":
let tables: (const MimeSniffCase[])[5] = [mime_test_scriptable_mappings, mime_test_text_plain_type_mappings,
mime_test_image_type_mappings, mime_test_audio_or_video_type_mappings, mime_test_archive_mappings]
for table in tables:
# Test in a non-specific sniffing context.
mime_test_expect_essences(table, MimeSniffingContext.none_)
# Test sniffing in a browsing context.
mime_test_expect_essences(table, MimeSniffingContext.browsing)
let jpeg: MimeSniffCase[1] = [mime_test_jpeg_mapping]
mime_test_expect_essences(jpeg, MimeSniffingContext.none_)
mime_test_expect_essences(jpeg, MimeSniffingContext.browsing)
# Resource::sniff with the default configuration is the non-specific context.
let computed_mime_type = mime_resource_sniff(b"<HTML>")
mime_test_expect("text/html", mime_type_essence(&computed_mime_type))
test "MimeSniff: compute_mime_type_given_unknown_supplied_type":
let unknown_supplied_types: MimeType[3] = [
mime_test_create("unknown", "unknown"),
mime_test_create("application", "unknown"),
mime_test_create("*", "*"),
]
let header_bytes = b"<HTML>"
for unknown_supplied_type in unknown_supplied_types:
let computed_mime_type = mime_resource_sniff(header_bytes, MimeSniffingConfiguration(supplied_type = unknown_supplied_type))
mime_test_expect("text/html", mime_type_essence(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_in_image_sniffing_context":
# Cover case where supplied type is an XML MIME type.
var mime_type = "application/rss+xml"
var supplied_type = mime_test_parse(mime_type)
var computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(sniffing_context = MimeSniffingContext.image, supplied_type = supplied_type))
mime_test_expect(mime_type, mime_type_serialized(&computed_mime_type))
mime_test_expect_essences(mime_test_image_type_mappings, MimeSniffingContext.image)
let jpeg: MimeSniffCase[1] = [mime_test_jpeg_mapping]
mime_test_expect_essences(jpeg, MimeSniffingContext.image)
# Also consider a resource that is not an image.
let octet_stream: MimeSniffCase[1] = [mime_test_octet_stream]
mime_test_expect_essences(octet_stream, MimeSniffingContext.image)
# Cover case where we aren't dealing with an image MIME type.
mime_type = "text/html"
supplied_type = mime_test_parse("text/html")
computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(sniffing_context = MimeSniffingContext.image, supplied_type = supplied_type))
mime_test_expect(mime_type, mime_type_essence(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_in_audio_or_video_sniffing_context":
# Cover case where supplied type is an XML MIME type.
var mime_type = "application/rss+xml"
var supplied_type = mime_test_parse(mime_type)
var computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.audio_or_video,
supplied_type = supplied_type,
))
mime_test_expect(mime_type, mime_type_serialized(&computed_mime_type))
mime_test_expect_essences(mime_test_audio_or_video_type_mappings, MimeSniffingContext.audio_or_video)
# Also consider a resource that is not an audio or video.
let octet_stream: MimeSniffCase[1] = [mime_test_octet_stream]
mime_test_expect_essences(octet_stream, MimeSniffingContext.audio_or_video)
# Cover case where we aren't dealing with an audio or video MIME type.
mime_type = "text/html"
supplied_type = mime_test_parse("text/html")
computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.audio_or_video,
supplied_type = supplied_type,
))
mime_test_expect(mime_type, mime_type_essence(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_when_trying_to_match_mp4_signature":
let cases: MimeSniffCase[7] = [
# Payload length < 12.
MimeSniffCase(mime_type = "application/octet-stream", header = b"!= 12"),
# Payload length < box size.
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00\x00\x00\x1F\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"),
# Box size % 4 != 0.
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00\x00\x00\x0D\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"),
# 4 bytes after box size header != "ftyp".
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00\x00\x00\x0C\x00\x00\x00\x00\x00\x00\x00\x00"),
# Sequence "mp4" couldn't be found in ftyp box.
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00\x00\x00\x18\x66\x74\x79\x70isom\x00\x00\x00\x00\x61\x76\x63\x31\x00\x00\x00\x00"),
# 3 bytes after "ftyp" sequence == "mp4".
MimeSniffCase(mime_type = "video/mp4", header = b"\x00\x00\x00\x0C\x66\x74\x79\x70mp42"),
# "mp4" sequence found while executing while loop (this input covers entire loop)
MimeSniffCase(mime_type = "video/mp4", header = b"\x00\x00\x00\x18\x66\x74\x79\x70isom\x00\x00\x00\x00\x61\x76\x63\x31mp41"),
]
mime_test_expect_serialized(cases, MimeSniffingContext.audio_or_video)
test "MimeSniff: determine_computed_mime_type_when_trying_to_match_webm_signature":
let cases: MimeSniffCase[4] = [
# Payload length < 4.
MimeSniffCase(mime_type = "application/octet-stream", header = b"<4"),
# First four bytes are not 0x1A 0x45 0xDF 0xA3.
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00\x00\x00\x00"),
# Correct first four bytes, but no following WebM element
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x1A\x45\xDF\xA3\x00\x00\x00\x00"),
# Input that should parse correctly.
MimeSniffCase(mime_type = "video/webm", header = b"\x1A\x45\xDF\xA3\x42\x82\x84\x77\x65\x62\x6D\x00"),
]
mime_test_expect_serialized(cases, MimeSniffingContext.audio_or_video)
## Ported from TestMimeSniff.cpp:327 MP3FrameOptions.
# http://mpgedit.org/mpgedit/mpeg_format/mpeghdr.htm
struct Mp3FrameOptions:
var valid_length: bool = true
# include the 0xFFF sync word?
var sync: bool = true
# 3=MPEG-1, 2=MPEG-2, 0=MPEG-2.5
var version: u8 = 3
# 1=III, 2=II, 3=I
var layer: u8 = 1
# true=no CRC, false=CRC follows
var protect: bool = true
# 1–14 valid
var bitrate_index: u8 = 9
# 0=44.1k,1=48k,2=32k
var samplerate_index: u8 = 0
# padding bit
var padded: bool = false
# filler bytes
var payload_bytes: usize = 100
## Ported from TestMimeSniff.cpp:349 make_mp3_frame.
func make_mp3_frame(opts: Mp3FrameOptions) -> ak.ByteBuffer:
if not opts.valid_length:
return ak.byte_buffer_create_zeroed(2) else trap("MUST: ByteBuffer::create_zeroed")
let total_size = 4 + opts.payload_bytes
var buffer = ak.byte_buffer_create_zeroed(total_size) else trap("MUST: ByteBuffer::create_zeroed")
let data = ak.byte_buffer_bytes(&buffer)
# first 8 bits of sync (0xFFF)
if opts.sync:
data[0] = 0xFF
# 1110 0000 = last three sync bits
# bits 4–3: version
# bits 2–1: layer
# bit 0: protection
data[1] = (0xE0 | ((opts.version & 0x3) << 3) | ((opts.layer & 0x3) << 1) | ((u8)opts.protect & 0x1))
# bits 7–4: bitrate index
# bits 3–2: samplerate index
# bit 1: pad
data[2] = (((opts.bitrate_index & 0xF) << 4) | ((opts.samplerate_index & 0x3) << 2) | (((u8)opts.padded & 0x1) << 1))
# bit 0: private (keep zero)
# 3) Rest of header (channel flags, etc.) – not needed for sniff
data[3] = 0x00
# Payload bytes are already zeroed
return buffer
## A frame and the MIME type sniffing it must give.
struct MimeSniffMp3Case:
var mime_type: str
var options: Mp3FrameOptions
test "MimeSniff: determine_computed_mime_type_when_trying_to_match_mp3_no_id3_signature":
let cases: MimeSniffMp3Case[7] = [
# Payload length < 4.
MimeSniffMp3Case(mime_type = "application/octet-stream", options = Mp3FrameOptions(valid_length = false)),
# invalid sync
MimeSniffMp3Case(mime_type = "application/octet-stream", options = Mp3FrameOptions(sync = false)),
# invalid layer (reserved)
MimeSniffMp3Case(mime_type = "application/octet-stream", options = Mp3FrameOptions(layer = 0)),
# invalid bitrate
MimeSniffMp3Case(mime_type = "application/octet-stream", options = Mp3FrameOptions(bitrate_index = 15)),
# invalid sample rate
MimeSniffMp3Case(mime_type = "application/octet-stream", options = Mp3FrameOptions(samplerate_index = 3)),
MimeSniffMp3Case(mime_type = "audio/mpeg", options = Mp3FrameOptions(padded = true)),
MimeSniffMp3Case(mime_type = "audio/mpeg", options = Mp3FrameOptions(padded = false)),
]
for row in cases:
let header = make_mp3_frame(row.options)
let computed_mime_type = mime_resource_sniff(ak.byte_buffer_bytes_const(&header), mime_test_context(MimeSniffingContext.audio_or_video))
mime_test_expect(row.mime_type, mime_type_serialized(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_in_a_font_context":
# Cover case where supplied type is an XML MIME type.
var mime_type = "application/rss+xml"
var supplied_type = mime_test_parse(mime_type)
var computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.font,
supplied_type = supplied_type,
))
mime_test_expect(mime_type, mime_type_serialized(&computed_mime_type))
let cases: MimeSniffCase[7] = [
MimeSniffCase(mime_type = "application/octet-stream", header = b"\x00"),
MimeSniffCase(mime_type = "application/vnd.ms-fontobject", header = b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00LP"),
MimeSniffCase(mime_type = "font/ttf", header = b"\x00\x01\x00\x00"),
MimeSniffCase(mime_type = "font/otf", header = b"OTTO"),
MimeSniffCase(mime_type = "font/collection", header = b"ttcf"),
MimeSniffCase(mime_type = "font/woff", header = b"wOFF"),
MimeSniffCase(mime_type = "font/woff2", header = b"wOF2"),
]
mime_test_expect_essences(cases, MimeSniffingContext.font)
# Cover case where we aren't dealing with a font MIME type.
mime_type = "text/html"
supplied_type = mime_test_parse("text/html")
computed_mime_type = mime_resource_sniff(b"", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.font,
supplied_type = supplied_type,
))
mime_test_expect(mime_type, mime_type_essence(&computed_mime_type))
test "MimeSniff: determine_computed_mime_type_given_text_or_binary_context":
let supplied_type = mime_test_create("text", "plain")
let computed_mime_type = mime_resource_sniff(b"\x00", MimeSniffingConfiguration(
sniffing_context = MimeSniffingContext.text_or_binary,
supplied_type = supplied_type,
))
mime_test_expect("application/octet-stream", mime_type_serialized(&computed_mime_type))
## A MIME type and its minimised form.
struct MimeMinimiseCase:
var mime_type: str
var minimised: str
test "MimeSniff: determine_minimised_mime_type":
let cases: MimeMinimiseCase[13] = [
# JavaScript MIME types should always be "text/javascript".
MimeMinimiseCase(mime_type = "text/javascript", minimised = "text/javascript"),
MimeMinimiseCase(mime_type = "application/javascript", minimised = "text/javascript"),
MimeMinimiseCase(mime_type = "text/javascript; charset=utf-8", minimised = "text/javascript"),
# JSON MIME types should always be "application/json".
MimeMinimiseCase(mime_type = "application/json", minimised = "application/json"),
MimeMinimiseCase(mime_type = "text/json", minimised = "application/json"),
MimeMinimiseCase(mime_type = "application/json; charset=utf-8", minimised = "application/json"),
# SVG MIME types should always be "image/svg+xml".
MimeMinimiseCase(mime_type = "image/svg+xml", minimised = "image/svg+xml"),
MimeMinimiseCase(mime_type = "image/svg+xml; charset=utf-8", minimised = "image/svg+xml"),
# XML MIME types should always be "application/xml".
MimeMinimiseCase(mime_type = "application/xml", minimised = "application/xml"),
MimeMinimiseCase(mime_type = "text/xml", minimised = "application/xml"),
MimeMinimiseCase(mime_type = "application/xml; charset=utf-8", minimised = "application/xml"),
# MIME types not supported by the user-agent should return an empty string.
MimeMinimiseCase(mime_type = "application/java-archive", minimised = ""),
MimeMinimiseCase(mime_type = "application/zip", minimised = ""),
]
for row in cases:
let mime_type = mime_test_parse(row.mime_type)
mime_test_expect(row.minimised, mime_minimise_a_supported_mime_type(&mime_type))
# mark: Parsing and serialization ===============================================================
## A string, what MimeType::parse(string)->serialized() gives ("" for failure), and its essence.
struct MimeParseCase:
var input: str
var serialized: str
test "MimeSniff: parse and serialize":
# Expected values from the donor's MimeType::parse and serialized().
let cases: MimeParseCase[14] = [
MimeParseCase(input = "text/html", serialized = "text/html"),
MimeParseCase(input = " TEXT/HTML ", serialized = "text/html"),
MimeParseCase(input = "text/html;charset=UTF-8", serialized = "text/html;charset=UTF-8"),
MimeParseCase(input = "text/html ; Charset = utf-8", serialized = "text/html"),
MimeParseCase(input = "text/html; CHARSET=\"utf-8\"; foo=bar", serialized = "text/html;charset=utf-8;foo=bar"),
MimeParseCase(input = "text/plain; a=\"b c\"", serialized = "text/plain;a=\"b c\""),
MimeParseCase(input = "text/plain; a=\"\\\"q\\\\\"", serialized = "text/plain;a=\"\\\"q\\\\\""),
MimeParseCase(input = "text/plain; a=\"\"", serialized = "text/plain;a=\"\""),
MimeParseCase(input = "text/plain; a=1; a=2", serialized = "text/plain;a=1"),
MimeParseCase(input = "text/plain;;;x=y;=z;w", serialized = "text/plain;x=y"),
MimeParseCase(input = "text", serialized = ""),
MimeParseCase(input = "/html", serialized = ""),
MimeParseCase(input = "te xt/html", serialized = ""),
MimeParseCase(input = "text/", serialized = ""),
]
for row in cases:
if let mime_type = mime_type_parse(ak.sv(row.input)):
mime_test_expect(row.serialized, mime_type_serialized(&mime_type))
else:
assert(row.serialized.length == 0)
test "MimeSniff: MIME type groups":
let svg = mime_test_parse("image/svg+xml")
assert(mime_type_is_image(&svg) and mime_type_is_xml(&svg) and mime_type_is_scriptable(&svg))
assert(not mime_type_is_html(&svg) and not mime_type_is_json(&svg))
let ogg = mime_test_parse("application/ogg")
assert(mime_type_is_audio_or_video(&ogg) and not mime_type_is_image(&ogg))
let woff = mime_test_parse("application/font-woff")
assert(mime_type_is_font(&woff))
let epub = mime_test_parse("application/epub+zip")
assert(mime_type_is_zip_based(&epub) and not mime_type_is_archive(&epub))
let gzip = mime_test_parse("application/x-gzip")
assert(mime_type_is_archive(&gzip))
let pdf = mime_test_parse("application/pdf")
assert(mime_type_is_scriptable(&pdf))
let ecmascript = mime_test_parse("text/ECMAScript")
assert(mime_type_is_javascript(&ecmascript))
assert(mime_is_javascript_mime_type_essence_match(ak.sv("Text/JavaScript1.5")))
assert(not mime_is_javascript_mime_type_essence_match(ak.sv("text/javascript2")))
var html = mime_test_create("text", "html")
mime_type_set_parameter(&html, ak.string_from_str("charset"), ak.string_from_str("utf-8"))
mime_test_expect("text/html;charset=utf-8", mime_type_serialized(&html))
let copy = mime_type_copy(&html)
mime_type_set_parameter(&html, ak.string_from_str("x"), ak.string_from_str("y"))
mime_test_expect("text/html;charset=utf-8", mime_type_serialized(©))