hono 1.2.2 → 1.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -19
- package/dist/context.d.ts +2 -2
- package/dist/context.js +24 -6
- package/dist/hono.d.ts +21 -29
- package/dist/hono.js +27 -65
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -2
- package/dist/middleware/cookie/index.d.ts +4 -1
- package/dist/middleware/cookie/index.js +9 -4
- package/dist/middleware/serve-static/index.d.ts +1 -1
- package/dist/middleware/serve-static/index.js +7 -2
- package/dist/utils/url.js +4 -0
- package/package.json +3 -3
- package/dist/compose.test.d.ts +0 -1
- package/dist/compose.test.js +0 -511
- package/dist/context.test.d.ts +0 -1
- package/dist/context.test.js +0 -127
- package/dist/hono.test.d.ts +0 -1
- package/dist/hono.test.js +0 -592
- package/dist/middleware/basic-auth/index.test.d.ts +0 -1
- package/dist/middleware/basic-auth/index.test.js +0 -119
- package/dist/middleware/body-parse/index.test.d.ts +0 -1
- package/dist/middleware/body-parse/index.test.js +0 -59
- package/dist/middleware/cookie/index.test.d.ts +0 -1
- package/dist/middleware/cookie/index.test.js +0 -54
- package/dist/middleware/cors/index.test.d.ts +0 -1
- package/dist/middleware/cors/index.test.js +0 -59
- package/dist/middleware/etag/index.test.d.ts +0 -1
- package/dist/middleware/etag/index.test.js +0 -45
- package/dist/middleware/graphql-server/index.test.d.ts +0 -1
- package/dist/middleware/graphql-server/index.test.js +0 -480
- package/dist/middleware/graphql-server/parse-body.test.d.ts +0 -1
- package/dist/middleware/graphql-server/parse-body.test.js +0 -57
- package/dist/middleware/jwt/index.test.d.ts +0 -1
- package/dist/middleware/jwt/index.test.js +0 -51
- package/dist/middleware/logger/index.test.d.ts +0 -1
- package/dist/middleware/logger/index.test.js +0 -49
- package/dist/middleware/mustache/index.test.d.ts +0 -1
- package/dist/middleware/mustache/index.test.js +0 -49
- package/dist/middleware/powered-by/index.test.d.ts +0 -1
- package/dist/middleware/powered-by/index.test.js +0 -15
- package/dist/middleware/pretty-json/index.test.d.ts +0 -1
- package/dist/middleware/pretty-json/index.test.js +0 -28
- package/dist/middleware/serve-static/index.test.d.ts +0 -1
- package/dist/middleware/serve-static/index.test.js +0 -58
- package/dist/router/reg-exp-router/router.test.d.ts +0 -1
- package/dist/router/reg-exp-router/router.test.js +0 -212
- package/dist/router/trie-router/node.test.d.ts +0 -1
- package/dist/router/trie-router/node.test.js +0 -351
- package/dist/router/trie-router/router.test.d.ts +0 -1
- package/dist/router/trie-router/router.test.js +0 -98
- package/dist/utils/body.test.d.ts +0 -1
- package/dist/utils/body.test.js +0 -45
- package/dist/utils/buffer.test.d.ts +0 -1
- package/dist/utils/buffer.test.js +0 -36
- package/dist/utils/cloudflare.test.d.ts +0 -1
- package/dist/utils/cloudflare.test.js +0 -42
- package/dist/utils/crypto.test.d.ts +0 -1
- package/dist/utils/crypto.test.js +0 -15
- package/dist/utils/encode.test.d.ts +0 -1
- package/dist/utils/encode.test.js +0 -54
- package/dist/utils/http-status.test.d.ts +0 -1
- package/dist/utils/http-status.test.js +0 -8
- package/dist/utils/jwt/jwt.test.d.ts +0 -1
- package/dist/utils/jwt/jwt.test.js +0 -171
- package/dist/utils/jwt/types.test.d.ts +0 -1
- package/dist/utils/jwt/types.test.js +0 -12
- package/dist/utils/mime.test.d.ts +0 -1
- package/dist/utils/mime.test.js +0 -13
- package/dist/utils/url.test.d.ts +0 -1
- package/dist/utils/url.test.js +0 -87
package/dist/hono.test.js
DELETED
|
@@ -1,592 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const hono_1 = require("./hono");
|
|
4
|
-
const powered_by_1 = require("./middleware/powered-by");
|
|
5
|
-
describe('GET Request', () => {
|
|
6
|
-
const app = new hono_1.Hono();
|
|
7
|
-
app.get('/hello', async () => {
|
|
8
|
-
return new Response('hello', {
|
|
9
|
-
status: 200,
|
|
10
|
-
statusText: 'Hono is OK',
|
|
11
|
-
});
|
|
12
|
-
});
|
|
13
|
-
app.get('/hello-with-shortcuts', (c) => {
|
|
14
|
-
c.header('X-Custom', 'This is Hono');
|
|
15
|
-
c.status(201);
|
|
16
|
-
return c.html('<h1>Hono!!!</h1>');
|
|
17
|
-
});
|
|
18
|
-
it('GET /hello is ok', async () => {
|
|
19
|
-
const res = await app.request('http://localhost/hello');
|
|
20
|
-
expect(res).not.toBeNull();
|
|
21
|
-
expect(res.status).toBe(200);
|
|
22
|
-
expect(res.statusText).toBe('Hono is OK');
|
|
23
|
-
expect(await res.text()).toBe('hello');
|
|
24
|
-
});
|
|
25
|
-
it('GET /hello-with-shortcuts is ok', async () => {
|
|
26
|
-
const res = await app.request('http://localhost/hello-with-shortcuts');
|
|
27
|
-
expect(res).not.toBeNull();
|
|
28
|
-
expect(res.status).toBe(201);
|
|
29
|
-
expect(res.statusText).toBe('Created');
|
|
30
|
-
expect(res.headers.get('X-Custom')).toBe('This is Hono');
|
|
31
|
-
expect(res.headers.get('Content-Type')).toMatch(/text\/html/);
|
|
32
|
-
expect(await res.text()).toBe('<h1>Hono!!!</h1>');
|
|
33
|
-
});
|
|
34
|
-
it('GET / is not found', async () => {
|
|
35
|
-
const res = await app.request('http://localhost/');
|
|
36
|
-
expect(res).not.toBeNull();
|
|
37
|
-
expect(res.status).toBe(404);
|
|
38
|
-
expect(res.statusText).toBe('Not Found');
|
|
39
|
-
});
|
|
40
|
-
});
|
|
41
|
-
describe('strict parameter', () => {
|
|
42
|
-
describe('strict is true with not slash', () => {
|
|
43
|
-
const app = new hono_1.Hono();
|
|
44
|
-
app.get('/hello', (c) => {
|
|
45
|
-
return c.text('/hello');
|
|
46
|
-
});
|
|
47
|
-
it('/hello/ is not found', async () => {
|
|
48
|
-
let res = await app.request('http://localhost/hello');
|
|
49
|
-
expect(res).not.toBeNull();
|
|
50
|
-
expect(res.status).toBe(200);
|
|
51
|
-
res = await app.request('http://localhost/hello/');
|
|
52
|
-
expect(res).not.toBeNull();
|
|
53
|
-
expect(res.status).toBe(404);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
describe('strict is true with slash', () => {
|
|
57
|
-
const app = new hono_1.Hono();
|
|
58
|
-
app.get('/hello/', (c) => {
|
|
59
|
-
return c.text('/hello/');
|
|
60
|
-
});
|
|
61
|
-
it('/hello is not found', async () => {
|
|
62
|
-
let res = await app.request('http://localhost/hello/');
|
|
63
|
-
expect(res).not.toBeNull();
|
|
64
|
-
expect(res.status).toBe(200);
|
|
65
|
-
res = await app.request('http://localhost/hello');
|
|
66
|
-
expect(res).not.toBeNull();
|
|
67
|
-
expect(res.status).toBe(404);
|
|
68
|
-
});
|
|
69
|
-
});
|
|
70
|
-
describe('strict is false', () => {
|
|
71
|
-
const app = new hono_1.Hono({ strict: false });
|
|
72
|
-
app.get('/hello', (c) => {
|
|
73
|
-
return c.text('/hello');
|
|
74
|
-
});
|
|
75
|
-
it('/hello and /hello/ are treated as the same', async () => {
|
|
76
|
-
let res = await app.request('http://localhost/hello');
|
|
77
|
-
expect(res).not.toBeNull();
|
|
78
|
-
expect(res.status).toBe(200);
|
|
79
|
-
res = await app.request('http://localhost/hello/');
|
|
80
|
-
expect(res).not.toBeNull();
|
|
81
|
-
expect(res.status).toBe(200);
|
|
82
|
-
});
|
|
83
|
-
});
|
|
84
|
-
});
|
|
85
|
-
describe('Routing', () => {
|
|
86
|
-
const app = new hono_1.Hono();
|
|
87
|
-
it('Return it self', async () => {
|
|
88
|
-
const app2 = app.get('/', () => new Response('get /'));
|
|
89
|
-
expect(app2).not.toBeUndefined();
|
|
90
|
-
app2.delete('/', () => new Response('delete /'));
|
|
91
|
-
let res = await app2.request('http://localhost/', { method: 'GET' });
|
|
92
|
-
expect(res).not.toBeNull();
|
|
93
|
-
expect(res.status).toBe(200);
|
|
94
|
-
expect(await res.text()).toBe('get /');
|
|
95
|
-
res = await app2.request('http://localhost/', { method: 'DELETE' });
|
|
96
|
-
expect(res).not.toBeNull();
|
|
97
|
-
expect(res.status).toBe(200);
|
|
98
|
-
expect(await res.text()).toBe('delete /');
|
|
99
|
-
});
|
|
100
|
-
it('Nested route', async () => {
|
|
101
|
-
const book = app.route('/book');
|
|
102
|
-
book.get('/', (c) => c.text('get /book'));
|
|
103
|
-
book.get('/:id', (c) => {
|
|
104
|
-
return c.text('get /book/' + c.req.param('id'));
|
|
105
|
-
});
|
|
106
|
-
book.post('/', (c) => c.text('post /book'));
|
|
107
|
-
const user = app.route('/user');
|
|
108
|
-
user.get('/login', (c) => c.text('get /user/login'));
|
|
109
|
-
user.post('/register', (c) => c.text('post /user/register'));
|
|
110
|
-
let res = await app.request('http://localhost/book', { method: 'GET' });
|
|
111
|
-
expect(res.status).toBe(200);
|
|
112
|
-
expect(await res.text()).toBe('get /book');
|
|
113
|
-
res = await app.request('http://localhost/book/123', { method: 'GET' });
|
|
114
|
-
expect(res.status).toBe(200);
|
|
115
|
-
expect(await res.text()).toBe('get /book/123');
|
|
116
|
-
res = await app.request('http://localhost/book', { method: 'POST' });
|
|
117
|
-
expect(res.status).toBe(200);
|
|
118
|
-
expect(await res.text()).toBe('post /book');
|
|
119
|
-
res = await app.request('http://localhost/book/', { method: 'GET' });
|
|
120
|
-
expect(res.status).toBe(404);
|
|
121
|
-
res = await app.request('http://localhost/user/login', { method: 'GET' });
|
|
122
|
-
expect(res.status).toBe(200);
|
|
123
|
-
expect(await res.text()).toBe('get /user/login');
|
|
124
|
-
res = await app.request('http://localhost/user/register', { method: 'POST' });
|
|
125
|
-
expect(res.status).toBe(200);
|
|
126
|
-
expect(await res.text()).toBe('post /user/register');
|
|
127
|
-
});
|
|
128
|
-
describe('Chained route', () => {
|
|
129
|
-
app
|
|
130
|
-
.get('/chained/:abc', (c) => {
|
|
131
|
-
const abc = c.req.param('abc');
|
|
132
|
-
return c.text(`GET for ${abc}`);
|
|
133
|
-
})
|
|
134
|
-
.post((c) => {
|
|
135
|
-
const abc = c.req.param('abc');
|
|
136
|
-
return c.text(`POST for ${abc}`);
|
|
137
|
-
});
|
|
138
|
-
it('Should return 200 response from GET request', async () => {
|
|
139
|
-
const res = await app.request('http://localhost/chained/abc', { method: 'GET' });
|
|
140
|
-
expect(res.status).toBe(200);
|
|
141
|
-
expect(await res.text()).toBe('GET for abc');
|
|
142
|
-
});
|
|
143
|
-
it('Should return 200 response from POST request', async () => {
|
|
144
|
-
const res = await app.request('http://localhost/chained/abc', { method: 'POST' });
|
|
145
|
-
expect(res.status).toBe(200);
|
|
146
|
-
expect(await res.text()).toBe('POST for abc');
|
|
147
|
-
});
|
|
148
|
-
it('Should return 404 response from PUT request', async () => {
|
|
149
|
-
const res = await app.request('http://localhost/chained/abc', { method: 'PUT' });
|
|
150
|
-
expect(res.status).toBe(404);
|
|
151
|
-
});
|
|
152
|
-
});
|
|
153
|
-
});
|
|
154
|
-
describe('param and query', () => {
|
|
155
|
-
const app = new hono_1.Hono();
|
|
156
|
-
app.get('/entry/:id', (c) => {
|
|
157
|
-
const id = c.req.param('id');
|
|
158
|
-
return c.text(`id is ${id}`);
|
|
159
|
-
});
|
|
160
|
-
app.get('/date/:date{[0-9]+}', (c) => {
|
|
161
|
-
const date = c.req.param('date');
|
|
162
|
-
return c.text(`date is ${date}`);
|
|
163
|
-
});
|
|
164
|
-
app.get('/search', (c) => {
|
|
165
|
-
const name = c.req.query('name');
|
|
166
|
-
return c.text(`name is ${name}`);
|
|
167
|
-
});
|
|
168
|
-
app.get('/add-header', (c) => {
|
|
169
|
-
const bar = c.req.header('X-Foo');
|
|
170
|
-
return c.text(`foo is ${bar}`);
|
|
171
|
-
});
|
|
172
|
-
it('param of /entry/:id is found', async () => {
|
|
173
|
-
const res = await app.request('http://localhost/entry/123');
|
|
174
|
-
expect(res.status).toBe(200);
|
|
175
|
-
expect(await res.text()).toBe('id is 123');
|
|
176
|
-
});
|
|
177
|
-
it('param of /date/:date is found', async () => {
|
|
178
|
-
const res = await app.request('http://localhost/date/0401');
|
|
179
|
-
expect(res.status).toBe(200);
|
|
180
|
-
expect(await res.text()).toBe('date is 0401');
|
|
181
|
-
});
|
|
182
|
-
it('query of /search?name=sam is found', async () => {
|
|
183
|
-
const res = await app.request('http://localhost/search?name=sam');
|
|
184
|
-
expect(res.status).toBe(200);
|
|
185
|
-
expect(await res.text()).toBe('name is sam');
|
|
186
|
-
});
|
|
187
|
-
it('/add-header header - X-Foo is Bar', async () => {
|
|
188
|
-
const req = new Request('http://localhost/add-header');
|
|
189
|
-
req.headers.append('X-Foo', 'Bar');
|
|
190
|
-
const res = await app.request(req);
|
|
191
|
-
expect(res.status).toBe(200);
|
|
192
|
-
expect(await res.text()).toBe('foo is Bar');
|
|
193
|
-
});
|
|
194
|
-
});
|
|
195
|
-
describe('Middleware', () => {
|
|
196
|
-
describe('Basic', () => {
|
|
197
|
-
const app = new hono_1.Hono();
|
|
198
|
-
// Custom Logger
|
|
199
|
-
app.use('*', async (c, next) => {
|
|
200
|
-
console.log(`${c.req.method} : ${c.req.url}`);
|
|
201
|
-
await next();
|
|
202
|
-
});
|
|
203
|
-
// Append Custom Header
|
|
204
|
-
app.use('*', async (c, next) => {
|
|
205
|
-
await next();
|
|
206
|
-
c.res.headers.append('x-custom', 'root');
|
|
207
|
-
});
|
|
208
|
-
app.use('/hello', async (c, next) => {
|
|
209
|
-
await next();
|
|
210
|
-
c.res.headers.append('x-message', 'custom-header');
|
|
211
|
-
});
|
|
212
|
-
app.use('/hello/*', async (c, next) => {
|
|
213
|
-
await next();
|
|
214
|
-
c.res.headers.append('x-message-2', 'custom-header-2');
|
|
215
|
-
});
|
|
216
|
-
app.get('/hello', (c) => {
|
|
217
|
-
return c.text('hello');
|
|
218
|
-
});
|
|
219
|
-
app.get('/hello/:message', (c) => {
|
|
220
|
-
const message = c.req.param('message');
|
|
221
|
-
return c.text(`${message}`);
|
|
222
|
-
});
|
|
223
|
-
app.get('/error', () => {
|
|
224
|
-
throw new Error('Error!');
|
|
225
|
-
});
|
|
226
|
-
app.notFound((c) => {
|
|
227
|
-
return c.text('Not Found Foo', 404);
|
|
228
|
-
});
|
|
229
|
-
it('logging and custom header', async () => {
|
|
230
|
-
const res = await app.request('http://localhost/hello');
|
|
231
|
-
expect(res.status).toBe(200);
|
|
232
|
-
expect(await res.text()).toBe('hello');
|
|
233
|
-
expect(res.headers.get('x-custom')).toBe('root');
|
|
234
|
-
expect(res.headers.get('x-message')).toBe('custom-header');
|
|
235
|
-
expect(res.headers.get('x-message-2')).toBe('custom-header-2');
|
|
236
|
-
});
|
|
237
|
-
it('logging and custom header with named param', async () => {
|
|
238
|
-
const res = await app.request('http://localhost/hello/message');
|
|
239
|
-
expect(res.status).toBe(200);
|
|
240
|
-
expect(await res.text()).toBe('message');
|
|
241
|
-
expect(res.headers.get('x-custom')).toBe('root');
|
|
242
|
-
expect(res.headers.get('x-message-2')).toBe('custom-header-2');
|
|
243
|
-
});
|
|
244
|
-
it('not found', async () => {
|
|
245
|
-
const res = await app.request('http://localhost/foo');
|
|
246
|
-
expect(res.status).toBe(404);
|
|
247
|
-
expect(await res.text()).toBe('Not Found Foo');
|
|
248
|
-
});
|
|
249
|
-
it('internal server error', async () => {
|
|
250
|
-
const res = await app.request('http://localhost/error');
|
|
251
|
-
expect(res.status).toBe(500);
|
|
252
|
-
console.log(await res.text());
|
|
253
|
-
});
|
|
254
|
-
});
|
|
255
|
-
describe('Chained route', () => {
|
|
256
|
-
const app = new hono_1.Hono();
|
|
257
|
-
app
|
|
258
|
-
.use('/chained/*', async (c, next) => {
|
|
259
|
-
c.req.headers.append('x-before', 'abc');
|
|
260
|
-
await next();
|
|
261
|
-
})
|
|
262
|
-
.use(async (c, next) => {
|
|
263
|
-
await next();
|
|
264
|
-
c.header('x-after', c.req.header('x-before'));
|
|
265
|
-
})
|
|
266
|
-
.get('/chained/abc', (c) => {
|
|
267
|
-
return c.text('GET chained');
|
|
268
|
-
});
|
|
269
|
-
it('GET /chained/abc', async () => {
|
|
270
|
-
const res = await app.request('http://localhost/chained/abc');
|
|
271
|
-
expect(res.status).toBe(200);
|
|
272
|
-
expect(await res.text()).toBe('GET chained');
|
|
273
|
-
expect(res.headers.get('x-after')).toBe('abc');
|
|
274
|
-
});
|
|
275
|
-
});
|
|
276
|
-
describe('Multiple handler', () => {
|
|
277
|
-
const app = new hono_1.Hono();
|
|
278
|
-
app
|
|
279
|
-
.use('/multiple/*', async (c, next) => {
|
|
280
|
-
c.req.headers.append('x-before', 'abc');
|
|
281
|
-
await next();
|
|
282
|
-
}, async (c, next) => {
|
|
283
|
-
await next();
|
|
284
|
-
c.header('x-after', c.req.header('x-before'));
|
|
285
|
-
})
|
|
286
|
-
.get('/multiple/abc', (c) => {
|
|
287
|
-
return c.text('GET multiple');
|
|
288
|
-
});
|
|
289
|
-
it('GET /multiple/abc', async () => {
|
|
290
|
-
const res = await app.request('http://localhost/multiple/abc');
|
|
291
|
-
expect(res.status).toBe(200);
|
|
292
|
-
expect(await res.text()).toBe('GET multiple');
|
|
293
|
-
expect(res.headers.get('x-after')).toBe('abc');
|
|
294
|
-
});
|
|
295
|
-
});
|
|
296
|
-
});
|
|
297
|
-
describe('Builtin Middleware', () => {
|
|
298
|
-
const app = new hono_1.Hono();
|
|
299
|
-
app.use('/abc', (0, powered_by_1.poweredBy)());
|
|
300
|
-
app.use('/def', async (c, next) => {
|
|
301
|
-
const middleware = (0, powered_by_1.poweredBy)();
|
|
302
|
-
await middleware(c, next);
|
|
303
|
-
});
|
|
304
|
-
app.get('/abc', () => new Response());
|
|
305
|
-
app.get('/def', () => new Response());
|
|
306
|
-
it('"powered-by" middleware', async () => {
|
|
307
|
-
const res = await app.request('http://localhost/abc');
|
|
308
|
-
expect(res.headers.get('x-powered-by')).toBe('Hono');
|
|
309
|
-
});
|
|
310
|
-
it('"powered-by" middleware in a handler', async () => {
|
|
311
|
-
const res = await app.request('http://localhost/def');
|
|
312
|
-
expect(res.headers.get('x-powered-by')).toBe('Hono');
|
|
313
|
-
});
|
|
314
|
-
});
|
|
315
|
-
describe('Middleware with app.HTTP_METHOD', () => {
|
|
316
|
-
describe('Basic', () => {
|
|
317
|
-
const app = new hono_1.Hono();
|
|
318
|
-
app.all('*', async (c, next) => {
|
|
319
|
-
await next();
|
|
320
|
-
c.header('x-custom-message', 'hello');
|
|
321
|
-
});
|
|
322
|
-
const customHeader = async (c, next) => {
|
|
323
|
-
c.req.headers.append('x-custom-foo', 'bar');
|
|
324
|
-
await next();
|
|
325
|
-
};
|
|
326
|
-
const customHeader2 = async (c, next) => {
|
|
327
|
-
await next();
|
|
328
|
-
c.header('x-custom-foo-2', 'bar-2');
|
|
329
|
-
};
|
|
330
|
-
app
|
|
331
|
-
.get('/abc', customHeader, (c) => {
|
|
332
|
-
const foo = c.req.header('x-custom-foo') || '';
|
|
333
|
-
return c.text(foo);
|
|
334
|
-
})
|
|
335
|
-
.post(customHeader2, (c) => {
|
|
336
|
-
return c.text('POST /abc');
|
|
337
|
-
});
|
|
338
|
-
it('GET /abc', async () => {
|
|
339
|
-
const res = await app.request('http://localhost/abc');
|
|
340
|
-
expect(res.status).toBe(200);
|
|
341
|
-
expect(res.headers.get('x-custom-message')).toBe('hello');
|
|
342
|
-
expect(await res.text()).toBe('bar');
|
|
343
|
-
});
|
|
344
|
-
it('POST /abc', async () => {
|
|
345
|
-
const res = await app.request('http://localhost/abc', { method: 'POST' });
|
|
346
|
-
expect(res.status).toBe(200);
|
|
347
|
-
expect(await res.text()).toBe('POST /abc');
|
|
348
|
-
expect(res.headers.get('x-custom-foo-2')).toBe('bar-2');
|
|
349
|
-
});
|
|
350
|
-
});
|
|
351
|
-
describe('With builtin middleware', () => {
|
|
352
|
-
const app = new hono_1.Hono();
|
|
353
|
-
app.get('/abc', (0, powered_by_1.poweredBy)(), (c) => {
|
|
354
|
-
return c.text('GET /abc');
|
|
355
|
-
});
|
|
356
|
-
it('GET /abc', async () => {
|
|
357
|
-
const res = await app.request('http://localhost/abc');
|
|
358
|
-
expect(res.status).toBe(200);
|
|
359
|
-
expect(await res.text()).toBe('GET /abc');
|
|
360
|
-
expect(res.headers.get('x-powered-by')).toBe('Hono');
|
|
361
|
-
});
|
|
362
|
-
});
|
|
363
|
-
});
|
|
364
|
-
describe('Not Found', () => {
|
|
365
|
-
const app = new hono_1.Hono();
|
|
366
|
-
app.notFound((c) => {
|
|
367
|
-
return c.text('Custom 404 Not Found', 404);
|
|
368
|
-
});
|
|
369
|
-
app.get('/hello', (c) => {
|
|
370
|
-
return c.text('hello');
|
|
371
|
-
});
|
|
372
|
-
app.get('/notfound', (c) => {
|
|
373
|
-
return c.notFound();
|
|
374
|
-
});
|
|
375
|
-
it('Custom 404 Not Found', async () => {
|
|
376
|
-
let res = await app.request('http://localhost/hello');
|
|
377
|
-
expect(res.status).toBe(200);
|
|
378
|
-
res = await app.request('http://localhost/notfound');
|
|
379
|
-
expect(res.status).toBe(404);
|
|
380
|
-
res = await app.request('http://localhost/foo');
|
|
381
|
-
expect(res.status).toBe(404);
|
|
382
|
-
expect(await res.text()).toBe('Custom 404 Not Found');
|
|
383
|
-
});
|
|
384
|
-
});
|
|
385
|
-
describe('Redirect', () => {
|
|
386
|
-
const app = new hono_1.Hono();
|
|
387
|
-
app.get('/redirect', (c) => {
|
|
388
|
-
return c.redirect('/');
|
|
389
|
-
});
|
|
390
|
-
it('Absolute URL', async () => {
|
|
391
|
-
const res = await app.request('https://example.com/redirect');
|
|
392
|
-
expect(res.status).toBe(302);
|
|
393
|
-
expect(res.headers.get('Location')).toBe('https://example.com/');
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
describe('Error handle', () => {
|
|
397
|
-
const app = new hono_1.Hono();
|
|
398
|
-
app.get('/error', () => {
|
|
399
|
-
throw new Error('This is Error');
|
|
400
|
-
});
|
|
401
|
-
app.use('/error-middleware', async () => {
|
|
402
|
-
throw new Error('This is Middleware Error');
|
|
403
|
-
});
|
|
404
|
-
app.onError((err, c) => {
|
|
405
|
-
c.header('x-debug', err.message);
|
|
406
|
-
return c.text('Custom Error Message', 500);
|
|
407
|
-
});
|
|
408
|
-
it('Custom Error Message', async () => {
|
|
409
|
-
let res = await app.request('https://example.com/error');
|
|
410
|
-
expect(res.status).toBe(500);
|
|
411
|
-
expect(await res.text()).toBe('Custom Error Message');
|
|
412
|
-
expect(res.headers.get('x-debug')).toBe('This is Error');
|
|
413
|
-
res = await app.request('https://example.com/error-middleware');
|
|
414
|
-
expect(res.status).toBe(500);
|
|
415
|
-
expect(await res.text()).toBe('Custom Error Message');
|
|
416
|
-
expect(res.headers.get('x-debug')).toBe('This is Middleware Error');
|
|
417
|
-
});
|
|
418
|
-
});
|
|
419
|
-
describe('Request methods with custom middleware', () => {
|
|
420
|
-
const app = new hono_1.Hono();
|
|
421
|
-
app.use('*', async (c, next) => {
|
|
422
|
-
const query = c.req.query('foo');
|
|
423
|
-
const param = c.req.param('foo');
|
|
424
|
-
const header = c.req.header('User-Agent');
|
|
425
|
-
await next();
|
|
426
|
-
c.header('X-Query-2', query);
|
|
427
|
-
c.header('X-Param-2', param);
|
|
428
|
-
c.header('X-Header-2', header);
|
|
429
|
-
});
|
|
430
|
-
app.get('/:foo', (c) => {
|
|
431
|
-
const query = c.req.query('foo');
|
|
432
|
-
const param = c.req.param('foo');
|
|
433
|
-
const header = c.req.header('User-Agent');
|
|
434
|
-
c.header('X-Query', query);
|
|
435
|
-
c.header('X-Param', param);
|
|
436
|
-
c.header('X-Header', header);
|
|
437
|
-
return c.body('Hono');
|
|
438
|
-
});
|
|
439
|
-
it('query', async () => {
|
|
440
|
-
const url = new URL('http://localhost/bar');
|
|
441
|
-
url.searchParams.append('foo', 'bar');
|
|
442
|
-
const req = new Request(url.toString());
|
|
443
|
-
req.headers.append('User-Agent', 'bar');
|
|
444
|
-
const res = await app.request(req);
|
|
445
|
-
expect(res.status).toBe(200);
|
|
446
|
-
expect(res.headers.get('X-Query')).toBe('bar');
|
|
447
|
-
expect(res.headers.get('X-Param')).toBe('bar');
|
|
448
|
-
expect(res.headers.get('X-Header')).toBe('bar');
|
|
449
|
-
expect(res.headers.get('X-Query-2')).toBe('bar');
|
|
450
|
-
expect(res.headers.get('X-Param-2')).toBe('bar');
|
|
451
|
-
expect(res.headers.get('X-Header-2')).toBe('bar');
|
|
452
|
-
});
|
|
453
|
-
});
|
|
454
|
-
describe('`Route` with app.route', () => {
|
|
455
|
-
const app = new hono_1.Hono();
|
|
456
|
-
describe('Basic', () => {
|
|
457
|
-
const route = new hono_1.Route();
|
|
458
|
-
route.get('/post', (c) => c.text('GET /POST'));
|
|
459
|
-
route.post('/post', (c) => c.text('POST /POST'));
|
|
460
|
-
app.route('/v1', route);
|
|
461
|
-
it('Should return 200 response - GET /v1/post', async () => {
|
|
462
|
-
const res = await app.request('http://localhost/v1/post');
|
|
463
|
-
expect(res.status).toBe(200);
|
|
464
|
-
expect(await res.text()).toBe('GET /POST');
|
|
465
|
-
});
|
|
466
|
-
it('Should return 200 response - POST /v1/post', async () => {
|
|
467
|
-
const res = await app.request('http://localhost/v1/post', { method: 'POST' });
|
|
468
|
-
expect(res.status).toBe(200);
|
|
469
|
-
expect(await res.text()).toBe('POST /POST');
|
|
470
|
-
});
|
|
471
|
-
it('Should return 404 response - DELETE /v1/post', async () => {
|
|
472
|
-
const res = await app.request('http://localhost/v1/post', { method: 'DELETE' });
|
|
473
|
-
expect(res.status).toBe(404);
|
|
474
|
-
});
|
|
475
|
-
it('Should return 404 response - GET /post', async () => {
|
|
476
|
-
const res = await app.request('http://localhost/post');
|
|
477
|
-
expect(res.status).toBe(404);
|
|
478
|
-
});
|
|
479
|
-
});
|
|
480
|
-
describe('Chaining', () => {
|
|
481
|
-
const route = new hono_1.Route();
|
|
482
|
-
route.get('/post', (c) => c.text('GET /POST v2')).post((c) => c.text('POST /POST v2'));
|
|
483
|
-
app.route('/v2', route);
|
|
484
|
-
it('Should return 200 response - GET /v2/post', async () => {
|
|
485
|
-
const res = await app.request('http://localhost/v2/post');
|
|
486
|
-
expect(res.status).toBe(200);
|
|
487
|
-
expect(await res.text()).toBe('GET /POST v2');
|
|
488
|
-
});
|
|
489
|
-
it('Should return 200 response - POST /v2/post', async () => {
|
|
490
|
-
const res = await app.request('http://localhost/v2/post', { method: 'POST' });
|
|
491
|
-
expect(res.status).toBe(200);
|
|
492
|
-
expect(await res.text()).toBe('POST /POST v2');
|
|
493
|
-
});
|
|
494
|
-
it('Should return 404 response - DELETE /v2/post', async () => {
|
|
495
|
-
const res = await app.request('http://localhost/v2/post', { method: 'DELETE' });
|
|
496
|
-
expect(res.status).toBe(404);
|
|
497
|
-
});
|
|
498
|
-
});
|
|
499
|
-
describe('Named parameter', () => {
|
|
500
|
-
const route = new hono_1.Route();
|
|
501
|
-
route.get('/post/:id', (c) => {
|
|
502
|
-
const id = c.req.param('id');
|
|
503
|
-
return c.text(`GET /post/${id} v3`);
|
|
504
|
-
});
|
|
505
|
-
app.route('/v3', route);
|
|
506
|
-
it('Should return 200 response - GET /v3/post/1', async () => {
|
|
507
|
-
const res = await app.request('http://localhost/v3/post/1');
|
|
508
|
-
expect(res.status).toBe(200);
|
|
509
|
-
expect(await res.text()).toBe('GET /post/1 v3');
|
|
510
|
-
});
|
|
511
|
-
it('Should return 404 response - GET /v3/post', async () => {
|
|
512
|
-
const res = await app.request('http://localhost/v3/post/abc/def');
|
|
513
|
-
expect(res.status).toBe(404);
|
|
514
|
-
});
|
|
515
|
-
});
|
|
516
|
-
});
|
|
517
|
-
describe('Multiple handler', () => {
|
|
518
|
-
describe('handler + handler', () => {
|
|
519
|
-
const app = new hono_1.Hono();
|
|
520
|
-
app.get('/:type/:id', (c) => {
|
|
521
|
-
return c.text('foo');
|
|
522
|
-
});
|
|
523
|
-
app.get('/posts/:id', (c) => {
|
|
524
|
-
const id = c.req.param('id');
|
|
525
|
-
return c.text(`id is ${id}`);
|
|
526
|
-
});
|
|
527
|
-
it('Should return response from `specialized` route', async () => {
|
|
528
|
-
const res = await app.request('http://localhost/posts/123');
|
|
529
|
-
expect(res.status).toBe(200);
|
|
530
|
-
expect(await res.text()).toBe('id is 123');
|
|
531
|
-
});
|
|
532
|
-
});
|
|
533
|
-
describe('Duplicate param name', () => {
|
|
534
|
-
it('self', () => {
|
|
535
|
-
const app = new hono_1.Hono();
|
|
536
|
-
expect(() => {
|
|
537
|
-
app.get('/:id/:id', (c) => {
|
|
538
|
-
const id = c.req.param('id');
|
|
539
|
-
return c.text(`id is ${id}`);
|
|
540
|
-
});
|
|
541
|
-
}).toThrowError(/Duplicate param name/);
|
|
542
|
-
});
|
|
543
|
-
it('parent', () => {
|
|
544
|
-
const app = new hono_1.Hono();
|
|
545
|
-
app.get('/:id/:action', (c) => {
|
|
546
|
-
return c.text('foo');
|
|
547
|
-
});
|
|
548
|
-
expect(() => {
|
|
549
|
-
app.get('/posts/:id', (c) => {
|
|
550
|
-
const id = c.req.param('id');
|
|
551
|
-
return c.text(`id is ${id}`);
|
|
552
|
-
});
|
|
553
|
-
}).toThrowError(/Duplicate param name/);
|
|
554
|
-
});
|
|
555
|
-
it('child', () => {
|
|
556
|
-
const app = new hono_1.Hono();
|
|
557
|
-
app.get('/posts/:id', (c) => {
|
|
558
|
-
return c.text('foo');
|
|
559
|
-
});
|
|
560
|
-
expect(() => {
|
|
561
|
-
app.get('/:id/:action', (c) => {
|
|
562
|
-
const id = c.req.param('id');
|
|
563
|
-
return c.text(`id is ${id}`);
|
|
564
|
-
});
|
|
565
|
-
}).toThrowError(/Duplicate param name/);
|
|
566
|
-
});
|
|
567
|
-
it('hierarchy', () => {
|
|
568
|
-
const app = new hono_1.Hono();
|
|
569
|
-
app.get('/posts/:id/comments/:comment_id', (c) => {
|
|
570
|
-
return c.text('foo');
|
|
571
|
-
});
|
|
572
|
-
expect(() => {
|
|
573
|
-
app.get('/posts/:id', (c) => {
|
|
574
|
-
const id = c.req.param('id');
|
|
575
|
-
return c.text(`id is ${id}`);
|
|
576
|
-
});
|
|
577
|
-
}).not.toThrow();
|
|
578
|
-
});
|
|
579
|
-
it('different regular expression', () => {
|
|
580
|
-
const app = new hono_1.Hono();
|
|
581
|
-
app.get('/:id/:action{create|update}', (c) => {
|
|
582
|
-
return c.text('foo');
|
|
583
|
-
});
|
|
584
|
-
expect(() => {
|
|
585
|
-
app.get('/:id/:action{delete}', (c) => {
|
|
586
|
-
const id = c.req.param('id');
|
|
587
|
-
return c.text(`id is ${id}`);
|
|
588
|
-
});
|
|
589
|
-
}).not.toThrow();
|
|
590
|
-
});
|
|
591
|
-
});
|
|
592
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|