forked from mirrors/probot
test: convert webhook proxy test to TS (#851)
This commit is contained in:
parent
d76c70ba23
commit
fb4c06950c
File diff suppressed because it is too large
Load Diff
|
@ -77,6 +77,7 @@
|
|||
"cache-manager": "^2.4.0",
|
||||
"commander": "^2.19.0",
|
||||
"dotenv": "~6.2.0",
|
||||
"eventsource": "^1.0.7",
|
||||
"express": "^4.16.2",
|
||||
"express-async-errors": "^3.0.0",
|
||||
"hbs": "^4.0.3",
|
||||
|
@ -100,6 +101,7 @@
|
|||
"@types/bunyan-format": "^0.2.0",
|
||||
"@types/cache-manager": "^1.2.6",
|
||||
"@types/dotenv": "^4.0.2",
|
||||
"@types/eventsource": "^1.1.0",
|
||||
"@types/express": "^4.0.39",
|
||||
"@types/ioredis": "^4.0.6",
|
||||
"@types/jest": "^24.0.9",
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
declare module "smee-client" {
|
||||
import EventSource = require('eventsource')
|
||||
|
||||
type Severity = 'info' | 'error'
|
||||
|
||||
interface Options {
|
||||
source?: string
|
||||
target: string
|
||||
logger?: Pick<Console, Severity>
|
||||
}
|
||||
|
||||
class Client {
|
||||
constructor ({ source, target, logger}: Options)
|
||||
public onmessage (msg: any): void
|
||||
public onopen(): void
|
||||
public onerror(err: any): void
|
||||
public start(): EventSource
|
||||
public static createChannel(): Promise<string>
|
||||
}
|
||||
|
||||
export = Client
|
||||
}
|
|
@ -1,10 +1,9 @@
|
|||
import Logger from 'bunyan'
|
||||
import EventSource from 'eventsource'
|
||||
import SmeeClient from 'smee-client'
|
||||
|
||||
export const createWebhookProxy = (opts: WebhookProxyOptions) => {
|
||||
export const createWebhookProxy = (opts: WebhookProxyOptions): EventSource | undefined => {
|
||||
try {
|
||||
// tslint:disable-next-line
|
||||
const SmeeClient = require('smee-client')
|
||||
|
||||
const smee = new SmeeClient({
|
||||
logger: opts.logger,
|
||||
source: opts.url,
|
||||
|
@ -13,6 +12,7 @@ export const createWebhookProxy = (opts: WebhookProxyOptions) => {
|
|||
return smee.start()
|
||||
} catch (err) {
|
||||
opts.logger.warn('Run `npm install --save-dev smee-client` to proxy webhooks to localhost.')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,64 +0,0 @@
|
|||
const express = require('express')
|
||||
const sse = require('connect-sse')()
|
||||
const nock = require('nock')
|
||||
const {createWebhookProxy} = require('../src/webhook-proxy')
|
||||
const {logger} = require('../src/logger')
|
||||
|
||||
const targetPort = 999999
|
||||
|
||||
describe('webhook-proxy', () => {
|
||||
let app, server, proxy, url, emit
|
||||
|
||||
afterEach(() => {
|
||||
server && server.close()
|
||||
proxy && proxy.close()
|
||||
})
|
||||
|
||||
describe('with a valid proxy server', () => {
|
||||
beforeEach((done) => {
|
||||
app = express()
|
||||
|
||||
app.get('/events', sse, function (req, res) {
|
||||
res.json({}, 'ready')
|
||||
emit = res.json
|
||||
})
|
||||
|
||||
server = app.listen(0, () => {
|
||||
url = `http://127.0.0.1:${server.address().port}/events`
|
||||
proxy = createWebhookProxy({url, port: targetPort, path: '/test', logger})
|
||||
|
||||
// Wait for proxy to be ready
|
||||
proxy.addEventListener('ready', () => done())
|
||||
})
|
||||
})
|
||||
|
||||
test('forwards events to server', (done) => {
|
||||
nock(`http://localhost:${targetPort}`).post('/test').reply(200, () => {
|
||||
done()
|
||||
})
|
||||
|
||||
const body = {action: 'foo'}
|
||||
|
||||
emit({
|
||||
'x-github-event': 'test',
|
||||
body
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('logs an error when the proxy server is not found', (done) => {
|
||||
const url = 'http://bad.proxy/events'
|
||||
nock('http://bad.proxy').get('/events').reply(404)
|
||||
|
||||
const log = logger.child()
|
||||
log.error = jest.fn()
|
||||
|
||||
proxy = createWebhookProxy({url, logger: log})
|
||||
|
||||
proxy.on('error', err => {
|
||||
expect(err.status).toBe(404)
|
||||
expect(log.error).toHaveBeenCalledWith(err)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
|
@ -0,0 +1,75 @@
|
|||
import express, { Response } from 'express'
|
||||
// tslint:disable-next-line:no-var-requires
|
||||
const sse: (req: express.Request, res: express.Response, next: express.NextFunction) => void = require('connect-sse')()
|
||||
import EventSource from 'eventsource'
|
||||
import http from 'http'
|
||||
import net from 'net'
|
||||
import nock from 'nock'
|
||||
import { logger } from '../src/logger'
|
||||
import { createWebhookProxy } from '../src/webhook-proxy'
|
||||
|
||||
const targetPort = 999999
|
||||
|
||||
interface SSEResponse extends Response {
|
||||
json (body: any, status?: string): Response
|
||||
}
|
||||
|
||||
describe('webhook-proxy', () => {
|
||||
// tslint:disable-next-line:one-variable-per-declaration
|
||||
let emit: SSEResponse['json'],
|
||||
proxy: EventSource,
|
||||
server: http.Server
|
||||
|
||||
afterEach(() => {
|
||||
server && server.close()
|
||||
proxy && proxy.close()
|
||||
})
|
||||
|
||||
describe('with a valid proxy server', () => {
|
||||
beforeEach((done) => {
|
||||
const app = express()
|
||||
|
||||
app.get('/events', sse, (req, res: SSEResponse) => {
|
||||
res.json({}, 'ready')
|
||||
emit = res.json
|
||||
})
|
||||
|
||||
server = app.listen(0, () => {
|
||||
const url = `http://127.0.0.1:${(server.address() as net.AddressInfo).port}/events`
|
||||
proxy = createWebhookProxy({ url, port: targetPort, path: '/test', logger })!
|
||||
|
||||
// Wait for proxy to be ready
|
||||
proxy.addEventListener('ready', () => done())
|
||||
})
|
||||
})
|
||||
|
||||
test('forwards events to server', (done) => {
|
||||
nock(`http://localhost:${targetPort}`).post('/test').reply(200, () => {
|
||||
done()
|
||||
})
|
||||
|
||||
const body = { action: 'foo' }
|
||||
|
||||
emit({
|
||||
body,
|
||||
'x-github-event': 'test'
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('logs an error when the proxy server is not found', (done) => {
|
||||
const url = 'http://bad.proxy/events'
|
||||
nock('http://bad.proxy').get('/events').reply(404)
|
||||
|
||||
const log = logger.child({})
|
||||
log.error = jest.fn()
|
||||
|
||||
proxy = createWebhookProxy({ url, logger: log })!
|
||||
|
||||
proxy.addEventListener('error', (err: any) => {
|
||||
expect(err.status).toBe(404)
|
||||
expect(log.error).toHaveBeenCalledWith(err)
|
||||
done()
|
||||
})
|
||||
})
|
||||
})
|
Loading…
Reference in New Issue