forked from mirrors/probot
Use express for http server
This commit is contained in:
parent
20743926df
commit
767c852553
|
@ -1,18 +1,10 @@
|
|||
const http = require('http');
|
||||
const express = require('express');
|
||||
|
||||
module.exports = function (webhook) {
|
||||
return http.createServer((req, res) => {
|
||||
webhook(req, res, err => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
res.statusCode = 500;
|
||||
res.end('Something has gone terribly wrong.');
|
||||
} else if (req.url.split('?').shift() === '/ping') {
|
||||
res.end('PONG');
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
res.end('no such location');
|
||||
}
|
||||
});
|
||||
});
|
||||
const app = express();
|
||||
|
||||
app.use(webhook);
|
||||
app.get('/ping', (req, res) => res.end('PONG'));
|
||||
|
||||
return app;
|
||||
};
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -23,6 +23,7 @@
|
|||
"cache-manager": "^2.4.0",
|
||||
"commander": "^2.10.0",
|
||||
"dotenv": "~4.0.0",
|
||||
"express": "^4.15.3",
|
||||
"github": "^9.2.0",
|
||||
"github-app": "^3.0.0",
|
||||
"github-webhook-handler": "^0.6.0",
|
||||
|
@ -41,6 +42,7 @@
|
|||
"jsdoc-strip-async-await": "^0.1.0",
|
||||
"minami": "^1.1.1",
|
||||
"mocha": "^3.0.2",
|
||||
"supertest": "^3.0.0",
|
||||
"xo": "^0.19.0"
|
||||
},
|
||||
"xo": {
|
||||
|
|
|
@ -0,0 +1,32 @@
|
|||
const expect = require('expect');
|
||||
const request = require('supertest');
|
||||
const createServer = require('../lib/server');
|
||||
|
||||
describe('server', function () {
|
||||
let server;
|
||||
let webhook;
|
||||
|
||||
beforeEach(() => {
|
||||
webhook = expect.createSpy().andCall((req, res, next) => next());
|
||||
server = createServer(webhook);
|
||||
});
|
||||
|
||||
describe('GET /ping', () => {
|
||||
it('returns a 200 repsonse', () => {
|
||||
return request(server).get('/ping').expect(200, 'PONG');
|
||||
});
|
||||
});
|
||||
|
||||
describe('webhook handler', () => {
|
||||
it('should 500 on a webhook error', () => {
|
||||
webhook.andCall((req, res, callback) => callback('webhook error'));
|
||||
return request(server).post('/').expect(500);
|
||||
});
|
||||
});
|
||||
|
||||
describe('with an unknown url', () => {
|
||||
it('responds with 404', () => {
|
||||
return request(server).get('/lolnotfound').expect(404);
|
||||
});
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue