Skip to content
Myr

middleware tests

Before adoption: this commit came in with the history the project adopted, so no landing records it.

that dont quite work yet because node is being a PITA

authored by Tj Holowaychuk · committed by Tj Holowaychuk · parent 1a5c32f · Browse files · 4 files+166 added−19 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+166 added−19 removed

  • tests:+132 added−19 removed
  • other:+34 added−0 removed

4 files+166 added−19 removed

added:lib/test.js+34 added
added:test/support/http.js+89 added
12/**3 * Module dependencies.4 */56var EventEmitter = require('events').EventEmitter7 , http = require('http');89module.exports = request;1011function request(app) {12 return new Request(app);13}1415function Request(app) {16 var self = this;17 this.data = [];18 this.header = {};19 this.app = app;20 if (!this.server) {21 this.server = http.Server(app);22 this.server.listen(0, function(){23 self.addr = self.server.address();24 self.listening = true;25 });26 }27}2829/**30 * Inherit from `EventEmitter.prototype`.31 */3233Request.prototype.__proto__ = EventEmitter.prototype;3435Request.prototype.get = function(path){36 return this.request('GET', path);37};3839Request.prototype.set = function(field, val){40 this.header[field] = val;41 return this;42};4344Request.prototype.write = function(data){45 this.data.push(data);46 return this;47};4849Request.prototype.request = function(method, path){50 this.method = method;51 this.path = path;52 return this;53};5455Request.prototype.end = function(fn){56 var self = this;5758 if (this.listening) {59 var req = http.request({60 method: this.method61 , port: this.addr.port62 , host: this.addr.address63 , path: this.path64 , headers: this.header65 });6667 this.data.forEach(function(chunk){68 req.write(chunk);69 });70 71 req.on('response', function(res){72 var buf = '';73 res.setEncoding('utf8');74 res.on('data', function(chunk){ buf += chunk });75 res.on('end', function(){76 res.body = buf;77 fn(res);78 });79 });8081 req.end();82 } else {83 this.server.on('listening', function(){84 self.end(fn);85 });86 }8788 return this;89};
added:test/middleware.basic.js+43 added
deleted:test/middleware.js-19 removed