-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
41 additions
and
3 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,19 @@ | ||
import { ConsoleHandler } from "https://deno.land/[email protected]/log/handlers.ts"; | ||
import { | ||
getLogger, | ||
setup as setupLog, | ||
} from "https://deno.land/[email protected]/log/mod.ts"; | ||
|
||
import { | ||
Application as OakApplication, | ||
} from "https://deno.land/x/[email protected]/mod.ts"; | ||
|
||
import { parse } from "npm:lossless-json"; | ||
|
||
import { | ||
defaultLogFormatter, | ||
TestWebhookReceiverLoggerName, | ||
} from "./logUtils.ts"; | ||
import { runAndCleanup } from "./runHelpers.ts"; | ||
|
||
function numberParser(value: string) { | ||
|
@@ -11,10 +23,11 @@ function numberParser(value: string) { | |
} | ||
|
||
export async function testWebhookReceiver() { | ||
const logger = getLogger(TestWebhookReceiverLoggerName); | ||
const abortController = new AbortController(); | ||
const app = new OakApplication(); | ||
app.use(async (ctx) => { | ||
console.log( | ||
logger.info( | ||
"Received Webhook:", | ||
parse( | ||
await ctx.request.body({ type: "text" }).value, | ||
|
@@ -25,17 +38,36 @@ export async function testWebhookReceiver() { | |
ctx.response.body = ""; | ||
}); | ||
|
||
const port = 8888; | ||
logger.info(`Test webhook receiver listening on port ${port}.`); | ||
const runningPromise = app.listen({ | ||
port: 8888, | ||
port, | ||
signal: abortController.signal, | ||
}); | ||
|
||
async function cleanup() { | ||
logger.info(`Stopping test webhook receiver.`); | ||
abortController.abort(); | ||
await runningPromise; | ||
} | ||
|
||
return await Promise.resolve({ runningPromise, cleanup }); | ||
} | ||
|
||
if (import.meta.main) await runAndCleanup(testWebhookReceiver); | ||
if (import.meta.main) { | ||
setupLog({ | ||
handlers: { | ||
console: new ConsoleHandler("DEBUG", { | ||
formatter: defaultLogFormatter, | ||
}), | ||
}, | ||
|
||
loggers: { | ||
[TestWebhookReceiverLoggerName]: { | ||
level: "DEBUG", | ||
handlers: ["console"], | ||
}, | ||
}, | ||
}); | ||
await runAndCleanup(testWebhookReceiver); | ||
} |