forked from koajs/router
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter-validation.test.ts
More file actions
139 lines (108 loc) · 3.64 KB
/
parameter-validation.test.ts
File metadata and controls
139 lines (108 loc) · 3.64 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
/**
* Tests for Parameter Validation Recipe
*/
import { describe, it } from 'node:test';
import * as assert from 'node:assert';
import * as http from 'node:http';
import Router, { RouterContext } from '../router-module-loader';
import request from 'supertest';
import Koa from 'koa';
import { Next } from '../common';
describe('Parameter Validation', () => {
it('should validate UUID format with router.param()', async () => {
const app = new Koa();
const router = new Router();
router.param('id', (value: string, ctx: RouterContext, next: Next) => {
const uuidRegex =
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
if (!uuidRegex.test(value)) {
ctx.throw(400, 'Invalid ID format');
}
return next();
});
router.get('/users/:id', (ctx: RouterContext) => {
ctx.body = { id: ctx.params.id, valid: true };
});
app.use(router.routes());
const validUUID = '123e4567-e89b-12d3-a456-426614174000';
const res1 = await request(http.createServer(app.callback()))
.get(`/users/${validUUID}`)
.expect(200);
assert.strictEqual(res1.body.id, validUUID);
assert.strictEqual(res1.body.valid, true);
await request(http.createServer(app.callback()))
.get('/users/invalid-id')
.expect(400);
});
it('should load resource from database with router.param()', async () => {
const app = new Koa();
const router = new Router();
const User = {
findById: async (id: string) => {
if (id === '123') {
return { id: '123', name: 'John' };
}
return null;
}
};
router.param('user', async (id: string, ctx: RouterContext, next: Next) => {
const user = await User.findById(id);
if (!user) {
ctx.throw(404, 'User not found');
}
ctx.state.user = user;
return next();
});
router.get('/users/:user', (ctx: RouterContext) => {
ctx.body = ctx.state.user;
});
router.get('/users/:user/posts', async (ctx: RouterContext) => {
ctx.body = { userId: ctx.state.user.id, posts: [] };
});
app.use(router.routes());
const res1 = await request(http.createServer(app.callback()))
.get('/users/123')
.expect(200);
assert.strictEqual(res1.body.id, '123');
assert.strictEqual(res1.body.name, 'John');
const res2 = await request(http.createServer(app.callback()))
.get('/users/123/posts')
.expect(200);
assert.strictEqual(res2.body.userId, '123');
await request(http.createServer(app.callback()))
.get('/users/999')
.expect(404);
});
it('should support multiple param handlers', async () => {
const app = new Koa();
const router = new Router();
let validationCalled = false;
let loadCalled = false;
router.param('id', (value: string, ctx: RouterContext, next: Next) => {
if (!/^\d+$/.test(value)) {
ctx.throw(400, 'Invalid ID format');
}
validationCalled = true;
return next();
});
router.param(
'id',
async (value: string, ctx: RouterContext, next: Next) => {
ctx.state.resource = { id: value, loaded: true };
loadCalled = true;
return next();
}
);
router.get('/resource/:id', (ctx: RouterContext) => {
ctx.body = ctx.state.resource;
});
app.use(router.routes());
const res = await request(http.createServer(app.callback()))
.get('/resource/123')
.expect(200);
assert.strictEqual(validationCalled, true);
assert.strictEqual(loadCalled, true);
assert.strictEqual(res.body.id, '123');
assert.strictEqual(res.body.loaded, true);
});
});