-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathapi.test.ts
49 lines (41 loc) · 1.39 KB
/
api.test.ts
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
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import startAPIServer, { APIProcess } from "../src/server/api";
import axios from "axios";
let apiServer: APIProcess;
describe('API test', () => {
beforeEach(async () => {
vi.resetModules();
apiServer = await startAPIServer('randomSecret');
axios.defaults.baseURL = `http://localhost:${apiServer.port}`;
});
afterEach(async () => {
await new Promise<void>((resolve) => {
apiServer.server.close(() => resolve());
});
});
it('starts API server on port 4000', async () => {
expect(apiServer.port).toBe(4000);
});
it('uses the next available API port', async () => {
const nextApiProcess = await startAPIServer('randomSecret');
expect(nextApiProcess.port).toBe(apiServer.port + 1);
nextApiProcess.server.close();
});
it('protects API endpoints with a secret', async () => {
try {
await axios.get('/api/process');
} catch (error) {
expect(error.response.status).toBe(403);
}
let response;
try {
response = await axios.get('/api/process', {
headers: {
'x-nativephp-secret': 'randomSecret',
}
});
} finally {
expect(response.status).toBe(200);
}
});
});