mjs*_*mjs 25
看起来https://github.com/howardabrams/node-mocks-http和https://github.com/vojtajina/node-mocks都可用于创建模拟http.ServerRequest和http.ServerResponse对象.
从标签看,这个问题看起来像是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;
});
Run Code Online (Sandbox Code Playgroud)
对于一般节点使用,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();
Run Code Online (Sandbox Code Playgroud)
输出:
BODY:{"foo":"bar"}
我正在使用nodejutsu模拟:
https://github.com/nodejitsu/mock-request
也许这就是您正在寻找的。
| 归档时间: |
|
| 查看次数: |
35178 次 |
| 最近记录: |