Use express for http server

This commit is contained in:
Brandon Keepers 2017-07-31 12:34:28 -05:00
parent 20743926df
commit 767c852553
No known key found for this signature in database
GPG Key ID: F9533396D5FACBF6
4 changed files with 1867 additions and 243 deletions

View File

@ -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;
};

2054
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -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": {

32
test/server.js Normal file
View File

@ -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);
});
});
});