1.2.0 • Published 6 years ago

koa-offline v1.2.0

Weekly downloads
-
License
ISC
Repository
-
Last release
6 years ago

koa-offline

Send requests to koa handlers without any network traffic.

  • Convenience for using koa as some kinds of backend service, and reseal the external interface to any other protocols.
  • Convenience for unit test

install

npm i koa-offline

usage

const KoaOffline = require('koa-offline')
const Koa = require('koa')
const Router = require('koa-router')

// config your koa as needed
const app = new Koa()
const router = new Router()

router.get('/', async (ctx) => {
	ctx.body = 'hello world'
})

app.use(router.routes())
app.use(router.allowedMethods())

// use koa offline
const ko = new KoaOffline(app)
let res
res = await ko.request('/')
console.log(res.statusCode, res.body) // 200 hello world

res = await ko.request({ url: '/', method: 'POST', json: {} })
console.log(res.statusCode, res.body) // 405 Method Not Allowed

res = await ko.request('/not/exist')
console.log(res.statusCode, res.body) // 404 Not Found

See more examples in unit test.

options

namedesctypedefault
urlrequest urlstringrequired
methodhttp actions: GET,POST,PUT,DELETE,...string'GET'
body/form/jsonset to ctx.request.body and ctx.req.body if providedobjectundefined
queryset to ctx.request.query and ctx.req.query if providedobjectundefined

run test

npm test