1 | 'use strict';
|
2 |
|
3 | const { Snekfetch, TestRoot } = require('./interop');
|
4 |
|
5 | const server = require('./server');
|
6 |
|
7 | function makeTestObj({ unicode = true, numbers = false } = {}) {
|
8 | const test = {
|
9 | Hello: 'world',
|
10 | Test: numbers ? 1337 : '1337',
|
11 | };
|
12 | if (unicode)
|
13 | test.Unicode = '( ͡° ͜ʖ ͡°)';
|
14 | const single = { key: 'singleKey', value: 'awoooo' };
|
15 | return {
|
16 | test,
|
17 | single,
|
18 | check(obj) {
|
19 | expect(obj).not.toBeUndefined();
|
20 | expect(obj.Hello).toBe(test.Hello);
|
21 | expect(obj.Test).toBe(test.Test);
|
22 | if (unicode)
|
23 | expect(obj.Unicode).toBe(test.Unicode);
|
24 | if (obj[single.key])
|
25 | expect(obj[single.key]).toBe(single.value);
|
26 | },
|
27 | };
|
28 | }
|
29 |
|
30 | test('should return a promise', () => {
|
31 | for (const C of [
|
32 | Snekfetch.get(`${TestRoot}/get`).end(),
|
33 | Snekfetch.get.call(null, `${TestRoot}/get`).end(),
|
34 | Snekfetch.get.call({}, `${TestRoot}/get`).end(),
|
35 | ]) {
|
36 | expect(C)
|
37 | .toBeInstanceOf(Promise);
|
38 | }
|
39 | });
|
40 |
|
41 | test('should reject with error on network failure', () => {
|
42 | const invalid = 'http://localhost:0/';
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 | return Snekfetch.get(invalid).catch((err) => {
|
49 | expect(err.name).toMatch(/(Fetch)?Error/);
|
50 | });
|
51 | });
|
52 |
|
53 | test('should resolve on success', () =>
|
54 | Snekfetch.get(`${TestRoot}/get`).then((res) => {
|
55 | expect(res.statusCode).toBe(200);
|
56 | expect(res.ok).toBe(true);
|
57 | expect(res).toHaveProperty('raw');
|
58 | expect(res).toHaveProperty('body');
|
59 | }));
|
60 |
|
61 | test('end should work', () =>
|
62 | Snekfetch.get(`${TestRoot}/get`).end((err, res) => {
|
63 | expect(err).toBe(null);
|
64 | expect(res.body).not.toBeUndefined();
|
65 | }));
|
66 |
|
67 | test('should reject if response is not between 200 and 300', () =>
|
68 | Snekfetch.get(`${TestRoot}/404`).catch((err) => {
|
69 | expect(err.statusCode).toBe(404);
|
70 | expect(err.ok).toBe(false);
|
71 | }));
|
72 |
|
73 | test('unzipping works', () =>
|
74 | Snekfetch.get(`${TestRoot}/gzip`)
|
75 | .then((res) => {
|
76 | expect(res.body).not.toBeUndefined();
|
77 | expect(res.body.gzipped).toBe(true);
|
78 | }));
|
79 |
|
80 | test('query should work', () => {
|
81 | const { test, check, single } = makeTestObj();
|
82 | Promise.all([
|
83 | Snekfetch.get(`${TestRoot}/get?inline=true`)
|
84 | .query(test)
|
85 | .query(single.key, single.value)
|
86 | .end(),
|
87 | Snekfetch.get(`${TestRoot}/get?inline=true`, { query: test })
|
88 | .end(),
|
89 | ])
|
90 | .then((ress) => {
|
91 | for (const res of ress) {
|
92 | const { args } = res.body;
|
93 | check(args);
|
94 | expect(args.inline).toBe('true');
|
95 | }
|
96 | });
|
97 | });
|
98 |
|
99 | test('headers should work', () => {
|
100 | const { test, check } = makeTestObj({ unicode: false });
|
101 | return Promise.all([
|
102 | Snekfetch.get(`${TestRoot}/get`)
|
103 | .set(test).end(),
|
104 | Snekfetch.get(`${TestRoot}/get`, { headers: test })
|
105 | .end(),
|
106 | ])
|
107 | .then((ress) => {
|
108 | for (const res of ress)
|
109 | check(res.body.headers);
|
110 | });
|
111 | });
|
112 |
|
113 | test('attach should work', () => {
|
114 | const { test, check } = makeTestObj();
|
115 | return Snekfetch.post(`${TestRoot}/post`)
|
116 | .attach(test)
|
117 | .then((res) => check(res.body.form));
|
118 | });
|
119 |
|
120 | test('send should work with json', () => {
|
121 | const { test, check } = makeTestObj({ numbers: true });
|
122 | return Promise.all([
|
123 | Snekfetch.post(`${TestRoot}/post`)
|
124 | .send(test).end(),
|
125 | Snekfetch.post(`${TestRoot}/post`, { data: test })
|
126 | .end(),
|
127 | ])
|
128 | .then((ress) => {
|
129 | for (const res of ress)
|
130 | check(res.body.json);
|
131 | });
|
132 | });
|
133 |
|
134 | test('send should work with urlencoded', () => {
|
135 | const { test, check } = makeTestObj();
|
136 | return Snekfetch.post(`${TestRoot}/post`)
|
137 | .set('content-type', 'application/x-www-form-urlencoded')
|
138 | .send(test)
|
139 | .then((res) => check(res.body.form));
|
140 | });
|
141 |
|
142 | test('invalid json is text', () =>
|
143 | Snekfetch.get(`http://localhost:${server.port}/invalid-json`)
|
144 | .then((res) => {
|
145 | expect(res.body).toBe('{ "a": 1');
|
146 | }));
|
147 |
|
148 | test('x-www-form-urlencoded response body', () =>
|
149 | Snekfetch.get(`http://localhost:${server.port}/form-urlencoded`)
|
150 | .then((res) => {
|
151 | const { body } = res;
|
152 | expect(body.test).toBe('1');
|
153 | expect(body.hello).toBe('world');
|
154 | }));
|
155 |
|
156 | test('redirects', () =>
|
157 | Snekfetch.get(`${TestRoot}/redirect/1`)
|
158 | .then((res) => {
|
159 | expect(res.body).not.toBeUndefined();
|
160 | expect(res.body.url).toBe(`${TestRoot}/get`);
|
161 | }));
|