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.
Files changed (71) hide show
  1. package/README.md +90 -19
  2. package/dist/context.d.ts +2 -2
  3. package/dist/context.js +24 -6
  4. package/dist/hono.d.ts +21 -29
  5. package/dist/hono.js +27 -65
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +1 -2
  8. package/dist/middleware/cookie/index.d.ts +4 -1
  9. package/dist/middleware/cookie/index.js +9 -4
  10. package/dist/middleware/serve-static/index.d.ts +1 -1
  11. package/dist/middleware/serve-static/index.js +7 -2
  12. package/dist/utils/url.js +4 -0
  13. package/package.json +3 -3
  14. package/dist/compose.test.d.ts +0 -1
  15. package/dist/compose.test.js +0 -511
  16. package/dist/context.test.d.ts +0 -1
  17. package/dist/context.test.js +0 -127
  18. package/dist/hono.test.d.ts +0 -1
  19. package/dist/hono.test.js +0 -592
  20. package/dist/middleware/basic-auth/index.test.d.ts +0 -1
  21. package/dist/middleware/basic-auth/index.test.js +0 -119
  22. package/dist/middleware/body-parse/index.test.d.ts +0 -1
  23. package/dist/middleware/body-parse/index.test.js +0 -59
  24. package/dist/middleware/cookie/index.test.d.ts +0 -1
  25. package/dist/middleware/cookie/index.test.js +0 -54
  26. package/dist/middleware/cors/index.test.d.ts +0 -1
  27. package/dist/middleware/cors/index.test.js +0 -59
  28. package/dist/middleware/etag/index.test.d.ts +0 -1
  29. package/dist/middleware/etag/index.test.js +0 -45
  30. package/dist/middleware/graphql-server/index.test.d.ts +0 -1
  31. package/dist/middleware/graphql-server/index.test.js +0 -480
  32. package/dist/middleware/graphql-server/parse-body.test.d.ts +0 -1
  33. package/dist/middleware/graphql-server/parse-body.test.js +0 -57
  34. package/dist/middleware/jwt/index.test.d.ts +0 -1
  35. package/dist/middleware/jwt/index.test.js +0 -51
  36. package/dist/middleware/logger/index.test.d.ts +0 -1
  37. package/dist/middleware/logger/index.test.js +0 -49
  38. package/dist/middleware/mustache/index.test.d.ts +0 -1
  39. package/dist/middleware/mustache/index.test.js +0 -49
  40. package/dist/middleware/powered-by/index.test.d.ts +0 -1
  41. package/dist/middleware/powered-by/index.test.js +0 -15
  42. package/dist/middleware/pretty-json/index.test.d.ts +0 -1
  43. package/dist/middleware/pretty-json/index.test.js +0 -28
  44. package/dist/middleware/serve-static/index.test.d.ts +0 -1
  45. package/dist/middleware/serve-static/index.test.js +0 -58
  46. package/dist/router/reg-exp-router/router.test.d.ts +0 -1
  47. package/dist/router/reg-exp-router/router.test.js +0 -212
  48. package/dist/router/trie-router/node.test.d.ts +0 -1
  49. package/dist/router/trie-router/node.test.js +0 -351
  50. package/dist/router/trie-router/router.test.d.ts +0 -1
  51. package/dist/router/trie-router/router.test.js +0 -98
  52. package/dist/utils/body.test.d.ts +0 -1
  53. package/dist/utils/body.test.js +0 -45
  54. package/dist/utils/buffer.test.d.ts +0 -1
  55. package/dist/utils/buffer.test.js +0 -36
  56. package/dist/utils/cloudflare.test.d.ts +0 -1
  57. package/dist/utils/cloudflare.test.js +0 -42
  58. package/dist/utils/crypto.test.d.ts +0 -1
  59. package/dist/utils/crypto.test.js +0 -15
  60. package/dist/utils/encode.test.d.ts +0 -1
  61. package/dist/utils/encode.test.js +0 -54
  62. package/dist/utils/http-status.test.d.ts +0 -1
  63. package/dist/utils/http-status.test.js +0 -8
  64. package/dist/utils/jwt/jwt.test.d.ts +0 -1
  65. package/dist/utils/jwt/jwt.test.js +0 -171
  66. package/dist/utils/jwt/types.test.d.ts +0 -1
  67. package/dist/utils/jwt/types.test.js +0 -12
  68. package/dist/utils/mime.test.d.ts +0 -1
  69. package/dist/utils/mime.test.js +0 -13
  70. package/dist/utils/url.test.d.ts +0 -1
  71. package/dist/utils/url.test.js +0 -87
@@ -1,480 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const graphql_1 = require("graphql");
4
- const hono_1 = require("../../hono");
5
- const _1 = require(".");
6
- // Do not show `console.error` messages
7
- jest.spyOn(console, 'error').mockImplementation();
8
- describe('errorMessages', () => {
9
- const messages = (0, _1.errorMessages)(['message a', 'message b']);
10
- expect(messages).toEqual({
11
- errors: [
12
- {
13
- message: 'message a',
14
- },
15
- {
16
- message: 'message b',
17
- },
18
- ],
19
- });
20
- });
21
- describe('GraphQL Middleware - Simple way', () => {
22
- // Construct a schema, using GraphQL schema language
23
- const schema = (0, graphql_1.buildSchema)(`
24
- type Query {
25
- hello: String
26
- }
27
- `);
28
- // The root provides a resolver function for each API endpoint
29
- const rootValue = {
30
- hello: () => 'Hello world!',
31
- };
32
- const app = new hono_1.Hono();
33
- app.use('/graphql', (0, _1.graphqlServer)({
34
- schema,
35
- rootValue,
36
- }));
37
- it('Should return GraphQL response', async () => {
38
- const query = 'query { hello }';
39
- const body = {
40
- query: query,
41
- };
42
- const res = await app.request('http://localhost/graphql', {
43
- method: 'POST',
44
- headers: {
45
- 'Content-Type': 'application/json',
46
- },
47
- body: JSON.stringify(body),
48
- });
49
- expect(res).not.toBeNull();
50
- expect(res.status).toBe(200);
51
- expect(await res.text()).toBe('{"data":{"hello":"Hello world!"}}');
52
- });
53
- });
54
- const QueryRootType = new graphql_1.GraphQLObjectType({
55
- name: 'QueryRoot',
56
- fields: {
57
- test: {
58
- type: graphql_1.GraphQLString,
59
- args: {
60
- who: { type: graphql_1.GraphQLString },
61
- },
62
- resolve: (_root, args) => { var _a; return 'Hello ' + ((_a = args.who) !== null && _a !== void 0 ? _a : 'World'); },
63
- },
64
- thrower: {
65
- type: graphql_1.GraphQLString,
66
- resolve() {
67
- throw new Error('Throws!');
68
- },
69
- },
70
- },
71
- });
72
- const TestSchema = new graphql_1.GraphQLSchema({
73
- query: QueryRootType,
74
- mutation: new graphql_1.GraphQLObjectType({
75
- name: 'MutationRoot',
76
- fields: {
77
- writeTest: {
78
- type: QueryRootType,
79
- resolve: () => ({}),
80
- },
81
- },
82
- }),
83
- });
84
- const urlString = (query) => {
85
- const base = 'http://localhost/graphql';
86
- if (!query)
87
- return base;
88
- const queryString = new URLSearchParams(query).toString();
89
- return `${base}?${queryString}`;
90
- };
91
- describe('GraphQL Middleware - GET functionality', () => {
92
- const app = new hono_1.Hono();
93
- app.use('/graphql', (0, _1.graphqlServer)({
94
- schema: TestSchema,
95
- }));
96
- it('Allows GET with variable values', async () => {
97
- const query = {
98
- query: 'query helloWho($who: String){ test(who: $who) }',
99
- variables: JSON.stringify({ who: 'Dolly' }),
100
- };
101
- const res = await app.request(urlString(query), {
102
- method: 'GET',
103
- });
104
- expect(res.status).toBe(200);
105
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
106
- });
107
- it('Allows GET with operation name', async () => {
108
- const query = {
109
- query: `
110
- query helloYou { test(who: "You"), ...shared }
111
- query helloWorld { test(who: "World"), ...shared }
112
- query helloDolly { test(who: "Dolly"), ...shared }
113
- fragment shared on QueryRoot {
114
- shared: test(who: "Everyone")
115
- }
116
- `,
117
- operationName: 'helloWorld',
118
- };
119
- const res = await app.request(urlString(query), {
120
- method: 'GET',
121
- });
122
- expect(res.status).toBe(200);
123
- expect(await res.json()).toEqual({
124
- data: {
125
- test: 'Hello World',
126
- shared: 'Hello Everyone',
127
- },
128
- });
129
- });
130
- it('Reports validation errors', async () => {
131
- const query = { query: '{ test, unknownOne, unknownTwo }' };
132
- const res = await app.request(urlString(query), {
133
- method: 'GET',
134
- });
135
- expect(res.status).toBe(400);
136
- });
137
- it('Errors when missing operation name', async () => {
138
- const query = {
139
- query: `
140
- query TestQuery { test }
141
- mutation TestMutation { writeTest { test } }
142
- `,
143
- };
144
- const res = await app.request(urlString(query), {
145
- method: 'GET',
146
- });
147
- expect(res.status).toBe(500);
148
- });
149
- it('Errors when sending a mutation via GET', async () => {
150
- const query = {
151
- query: 'mutation TestMutation { writeTest { test } }',
152
- };
153
- const res = await app.request(urlString(query), {
154
- method: 'GET',
155
- });
156
- expect(res.status).toBe(405);
157
- });
158
- it('Errors when selecting a mutation within a GET', async () => {
159
- const query = {
160
- operationName: 'TestMutation',
161
- query: `
162
- query TestQuery { test }
163
- mutation TestMutation { writeTest { test } }
164
- `,
165
- };
166
- const res = await app.request(urlString(query), {
167
- method: 'GET',
168
- });
169
- expect(res.status).toBe(405);
170
- });
171
- it('Allows a mutation to exist within a GET', async () => {
172
- const query = {
173
- operationName: 'TestQuery',
174
- query: `
175
- mutation TestMutation { writeTest { test } }
176
- query TestQuery { test }
177
- `,
178
- };
179
- const res = await app.request(urlString(query), {
180
- method: 'GET',
181
- });
182
- expect(res.status).toBe(200);
183
- expect(await res.json()).toEqual({
184
- data: {
185
- test: 'Hello World',
186
- },
187
- });
188
- });
189
- });
190
- describe('GraphQL Middleware - POST functionality', () => {
191
- const app = new hono_1.Hono();
192
- app.use('/graphql', (0, _1.graphqlServer)({
193
- schema: TestSchema,
194
- }));
195
- it('Allows POST with JSON encoding', async () => {
196
- const query = { query: '{test}' };
197
- const res = await app.request(urlString(), {
198
- method: 'POST',
199
- headers: {
200
- 'Content-Type': 'application/json',
201
- },
202
- body: JSON.stringify(query),
203
- });
204
- expect(res.status).toBe(200);
205
- expect(await res.text()).toBe('{"data":{"test":"Hello World"}}');
206
- });
207
- it('Allows sending a mutation via POST', async () => {
208
- const query = { query: 'mutation TestMutation { writeTest { test } }' };
209
- const res = await app.request(urlString(), {
210
- method: 'POST',
211
- headers: {
212
- 'Content-Type': 'application/json',
213
- },
214
- body: JSON.stringify(query),
215
- });
216
- expect(res.status).toBe(200);
217
- expect(await res.text()).toBe('{"data":{"writeTest":{"test":"Hello World"}}}');
218
- });
219
- it('Allows POST with url encoding', async () => {
220
- const query = {
221
- query: '{test}',
222
- };
223
- const res = await app.request(urlString(query), {
224
- method: 'POST',
225
- });
226
- expect(res.status).toBe(200);
227
- expect(await res.text()).toBe('{"data":{"test":"Hello World"}}');
228
- });
229
- it('Supports POST JSON query with string variables', async () => {
230
- const query = {
231
- query: 'query helloWho($who: String){ test(who: $who) }',
232
- variables: JSON.stringify({ who: 'Dolly' }),
233
- };
234
- const res = await app.request(urlString(), {
235
- method: 'POST',
236
- headers: {
237
- 'Content-Type': 'application/json',
238
- },
239
- body: JSON.stringify(query),
240
- });
241
- expect(res.status).toBe(200);
242
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
243
- });
244
- it('Supports POST url encoded query with string variables', async () => {
245
- const searchParams = new URLSearchParams({
246
- query: 'query helloWho($who: String){ test(who: $who) }',
247
- variables: JSON.stringify({ who: 'Dolly' }),
248
- });
249
- const res = await app.request(urlString(), {
250
- method: 'POST',
251
- headers: {
252
- 'Content-Type': 'application/x-www-form-urlencoded',
253
- },
254
- body: searchParams.toString(),
255
- });
256
- expect(res.status).toBe(200);
257
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
258
- });
259
- it('Supports POST JSON query with GET variable values', async () => {
260
- const variables = {
261
- variables: JSON.stringify({ who: 'Dolly' }),
262
- };
263
- const query = { query: 'query helloWho($who: String){ test(who: $who) }' };
264
- const res = await app.request(urlString(variables), {
265
- method: 'POST',
266
- headers: {
267
- 'Content-Type': 'application/json',
268
- },
269
- body: JSON.stringify(query),
270
- });
271
- expect(res.status).toBe(200);
272
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
273
- });
274
- it('Supports POST url encoded query with GET variable values', async () => {
275
- const searchParams = new URLSearchParams({
276
- query: 'query helloWho($who: String){ test(who: $who) }',
277
- });
278
- const variables = {
279
- variables: JSON.stringify({ who: 'Dolly' }),
280
- };
281
- const res = await app.request(urlString(variables), {
282
- method: 'POST',
283
- headers: {
284
- 'Content-Type': 'application/x-www-form-urlencoded',
285
- },
286
- body: searchParams.toString(),
287
- });
288
- expect(res.status).toBe(200);
289
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
290
- });
291
- it('Supports POST raw text query with GET variable values', async () => {
292
- const variables = {
293
- variables: JSON.stringify({ who: 'Dolly' }),
294
- };
295
- const res = await app.request(urlString(variables), {
296
- method: 'POST',
297
- headers: {
298
- 'Content-Type': 'application/graphql',
299
- },
300
- body: 'query helloWho($who: String){ test(who: $who) }',
301
- });
302
- expect(res.status).toBe(200);
303
- expect(await res.text()).toBe('{"data":{"test":"Hello Dolly"}}');
304
- });
305
- it('Allows POST with operation name', async () => {
306
- const query = {
307
- query: `
308
- query helloYou { test(who: "You"), ...shared }
309
- query helloWorld { test(who: "World"), ...shared }
310
- query helloDolly { test(who: "Dolly"), ...shared }
311
- fragment shared on QueryRoot {
312
- shared: test(who: "Everyone")
313
- }
314
- `,
315
- operationName: 'helloWorld',
316
- };
317
- const res = await app.request(urlString(), {
318
- method: 'POST',
319
- headers: {
320
- 'Content-Type': 'application/json',
321
- },
322
- body: JSON.stringify(query),
323
- });
324
- expect(res.status).toBe(200);
325
- expect(await res.json()).toEqual({
326
- data: {
327
- test: 'Hello World',
328
- shared: 'Hello Everyone',
329
- },
330
- });
331
- });
332
- it('Allows POST with GET operation name', async () => {
333
- const res = await app.request(urlString({
334
- operationName: 'helloWorld',
335
- }), {
336
- method: 'POST',
337
- headers: {
338
- 'Content-Type': 'application/graphql',
339
- },
340
- body: `
341
- query helloYou { test(who: "You"), ...shared }
342
- query helloWorld { test(who: "World"), ...shared }
343
- query helloDolly { test(who: "Dolly"), ...shared }
344
- fragment shared on QueryRoot {
345
- shared: test(who: "Everyone")
346
- }
347
- `,
348
- });
349
- expect(res.status).toBe(200);
350
- expect(await res.json()).toEqual({
351
- data: {
352
- test: 'Hello World',
353
- shared: 'Hello Everyone',
354
- },
355
- });
356
- });
357
- });
358
- describe('Pretty printing', () => {
359
- it('Supports pretty printing', async () => {
360
- const app = new hono_1.Hono();
361
- app.use('/graphql', (0, _1.graphqlServer)({
362
- schema: TestSchema,
363
- pretty: true,
364
- }));
365
- const res = await app.request(urlString({ query: '{test}' }));
366
- expect(await res.text()).toEqual([
367
- // Pretty printed JSON
368
- '{',
369
- ' "data": {',
370
- ' "test": "Hello World"',
371
- ' }',
372
- '}',
373
- ].join('\n'));
374
- });
375
- });
376
- describe('Error handling functionality', () => {
377
- const app = new hono_1.Hono();
378
- app.use('/graphql', (0, _1.graphqlServer)({
379
- schema: TestSchema,
380
- }));
381
- it('Handles query errors from non-null top field errors', async () => {
382
- const schema = new graphql_1.GraphQLSchema({
383
- query: new graphql_1.GraphQLObjectType({
384
- name: 'Query',
385
- fields: {
386
- test: {
387
- type: new graphql_1.GraphQLNonNull(graphql_1.GraphQLString),
388
- resolve() {
389
- throw new Error('Throws!');
390
- },
391
- },
392
- },
393
- }),
394
- });
395
- const app = new hono_1.Hono();
396
- app.use('/graphql', (0, _1.graphqlServer)({ schema }));
397
- const res = await app.request(urlString({
398
- query: '{ test }',
399
- }));
400
- expect(res.status).toBe(500);
401
- });
402
- it('Handles syntax errors caught by GraphQL', async () => {
403
- const res = await app.request(urlString({
404
- query: 'syntax_error',
405
- }), {
406
- method: 'GET',
407
- });
408
- expect(res.status).toBe(400);
409
- });
410
- it('Handles errors caused by a lack of query', async () => {
411
- const res = await app.request(urlString(), {
412
- method: 'GET',
413
- });
414
- expect(res.status).toBe(400);
415
- });
416
- it('Handles invalid JSON bodies', async () => {
417
- const res = await app.request(urlString(), {
418
- method: 'POST',
419
- headers: {
420
- 'Content-Type': 'application/json',
421
- },
422
- body: JSON.stringify([]),
423
- });
424
- expect(res.status).toBe(400);
425
- });
426
- it('Handles incomplete JSON bodies', async () => {
427
- const res = await app.request(urlString(), {
428
- method: 'POST',
429
- headers: {
430
- 'Content-Type': 'application/json',
431
- },
432
- body: '{"query":',
433
- });
434
- expect(res.status).toBe(400);
435
- });
436
- it('Handles plain POST text', async () => {
437
- const res = await app.request(urlString({
438
- variables: JSON.stringify({ who: 'Dolly' }),
439
- }), {
440
- method: 'POST',
441
- headers: {
442
- 'Content-Type': 'text/plain',
443
- },
444
- body: 'query helloWho($who: String){ test(who: $who) }',
445
- });
446
- expect(res.status).toBe(400);
447
- });
448
- it('Handles poorly formed variables', async () => {
449
- const res = await app.request(urlString({
450
- variables: 'who:You',
451
- query: 'query helloWho($who: String){ test(who: $who) }',
452
- }), {
453
- method: 'GET',
454
- });
455
- expect(res.status).toBe(400);
456
- });
457
- it('Handles invalid variables', async () => {
458
- const res = await app.request(urlString(), {
459
- method: 'POST',
460
- headers: {
461
- 'Content-Type': 'application/json',
462
- },
463
- body: JSON.stringify({
464
- query: 'query helloWho($who: String){ test(who: $who) }',
465
- variables: { who: ['John', 'Jane'] },
466
- }),
467
- });
468
- expect(res.status).toBe(500);
469
- });
470
- it('Handles unsupported HTTP methods', async () => {
471
- const res = await app.request(urlString({ query: '{test}' }), {
472
- method: 'PUT',
473
- });
474
- expect(res.status).toBe(405);
475
- expect(res.headers.get('allow')).toBe('GET, POST');
476
- expect(await res.json()).toEqual({
477
- errors: [{ message: 'GraphQL only supports GET and POST requests.' }],
478
- });
479
- });
480
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,57 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const parse_body_1 = require("./parse-body");
4
- describe('parseBody', () => {
5
- it('Should return a blank JSON object', async () => {
6
- const req = new Request('http://localhost/graphql');
7
- const res = await (0, parse_body_1.parseBody)(req);
8
- expect(res).toEqual({});
9
- });
10
- it('With Content-Type: application/graphql', async () => {
11
- const req = new Request('http://localhost/graphql', {
12
- method: 'POST',
13
- headers: {
14
- 'Content-Type': 'application/graphql',
15
- },
16
- body: 'query { hello }',
17
- });
18
- const res = await (0, parse_body_1.parseBody)(req);
19
- expect(res).toEqual({ query: 'query { hello }' });
20
- });
21
- it('With Content-Type: application/json', async () => {
22
- const variables = JSON.stringify({ who: 'Dolly' });
23
- const req = new Request('http://localhost/graphql', {
24
- method: 'POST',
25
- headers: {
26
- 'Content-Type': 'application/json',
27
- },
28
- body: JSON.stringify({
29
- query: 'query { hello }',
30
- variables: variables,
31
- }),
32
- });
33
- const res = await (0, parse_body_1.parseBody)(req);
34
- expect(res).toEqual({
35
- query: 'query { hello }',
36
- variables: variables,
37
- });
38
- });
39
- it('With Content-Type: application/x-www-form-urlencoded', async () => {
40
- const variables = JSON.stringify({ who: 'Dolly' });
41
- const searchParams = new URLSearchParams();
42
- searchParams.append('query', 'query { hello }');
43
- searchParams.append('variables', variables);
44
- const req = new Request('http://localhost/graphql', {
45
- method: 'POST',
46
- headers: {
47
- 'Content-Type': 'application/x-www-form-urlencoded',
48
- },
49
- body: searchParams.toString(),
50
- });
51
- const res = await (0, parse_body_1.parseBody)(req);
52
- expect(res).toEqual({
53
- query: 'query { hello }',
54
- variables: variables,
55
- });
56
- });
57
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,51 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const hono_1 = require("../../hono");
4
- const _1 = require(".");
5
- describe('Jwt Auth by Middleware', () => {
6
- const crypto = global.crypto;
7
- beforeAll(() => {
8
- global.crypto = require('crypto').webcrypto;
9
- });
10
- afterAll(() => {
11
- global.crypto = crypto;
12
- });
13
- const app = new hono_1.Hono();
14
- app.use('/auth/*', (0, _1.jwt)({ secret: 'a-secret' }));
15
- app.use('/auth-unicode/*', (0, _1.jwt)({ secret: 'a-secret' }));
16
- app.get('/auth/*', () => new Response('auth'));
17
- app.get('/auth-unicode/*', () => new Response('auth'));
18
- it('Should not authorize', async () => {
19
- const req = new Request('http://localhost/auth/a');
20
- const res = await app.request(req);
21
- expect(res).not.toBeNull();
22
- expect(res.status).toBe(401);
23
- expect(await res.text()).toBe('Unauthorized');
24
- });
25
- it('Should authorize', async () => {
26
- const credential = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE';
27
- const req = new Request('http://localhost/auth/a');
28
- req.headers.set('Authorization', `Bearer ${credential}`);
29
- const res = await app.request(req);
30
- expect(res).not.toBeNull();
31
- expect(res.status).toBe(200);
32
- expect(await res.text()).toBe('auth');
33
- });
34
- it('Should authorize Unicode', async () => {
35
- const credential = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE';
36
- const req = new Request('http://localhost/auth-unicode/a');
37
- req.headers.set('Authorization', `Basic ${credential}`);
38
- const res = await app.request(req);
39
- expect(res).not.toBeNull();
40
- expect(res.status).toBe(200);
41
- expect(await res.text()).toBe('auth');
42
- });
43
- it('Should authorize Unicode', async () => {
44
- const invalidToken = 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE';
45
- const req = new Request('http://localhost/auth-unicode/a');
46
- req.headers.set('Authorization', `Basic ${invalidToken}`);
47
- const res = await app.request(req);
48
- expect(res).not.toBeNull();
49
- expect(res.status).toBe(401);
50
- });
51
- });
@@ -1 +0,0 @@
1
- export {};
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- const hono_1 = require("../../hono");
4
- const _1 = require(".");
5
- describe('Logger by Middleware', () => {
6
- const app = new hono_1.Hono();
7
- let log = '';
8
- const logFn = (str) => {
9
- log = str;
10
- };
11
- const shortRandomString = 'hono';
12
- const longRandomString = 'hono'.repeat(1000);
13
- app.use('*', (0, _1.logger)(logFn));
14
- app.get('/short', (c) => c.text(shortRandomString));
15
- app.get('/long', (c) => c.text(longRandomString));
16
- app.get('/empty', (c) => c.text(''));
17
- it('Log status 200 with empty body', async () => {
18
- const res = await app.request('http://localhost/empty');
19
- expect(res).not.toBeNull();
20
- expect(res.status).toBe(200);
21
- expect(log.startsWith(' --> GET /empty \x1b[32m200\x1b[0m')).toBe(true);
22
- expect(log).toMatch(/m?s$/);
23
- });
24
- it('Log status 200 with small body', async () => {
25
- const res = await app.request('http://localhost/short');
26
- expect(res).not.toBeNull();
27
- expect(res.status).toBe(200);
28
- expect(log.startsWith(' --> GET /short \x1b[32m200\x1b[0m')).toBe(true);
29
- expect(log).toMatch(/m?s$/);
30
- });
31
- it('Log status 200 with big body', async () => {
32
- const res = await app.request('http://localhost/long');
33
- expect(res).not.toBeNull();
34
- expect(res.status).toBe(200);
35
- expect(log.startsWith(' --> GET /long \x1b[32m200\x1b[0m')).toBe(true);
36
- expect(log).toMatch(/m?s$/);
37
- });
38
- it('Log status 404', async () => {
39
- const msg = 'Default 404 Not Found';
40
- app.all('*', (c) => {
41
- return c.text(msg, 404);
42
- });
43
- const res = await app.request('http://localhost/notfound');
44
- expect(res).not.toBeNull();
45
- expect(res.status).toBe(404);
46
- expect(log.startsWith(' --> GET /notfound \x1b[33m404\x1b[0m')).toBe(true);
47
- expect(log).toMatch(/m?s$/);
48
- });
49
- });
@@ -1 +0,0 @@
1
- export {};