Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mock-express-response

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mock-express-response - npm Package Compare versions

Comparing version 0.1.0 to 0.1.1

379

index.js
'use strict';
//dependencies
var path = require('path');
var contentDisposition = require('content-disposition');
var onFinished = require('on-finished');
var escapeHtml = require('escape-html');

@@ -18,5 +21,7 @@ var merge = require('utils-merge');

var normalizeTypes = Utils.normalizeTypes;
var isAbsolute = Utils.isAbsolute;
var vary = require('vary');
var sign = require('cookie-signature').sign;
var cookie = require('cookie');
var extname = path.extname;

@@ -35,3 +40,15 @@

this.app = {
'jsonp callback name': 'callback'
'jsonp callback name': 'callback',
render: function(view, data, fn) {
//default implementation is
//to return uncompiled view
//
//this must me ovveriden by view engine
//of choice
if ('function' === typeof options.render) {
options.render(view, data, fn);
} else {
fn(null, view);
}
}
};

@@ -349,3 +366,110 @@

//TODO fix this
// pipe the send file stream
function sendfile(res, file, options, callback) {
var done = false;
var streaming;
// request aborted
function onaborted() {
if (done) {
return;
}
done = true;
var err = new Error('Request aborted');
err.code = 'ECONNABORTED';
callback(err);
}
// directory
function ondirectory() {
if (done) {
return;
}
done = true;
var err = new Error('EISDIR, read');
err.code = 'EISDIR';
callback(err);
}
// errors
function onerror(err) {
if (done) {
return;
}
done = true;
callback(err);
}
// ended
function onend() {
if (done) {
return;
}
done = true;
callback();
}
// file
function onfile() {
streaming = false;
}
// finished
function onfinish(err) {
if (err && err.code === 'ECONNRESET') {
return onaborted();
}
if (err) {
return onerror(err);
}
if (done) {
return;
}
setImmediate(function() {
if (streaming !== false && !done) {
onaborted();
return;
}
if (done) {
return;
}
done = true;
callback();
});
}
// streaming
function onstream() {
streaming = true;
}
file.on('directory', ondirectory);
file.on('end', onend);
file.on('error', onerror);
file.on('file', onfile);
file.on('stream', onstream);
onFinished(res, onfinish);
if (options.headers) {
// set headers on successful transfer
file.on('headers', function headers(res) {
var obj = options.headers;
var keys = Object.keys(obj);
for (var i = 0; i < keys.length; i++) {
var k = keys[i];
res.setHeader(k, obj[k]);
}
});
}
// pipe
file.pipe(res);
}
/**

@@ -391,43 +515,44 @@ * Transfer the file at the given `path`.

*/
// MockExpressResponse.prototype.sendFile = function sendFile(path, options, fn) {
// var req = this.req;
// var res = this;
// var next = req.next;
MockExpressResponse.prototype.sendFile = function sendFile(path, options, fn) {
var req = this.req;
var res = this;
var next = req.next;
// if (!path) {
// throw new TypeError('path argument is required to res.sendFile');
// }
if (!path) {
throw new TypeError('path argument is required to res.sendFile');
}
// // support function as second arg
// if (typeof options === 'function') {
// fn = options;
// options = {};
// }
// support function as second arg
if (typeof options === 'function') {
fn = options;
options = {};
}
// options = options || {};
options = options || {};
// if (!options.root && !isAbsolute(path)) {
// throw new TypeError('path must be absolute or specify root to res.sendFile');
// }
if (!options.root && !isAbsolute(path)) {
throw new TypeError('path must be absolute or specify root to res.sendFile');
}
// // create file stream
// var pathname = encodeURI(path);
// var file = send(req, pathname, options);
// create file stream
var pathname = encodeURI(path);
var file = send(req, pathname, options);
// // transfer
// sendfile(res, file, options, function(err) {
// if (fn) {
// return fn(err);
// }
// if (err && err.code === 'EISDIR') {
// return next();
// }
// transfer
sendfile(res, file, options, function(err) {
if (fn) {
return fn(err);
}
if (err && err.code === 'EISDIR') {
return next();
}
// // next() all but write errors
// if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {
// next(err);
// }
// });
// };
// next() all but write errors
if (err && err.code !== 'ECONNABORTED' && err.syscall !== 'write') {
next(err);
}
});
};
// /**

@@ -650,19 +775,19 @@ // * Transfer the file at the given `path`.

// /**
// * Set _Content-Disposition_ header to _attachment_ with optional `filename`.
// *
// * @param {String} filename
// * @return {ServerResponse}
// * @api public
// */
// res.attachment = function attachment(filename) {
// if (filename) {
// this.type(extname(filename));
// }
/**
* Set _Content-Disposition_ header to _attachment_ with optional `filename`.
*
* @param {String} filename
* @return {ServerResponse}
* @api public
*/
MockExpressResponse.prototype.attachment = function attachment(filename) {
if (filename) {
this.type(extname(filename));
}
// this.set('Content-Disposition', contentDisposition(filename));
this.set('Content-Disposition', contentDisposition(filename));
// return this;
// };
return this;
};

@@ -945,132 +1070,42 @@

// /**
// * Render `view` with the given `options` and optional callback `fn`.
// * When a callback function is given a response will _not_ be made
// * automatically, otherwise a response of _200_ and _text/html_ is given.
// *
// * Options:
// *
// * - `cache` boolean hinting to the engine it should cache
// * - `filename` filename of the view being rendered
// *
// * @api public
// */
/**
* Render `view` with the given `options` and optional callback `fn`.
* When a callback function is given a response will _not_ be made
* automatically, otherwise a response of _200_ and _text/html_ is given.
*
* Options:
*
* - `cache` boolean hinting to the engine it should cache
* - `filename` filename of the view being rendered
*
* @api public
*/
MockExpressResponse.prototype.render = function(view, options, fn) {
options = options || {};
var self = this;
var req = this.req;
var app = this.app;
// res.render = function(view, options, fn){
// options = options || {};
// var self = this;
// var req = this.req;
// var app = req.app;
// support callback function as second arg
if ('function' === typeof options) {
fn = options, options = {};
}
// // support callback function as second arg
// if ('function' == typeof options) {
// fn = options, options = {};
// }
// merge res.locals
options._locals = self.locals;
// // merge res.locals
// options._locals = self.locals;
// default callback to respond
fn = fn || function(err, str) {
if (err) {
return req.next(err);
}
// // default callback to respond
// fn = fn || function(err, str){
// if (err) return req.next(err);
// self.send(str);
// };
self.send(str);
};
// // render
// app.render(view, options, fn);
// };
// render
app.render(view, options, fn);
};
// // pipe the send file stream
// function sendfile(res, file, options, callback) {
// var done = false;
// var streaming;
// // request aborted
// function onaborted() {
// if (done) return;
// done = true;
// var err = new Error('Request aborted');
// err.code = 'ECONNABORTED';
// callback(err);
// }
// // directory
// function ondirectory() {
// if (done) return;
// done = true;
// var err = new Error('EISDIR, read');
// err.code = 'EISDIR';
// callback(err);
// }
// // errors
// function onerror(err) {
// if (done) return;
// done = true;
// callback(err);
// }
// // ended
// function onend() {
// if (done) return;
// done = true;
// callback();
// }
// // file
// function onfile() {
// streaming = false;
// }
// // finished
// function onfinish(err) {
// if (err && err.code === 'ECONNRESET') return onaborted();
// if (err) return onerror(err);
// if (done) return;
// setImmediate(function () {
// if (streaming !== false && !done) {
// onaborted();
// return;
// }
// if (done) return;
// done = true;
// callback();
// });
// }
// // streaming
// function onstream() {
// streaming = true;
// }
// file.on('directory', ondirectory);
// file.on('end', onend);
// file.on('error', onerror);
// file.on('file', onfile);
// file.on('stream', onstream);
// onFinished(res, onfinish);
// if (options.headers) {
// // set headers on successful transfer
// file.on('headers', function headers(res) {
// var obj = options.headers;
// var keys = Object.keys(obj);
// for (var i = 0; i < keys.length; i++) {
// var k = keys[i];
// res.setHeader(k, obj[k]);
// }
// });
// }
// // pipe
// file.pipe(res);
// }
/**

@@ -1077,0 +1112,0 @@ * @description export MockExpressResponse

{
"name": "mock-express-response",
"version": "0.1.0",
"version": "0.1.1",
"description": "Nodejs library to mock express http response",
"keywords": [
"express",
"connect",
"mock",

@@ -12,3 +13,8 @@ "stab",

"spec",
"response"
"specification",
"bdd",
"tdd",
"response",
"http",
"htttps"
],

@@ -39,2 +45,3 @@ "main": "index.js",

"chai": "^2.2.0",
"ejs": "^2.3.1",
"grunt": "^0.4.5",

@@ -47,3 +54,3 @@ "grunt-contrib-jshint": "^0.11.2",

"mocha": "^2.2.4",
"mock-express-request": "^0.1.0"
"mock-express-request": "^0.1.1"
},

@@ -67,4 +74,4 @@ "dependencies": {

"peerDependencies": {
"mock-express-request": "^0.1.0"
"mock-express-request": "^0.1.1"
}
}

@@ -7,2 +7,4 @@ # mock-express-response

See [mock-express-request](https://github.com/lykmapipo/mock-express-request) to mock express http request.
*Note: The mocked response instance have the same properties and methods as an instance of express http response*

@@ -17,2 +19,3 @@

```js
var ejs = require('ejs');
var MockExpressRequest = require('mock-express-request');

@@ -26,2 +29,3 @@ var MockExpressResponse = require('mock-express-response');

var response = new MockExpressResponse({
render: ejs.renderFile, //use ejs as template engine
request: new MockExpressRequest({

@@ -35,6 +39,15 @@ //request options

//express response methods
response.json({user:{active:true}})
response.send('<p>Hi</p>')
//etc
//and properties
//send json response
response.json({user:{active:true}});
//render a template
response.render('user.ejs',{user:{active:true}});
//send a response
response.send('<p>Hi</p>');
...
//to obtain json response

@@ -50,2 +63,6 @@ var result = response._getJSON();

## TODO
- [ ] File send mock
## Testing

@@ -52,0 +69,0 @@ * Clone this repository

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc