Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/format.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,25 @@ describe("format(obj)", function () {

it("should reject invalid type", function () {
const obj = { type: "text/" };
assert.throws(format.bind(null, obj), /Invalid type: text\//);
assert.throws(format.bind(null, obj), /Invalid type: "text\/"/);
});

it("should reject invalid type with LWS", function () {
const obj = { type: " text/html" };
assert.throws(format.bind(null, obj), /Invalid type: text\/html/);
assert.throws(format.bind(null, obj), /Invalid type: " text\/html"/);
});

it("should reject invalid parameter name", function () {
const obj = { type: "image/svg", parameters: { "foo/": "bar" } };
assert.throws(format.bind(null, obj), /Invalid parameter name: foo\//);
assert.throws(format.bind(null, obj), /Invalid parameter name: "foo\/"/);
});

it("should reject invalid parameter value", function () {
const obj = { type: "image/svg", parameters: { foo: "bar\u0000" } };
assert.throws(format.bind(null, obj), /Invalid parameter value: bar\u0000/);
assert.throws(
format.bind(null, obj),
/Invalid parameter value: "bar\\u0000"/,
);
});

it("should quote parameter value containing HTAB", function () {
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@ export function format(obj: Partial<ContentType>): string {
const { type, parameters } = obj;

if (!type || !TYPE_REGEXP.test(type)) {
throw new TypeError(`Invalid type: ${type}`);
throw new TypeError(`Invalid type: ${JSON.stringify(type)}`);
}

let result = type;

if (parameters) {
for (const param of Object.keys(parameters)) {
if (!TOKEN_REGEXP.test(param)) {
throw new TypeError(`Invalid parameter name: ${param}`);
throw new TypeError(`Invalid parameter name: ${JSON.stringify(param)}`);
}

result += `; ${param}=${qstring(parameters[param])}`;
Expand Down Expand Up @@ -210,5 +210,5 @@ function qstring(str: string): string {
if (TOKEN_REGEXP.test(str)) return str;
if (TEXT_REGEXP.test(str)) return `"${str.replace(QUOTE_REGEXP, "\\$&")}"`;

throw new TypeError(`Invalid parameter value: ${str}`);
throw new TypeError(`Invalid parameter value: ${JSON.stringify(str)}`);
}
Loading