Skip to content

Commit

Permalink
assemble-app: Extract AppHelper, return app object
Browse files Browse the repository at this point in the history
Makes assemble-app-test.js easier to read in preparation for testing
more API methods.
  • Loading branch information
mbland committed May 3, 2017
1 parent 11a0408 commit dc9bff6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 46 deletions.
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ function assembleApp(redirectDb, app) {
app.put('/', function(req, res) {
})
*/
return app
}
55 changes: 9 additions & 46 deletions tests/assemble-app-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var assembleApp = require('../lib').assembleApp
var RedirectDb = require('../lib/redirect-db')
var http = require('http')
var AppHelper = require('./helpers/app-helper')
var express = require('express')
var chai = require('chai')
var chaiAsPromised = require('chai-as-promised')
Expand All @@ -12,14 +12,11 @@ chai.should()
chai.use(chaiAsPromised)

describe('assembleApp', function() {
var redirectDb, getRedirect, expressApp, server, port, sendRequest
var redirectDb, getRedirect, app

before(function() {
redirectDb = new RedirectDb
expressApp = express()
assembleApp(redirectDb, expressApp)
server = http.createServer(expressApp).listen(0)
port = server.address().port
app = new AppHelper(assembleApp(redirectDb, express()))
})

beforeEach(function() {
Expand All @@ -30,58 +27,24 @@ describe('assembleApp', function() {
getRedirect.restore()
})

sendRequest = function(method, url) {
return new Promise(function(resolve, reject) {
var options = {
protocol: 'http:',
host: 'localhost',
port: port,
path: url,
method: method
},
req

req = http.request(options, function(res) {
var result = ''

res.setEncoding('utf8')
res.on('data', function(chunk) {
result += chunk
})
res.on('end', function() {
if (res.statusCode === 200) {
resolve(result)
} else if (res.statusCode === 302) {
resolve(res.headers['location'] || 'Location missing')
} else {
reject(new Error(res.statusCode + ': ' + result))
}
})
})
req.on('error', function(err) {
reject(new Error('Unexpected HTTP error: ' + err.message))
})
req.end()
})
}

it('returns the index page', function() {
return sendRequest('GET', '/').should.be.fulfilled.then(function(result) {
result.match('Url Pointers').should.exist
})
return app.sendRequest('GET', '/')
.should.be.fulfilled.then(function(result) {
result.match('Url Pointers').should.exist
})
})

it('redirects to the url returned by the RedirectDb', function() {
var redirectLocation = 'https://mike-bland.com/'
getRedirect.withArgs('/foo').returns(Promise.resolve(redirectLocation))
return sendRequest('GET', '/foo').should.become(redirectLocation)
return app.sendRequest('GET', '/foo').should.become(redirectLocation)
})

it('reports an error', function() {
getRedirect.withArgs('/foo').callsFake(function() {
return Promise.reject('forced error')
})
return sendRequest('GET', '/foo').should.be.rejectedWith(Error,
return app.sendRequest('GET', '/foo').should.be.rejectedWith(Error,
'500: Error while processing /foo: forced error')
})
})
47 changes: 47 additions & 0 deletions tests/helpers/app-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
'use strict'

var http = require('http')

module.exports = AppHelper

function AppHelper(app) {
this.server = http.createServer(app).listen(0)
this.port = this.server.address().port
}

AppHelper.prototype.sendRequest = function(method, url) {
var port = this.port

return new Promise(function(resolve, reject) {
var options = {
protocol: 'http:',
host: 'localhost',
port: port,
path: url,
method: method
},
req

req = http.request(options, function(res) {
var result = ''

res.setEncoding('utf8')
res.on('data', function(chunk) {
result += chunk
})
res.on('end', function() {
if (res.statusCode === 200) {
resolve(result)
} else if (res.statusCode === 302) {
resolve(res.headers['location'] || 'Location missing')
} else {
reject(new Error(res.statusCode + ': ' + result))
}
})
})
req.on('error', function(err) {
reject(new Error('Unexpected HTTP error: ' + err.message))
})
req.end()
})
}

0 comments on commit dc9bff6

Please sign in to comment.