http.js
This file
Path
test/support/http.js
103 lines · 1.9 KiB · JavaScript
Last changed by
param single-segment testTj Holowaychukbefore adoption730a419
History of this fileTake it
middleware testsTj Holowaychukbefore adoption5b296b5
lines 1–6why, one step back12/**3 * Module dependencies.4 */56var EventEmitter = require('events').EventEmittergenerated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
line 7why, one step back7 , methods = require('../../').methodsmiddleware testsTj Holowaychukbefore adoption5b296b5
lines 8–35why, one step back8 , http = require('http');910module.exports = request;1112function request(app) {13 return new Request(app);14}1516function Request(app) {17 var self = this;18 this.data = [];19 this.header = {};20 this.app = app;21 if (!this.server) {22 this.server = http.Server(app);23 this.server.listen(0, function(){24 self.addr = self.server.address();25 self.listening = true;26 });27 }28}2930/**31 * Inherit from `EventEmitter.prototype`.32 */3334Request.prototype.__proto__ = EventEmitter.prototype;35generated helper test methods from express.methodsTj Holowaychukbefore adoption4106bae
lines 36–40why, one step back36methods.forEach(function(method){37 Request.prototype[method] = function(path){38 return this.request(method, path);39 };40});Added res.send() HEAD testsTj Holowaychukbefore adoptionb756474
line 41why, one step back41middleware testsTj Holowaychukbefore adoption5b296b5
lines 42–57why, one step back42Request.prototype.set = function(field, val){43 this.header[field] = val;44 return this;45};4647Request.prototype.write = function(data){48 this.data.push(data);49 return this;50};5152Request.prototype.request = function(method, path){53 this.method = method;54 this.path = path;55 return this;56};57tests for optional capture groupsTj Holowaychukbefore adoptione9c96d8
lines 58–59why, one step back58Request.prototype.expect = function(body, fn){59 this.end(function(res){param single-segment testTj Holowaychukbefore adoption730a419
lines 60–64why, one step back60 if ('number' == typeof body) {61 res.statusCode.should.equal(body);62 } else {63 res.body.should.equal(body);64 }tests for optional capture groupsTj Holowaychukbefore adoptione9c96d8
lines 65–68why, one step back65 fn();66 });67};68middleware testsTj Holowaychukbefore adoption5b296b5
lines 69–103why, one step back69Request.prototype.end = function(fn){70 var self = this;7172 if (this.listening) {73 var req = http.request({74 method: this.method75 , port: this.addr.port76 , host: this.addr.address77 , path: this.path78 , headers: this.header79 });8081 this.data.forEach(function(chunk){82 req.write(chunk);83 });84 85 req.on('response', function(res){86 var buf = '';87 res.setEncoding('utf8');88 res.on('data', function(chunk){ buf += chunk });89 res.on('end', function(){90 res.body = buf;91 fn(res);92 });93 });9495 req.end();96 } else {97 this.server.on('listening', function(){98 self.end(fn);99 });100 }101102 return this;103};