Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ulixee
GitHub Repository: ulixee/secret-agent
Path: blob/main/full-client/test/fetch.test.ts
1028 views
1
import { Helpers } from '@secret-agent/testing';
2
import { ITestKoaServer } from '@secret-agent/testing/helpers';
3
import { Handler } from '../index';
4
5
let koaServer: ITestKoaServer;
6
let handler: Handler;
7
beforeAll(async () => {
8
handler = new Handler();
9
Helpers.onClose(() => handler.close(), true);
10
koaServer = await Helpers.runKoaServer();
11
});
12
afterAll(Helpers.afterAll);
13
afterEach(Helpers.afterEach);
14
15
describe('Fetch tests', () => {
16
it('should be able to fetch from top level', async () => {
17
const agent = await handler.createAgent();
18
Helpers.needsClosing.push(agent);
19
20
await expect(agent.fetch('https://dataliberationfoundation.org')).rejects.toThrowError(
21
'need to use a "goto"',
22
);
23
});
24
25
it('should be able to run a fetch from the browser', async () => {
26
koaServer.get('/fetch', ctx => {
27
ctx.body = { got: 'it' };
28
});
29
const agent = await handler.createAgent();
30
Helpers.needsClosing.push(agent);
31
32
await agent.goto(`${koaServer.baseUrl}/`);
33
await agent.waitForPaintingStable();
34
const result = await agent.fetch('/fetch');
35
const json = await result.json();
36
expect(json).toStrictEqual({ got: 'it' });
37
});
38
39
it('should be able to do an http post', async () => {
40
let posted: string;
41
koaServer.post('/post', async ctx => {
42
let body = '';
43
for await (const chunk of ctx.req) {
44
body += chunk.toString();
45
}
46
posted = body;
47
48
ctx.body = { got: '2' };
49
});
50
const agent = await handler.createAgent();
51
52
await agent.goto(`${koaServer.baseUrl}/`);
53
await agent.waitForPaintingStable();
54
55
const response = await agent.fetch(`${koaServer.baseUrl}/post`, {
56
method: 'post',
57
headers: {
58
Accept: 'application/json',
59
'Content-Type': 'application/json',
60
},
61
body: JSON.stringify({ sent: 'it' }),
62
});
63
64
expect(await response.json()).toStrictEqual({ got: '2' });
65
expect(posted).toStrictEqual(JSON.stringify({ sent: 'it' }));
66
});
67
68
it('should be able to create a request object', async () => {
69
let header1: string;
70
koaServer.get('/request', ctx => {
71
header1 = ctx.headers.header1 as string;
72
73
ctx.body = { got: 'request' };
74
});
75
const agent = await handler.createAgent();
76
77
await agent.goto(`${koaServer.baseUrl}/`);
78
await agent.waitForPaintingStable();
79
80
const { Request, fetch } = agent;
81
const request = new Request(`${koaServer.baseUrl}/request`, {
82
headers: {
83
header1: 'sent',
84
},
85
});
86
87
const response = await fetch(request);
88
89
expect(await response.json()).toStrictEqual({ got: 'request' });
90
expect(header1).toBe('sent');
91
});
92
93
it('should be able to get a byte array back', async () => {
94
koaServer.get('/buffer', ctx => {
95
ctx.body = Buffer.from('This is a test');
96
});
97
const agent = await handler.createAgent();
98
99
await agent.goto(`${koaServer.baseUrl}/`);
100
await agent.waitForPaintingStable();
101
102
const response = await agent.fetch(`${koaServer.baseUrl}/buffer`);
103
104
const buff = await response.arrayBuffer();
105
106
expect(Buffer.from(buff)).toStrictEqual(Buffer.from('This is a test'));
107
});
108
109
it.todo('should be able to get a blob back');
110
});
111
112