node.js:模拟 http 请求和响应

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8021956/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:44:07  来源:igfitidea点击:

node.js: Mock http request and response

node.jsmockingexpress

提问by 7elephant

Is there convenient way to mock the HTTP Request and Response objects for unit testing middlewares?

是否有方便的方法来模拟单元测试中间件的 HTTP 请求和响应对象?

回答by mjs

It looks like both https://github.com/howardabrams/node-mocks-httpand https://github.com/vojtajina/node-mockscan be used to create mock http.ServerRequestand http.ServerResponseobjects.

看起来https://github.com/howardabrams/node-mocks-httphttps://github.com/vojtajina/node-mocks都可以用来创建模拟http.ServerRequesthttp.ServerResponse对象。

回答by Rich Apodaca

From the tag, it looks like this question is about Express. In that case, supertestis very good:

从标签上看,这个问题似乎是关于 Express 的。在这种情况下,supertest非常好:

var request = require('supertest')
  , express = require('express');

var app = express();

app.get('/user', function(req, res){
  res.send(201, { name: 'tobi' });
});

request(app)
  .get('/user')
  .expect('Content-Type', /json/)
  .expect('Content-Length', '20')
  .expect(201)
  .end(function(err, res){
    if (err) throw err;
  });

For general Node use, Flatiron Nocklooks like a good option:

对于一般 Node 使用,Flatiron Nock看起来是一个不错的选择:

var nock = require('nock');
var example = nock('http://example.com')
                .get('/foo')
                .reply(200, { foo: 'bar' });

var http = require('http');
var options = {
  host: 'example.com',
  port: 80,
  path: '/foo',
  method: 'GET'
}
var req = http.request(options, function(res) {
  res.on('data', function(chunk) {
    console.log('BODY: ' + chunk);
  });
});

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

req.end();

Output:

输出:

BODY: {"foo":"bar"}

正文:{"foo":"bar"}

回答by Tereska

i'm using nodejutsu mock:

我正在使用 nodejutsu 模拟:

https://github.com/nodejitsu/mock-request

https://github.com/nodejitsu/mock-request

Maybe this is what you are looking for.

也许这就是你正在寻找的。

回答by ctide

I wrote a library to mock out the responses of requests made via standard HTTP or via the request model:

我编写了一个库来模拟通过标准 HTTP 或通过请求模型发出的请求的响应:

https://github.com/ctide/fakeweb

https://github.com/ctide/fakeweb

回答by timsavery

Check out https://github.com/timsavery/node-hmockor npm install hmock...any feedback welcome! The solution has worked well for me thus far.

查看https://github.com/timsavery/node-hmocknpm install hmock...欢迎任何反馈!到目前为止,该解决方案对我来说效果很好。

回答by Ed Hinchliffe

Mockerylooks great for this.

嘲笑看起来很棒。

Essentially it hiHymans requirecalls, and returns a different object/function stub that you specify.

从本质上讲,它会劫持require调用,并返回您指定的不同对象/函数存根。

回答by Notice

I encourage you to use motty. why do we need an another code?

我鼓励你使用motty。为什么我们需要另一个代码?