| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,13 +1,12 @@ | |||
| 1 | - export default function(url, options) { | ||
| 1 | + export default function (url, options) { | ||
| 2 | 2 | options = options || {}; | |
| 3 | - return new Promise( (resolve, reject) => { | ||
| 3 | + return new Promise((resolve, reject) => { | ||
| 4 | 4 | const request = new XMLHttpRequest(); | |
| 5 | 5 | const keys = []; | |
| 6 | - const all = []; | ||
| 7 | 6 | const headers = {}; | |
| 8 | 7 | ||
| 9 | 8 | const response = () => ({ | |
| 10 | - ok: (request.status/100|0) == 2, // 200-299 | ||
| 9 | + ok: ((request.status / 100) | 0) == 2, // 200-299 | ||
| 11 | 10 | statusText: request.statusText, | |
| 12 | 11 | status: request.status, | |
| 13 | 12 | url: request.responseURL, | |
@@ -17,26 +16,27 @@ export default function(url, options) { | |||
| 17 | 16 | clone: response, | |
| 18 | 17 | headers: { | |
| 19 | 18 | keys: () => keys, | |
| 20 | - entries: () => all, | ||
| 21 | - get: n => headers[n.toLowerCase()], | ||
| 22 | - has: n => n.toLowerCase() in headers | ||
| 23 | - } | ||
| 19 | + entries: () => keys.map((n) => [n, request.getResponseHeader(n)]), | ||
| 20 | + get: (n) => request.getResponseHeader(n), | ||
| 21 | + has: (n) => request.getResponseHeader(n) != null, | ||
| 22 | + }, | ||
| 24 | 23 | }); | |
| 25 | 24 | ||
| 26 | - request.open(options.method || 'get', url, true); | ||
| 25 | + request.open(options.method || "get", url, true); | ||
| 27 | 26 | ||
| 28 | 27 | request.onload = () => { | |
| 29 | - request.getAllResponseHeaders().replace(/^(.*?):[^\S\n]*([\s\S]*?)$/gm, (m, key, value) => { | ||
| 30 | - keys.push(key = key.toLowerCase()); | ||
| 31 | - all.push([key, value]); | ||
| 32 | - headers[key] = headers[key] ? `${headers[key]},${value}` : value; | ||
| 33 | - }); | ||
| 28 | + request | ||
| 29 | + .getAllResponseHeaders() | ||
| 30 | + .toLowerCase() | ||
| 31 | + .replace(/^(.+?):/gm, (m, key) => { | ||
| 32 | + headers[key] || keys.push((headers[key] = key)); | ||
| 33 | + }); | ||
| 34 | 34 | resolve(response()); | |
| 35 | 35 | }; | |
| 36 | 36 | ||
| 37 | 37 | request.onerror = reject; | |
| 38 | 38 | ||
| 39 | - request.withCredentials = options.credentials=='include'; | ||
| 39 | + request.withCredentials = options.credentials == "include"; | ||
| 40 | 40 | ||
| 41 | 41 | for (const i in options.headers) { | |
| 42 | 42 | request.setRequestHeader(i, options.headers[i]); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,29 +1,34 @@ | |||
| 1 | - import fetch from '../src/index.mjs'; | ||
| 2 | - import fetchDist from '..'; | ||
| 1 | + import fetch from "../src/index.mjs"; | ||
| 2 | + import fetchDist from ".."; | ||
| 3 | 3 | ||
| 4 | - describe('unfetch', () => { | ||
| 5 | - it('should be a function', () => { | ||
| 4 | + describe("unfetch", () => { | ||
| 5 | + it("should be a function", () => { | ||
| 6 | 6 | expect(fetch).toEqual(expect.any(Function)); | |
| 7 | 7 | }); | |
| 8 | 8 | ||
| 9 | - it('should be compiled correctly', () => { | ||
| 9 | + it("should be compiled correctly", () => { | ||
| 10 | 10 | expect(fetchDist).toEqual(expect.any(Function)); | |
| 11 | 11 | expect(fetchDist).toHaveLength(2); | |
| 12 | 12 | }); | |
| 13 | 13 | ||
| 14 | - describe('fetch()', () => { | ||
| 14 | + describe("fetch()", () => { | ||
| 15 | 15 | let xhr; | |
| 16 | 16 | ||
| 17 | 17 | beforeEach(() => { | |
| 18 | 18 | xhr = { | |
| 19 | 19 | setRequestHeader: jest.fn(), | |
| 20 | - getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'), | ||
| 20 | + getAllResponseHeaders: jest | ||
| 21 | + .fn() | ||
| 22 | + .mockReturnValue( | ||
| 23 | + "X-Foo: bar\r\nX-Foo: baz\r\nX-Bar: bar, baz\r\nx-test: \r\nX-Baz: bar, baz\r\ndate: 18:23:22" | ||
| 24 | + ), | ||
| 25 | + getResponseHeader: jest.fn().mockReturnValue("bar, baz"), | ||
| 21 | 26 | open: jest.fn(), | |
| 22 | 27 | send: jest.fn(), | |
| 23 | 28 | status: 200, | |
| 24 | - statusText: 'OK', | ||
| 29 | + statusText: "OK", | ||
| 25 | 30 | responseText: '{"a":"b"}', | |
| 26 | - responseURL: '/foo?redirect' | ||
| 31 | + responseURL: "/foo?redirect", | ||
| 27 | 32 | }; | |
| 28 | 33 | ||
| 29 | 34 | global.XMLHttpRequest = jest.fn(() => xhr); | |
@@ -33,29 +38,36 @@ describe('unfetch', () => { | |||
| 33 | 38 | delete global.XMLHttpRequest; | |
| 34 | 39 | }); | |
| 35 | 40 | ||
| 36 | - it('sanity test', () => { | ||
| 37 | - let p = fetch('/foo', { headers: { a: 'b' } }) | ||
| 38 | - .then( r => { | ||
| 41 | + it("sanity test", () => { | ||
| 42 | + let p = fetch("/foo", { headers: { a: "b" } }) | ||
| 43 | + .then((r) => { | ||
| 39 | 44 | expect(r).toMatchObject({ | |
| 40 | 45 | text: expect.any(Function), | |
| 41 | 46 | json: expect.any(Function), | |
| 42 | 47 | blob: expect.any(Function), | |
| 43 | 48 | clone: expect.any(Function), | |
| 44 | - headers: expect.any(Object) | ||
| 49 | + headers: expect.any(Object), | ||
| 45 | 50 | }); | |
| 46 | 51 | expect(r.clone()).not.toBe(r); | |
| 47 | - expect(r.clone().url).toEqual('/foo?redirect'); | ||
| 52 | + expect(r.clone().url).toEqual("/foo?redirect"); | ||
| 48 | 53 | expect(r.headers.get).toEqual(expect.any(Function)); | |
| 49 | - expect(r.headers.get('x-foo')).toEqual('bar,baz'); | ||
| 54 | + expect(r.headers.get("x-foo")).toEqual("bar, baz"); | ||
| 55 | + expect(r.headers.keys()).toEqual([ | ||
| 56 | + "x-foo", | ||
| 57 | + "x-bar", | ||
| 58 | + "x-test", | ||
| 59 | + "x-baz", | ||
| 60 | + "date", | ||
| 61 | + ]); | ||
| 50 | 62 | return r.json(); | |
| 51 | 63 | }) | |
| 52 | - .then( data => { | ||
| 53 | - expect(data).toEqual({ a: 'b' }); | ||
| 64 | + .then((data) => { | ||
| 65 | + expect(data).toEqual({ a: "b" }); | ||
| 54 | 66 | ||
| 55 | 67 | expect(xhr.setRequestHeader).toHaveBeenCalledTimes(1); | |
| 56 | - expect(xhr.setRequestHeader).toHaveBeenCalledWith('a', 'b'); | ||
| 68 | + expect(xhr.setRequestHeader).toHaveBeenCalledWith("a", "b"); | ||
| 57 | 69 | expect(xhr.open).toHaveBeenCalledTimes(1); | |
| 58 | - expect(xhr.open).toHaveBeenCalledWith('get', '/foo', true); | ||
| 70 | + expect(xhr.open).toHaveBeenCalledWith("get", "/foo", true); | ||
| 59 | 71 | expect(xhr.send).toHaveBeenCalledTimes(1); | |
| 60 | 72 | expect(xhr.send).toHaveBeenCalledWith(null); | |
| 61 | 73 | }); | |
@@ -68,13 +80,21 @@ describe('unfetch', () => { | |||
| 68 | 80 | return p; | |
| 69 | 81 | }); | |
| 70 | 82 | ||
| 71 | - it('handles empty header values', () => { | ||
| 72 | - xhr.getAllResponseHeaders = jest.fn().mockReturnValue('Server: \nX-Foo:baz'); | ||
| 73 | - let p = fetch('/foo') | ||
| 74 | - .then(r => { | ||
| 75 | - expect(r.headers.get('server')).toEqual(''); | ||
| 76 | - expect(r.headers.get('X-foo')).toEqual('baz'); | ||
| 77 | - }); | ||
| 83 | + it("handles empty header values", () => { | ||
| 84 | + xhr.getAllResponseHeaders = jest | ||
| 85 | + .fn() | ||
| 86 | + .mockReturnValue("Server: \nX-Foo:baz"); | ||
| 87 | + const headers = { | ||
| 88 | + server: "", | ||
| 89 | + "x-foo": "baz", | ||
| 90 | + }; | ||
| 91 | + xhr.getResponseHeader = jest.fn( | ||
| 92 | + (header) => headers[header.toLowerCase()] ?? null | ||
| 93 | + ); | ||
| 94 | + let p = fetch("/foo").then((r) => { | ||
| 95 | + expect(r.headers.get("server")).toEqual(""); | ||
| 96 | + expect(r.headers.get("X-foo")).toEqual("baz"); | ||
| 97 | + }); | ||
| 78 | 98 | ||
| 79 | 99 | xhr.onload(); | |
| 80 | 100 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments