/usr/share/doc/node-mocks-http/examples/express-route.js is in node-mocks-http 1.5.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | 'use strict';
var httpMocks = require('../lib/http-mock');
// Suppose 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) {
var id = request.params.id;
console.log('We have a \'%s\' request for %s (ID: %d)', request.method, request.url, id);
var body = {
name: 'Bob Dog',
age: 42,
email: 'bob@dog.com'
};
response.setHeader('Content-Type', 'application/json');
response.statusCode = 200;
response.send(JSON.stringify(body), 'utf8');
response.end();
};
// -----------------------------------------------------------------
// In another file, you can easily test the routeHandler function
// with some code like this using the testing framework of your choice:
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 = JSON.parse(response._getData());
test.equal('Bob Dog', data.name);
test.equal(42, data.age);
test.equal('bob@dog.com', data.email);
test.equal(200, response.statusCode);
test.ok(response._isEndCalled());
test.ok(response._isJSON());
test.ok(response._isUTF8());
test.done();
};
|