-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathStubHeaders.ts
63 lines (47 loc) · 1.69 KB
/
StubHeaders.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
export default class StubHeaders implements Headers {
public static make(data: Record<string, string>): StubHeaders {
return new StubHeaders(data);
}
private data: Record<string, string>;
private constructor(data: Record<string, string>) {
this.data = {};
for (const [name, value] of Object.entries(data)) {
this.set(name, value);
}
}
public [Symbol.iterator](): IterableIterator<[string, string]> {
throw new Error('Method not implemented.');
}
public entries(): IterableIterator<[string, string]> {
throw new Error('Method not implemented.');
}
public keys(): IterableIterator<string> {
throw new Error('Method not implemented.');
}
public values(): IterableIterator<string> {
throw new Error('Method not implemented.');
}
public append(name: string, value: string): void {
this.data[this.normalizeHeader(name)] = value;
}
public delete(name: string): void {
delete this.data[this.normalizeHeader(name)];
}
public get(name: string): string | null {
return this.data[this.normalizeHeader(name)] ?? null;
}
public has(name: string): boolean {
return this.normalizeHeader(name) in this.data;
}
public set(name: string, value: string): void {
this.data[this.normalizeHeader(name)] = value;
}
public forEach(callbackfn: (value: string, name: string, parent: Headers) => void): void {
for (const [name, value] of Object.entries(this.data)) {
callbackfn(value, name, this);
}
}
private normalizeHeader(name: string): string {
return name.toLowerCase();
}
}