在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:howardabrams/node-mocks-http开源软件地址:https://github.com/howardabrams/node-mocks-http开源编程语言:JavaScript 100.0%开源软件介绍:Mock 'http' objects for testing Express and Koa
routing functions, but could be used for testing any
Node.js web server applications that have
code that requires mockups of the InstallationThis project is available as a NPM package. $ npm install --save-dev node-mocks-http
After installing the package include the following in your test files: var httpMocks = require('node-mocks-http'); UsageSuppose you have the following Express route: app.get('/user/:id', routeHandler); And you have created a function to handle that route's call: var routeHandler = function( request, response ) { ... }; You can easily test the exports['routeHandler - Simple testing'] = function(test) {
var request = httpMocks.createRequest({
method: 'GET',
url: '/user/42',
params: {
id: 42
}
});
var response = httpMocks.createResponse();
routeHandler(request, response);
var data = response._getJSONData(); // short-hand for JSON.parse( response._getData() );
test.equal("Bob Dog", data.name);
test.equal(42, data.age);
test.equal("[email protected]", data.email);
test.equal(200, response.statusCode );
test.ok( response._isEndCalled());
test.ok( response._isJSON());
test.ok( response._isUTF8());
test.done();
}; API.createRequest()
Where options is an object hash with any of the following values:
The object returned from this function also supports the Express request functions ( .createResponse()httpMocks.createResponse(options) Where options is an object hash with any of the following values:
var httpMocks = require('node-mocks-http');
var res = httpMocks.createResponse({
eventEmitter: require('events').EventEmitter
});
// ...
it('should do something', function(done) {
res.on('end', function() {
assert.equal(...);
done();
});
});
// ...
var httpMocks = require('node-mocks-http');
var req = httpMocks.createRequest();
var res = httpMocks.createResponse({
eventEmitter: require('events').EventEmitter
});
// ...
it('should do something', function(done) {
res.on('end', function() {
expect(response._getData()).to.equal('data sent in request');
done();
});
route(req,res);
req.send('data sent in request');
});
function route(req,res){
var data= [];
req.on("data", chunk => {
data.push(chunk)
});
req.on("end", () => {
data = Buffer.concat(data)
res.write(data);
res.end();
});
}
// ... .createMocks()httpMocks.createMocks(reqOptions, resOptions) Merges Design DecisionsWe wanted some simple mocks without a large framework. We also wanted the mocks to act like the original framework being mocked, but allow for setting of values before calling and inspecting of values after calling. For DevelopersWe are looking for more volunteers to bring value to this project, including the creation of more objects from the HTTP module. This project doesn't address all features that must be mocked, but it is a good start. Feel free to send pull requests, and a member of the team will be timely in merging them. If you wish to contribute please read our Contributing Guidelines. Release NotesMost releases fix bugs with our mocks or add features similar to the
actual See the Release History for details. LicenseLicensed under MIT. |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论