Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions lib/router/sequential.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand All @@ -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
})
Expand All @@ -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 || '/'
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
44 changes: 44 additions & 0 deletions test/config.test.js
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!')
})
})
86 changes: 86 additions & 0 deletions test/smoke.test.js
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' })
})
})