Skip to content
This repository has been archived by the owner on Dec 18, 2023. It is now read-only.

Commit

Permalink
Add some server tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Aug 19, 2017
1 parent f87f096 commit 19d060f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 31 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
22 changes: 20 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand Down
40 changes: 11 additions & 29 deletions server/createServer.js → server/createApp.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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())
Expand All @@ -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 changes: 32 additions & 0 deletions server/createApp.test.js
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit 19d060f

Please sign in to comment.