FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Merge #92 · developit/unfetch@628c907 · GitHub

Repository navigation

Commit 628c907

Browse files
Merge #92
Co-authored-by: Simon Buerger <simon@bluegrassdigital.com> Co-authored-by: Jason Miller <jason@developit.ca>
1 parent e3e7e60 commit 628c907

2 files changed

Lines changed: 61 additions & 41 deletions

File tree

‎src/index.mjs‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
export default function(url, options) {
1+
export default function (url, options) {
22
options = options || {};
3-
return new Promise( (resolve, reject) => {
3+
return new Promise((resolve, reject) => {
44
const request = new XMLHttpRequest();
55
const keys = [];
6-
const all = [];
76
const headers = {};
87

98
const response = () => ({
10-
ok: (request.status/100|0) == 2, // 200-299
9+
ok: ((request.status / 100) | 0) == 2, // 200-299
1110
statusText: request.statusText,
1211
status: request.status,
1312
url: request.responseURL,
@@ -17,26 +16,27 @@ export default function(url, options) {
1716
clone: response,
1817
headers: {
1918
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+
},
2423
});
2524

26-
request.open(options.method || 'get', url, true);
25+
request.open(options.method || "get", url, true);
2726

2827
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+
});
3434
resolve(response());
3535
};
3636

3737
request.onerror = reject;
3838

39-
request.withCredentials = options.credentials=='include';
39+
request.withCredentials = options.credentials == "include";
4040

4141
for (const i in options.headers) {
4242
request.setRequestHeader(i, options.headers[i]);

‎test/index.js‎

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff 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 "..";
33

4-
describe('unfetch', () => {
5-
it('should be a function', () => {
4+
describe("unfetch", () => {
5+
it("should be a function", () => {
66
expect(fetch).toEqual(expect.any(Function));
77
});
88

9-
it('should be compiled correctly', () => {
9+
it("should be compiled correctly", () => {
1010
expect(fetchDist).toEqual(expect.any(Function));
1111
expect(fetchDist).toHaveLength(2);
1212
});
1313

14-
describe('fetch()', () => {
14+
describe("fetch()", () => {
1515
let xhr;
1616

1717
beforeEach(() => {
1818
xhr = {
1919
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"),
2126
open: jest.fn(),
2227
send: jest.fn(),
2328
status: 200,
24-
statusText: 'OK',
29+
statusText: "OK",
2530
responseText: '{"a":"b"}',
26-
responseURL: '/foo?redirect'
31+
responseURL: "/foo?redirect",
2732
};
2833

2934
global.XMLHttpRequest = jest.fn(() => xhr);
@@ -33,29 +38,36 @@ describe('unfetch', () => {
3338
delete global.XMLHttpRequest;
3439
});
3540

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) => {
3944
expect(r).toMatchObject({
4045
text: expect.any(Function),
4146
json: expect.any(Function),
4247
blob: expect.any(Function),
4348
clone: expect.any(Function),
44-
headers: expect.any(Object)
49+
headers: expect.any(Object),
4550
});
4651
expect(r.clone()).not.toBe(r);
47-
expect(r.clone().url).toEqual('/foo?redirect');
52+
expect(r.clone().url).toEqual("/foo?redirect");
4853
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+
]);
5062
return r.json();
5163
})
52-
.then( data => {
53-
expect(data).toEqual({ a: 'b' });
64+
.then((data) => {
65+
expect(data).toEqual({ a: "b" });
5466

5567
expect(xhr.setRequestHeader).toHaveBeenCalledTimes(1);
56-
expect(xhr.setRequestHeader).toHaveBeenCalledWith('a', 'b');
68+
expect(xhr.setRequestHeader).toHaveBeenCalledWith("a", "b");
5769
expect(xhr.open).toHaveBeenCalledTimes(1);
58-
expect(xhr.open).toHaveBeenCalledWith('get', '/foo', true);
70+
expect(xhr.open).toHaveBeenCalledWith("get", "/foo", true);
5971
expect(xhr.send).toHaveBeenCalledTimes(1);
6072
expect(xhr.send).toHaveBeenCalledWith(null);
6173
});
@@ -68,13 +80,21 @@ describe('unfetch', () => {
6880
return p;
6981
});
7082

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+
});
7898

7999
xhr.onload();
80100

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL