strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const inject = require('../index')5 6test('basic async await', async t => {7 const dispatch = function (_req, res) {8 res.writeHead(200, { 'Content-Type': 'text/plain' })9 res.end('hello')10 }11 12 try {13 const res = await inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' })14 t.assert.strictEqual(res.payload, 'hello')15 } catch (err) {16 t.assert.fail(err)17 }18})19 20test('basic async await (errored)', async t => {21 const dispatch = function (_req, res) {22 res.connection.destroy(new Error('kaboom'))23 }24 25 await t.assert.rejects(() => inject(dispatch, { method: 'GET', url: 'http://example.com:8080/hello' }), Error)26})27 28test('chainable api with async await', async t => {29 const dispatch = function (_req, res) {30 res.writeHead(200, { 'Content-Type': 'text/plain' })31 res.end('hello')32 }33 34 try {35 const chain = inject(dispatch).get('http://example.com:8080/hello')36 const res = await chain.end()37 t.assert.strictEqual(res.payload, 'hello')38 } catch (err) {39 t.assert.fail(err)40 }41})42 43test('chainable api with async await without end()', async t => {44 const dispatch = function (_req, res) {45 res.writeHead(200, { 'Content-Type': 'text/plain' })46 res.end('hello')47 }48 49 try {50 const res = await inject(dispatch).get('http://example.com:8080/hello')51 t.assert.strictEqual(res.payload, 'hello')52 } catch (err) {53 t.assert.fail(err)54 }55})56 