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