UNPKG

2.61 kBPlain TextView Raw
1/*
2 * Copyright 2021 Inrupt Inc.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal in
6 * the Software without restriction, including without limitation the rights to use,
7 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
8 * Software, and to permit persons to whom the Software is furnished to do so,
9 * subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16 * PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 */
21
22import { Headers as NodeHeaders } from "node-fetch";
23import { flattenHeaders } from "../../../src/authenticatedFetch/headers/HeadersUtils";
24
25describe("Headers interoperability function", () => {
26 it("transforms an incoming Headers object into a flat headers structure", () => {
27 const myHeaders = new NodeHeaders();
28 myHeaders.append("accept", "application/json");
29 myHeaders.append("content-type", "text/turtle");
30 // The following needs to be ignored because `node-fetch::Headers` and
31 // `lib.dom.d.ts::Headers` don't align. It doesn't break the way we
32 // use them currently, but it's something that must be cleaned up
33 // at some point.
34 // eslint-disable-next-line
35 // @ts-ignore
36 const flatHeaders = flattenHeaders(myHeaders);
37 expect(Object.entries(flatHeaders)).toEqual([
38 ["accept", "application/json"],
39 ["content-type", "text/turtle"],
40 ]);
41 });
42
43 it("supports non-iterable headers if they provide a reasonably standard way of browsing them", () => {
44 // eslint-disable-next-line @typescript-eslint/no-explicit-any
45 const myHeaders: any = {};
46 myHeaders.forEach = (
47 callback: (value: string, key: string) => void
48 ): void => {
49 callback("application/json", "accept");
50 callback("text/turtle", "content-type");
51 };
52 const flatHeaders = flattenHeaders(myHeaders);
53 expect(Object.entries(flatHeaders)).toEqual([
54 ["accept", "application/json"],
55 ["content-type", "text/turtle"],
56 ]);
57 });
58});