Add some server tests

This commit is contained in:
MICHAEL JACKSON 2017-08-18 17:32:57 -07:00
parent f87f0962f4
commit 19d060f753
4 changed files with 64 additions and 31 deletions

View File

@ -73,6 +73,7 @@
"promise": "7.1.1",
"react-dev-utils": "^0.5.2",
"style-loader": "0.13.1",
"supertest": "^3.0.0",
"text-table": "^0.2.0",
"url-loader": "0.5.7",
"webpack": "1.14.0",

View File

@ -1,5 +1,6 @@
const http = require('http')
const throng = require('throng')
const createServer = require('./server/createServer')
const createApp = require('./server/createApp')
const port = parseInt(process.env.PORT, 10) || 5000
@ -8,7 +9,24 @@ throng({
lifetime: Infinity,
grace: 25000,
start: function (id) {
const server = createServer()
const server = http.createServer(createApp())
// Heroku dynos automatically timeout after 30s. Set our
// own timeout here to force sockets to close before that.
// https://devcenter.heroku.com/articles/request-timeout
server.setTimeout(25000, function (socket) {
const message = `Timeout of 25 seconds exceeded`
socket.end([
`HTTP/1.1 503 Service Unavailable`,
`Date: ${(new Date).toGMTString()}`,
`Content-Type: text/plain`,
`Content-Length: ${Buffer.byteLength(message)}`,
`Connection: close`,
``,
message
].join(`\r\n`))
})
server.listen(port, function () {
console.log('Server #%s listening on port %s, Ctrl+C to stop', id, port)

View File

@ -1,6 +1,5 @@
const fs = require('fs')
const path = require('path')
const http = require('http')
const express = require('express')
const cors = require('cors')
const morgan = require('morgan')
@ -52,17 +51,19 @@ function errorHandler(err, req, res, next) {
next(err)
}
function createServer() {
function createApp() {
const app = express()
app.disable('x-powered-by')
app.use(morgan(process.env.NODE_ENV === 'production'
// Modified version of the Heroku router's log format
// https://devcenter.heroku.com/articles/http-routing#heroku-router-log-format
? 'method=:method path=":url" host=:req[host] request_id=:req[x-request-id] cf_ray=:req[cf-ray] fwd=:fwd status=:status bytes=:res[content-length]'
: 'dev'
))
if (process.env.NODE_ENV !== 'test') {
app.use(morgan(process.env.NODE_ENV === 'production'
// Modified version of the Heroku router's log format
// https://devcenter.heroku.com/articles/http-routing#heroku-router-log-format
? 'method=:method path=":url" host=:req[host] request_id=:req[x-request-id] cf_ray=:req[cf-ray] fwd=:fwd status=:status bytes=:res[content-length]'
: 'dev'
))
}
app.use(errorHandler)
app.use(cors())
@ -80,26 +81,7 @@ function createServer() {
serveFile
)
const server = http.createServer(app)
// Heroku dynos automatically timeout after 30s. Set our
// own timeout here to force sockets to close before that.
// https://devcenter.heroku.com/articles/request-timeout
server.setTimeout(25000, function (socket) {
const message = `Timeout of 25 seconds exceeded`
socket.end([
`HTTP/1.1 503 Service Unavailable`,
`Date: ${(new Date).toGMTString()}`,
`Content-Type: text/plain`,
`Content-Length: ${Buffer.byteLength(message)}`,
`Connection: close`,
``,
message
].join(`\r\n`))
})
return server
return app
}
module.exports = createServer
module.exports = createApp

32
server/createApp.test.js Normal file
View File

@ -0,0 +1,32 @@
const request = require('supertest')
const createApp = require('./createApp')
describe('The server app', function () {
let app
beforeEach(function () {
app = createApp()
})
it('rejects invalid package names', function (done) {
request(app).get('/_invalid/index.js').then(function (res) {
expect(res.statusCode).toBe(403)
done()
})
})
it('redirects invalid query params', function (done) {
request(app).get('/react?main=index&invalid').then(function (res) {
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe('/react?main=index')
done()
})
})
it('redirects /_meta to ?meta', function (done) {
request(app).get('/_meta/react?main=index').then(function (res) {
expect(res.statusCode).toBe(302)
expect(res.headers.location).toBe('/react?main=index&meta')
done()
})
})
})