From 6415564b9259a4a6a2f6ec9cb934eab3d56a1677 Mon Sep 17 00:00:00 2001 From: Florian Dold Date: Mon, 9 Dec 2019 19:59:08 +0100 Subject: tos --- src/util/http.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) (limited to 'src/util/http.ts') diff --git a/src/util/http.ts b/src/util/http.ts index ab253b232..79039f516 100644 --- a/src/util/http.ts +++ b/src/util/http.ts @@ -24,7 +24,7 @@ */ export interface HttpResponse { status: number; - headers: { [name: string]: string }; + headers: Headers; json(): Promise; text(): Promise; } @@ -33,6 +33,31 @@ export interface HttpRequestOptions { headers?: { [name: string]: string }; } +/** + * Headers, roughly modeled after the fetch API's headers object. + */ +export class Headers { + private headerMap = new Map(); + + get(name: string): string | null { + const r = this.headerMap.get(name.toLowerCase()); + if (r) { + return r; + } + return null; + } + + set(name: string, value: string): void { + const normalizedName = name.toLowerCase(); + const existing = this.headerMap.get(normalizedName); + if (existing !== undefined) { + this.headerMap.set(normalizedName, existing + "," + value); + } else { + this.headerMap.set(normalizedName, value); + } + } +} + /** * The request library is bundled into an interface to m responseJson: object & any;ake mocking easy. */ @@ -103,12 +128,12 @@ export class BrowserHttpLib implements HttpRequestLibrary { const arr = headers.trim().split(/[\r\n]+/); // Create a map of header names to values - const headerMap: { [name: string]: string } = {}; + const headerMap = new Headers(); arr.forEach(function(line) { const parts = line.split(": "); const header = parts.shift(); const value = parts.join(": "); - headerMap[header!] = value; + headerMap.set(header!, value); }); const resp: HttpResponse = { status: myRequest.status, -- cgit v1.2.3