///
import * as http from 'node:http'
import { Readable } from 'node:stream'
import { expect } from 'tstyche'
import { bindInject, type BoundInjectFunction, type Chain, type DispatchFunc, inject, type InjectOptions, isInjection, type Response } from '.'
expect({ url: '/' }).type.toBeAssignableTo()
expect({ autoStart: true }).type.toBeAssignableTo()
expect({ autoStart: false }).type.toBeAssignableTo()
expect({ validate: true }).type.toBeAssignableTo()
expect({ validate: false }).type.toBeAssignableTo()
expect({ method: 'REBIND', url: '/' }).type.toBeAssignableTo()
expect({ method: 'REBIND' }).type.toBeAssignableTo()
const dispatch: http.RequestListener = function (req, res) {
expect(req).type.toBeAssignableTo()
expect(res).type.toBeAssignableTo()
expect(isInjection(req)).type.toBe()
expect(isInjection(res)).type.toBe()
const reply = 'Hello World'
res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length })
res.end(reply)
}
expect(dispatch).type.toBe()
const expectResponse = function (res: Response | undefined) {
if (!res) {
return
}
expect(res).type.toBe()
expect(res.json).type.toBeAssignableTo()
expect(res.stream).type.toBeAssignableTo()
expect(res.raw.res).type.toBeAssignableTo()
expect(res.raw.req).type.toBeAssignableTo()
expect(res.stream()).type.toBe()
expect(res.payload).type.toBe()
expect(res.body).type.toBe()
expect(res.cookies).type.toBeAssignableTo>()
expect(res.isLightMyRequest).type.toBe()
const cookie = res.cookies[0]
if (cookie) {
expect(cookie.name).type.toBe()
expect(cookie.value).type.toBe()
expect(cookie.path).type.toBe()
expect(cookie.domain).type.toBe()
expect(cookie.expires).type.toBe()
expect(cookie.maxAge).type.toBe()
expect(cookie.httpOnly).type.toBe()
expect(cookie.secure).type.toBe()
expect(cookie.sameSite).type.toBe()
expect(cookie.additional).type.toBe()
}
}
inject(dispatch, { method: 'get', url: '/' }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
const url = {
protocol: 'http',
hostname: 'example.com',
port: '8080',
pathname: 'hello',
query: {
test: '1234'
}
}
inject(dispatch, { method: 'get', url }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'get', url: '/', cookies: { name1: 'value1', value2: 'value2' } }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, {
method: 'get',
url: '/',
cookies: {
session: 'abc',
scoped: { value: 'uid', path: '/admin' }
}
}, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'get', url: '/', cookies: [{ name: 'session', value: 'abc', path: '/' }] }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'get', url: '/', query: { name1: 'value1', value2: 'value2' } }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'get', url: '/', query: { name1: ['value1', 'value2'] } }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'get', url: '/', query: 'name1=value1' }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'post', url: '/', payload: { name1: 'value1', value2: 'value2' } }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch, { method: 'post', url: '/', body: { name1: 'value1', value2: 'value2' } }, (err, res) => {
expect(err).type.toBe()
expectResponse(res)
})
inject(dispatch)
.get('/')
.then((value) => {
expect(value).type.toBe()
})
expect(inject(dispatch)).type.toBe()
expect(inject(dispatch).end()).type.toBe>()
expect(inject(dispatch, { method: 'get', url: '/' })).type.toBe()
const boundInject = bindInject(dispatch, { headers: { authorization: 'Bearer token' } });
(async () => {
expect(await inject(dispatch, { method: 'get', url: '/' })).type.toBe()
type ParsedValue = { field: string }
const response = await inject(dispatch)
expect(response).type.toBe()
const parsedValue: ParsedValue = response.json()
expect(parsedValue).type.toBe()
const parsedValueUsingGeneric = response.json()
expect(parsedValueUsingGeneric).type.toBe()
expect(response).type.not.toBeAssignableTo()
expect(await boundInject({ method: 'get', url: '/' })).type.toBe()
})()
const httpDispatch = function (req: http.IncomingMessage, res: http.ServerResponse) {
expect(isInjection(req)).type.toBe()
expect(isInjection(res)).type.toBe()
const reply = 'Hello World'
res.writeHead(200, { 'Content-Type': 'text/plain', 'Content-Length': reply.length })
res.end(reply)
}
inject(httpDispatch, { method: 'get', url: '/' }, (err, res) => {
expect(err).type.toBe()
})
inject(httpDispatch, { method: 'get', url: '/', payloadAsStream: true }, (err, res) => {
expect(err).type.toBe()
})
expect(boundInject).type.toBe()
boundInject({ method: 'get', url: '/' }, (err, res) => {
expect(err).type.toBe()
})
expect(boundInject({ method: 'get', url: '/' })).type.toBe()
boundInject()
.get('/')
.then((value) => {
expect(value).type.toBe()
})
const boundInjectWithDefaults = bindInject(dispatch, {
headers: { 'x-custom': 'value' },
cookies: { session: 'abc123' },
query: { version: '1' }
})
expect(boundInjectWithDefaults).type.toBe()