Node.js 如何创建模拟http.ServerRequest和http.ServerResponse?

Node.js 如何创建模拟http.ServerRequest和http.ServerResponse?,node.js,testing,Node.js,Testing,我已经为http.Server编写了一个处理程序(即带有签名函数(请求、响应){…})的函数),我想对它进行测试。我想通过mockhttp.ServerRequest和http.ServerResponse对象来实现这一点。如何创建这些 显而易见的方法似乎不起作用: $ node > var http = require('http'); > new http.ServerRequest(); TypeError: undefined is not a function at

我已经为
http.Server
编写了一个处理程序(即带有签名
函数(请求、响应){…}
)的函数),我想对它进行测试。我想通过mock
http.ServerRequest
http.ServerResponse
对象来实现这一点。如何创建这些

显而易见的方法似乎不起作用:

$ node
> var http = require('http');
> new http.ServerRequest();
TypeError: undefined is not a function
    at repl:1:9
...
我是否需要通过“真正的”HTTP服务器和客户端来测试此功能?

是的,您可以使用。它允许您从服务器发出请求,所以您可以使用代码对其进行测试。如果您想发送简单的GET请求,您可以使用它,这更简单。否则,您必须自己构造请求。文档中的示例:

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  console.log('STATUS: ' + res.statusCode);
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.end();
如果您正在使用,您也可以这样做

var request = require('request');
request.post( url, json_obj_to_pass,
    function (error, response, body) {
        if (!error && response.statusCode == 200)
            console.log(body)
    }
);
是的,你可以用电脑来做。它允许您从服务器发出请求,所以您可以使用代码对其进行测试。如果您想发送简单的GET请求,您可以使用它,这更简单。否则,您必须自己构造请求。文档中的示例:

var options = {
  host: 'www.google.com',
  port: 80,
  path: '/upload',
  method: 'POST'
};

var req = http.request(options, function(res) {
  res.setEncoding('utf8');
  console.log('STATUS: ' + res.statusCode);
  res.on('data', function (chunk) {
    console.log('BODY: ' + chunk);
  });
});

req.on('error', function(e) {
  console.log('problem with request: ' + e.message);
});

req.write('data\n');
req.end();
如果您正在使用,您也可以这样做

var request = require('request');
request.post( url, json_obj_to_pass,
    function (error, response, body) {
        if (!error && response.statusCode == 200)
            console.log(body)
    }
);

至少有两个项目允许模拟
http.ServerRequest
http.ServerResponse
:和

出于某种原因,通过真正的HTTP请求进行测试似乎更为常见;似乎是在这里使用的工具


另请参见。

至少有两个项目允许模拟
http.ServerRequest
http.ServerResponse
:和

出于某种原因,通过真正的HTTP请求进行测试似乎更为常见;似乎是在这里使用的工具


另请参见。

我为
http.ServerResponse
创建了一个简单而基本的模拟,要点是我使用的
http.ServerRequest

首先,加载依赖项:

var require = require('rewire'),
    events = require('events'),
    util = require('util');
这是http.ServerResponse
mock。它基本上使用与http.ServerResponse相同的方法创建一个对象,然后使用
util
模块继承
events
模块

/**
 * Mock https.serverResponse
 * @type {Object}
 */
var mockResponse;
Eventer = function(){
    events.EventEmitter.call(this);

    this.data = '';

    this.onData = function(){
        this.emit('data', this.data);
    }

    this.setEncoding = function(){

    }

    this.onEnd = function(){
        this.emit('end', this.data);
    }

    this.run = function(){
        this.onData();
        this.onEnd();
    }
};
util.inherits(Eventer, events.EventEmitter);
然后,我使用这个mock和rewre覆盖http模块的get()、request()或任何其他库

/**
 * Mocks literal object for rewire mocking.
 * @see https://github.com/jhnns/rewire
 * @type {Object}
 */
var mocks = {
    "https" : {
        "get" : function(url, fn){
            fn(mockResponse.data);
            mockResponse.run();
        }
    }
};

//instead of: var myModule = require('myModule');
//do:
var myModule = rewire('myModule');
myModule.__set__(mocks);

现在模块中的
http
库被模拟了

我为
http.ServerResponse
创建了一个简单而基本的模拟,要点是我使用的
http.ServerRequest

首先,加载依赖项:

var require = require('rewire'),
    events = require('events'),
    util = require('util');
这是http.ServerResponse
mock。它基本上使用与http.ServerResponse相同的方法创建一个对象,然后使用
util
模块继承
events
模块

/**
 * Mock https.serverResponse
 * @type {Object}
 */
var mockResponse;
Eventer = function(){
    events.EventEmitter.call(this);

    this.data = '';

    this.onData = function(){
        this.emit('data', this.data);
    }

    this.setEncoding = function(){

    }

    this.onEnd = function(){
        this.emit('end', this.data);
    }

    this.run = function(){
        this.onData();
        this.onEnd();
    }
};
util.inherits(Eventer, events.EventEmitter);
然后,我使用这个mock和rewre覆盖http模块的get()、request()或任何其他库

/**
 * Mocks literal object for rewire mocking.
 * @see https://github.com/jhnns/rewire
 * @type {Object}
 */
var mocks = {
    "https" : {
        "get" : function(url, fn){
            fn(mockResponse.data);
            mockResponse.run();
        }
    }
};

//instead of: var myModule = require('myModule');
//do:
var myModule = rewire('myModule');
myModule.__set__(mocks);

现在模块中的
http
库被模拟了

,如果你想测试它,那么用真实的方式做它有什么害处。@user568109好吧,代码本身没有网络依赖性,因此测试需要网络似乎是不必要的(而且速度较慢)。如果你想测试它,那么,以真实的方式进行操作有什么害处呢。@user568109代码本身没有网络依赖性,因此看起来不必要(而且速度较慢)测试需要网络。
节点模拟http
似乎相当有限,因为它不允许您以流的形式写入请求体
节点模拟http
似乎相当有限,因为它不允许您以流的形式写入请求体