mirror of
https://github.com/denoland/deno.git
synced 2025-03-03 17:34:47 -05:00
make Headers
follow spec (#1427)
This commit is contained in:
parent
73fb98ce70
commit
48e29c3c86
2 changed files with 37 additions and 4 deletions
|
@ -31,10 +31,11 @@ class HeadersBase {
|
||||||
// https://github.com/bitinn/node-fetch/blob/master/src/headers.js
|
// https://github.com/bitinn/node-fetch/blob/master/src/headers.js
|
||||||
// Copyright (c) 2016 David Frank. MIT License.
|
// Copyright (c) 2016 David Frank. MIT License.
|
||||||
private _validateName(name: string) {
|
private _validateName(name: string) {
|
||||||
if (invalidTokenRegex.test(name)) {
|
if (invalidTokenRegex.test(name) || name === "") {
|
||||||
throw new TypeError(`${name} is not a legal HTTP header name`);
|
throw new TypeError(`${name} is not a legal HTTP header name`);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private _validateValue(value: string) {
|
private _validateValue(value: string) {
|
||||||
if (invalidHeaderCharRegex.test(value)) {
|
if (invalidHeaderCharRegex.test(value)) {
|
||||||
throw new TypeError(`${value} is not a legal HTTP header value`);
|
throw new TypeError(`${value} is not a legal HTTP header value`);
|
||||||
|
@ -51,8 +52,17 @@ class HeadersBase {
|
||||||
} else {
|
} else {
|
||||||
this[headerMap] = new Map();
|
this[headerMap] = new Map();
|
||||||
if (Array.isArray(init)) {
|
if (Array.isArray(init)) {
|
||||||
for (const [rawName, rawValue] of init) {
|
for (const tuple of init) {
|
||||||
const [name, value] = this._normalizeParams(rawName, rawValue);
|
// If header does not contain exactly two items,
|
||||||
|
// then throw a TypeError.
|
||||||
|
// ref: https://fetch.spec.whatwg.org/#concept-headers-fill
|
||||||
|
if (tuple.length !== 2) {
|
||||||
|
// tslint:disable:max-line-length
|
||||||
|
// prettier-ignore
|
||||||
|
throw new TypeError("Failed to construct 'Headers'; Each header pair must be an iterable [name, value] tuple");
|
||||||
|
}
|
||||||
|
|
||||||
|
const [name, value] = this._normalizeParams(tuple[0], tuple[1]);
|
||||||
this._validateName(name);
|
this._validateName(name);
|
||||||
this._validateValue(value);
|
this._validateValue(value);
|
||||||
const existingValue = this[headerMap].get(name);
|
const existingValue = this[headerMap].get(name);
|
||||||
|
|
|
@ -224,11 +224,34 @@ test(function headerIllegalReject() {
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
errorCount++;
|
errorCount++;
|
||||||
}
|
}
|
||||||
assertEqual(errorCount, 8);
|
try {
|
||||||
|
headers.set("", "ok");
|
||||||
|
} catch (e) {
|
||||||
|
errorCount++;
|
||||||
|
}
|
||||||
|
assertEqual(errorCount, 9);
|
||||||
// 'o k' is valid value but invalid name
|
// 'o k' is valid value but invalid name
|
||||||
new Headers({ "He-y": "o k" });
|
new Headers({ "He-y": "o k" });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// If pair does not contain exactly two items,then throw a TypeError.
|
||||||
|
test(function headerParamsShouldThrowTypeError() {
|
||||||
|
let hasThrown = 0;
|
||||||
|
|
||||||
|
try {
|
||||||
|
new Headers(([["1"]] as unknown) as Array<[string, string]>);
|
||||||
|
hasThrown = 1;
|
||||||
|
} catch (err) {
|
||||||
|
if (err instanceof TypeError) {
|
||||||
|
hasThrown = 2;
|
||||||
|
} else {
|
||||||
|
hasThrown = 3;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEqual(hasThrown, 2);
|
||||||
|
});
|
||||||
|
|
||||||
test(function headerParamsArgumentsCheck() {
|
test(function headerParamsArgumentsCheck() {
|
||||||
const methodRequireOneParam = ["delete", "get", "has", "forEach"];
|
const methodRequireOneParam = ["delete", "get", "has", "forEach"];
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue