-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from BackendStack21/v1.0.0
v1.0.0
- Loading branch information
Showing
5 changed files
with
137 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* global describe, it, expect, beforeAll */ | ||
|
||
const http = require('../index') | ||
const { router } = http({ | ||
port: 3000, | ||
defaultRoute: (req) => { | ||
const res = new Response('Not Found!', { | ||
status: 404 | ||
}) | ||
|
||
return res | ||
}, | ||
errorHandler: (err) => { | ||
const res = new Response('Error: ' + err.message, { | ||
status: 500 | ||
}) | ||
|
||
return res | ||
} | ||
}) | ||
|
||
describe('Router Configuration', () => { | ||
beforeAll(async () => { | ||
router.get('/error', () => { | ||
throw new Error('Unexpected error') | ||
}) | ||
}) | ||
|
||
it('should return a 500 response for a route that throws an error', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/error', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(500) | ||
expect(await response.text()).toEqual('Error: Unexpected error') | ||
}) | ||
|
||
it('should return a 404 response for a route that does not exist', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/does-not-exist', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(404) | ||
expect(await response.text()).toEqual('Not Found!') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* global describe, it, expect, beforeAll */ | ||
|
||
const http = require('../index') | ||
const { router } = http({ port: 3000 }) | ||
|
||
describe('Router', () => { | ||
beforeAll(async () => { | ||
router.use((req, next) => { | ||
req.ctx = { | ||
engine: 'bun' | ||
} | ||
|
||
return next() | ||
}) | ||
|
||
router.get('/get-params/:id', (req) => { | ||
return Response.json(req.params) | ||
}) | ||
|
||
router.delete('/get-params/:id', () => { | ||
return Response.json('OK') | ||
}) | ||
|
||
router.get('/error', () => { | ||
throw new Error('Unexpected error') | ||
}) | ||
|
||
router.post('/create', async (req) => { | ||
const body = await req.text() | ||
|
||
return Response.json(JSON.parse(body)) | ||
}) | ||
|
||
router.get('/', (req) => { | ||
return Response.json(req.ctx) | ||
}) | ||
}) | ||
|
||
it('should return a JSON response with the request parameters for GET requests', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/get-params/123', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual({ id: '123' }) | ||
}) | ||
|
||
it('should return a JSON response with the request parameters for DELETE requests', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/get-params/123', { | ||
method: 'DELETE' | ||
})) | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual('OK') | ||
}) | ||
|
||
it('should return a JSON response with the request body for POST requests', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/create', { | ||
method: 'POST', | ||
body: JSON.stringify({ foo: 'bar' }) | ||
})) | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual({ foo: 'bar' }) | ||
}) | ||
|
||
it('should return a 404 response for a non-existent route', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/non-existent', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(404) | ||
}) | ||
|
||
it('should return a 500 response for a route that throws an error', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/error', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(500) | ||
expect(await response.text()).toEqual('Unexpected error') | ||
}) | ||
|
||
it('should return a 200 response for a route that returns a Response object', async () => { | ||
const response = await router.fetch(new Request('http://localhost:3000/', { | ||
method: 'GET' | ||
})) | ||
expect(response.status).toBe(200) | ||
expect(await response.json()).toEqual({ engine: 'bun' }) | ||
}) | ||
}) |