-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.ts
75 lines (62 loc) · 1.8 KB
/
server_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
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
import { jsonServer } from './server.ts';
import { assertEquals } from './test_deps.ts';
import { loadDatabase } from './data/loadData.ts';
const { test } = Deno;
const assertJSON = async (actual: Response, expected: unknown) => {
assertEquals(actual.headers.get('Content-Type'), 'application/json');
assertEquals(await actual.json(), expected);
};
test({
name: 'serve a whole json db',
fn: async () => {
const aborter = new AbortController();
const db = await loadDatabase('./example/db.json', {
signal: aborter.signal,
});
const server = await jsonServer({
dbPathOrObject: './example/db.json',
port: 8001,
watchDB: false,
});
const response = await fetch('http://localhost:8001');
await assertJSON(response, db);
server.close();
},
});
test({
name: 'load a json object db',
fn: async () => {
const db = { test: 'epic' };
const server = await jsonServer({ dbPathOrObject: db, watchDB: false });
const response = await fetch('http://localhost:8000');
await assertJSON(response, db);
server.close();
},
});
test({
name: 'serve a route from json file',
fn: async () => {
const server = await jsonServer({
dbPathOrObject: './example/db.json',
watchDB: false,
});
const response = await fetch('http://localhost:8000/profile');
await assertJSON(response, { user: 'magnattic' });
server.close();
},
});
test({
name: 'route by id',
fn: async () => {
const db = {
posts: [
{ id: 1, title: 'Yucatan' },
{ id: 2, title: 'Alice in Denoland' },
],
};
const server = await jsonServer({ dbPathOrObject: db, watchDB: false });
const response = await fetch('http://localhost:8000/posts/2');
await assertJSON(response, db.posts[1]);
server.close();
},
});