-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add slack mock * update slack mock debugger * tsconfig * publish v0.2.6
- Loading branch information
Showing
10 changed files
with
289 additions
and
517 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ yarn.lock | |
.scannerwork | ||
src | ||
samples | ||
fff | ||
config.env.test |
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
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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use strict"; | ||
|
||
import { logger } from "./logger"; | ||
|
||
const allResponses = new Map(); | ||
|
||
allResponses.set("incoming-webhooks", new Map()); | ||
|
||
export function set( | ||
type: string, | ||
opts: { url: string; statusCode: any; body: any; headers: any } | ||
) { | ||
const typeResponses = allResponses.get(type); | ||
if (!opts.url) { | ||
opts.url = "any"; | ||
} | ||
|
||
let urlResponses = typeResponses.get(opts.url); | ||
|
||
if (!urlResponses) { | ||
urlResponses = []; | ||
typeResponses.set(opts.url, urlResponses); | ||
} | ||
|
||
logger.debug(`added response for ${type}`, opts); | ||
|
||
urlResponses.push({ | ||
statusCode: opts.statusCode || 200, | ||
body: opts.body || (type === "web" ? { ok: true } : "OK"), | ||
headers: opts.headers || {} | ||
}); | ||
} | ||
|
||
export function get(type: string, url: string) { | ||
const defaultResponse = { statusCode: 200, body: "OK", headers: {} }; | ||
let response = defaultResponse; | ||
|
||
let urlResponses = allResponses.get(type).get(url); | ||
if (!urlResponses) { | ||
urlResponses = allResponses.get(type).get("any"); | ||
} | ||
|
||
if (urlResponses && urlResponses.length) { | ||
response = urlResponses.shift(); | ||
logger.debug(`responding to ${type} with override`, response); | ||
} | ||
|
||
return [response.statusCode, response.body, response.headers]; | ||
} | ||
|
||
export function reset(type: string) { | ||
logger.debug(`clearing responses for ${type}`); | ||
allResponses.get(type).clear(); | ||
} | ||
|
||
export function resetAll() { | ||
for (const key of allResponses.keys()) { | ||
logger.debug(`clearing responses for ${key}`); | ||
allResponses.get(key).clear(); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Logger, LoggerInstance, LoggerOptions, transports } from "winston"; | ||
|
||
const defaultLevel = process.env.LOG_LEVEL; | ||
|
||
const options: LoggerOptions = { | ||
handleExceptions: true, | ||
humanReadableUnhandledException: true, | ||
level: defaultLevel, | ||
transports: [ | ||
new transports.Console({ | ||
colorize: true, | ||
showLevel: true, | ||
timestamp: true | ||
}) | ||
] | ||
}; | ||
|
||
const logger: LoggerInstance = new Logger(options); | ||
|
||
export { logger }; |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
"use strict"; | ||
|
||
export const incomingWebhooks = module.exports as IncomingWebhooks<[]>; | ||
import * as nock from "nock"; | ||
import * as customResponses from "./custom-responses"; | ||
import { logger } from "./logger"; | ||
import parseParams from "./utils"; | ||
const baseUrl = "https://hooks.slack.com"; | ||
|
||
type IncomingWebhookUrl = string; | ||
type IncomingWebhookHttpHeaders = nock.HttpHeaders; | ||
|
||
// Slack accepts both GET and POST requests, will intercept API and OAuth calls | ||
|
||
nock(baseUrl) | ||
.persist() | ||
.post(/.*/, () => true) | ||
.reply(reply); | ||
|
||
incomingWebhooks.calls = []; | ||
|
||
incomingWebhooks.reset = () => { | ||
logger.debug(`resetting incoming-webhooks`); | ||
|
||
customResponses.reset("incoming-webhooks"); | ||
incomingWebhooks.calls.splice(0, incomingWebhooks.calls.length); | ||
}; | ||
|
||
incomingWebhooks.addResponse = opts => { | ||
logger.debug(`adding incoming-webhook response` + opts); | ||
customResponses.set("incoming-webhooks", opts); | ||
}; | ||
|
||
incomingWebhooks.shutdown = () => { | ||
logger.debug(`shutting down incoming-webhooks`); | ||
nock(baseUrl).done(); | ||
}; | ||
|
||
function reply(path: string, requestBody: string) { | ||
const url = `${baseUrl}${path}`; | ||
|
||
logger.debug(`intercepted incoming-webhooks request`); | ||
|
||
incomingWebhooks.calls.push({ | ||
url, | ||
params: parseParams(path, requestBody) as [], | ||
headers: {} | ||
}); | ||
|
||
return customResponses.get("incoming-webhooks", url) as Array<{}>; | ||
} | ||
|
||
interface IncomingWebhooks<T> { | ||
addResponse: (opts: IncomingWebhookOptions<T>) => void; | ||
reset: () => void; | ||
start: () => void; | ||
shutdown: () => void; | ||
calls: Array<IncomingWebhookCall<T>>; | ||
} | ||
|
||
interface IncomingWebhookOptions<T> { | ||
url: IncomingWebhookUrl; | ||
statusCode: number; | ||
body: T; | ||
headers: IncomingWebhookHttpHeaders; | ||
} | ||
|
||
interface IncomingWebhookCall<T> { | ||
url: IncomingWebhookUrl; | ||
params: T; | ||
headers: IncomingWebhookHttpHeaders; | ||
} |
Oops, something went wrong.