How to use the @adonisjs/http-server/build/standalone.HttpContext.create function in @adonisjs/http-server

To help you get started, we’ve selected a few @adonisjs/http-server examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adonisjs / adonis-framework / test / exception-handler.spec.ts View on Github external
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
  })
github adonisjs / adonis-framework / test / exception-handler.spec.ts View on Github external
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',
      },
    ])
  })
github adonisjs / adonis-framework / test / exception-handler.spec.ts View on Github external
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')
  })
github adonisjs / adonis-framework / test / exception-handler.spec.ts View on Github external
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)
  })
github adonisjs / adonis-framework / test / cors.spec.ts View on Github external
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()
      })

@adonisjs/http-server

AdonisJS HTTP server with support packed with Routing and Cookies

MIT
Latest version published 8 days ago

Package Health Score

87 / 100
Full package analysis

Popular @adonisjs/http-server functions