Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
test('print youch html in development', async (assert) => {
process.env.NODE_ENV = 'development'
class AppHandler extends HttpExceptionHandler {
protected context () {
return { username: 'virk' }
}
}
class InvalidAuth extends Exception {
}
const logger = new FakeLogger(loggerConfig)
const handler = new AppHandler(logger)
const ctx = HttpContext.create('/', {}, logger, new Profiler({}).create(''), encryption)
ctx.request.request.headers = { accept: 'text/html' }
await handler.handle(new InvalidAuth('bad request'), ctx)
assert.isTrue(/youch/.test(ctx.response.lazyBody!.args[0]))
delete process.env.NODE_ENV
})
test('report error with custom context', (assert) => {
class AppHandler extends HttpExceptionHandler {
protected context () {
return { username: 'virk' }
}
}
const logger = new FakeLogger(loggerConfig)
const handler = new AppHandler(logger)
const ctx = HttpContext.create('/', {}, logger, new Profiler({}).create(''), encryption)
handler.report(new Exception('bad request', 500, 'E_BAD_REQUEST'), ctx)
assert.deepEqual(logger.logs.map(({ level, msg, username }) => {
return { level, msg, username}
}), [
{
level: 50,
username: 'virk',
msg: 'E_BAD_REQUEST: bad request',
},
])
})
class AppHandler extends HttpExceptionHandler {
protected context () {
return { username: 'virk' }
}
}
class InvalidAuth extends Exception {
public async handle () {
return 'foo'
}
}
const logger = new FakeLogger(loggerConfig)
const handler = new AppHandler(logger)
const ctx = HttpContext.create('/', {}, logger, new Profiler({}).create(''), encryption)
ctx.request.request.headers = { accept: 'text/html' }
const response = await handler.handle(new InvalidAuth('bad request'), ctx)
assert.equal(response, 'foo')
})
protected context () {
return { username: 'virk' }
}
}
class InvalidAuth extends Exception {
constructor (message) {
super(message, 404, 'E_INVALID_AUTH')
}
}
const logger = new FakeLogger(loggerConfig)
const handler = new AppHandler(logger)
const ctx = HttpContext.create('/', {}, logger, new Profiler({}).create(''), encryption)
ctx['view'] = {
render (view, data) {
assert.equal(view, '404.edge')
assert.equal(data.error.message, 'E_INVALID_AUTH: bad request')
},
}
ctx.request.request.headers = { accept: 'text/html' }
await handler.handle(new InvalidAuth('bad request'), ctx)
assert.equal(ctx.response.response.statusCode, 404)
})
const server = createServer(async (req, res) => {
const cors = new Cors(fixture.configureOptions())
const logger = new Logger({ name: 'adonis', enabled: false, level: 'trace' })
fixture.configureRequest(req)
const ctx = HttpContext.create('/', {}, logger, new Profiler({}).create(''), encryption, req, res)
await cors.handle(ctx)
if (!ctx.response.hasLazyBody) {
ctx.response.send(null)
}
ctx.response.finish()
})