|
1 | 1 | /* global describe, it, expect, beforeAll */
|
2 | 2 |
|
3 | 3 | const http = require('../index')
|
4 |
| -const { router } = http({ port: 3000 }) |
| 4 | +const {router} = http({port: 3000}) |
5 | 5 |
|
6 | 6 | describe('Router', () => {
|
7 | 7 | beforeAll(async () => {
|
8 | 8 | router.use((req, next) => {
|
9 | 9 | req.ctx = {
|
10 |
| - engine: 'bun' |
| 10 | + engine: 'bun', |
11 | 11 | }
|
12 | 12 |
|
13 | 13 | return next()
|
@@ -41,58 +41,72 @@ describe('Router', () => {
|
41 | 41 | })
|
42 | 42 |
|
43 | 43 | it('should return a JSON response with the request parameters for GET requests', async () => {
|
44 |
| - const response = await router.fetch(new Request('http://localhost:3000/get-params/123', { |
45 |
| - method: 'GET' |
46 |
| - })) |
| 44 | + const response = await router.fetch( |
| 45 | + new Request('http://localhost:3000/get-params/123', { |
| 46 | + method: 'GET', |
| 47 | + }), |
| 48 | + ) |
47 | 49 | expect(response.status).toBe(200)
|
48 |
| - expect(await response.json()).toEqual({ id: '123' }) |
| 50 | + expect(await response.json()).toEqual({id: '123'}) |
49 | 51 | })
|
50 | 52 |
|
51 | 53 | it('should return a JSON response with the request parameters for DELETE requests', async () => {
|
52 |
| - const response = await router.fetch(new Request('http://localhost:3000/get-params/123', { |
53 |
| - method: 'DELETE' |
54 |
| - })) |
| 54 | + const response = await router.fetch( |
| 55 | + new Request('http://localhost:3000/get-params/123', { |
| 56 | + method: 'DELETE', |
| 57 | + }), |
| 58 | + ) |
55 | 59 | expect(response.status).toBe(200)
|
56 | 60 | expect(await response.json()).toEqual('OK')
|
57 | 61 | })
|
58 | 62 |
|
59 | 63 | it('should return a JSON response with the request body for POST requests', async () => {
|
60 |
| - const response = await router.fetch(new Request('http://localhost:3000/create', { |
61 |
| - method: 'POST', |
62 |
| - body: JSON.stringify({ foo: 'bar' }) |
63 |
| - })) |
| 64 | + const response = await router.fetch( |
| 65 | + new Request('http://localhost:3000/create', { |
| 66 | + method: 'POST', |
| 67 | + body: JSON.stringify({foo: 'bar'}), |
| 68 | + }), |
| 69 | + ) |
64 | 70 | expect(response.status).toBe(200)
|
65 |
| - expect(await response.json()).toEqual({ foo: 'bar' }) |
| 71 | + expect(await response.json()).toEqual({foo: 'bar'}) |
66 | 72 | })
|
67 | 73 |
|
68 | 74 | it('should return a 404 response for a non-existent route', async () => {
|
69 |
| - const response = await router.fetch(new Request('http://localhost:3000/non-existent', { |
70 |
| - method: 'GET' |
71 |
| - })) |
| 75 | + const response = await router.fetch( |
| 76 | + new Request('http://localhost:3000/non-existent', { |
| 77 | + method: 'GET', |
| 78 | + }), |
| 79 | + ) |
72 | 80 | expect(response.status).toBe(404)
|
73 | 81 | })
|
74 | 82 |
|
75 | 83 | it('should return a 500 response for a route that throws an error', async () => {
|
76 |
| - const response = await router.fetch(new Request('http://localhost:3000/error', { |
77 |
| - method: 'GET' |
78 |
| - })) |
| 84 | + const response = await router.fetch( |
| 85 | + new Request('http://localhost:3000/error', { |
| 86 | + method: 'GET', |
| 87 | + }), |
| 88 | + ) |
79 | 89 | expect(response.status).toBe(500)
|
80 | 90 | expect(await response.text()).toEqual('Unexpected error')
|
81 | 91 | })
|
82 | 92 |
|
83 | 93 | it('should return a 200 response for a route that returns a Response object', async () => {
|
84 |
| - const response = await router.fetch(new Request('http://localhost:3000/', { |
85 |
| - method: 'GET' |
86 |
| - })) |
| 94 | + const response = await router.fetch( |
| 95 | + new Request('http://localhost:3000/', { |
| 96 | + method: 'GET', |
| 97 | + }), |
| 98 | + ) |
87 | 99 | expect(response.status).toBe(200)
|
88 |
| - expect(await response.json()).toEqual({ engine: 'bun' }) |
| 100 | + expect(await response.json()).toEqual({engine: 'bun'}) |
89 | 101 | })
|
90 | 102 |
|
91 | 103 | it('should return a JSON response with the query string parameters', async () => {
|
92 |
| - const response = await router.fetch(new Request('http://localhost:3000/qs?foo=bar', { |
93 |
| - method: 'GET' |
94 |
| - })) |
| 104 | + const response = await router.fetch( |
| 105 | + new Request('http://localhost:3000/qs?foo=bar', { |
| 106 | + method: 'GET', |
| 107 | + }), |
| 108 | + ) |
95 | 109 | expect(response.status).toBe(200)
|
96 |
| - expect(await response.json()).toEqual({ foo: 'bar' }) |
| 110 | + expect(await response.json()).toEqual({foo: 'bar'}) |
97 | 111 | })
|
98 | 112 | })
|
0 commit comments