Created
March 27, 2022 05:08
-
-
Save jfhbrook/b02a65f54257eab20456198001fbebeb to your computer and use it in GitHub Desktop.
aghast test handler using shot^H^H^H^Hpickleback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { IncomingMessage, ServerResponse } from 'http'; | |
import { inject, RequestOptions } from 'pickleback'; | |
import { Opts, defaults } from '../opts'; | |
export interface TestHarness<R> { | |
(reqOpts: RequestOptions, opts?: Opts): Promise<R> | |
} | |
export interface TestHandler<R> { | |
(req: IncomingMessage, res: ServerResponse, opts: Opts): R | |
} | |
export function testHarness<R>( | |
handler: TestHandler<R> | |
): TestHarness<R> { | |
return async (reqOpts: RequestOptions, opts: Opts = defaults): Promise<R> => { | |
function httpHandler(req: IncomingMessage, res: ServerResponse): void { | |
const result: R = handler(req, res, opts); | |
res.writeHead(200); | |
res.end(JSON.stringify(result)); | |
} | |
return <R>JSON.parse(await inject(httpHandler, reqOpts)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment