diff --git a/index.js b/index.js index 815451e..507fbf2 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ module.exports = (config = {}) => { - const router = config.router || require('./lib/router/sequential')(config) + // Sequential is default and only router implementation for now + const router = require('./lib/router/sequential')(config) return { router diff --git a/lib/router/sequential.js b/lib/router/sequential.js index 58186b9..83e8447 100644 --- a/lib/router/sequential.js +++ b/lib/router/sequential.js @@ -5,7 +5,7 @@ const next = require('./../next') module.exports = (config = {}) => { if (!config.defaultRoute) { - config.defaultRoute = (req) => { + config.defaultRoute = () => { const res = new Response(null, { status: 404 }) @@ -14,7 +14,7 @@ module.exports = (config = {}) => { } } if (!config.errorHandler) { - config.errorHandler = (err, req) => { + config.errorHandler = (err) => { const res = new Response(err.message, { status: 500 }) @@ -38,7 +38,7 @@ module.exports = (config = {}) => { return this } - router.fetch = (req, step) => { + router.fetch = (req) => { const url = new URL(req.url) req.path = url.pathname || '/' diff --git a/package.json b/package.json index 9426d1c..454858b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "0http-bun", - "version": "0.0.3", - "description": "0http alternative for Bun", + "version": "1.0.0", + "description": "0http for Bun", "main": "index.js", "scripts": { "lint": "bun x standard", diff --git a/test/config.test.js b/test/config.test.js new file mode 100644 index 0000000..048bf72 --- /dev/null +++ b/test/config.test.js @@ -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!') + }) +}) diff --git a/test/smoke.test.js b/test/smoke.test.js new file mode 100644 index 0000000..90403ec --- /dev/null +++ b/test/smoke.test.js @@ -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' }) + }) +})