-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
assemble-app: Extract AppHelper, return app object
Makes assemble-app-test.js easier to read in preparation for testing more API methods.
- Loading branch information
Showing
3 changed files
with
57 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,4 +28,5 @@ function assembleApp(redirectDb, app) { | |
app.put('/', function(req, res) { | ||
}) | ||
*/ | ||
return app | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
} |