Repository navigation
Expand file tree
/
Copy pathcookies.ts
More file actions
224 lines (186 loc) · 5.69 KB
/
Copy pathcookies.ts
File metadata and controls
224 lines (186 loc) · 5.69 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import net from 'net';
import test from 'ava';
import toughCookie from 'tough-cookie';
import delay from 'delay';
import {got, RequestError} from '../source/index.js';
import withServer from './helpers/with-server.js';
test('reads a cookie', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('set-cookie', 'hello=world');
response.end();
});
const cookieJar = new toughCookie.CookieJar();
await got({cookieJar});
const cookie = cookieJar.getCookiesSync(server.url)[0];
t.is(cookie?.key, 'hello');
t.is(cookie?.value, 'world');
});
test('reads multiple cookies', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('set-cookie', ['hello=world', 'foo=bar']);
response.end();
});
const cookieJar = new toughCookie.CookieJar();
await got({cookieJar});
const [cookieA, cookieB] = cookieJar.getCookiesSync(server.url);
t.is(cookieA!.key, 'hello');
t.is(cookieA!.value, 'world');
t.is(cookieB!.key, 'foo');
t.is(cookieB!.value, 'bar');
});
test('cookies doesn\'t break on redirects', withServer, async (t, server, got) => {
server.get('/redirect', (_request, response) => {
response.setHeader('set-cookie', ['hello=world', 'foo=bar']);
response.setHeader('location', '/');
response.statusCode = 302;
response.end();
});
server.get('/', (request, response) => {
response.end(request.headers.cookie ?? '');
});
const cookieJar = new toughCookie.CookieJar();
const {body} = await got('redirect', {cookieJar});
t.is(body, 'hello=world; foo=bar');
});
test('throws on invalid cookies', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('set-cookie', 'invalid cookie; domain=localhost');
response.end();
});
const cookieJar = new toughCookie.CookieJar();
await t.throwsAsync(got({cookieJar}), {
instanceOf: RequestError,
message: 'Cookie failed to parse',
});
});
test('does not throw on invalid cookies when options.ignoreInvalidCookies is set', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.setHeader('set-cookie', 'invalid cookie; domain=localhost');
response.end();
});
const cookieJar = new toughCookie.CookieJar();
await got({
cookieJar,
ignoreInvalidCookies: true,
});
const cookies = cookieJar.getCookiesSync(server.url);
t.is(cookies.length, 0);
});
test('catches store errors', async t => {
const error = 'Some error';
const cookieJar = new toughCookie.CookieJar({
findCookies(_, __, ___, callback) {
callback(new Error(error), []);
},
findCookie() {},
getAllCookies() {},
putCookie() {},
removeCookies() {},
removeCookie() {},
updateCookie() {},
synchronous: false,
});
await t.throwsAsync(got('https://example.com', {cookieJar}), {
instanceOf: RequestError,
message: error,
});
});
test('overrides options.headers.cookie', withServer, async (t, server, got) => {
server.get('/redirect', (_request, response) => {
response.setHeader('set-cookie', ['hello=world', 'foo=bar']);
response.setHeader('location', '/');
response.statusCode = 302;
response.end();
});
server.get('/', (request, response) => {
response.end(request.headers.cookie ?? '');
});
const cookieJar = new toughCookie.CookieJar();
const {body} = await got('redirect', {
cookieJar,
headers: {
cookie: 'a=b',
},
});
t.is(body, 'hello=world; foo=bar');
});
test('no unhandled errors', async t => {
const server = net.createServer(connection => {
connection.end('blah');
}).listen(0);
const message = 'snap!';
const options = {
cookieJar: {
async setCookie(_rawCookie: string, _url: string) {},
async getCookieString(_url: string) {
throw new Error(message);
},
},
};
await t.throwsAsync(got(`http://127.0.0.1:${(server.address() as net.AddressInfo).port}`, options), {
instanceOf: RequestError,
message,
});
await delay(500);
t.pass();
server.close();
});
test('accepts custom `cookieJar` object', withServer, async (t, server, got) => {
server.get('/', (request, response) => {
response.setHeader('set-cookie', ['hello=world']);
response.end(request.headers.cookie);
});
const cookies: Record<string, string> = {};
const cookieJar = {
async getCookieString(url: string) {
t.is(typeof url, 'string');
return cookies[url] ?? '';
},
async setCookie(rawCookie: string, url: string) {
cookies[url] = rawCookie;
},
};
const first = await got('', {cookieJar});
const second = await got('', {cookieJar});
t.is(first.body, '');
t.is(second.body, 'hello=world');
});
test('throws on invalid `options.cookieJar.setCookie`', async t => {
await t.throwsAsync(got('https://example.com', {
cookieJar: {
// @ts-expect-error Error tests
setCookie: 123,
},
}), {
instanceOf: RequestError,
message: 'Expected value which is `Function`, received value of type `number`.',
});
});
test('throws on invalid `options.cookieJar.getCookieString`', async t => {
await t.throwsAsync(got('https://example.com', {
cookieJar: {
async setCookie() {},
// @ts-expect-error Error tests
getCookieString: 123,
},
}), {
instanceOf: RequestError,
message: 'Expected value which is `Function`, received value of type `number`.',
});
});
test('cookies are cleared when redirecting to a different hostname (no cookieJar)', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.writeHead(302, {
location: 'https://httpbin.org/anything',
});
response.end();
});
const {headers} = await got('', {
headers: {
cookie: 'foo=bar',
'user-agent': 'custom',
},
}).json<{headers: Record<string, string | undefined>}>();
t.is(headers.Cookie, undefined);
t.is(headers['User-Agent'], 'custom');
});