diff --git a/.gitignore b/.gitignore index d8c9bc3..dc945d8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .project .settings +coverage node_modules test/results npm-debug.log diff --git a/.travis.yml b/.travis.yml index 258b336..2e28e29 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,19 @@ node_js: - "0.12" notifications: - email: + email: - "howard.abrams@gmail.com" - "johnny.estilles@agentia.asia" + +addons: + code_climate: + repo_token: 75dc20817d25bb52614e495f87d69b228edac0016fb096ccab2a75b624c68d4e + +before_script: + - npm install -g gulp + - npm install -g codeclimate-test-reporter + - gulp lint + +after_script: + - gulp coverage + - codeclimate < ./coverage/lcov.info diff --git a/README.md b/README.md index 3e64263..ed51ff5 100644 --- a/README.md +++ b/README.md @@ -103,6 +103,7 @@ by Express. [Most Recent Releast Notes](https://github.com/howardabrams/node-mocks-http/releases) +* [v1.3.0](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.3.0) - April 5, 2015 * [v1.2.7](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.7) - March 24, 2015 * [v1.2.6](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.6) - March 19, 2015 * [v1.2.5](https://github.com/howardabrams/node-mocks-http/releases/tag/v1.2.5) - March 5, 2015 diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..d4c5b08 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,44 @@ +'use strict'; + +var gulp = require('gulp'); +var mocha = require('gulp-mocha'); +var istanbul = require('gulp-istanbul'); +var eslint = require('gulp-eslint'); + +var files = { + src: ['./lib/**/*.js'], + test: ['./test/**/*.spec.js'] +}; + +gulp.task('lint', function () { + return gulp.src(files.src.concat(files.test)) + .pipe(eslint()) + .pipe(eslint.format()) + .pipe(eslint.failOnError()); +}); + +gulp.task('test', function () { + return gulp.src(files.test, {read: false}) + .pipe(mocha({reporter: 'dot'})); +}); + +gulp.task('spec', function () { + return gulp.src(files.test, {read: false}) + .pipe(mocha({reporter: 'spec'})); +}); + +gulp.task('coverage', function (done) { + gulp.src(files.src) + .pipe(istanbul()) + .pipe(istanbul.hookRequire()) + .on('finish', function () { + gulp.src(files.test) + .pipe(mocha({reporter: 'dot'})) + .pipe(istanbul.writeReports({ + dir: './coverage', + reporters: ['lcov', 'json', 'html'], + reportOpts: { dir: './coverage' } + })) + .on('end', done); + }); +}); diff --git a/lib/mockResponse.js b/lib/mockResponse.js index 555c5c9..d288cc4 100644 --- a/lib/mockResponse.js +++ b/lib/mockResponse.js @@ -433,9 +433,6 @@ function createResponse(options) { switch (arguments.length) { case 2: - break; - - case 3: _renderData = b; break; @@ -449,9 +446,10 @@ function createResponse(options) { }; - mockResponse.writable = function() { - return writableStream.writable.apply(this, arguments); - }; + // WritableStream.writable is not a function + // mockResponse.writable = function() { + // return writableStream.writable.apply(this, arguments); + // }; // mockResponse.end = function(){ // return writableStream.end.apply(this, arguments); diff --git a/lib/mockWritableStream.js b/lib/mockWritableStream.js index 567532b..7d82644 100644 --- a/lib/mockWritableStream.js +++ b/lib/mockWritableStream.js @@ -6,7 +6,14 @@ function WritableStream() {} -WritableStream.prototype.writable = function () {}; +Object.defineProperty(WritableStream, 'writable', { + configurable: true, + enumerable: true, + get: function() { + return true; + } +}); + // WritableStream.prototype.write = function(string, [encoding], [fd]){} // WritableStream.prototype.write = function(buffer){} WritableStream.prototype.end = function () {}; diff --git a/package.json b/package.json index fa39483..60ec471 100644 --- a/package.json +++ b/package.json @@ -2,21 +2,21 @@ "author": "Howard Abrams (http://www.github.com/howardabrams)", "name": "node-mocks-http", "description": "Mock 'http' objects for testing Express routing functions", - "version": "1.3.0", + "version": "1.4.0", "homepage": "https://github.com/howardabrams/node-mocks-http", "bugs": { - "url" : "https://github.com/howardabrams/node-mocks-http/issues" + "url": "https://github.com/howardabrams/node-mocks-http/issues" }, "contributors": [ { - "name" : "Howard Abrams", - "email" : "howard.abrams@gmail.com", - "url" : "https://github.com/howardabrams" + "name": "Howard Abrams", + "email": "howard.abrams@gmail.com", + "url": "https://github.com/howardabrams" }, { - "name" : "Johnny Estilles", - "email" : "johnny.estilles@agentia.asia", - "url" : "https://github.com/JohnnyEstilles" + "name": "Johnny Estilles", + "email": "johnny.estilles@agentia.asia", + "url": "https://github.com/JohnnyEstilles" } ], "license": "MIT", @@ -43,12 +43,19 @@ "mime": "^1.3.4" }, "devDependencies": { + "chai": "^2.2.0", "eslint": "^0.18.0", - "node-restify-errors": "git://github.com/m9dfukc/node-restify-errors.git", - "nodeunit": "^0.9.1" + "eslint-plugin-mocha": "^0.2.2", + "gulp": "^3.8.11", + "gulp-eslint": "^0.8.0", + "gulp-istanbul": "^0.8.1", + "gulp-mocha": "^2.0.1", + "istanbul": "^0.3.13", + "mocha": "^2.2.4", + "sinon": "^1.14.1", + "sinon-chai": "^2.7.0" }, "scripts": { - "lint": "DEBUG=eslint:cli-engine node_modules/eslint/bin/eslint.js ./lib ./examples ./test", - "test": "npm run lint && ./run-tests" + "test": "gulp spec" } } diff --git a/run-tests b/run-tests deleted file mode 100755 index 3f51eed..0000000 --- a/run-tests +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/sh -echo -echo "Executing tests..." - -# OPTS="--reporter junit --output test/results" - -FILES="`find test -name 'test-*.js'` `find examples -name '*.js'`" - -# echo nodeunit $OPTS $FILES -PATH=./node_modules/nodeunit/bin:$PATH -exec nodeunit $OPTS $FILES diff --git a/test/.eslintrc b/test/.eslintrc new file mode 100644 index 0000000..478c89f --- /dev/null +++ b/test/.eslintrc @@ -0,0 +1,88 @@ +{ + "env": { + "node": true + }, + + "plugins": ["mocha"], + + "globals": { + "describe": 1, + "it": 1, + "before": 1, + "beforeEach": 1, + "after": 1, + "afterEach": 1 + }, + + "rules": { + "mocha/no-exclusive-tests": 2, + "comma-dangle": 2, + "no-alert": 2, + "no-array-constructor": 2, + "no-caller": 2, + "no-catch-shadow": 2, + "no-control-regex": 2, + "no-debugger": 2, + "no-div-regex": 2, + "no-dupe-keys": 2, + "no-else-return": 2, + "no-empty": 2, + "no-empty-class": 2, + "no-eq-null": 2, + "no-eval": 2, + "no-ex-assign": 2, + "no-func-assign": 0, + "no-floating-decimal": 2, + "no-implied-eval": 2, + "no-with": 2, + "no-fallthrough": 2, + "no-unreachable": 2, + "no-undef": 2, + "no-undef-init": 2, + "no-unused-expressions": 0, + "no-octal": 2, + "no-octal-escape": 2, + "no-obj-calls": 2, + "no-multi-str": 2, + "no-new-wrappers": 2, + "no-new": 2, + "no-new-func": 2, + "no-native-reassign": 2, + "no-delete-var": 2, + "no-return-assign": 2, + "no-new-object": 2, + "no-label-var": 2, + "no-self-compare": 2, + "no-sync": 2, + "no-loop-func": 2, + "no-empty-label": 2, + "no-unused-vars": 1, + "no-script-url": 2, + "no-proto": 2, + "no-iterator": 2, + "no-mixed-requires": [0, false], + "no-wrap-func": 2, + "no-shadow": 2, + "no-use-before-define": 2, + "no-redeclare": 2, + "no-regex-spaces": 2, + "no-mixed-spaces-and-tabs": 2, + "no-underscore-dangle": 0, + + "brace-style": 2, + "camelcase": 2, + "consistent-this": [2, "self"], + "curly": 2, + "dot-notation": 2, + "eqeqeq": 2, + "new-cap": 2, + "new-parens": 2, + "quotes": [2, "single"], + "semi": 2, + "strict": [2, "global"], + "use-isnan": 2, + "valid-typeof": 2, + "wrap-iife": 2, + "indent": [2, 2, {"indentSwitchCase": true}] + } +} diff --git a/test/lib/http-mock.spec.js b/test/lib/http-mock.spec.js new file mode 100644 index 0000000..9939a80 --- /dev/null +++ b/test/lib/http-mock.spec.js @@ -0,0 +1,21 @@ +'use strict'; + +var chai = require('chai'); +var expect = chai.expect; + +var httpMock; + +describe('http-mock', function() { + before(function() { + httpMock = require('../../lib/http-mock'); + }); + + it('should export .createRequest()', function() { + expect(httpMock.createRequest).to.be.a('function'); + }); + + it('should export .createResponse()', function() { + expect(httpMock.createResponse).to.be.a('function'); + }); + +}); diff --git a/test/lib/mockEventEmitter.spec.js b/test/lib/mockEventEmitter.spec.js new file mode 100644 index 0000000..c113474 --- /dev/null +++ b/test/lib/mockEventEmitter.spec.js @@ -0,0 +1,61 @@ +'use strict'; + +var chai = require('chai'); +var expect = chai.expect; + +var MockEventEmitter = require('../../lib/mockEventEmitter'); +var mockEventEmitter; + +describe('mockEventEmitter', function() { + + before(function() { + mockEventEmitter = new MockEventEmitter(); + }); + + it('should be a function', function() { + expect(MockEventEmitter).to.be.a('function'); + }); + + it('should be an object factory', function() { + expect(mockEventEmitter).to.be.a('object'); + expect(mockEventEmitter).to.be.an.instanceof(MockEventEmitter); + }); + + it('should expose "MockEventEmitter" prototype', function() { + expect(mockEventEmitter).to.have.property('addListener'); + expect(mockEventEmitter.addListener).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('on'); + expect(mockEventEmitter.on).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('once'); + expect(mockEventEmitter.once).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('removeListener'); + expect(mockEventEmitter.removeListener).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('removeAllListeners'); + expect(mockEventEmitter.removeAllListeners).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('setMaxListeners'); + expect(mockEventEmitter.setMaxListeners).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('listeners'); + expect(mockEventEmitter.listeners).to.be.a('function'); + + expect(mockEventEmitter).to.have.property('emit'); + expect(mockEventEmitter.emit).to.be.a('function'); + }); + + it('should return undefined when methods called', function() { + expect(mockEventEmitter.addListener()).to.be.undefined; + expect(mockEventEmitter.on()).to.be.undefined; + expect(mockEventEmitter.once()).to.be.undefined; + expect(mockEventEmitter.removeListener()).to.be.undefined; + expect(mockEventEmitter.removeAllListeners()).to.be.undefined; + expect(mockEventEmitter.setMaxListeners()).to.be.undefined; + expect(mockEventEmitter.listeners()).to.be.undefined; + expect(mockEventEmitter.emit()).to.be.undefined; + }); + +}); diff --git a/test/lib/mockRequest.spec.js b/test/lib/mockRequest.spec.js new file mode 100644 index 0000000..8958da6 --- /dev/null +++ b/test/lib/mockRequest.spec.js @@ -0,0 +1,513 @@ +'use strict'; + +var chai = require('chai'); +var expect = chai.expect; +var url = require('url'); +var querystring = require('querystring'); + +var mockRequest = require('../../lib/mockRequest'); + +describe('mockRequest', function() { + + it('should expose .createRequest()', function() { + expect(mockRequest.createRequest).to.be.a('function'); + }); + + describe('.createRequest()', function() { + var request; + + describe('without options', function() { + + before(function() { + request = mockRequest.createRequest(); + }); + + it('should return an object', function() { + expect(request).to.be.an('object'); + }); + + it('should expose Express Request object methods', function() { + expect(request).to.have.property('get'); + expect(request.get).to.be.a('function'); + + expect(request).to.have.property('header'); + expect(request.header).to.be.a('function'); + + expect(request).to.have.property('param'); + expect(request.param).to.be.a('function'); + }); + + it('shoud initialize with default options', function() { + expect(request.method).to.equal('GET'); + expect(request.url).to.equal(''); + expect(request.originalUrl).to.equal(request.url); + expect(request.path).to.equal(''); + expect(request.params).to.deep.equal({}); + expect(request.session).to.not.exist; + expect(request.cookies).to.deep.equal({}); + expect(request.signedCookies).to.not.exist; + expect(request.headers).to.deep.equal({}); + expect(request.body).to.deep.equal({}); + expect(request.query).to.deep.equal({}); + expect(request.files).to.deep.equal({}); + }); + + }); + + describe('with options', function() { + + afterEach(function() { + request = null; + }); + + it('should set .method to options.method', function() { + var options = { + method: 'POST' + }; + + request = mockRequest.createRequest(options); + expect(request.method).to.equal(options.method); + }); + + it('should set .url to options.url', function() { + var options = { + url: '/this/is/a/url' + }; + + request = mockRequest.createRequest(options); + expect(request.url).to.equal(options.url); + expect(request.originalUrl).to.equal(options.url); + }); + + it('should set .originalUrl to options.originalUrl', function() { + var options = { + originalUrl: '/this/is/a/url' + }; + + request = mockRequest.createRequest(options); + expect(request.originalUrl).to.equal(options.originalUrl); + }); + + it('should set .path to pathname of options.url', function() { + var options = { + url: '/this/is/a/url' + }; + + request = mockRequest.createRequest(options); + expect(request.path).to.equal(url.parse(options.url).pathname); + }); + + it('should set .params to options.params', function() { + var options = { + params: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.params).to.deep.equal(options.params); + }); + + it('should set .session to options.session', function() { + var options = { + session: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.session).to.deep.equal(options.session); + }); + + it('should set .cookies to options.cookies', function() { + var options = { + cookies: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.cookies).to.deep.equal(options.cookies); + }); + + it('should set .signedCookies to options.signedCookies', function() { + var options = { + signedCookies: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.signedCookies).to.deep.equal(options.signedCookies); + }); + + it('should set .headers to options.headers', function() { + var options = { + headers: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.headers).to.deep.equal(options.headers); + }); + + it('should set .body to options.body', function() { + var options = { + body: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.body).to.deep.equal(options.body); + }); + + it('should set .query to options.query', function() { + var options = { + query: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.query).to.deep.equal(options.query); + }); + + it('should set .files to options.files', function() { + var options = { + files: { + key1: 'value1', + key2: 'value2' + } + }; + + request = mockRequest.createRequest(options); + expect(request.files).to.deep.equal(options.files); + }); + + it('should set .query to url query params when options.query not set', function() { + var options = { + url: '/path/to/url?key1=value1&key2=value2' + }; + var parsedOptions = querystring.parse(options.url.split('?')[1]); + + request = mockRequest.createRequest(options); + expect(request.query).to.deep.equal(parsedOptions); + }); + + }); + + }); + + describe('.get()/.header()', function() { + var request; + + afterEach(function() { + request = null; + }); + + it('should return header, when set', function() { + var options = { + headers: { + key: 'value' + } + }; + request = mockRequest.createRequest(options); + expect(request.get('key')).to.equal('value'); + expect(request.header('key')).to.equal('value'); + }); + + it('should return referer, when request as referrer', function() { + var options = { + headers: { + referer: 'value' + } + }; + + request = mockRequest.createRequest(options); + expect(request.get('referrer')).to.equal('value'); + expect(request.header('referrer')).to.equal('value'); + }); + + it('should return referrer, when request as referer', function() { + var options = { + headers: { + referrer: 'value' + } + }; + + request = mockRequest.createRequest(options); + expect(request.get('referer')).to.equal('value'); + expect(request.header('referer')).to.equal('value'); + }); + + it('should not return header, when not set', function() { + request = mockRequest.createRequest(); + expect(request.get('key')).to.not.exist; + expect(request.header('key')).to.not.exist; + }); + + }); + + describe('.param()', function() { + var request; + + afterEach(function() { + request = null; + }); + + it('should return param, when found in params', function() { + var options = { + params: { + key: 'value' + } + }; + + request = mockRequest.createRequest(options); + expect(request.param('key')).to.equal('value'); + }); + + it('should return param, when found in body', function() { + var options = { + body: { + key: 'value' + } + }; + + request = mockRequest.createRequest(options); + expect(request.param('key')).to.equal('value'); + }); + + it('should return param, when found in query', function() { + var options = { + query: { + key: 'value' + } + }; + + request = mockRequest.createRequest(options); + expect(request.param('key')).to.equal('value'); + }); + + it('should not return param, when not found in params/body/query', function() { + request = mockRequest.createRequest(); + expect(request.get('key')).to.not.exist; + expect(request.header('key')).to.not.exist; + }); + + }); + + describe('helper functions', function() { + var request; + + beforeEach(function() { + request = mockRequest.createRequest(); + }); + + afterEach(function() { + request = null; + }); + + describe('._setParameter()', function() { + + it('should set param, when called with key and value', function() { + request._setParameter('key', 'value'); + expect(request.param('key')).to.equal('value'); + }); + + it('should unset param, when called with key and no value', function() { + request._setParameter('key', 'value'); + request._setParameter('key'); + expect(request.param('key')).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._setParameter).to.throw; + }); + + }); + + describe('._setSessionVariable()', function() { + + it('should set session variable, when called with key and value', function() { + request._setSessionVariable('key', 'value'); + expect(request.session.key).to.equal('value'); + }); + + it('should unset session variable, when called with key and no value', function() { + request._setSessionVariable('key', 'value'); + request._setSessionVariable('key'); + expect(request.param('key')).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._setSessionVariable).to.throw; + }); + + }); + + describe('._setCookiesVariable()', function() { + + it('should set cookie, when called with key and value', function() { + request._setCookiesVariable('key', 'value'); + expect(request.cookies.key).to.equal('value'); + }); + + it('should unset cookie, when called with key and no value', function() { + request._setCookiesVariable('key', 'value'); + request._setCookiesVariable('key'); + expect(request.cookies.key).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._setCookiesVariable).to.throw; + }); + + }); + + describe('._setSignedCookiesVariable()', function() { + + it('should set signed cookie, when called with key and value', function() { + request._setSignedCookiesVariable('key', 'value'); + expect(request.signedCookies.key).to.equal('value'); + }); + + it('should unset signed cookie, when called with key and no value', function() { + request._setSignedCookiesVariable('key', 'value'); + request._setSignedCookiesVariable('key'); + expect(request.signedCookies.key).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._setSignedCookiesVariable).to.throw; + }); + + }); + + describe('._setHeadersVariable()', function() { + + it('should set header, when called with key and value', function() { + request._setHeadersVariable('key', 'value'); + expect(request.get('key')).to.equal('value'); + expect(request.header('key')).to.equal('value'); + }); + + it('should throw an error, when called with missing arguments', function () { + expect(request._setHeadersVariable).to.throw; + expect(request._setHeadersVariable.bind(null, 'key')).to.throw; + }); + + }); + + describe('._setFilesVariable()', function() { + + it('should set file, when called with key and value', function() { + request._setFilesVariable('key', 'value'); + expect(request.files.key).to.equal('value'); + }); + + it('should unset file, when called with key and no value', function() { + request._setFilesVariable('key', 'value'); + request._setFilesVariable('key'); + expect(request.files.key).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._setFilesVariable).to.throw; + }); + + }); + + describe('._setMethod()', function() { + + it('should set method, when called with value', function() { + var value = 'HEAD'; + request._setMethod(value); + expect(request.method).to.equal(value); + }); + + it('should unset method, when called with no arguments', function() { + request._setMethod(); + expect(request.method).to.not.exist; + }); + + }); + + describe('._setURL()', function() { + + it('should set url, when called with value', function() { + var value = '/path/to/url'; + request._setURL(value); + expect(request.url).to.equal(value); + }); + + it('should unset url, when called with no arguments', function() { + request._setURL(); + expect(request.url).to.not.exist; + }); + + }); + + describe('._setOriginalUrl()', function() { + + it('should set originalUrl, when called with value', function() { + var value = '/path/to/url'; + request._setOriginalUrl(value); + expect(request.originalUrl).to.equal(value); + }); + + it('should unset originalUrl, when called with no arguments', function() { + request._setOriginalUrl(); + expect(request.originalUrl).to.not.exist; + }); + + }); + + describe('._setBody()', function() { + + it('should set body, when called with value', function() { + var value = { + key1: 'value1', + key2: 'value2' + }; + request._setBody(value); + expect(request.body).to.deep.equal(value); + }); + + it('should unset body, when called with no arguments', function() { + request._setBody(); + expect(request.body).to.not.exist; + }); + + }); + + describe('._addBody()', function() { + + it('should add body variable, when called with key and value', function() { + request._addBody('key', 'value'); + expect(request.body.key).to.equal('value'); + }); + + it('should unset body variable, when called with key and no value', function() { + request._addBody('key', 'value'); + request._addBody('key'); + expect(request.body.key).to.not.exist; + }); + + it('should throw an error, when called with no arguments', function () { + expect(request._addBody).to.throw; + }); + + }); + + }); + +}); diff --git a/test/lib/mockResponse.spec.js b/test/lib/mockResponse.spec.js new file mode 100644 index 0000000..d34a6d4 --- /dev/null +++ b/test/lib/mockResponse.spec.js @@ -0,0 +1,841 @@ +'use strict'; + +var chai = require('chai'); +var expect = chai.expect; +var sinon = require('sinon'); +var sinonChai = require('sinon-chai'); +chai.use(sinonChai); + +var mockResponse = require('../../lib/mockResponse'); + +describe('mockResponse', function() { + + it('should expose .createResponse()', function() { + expect(mockResponse.createResponse).to.be.a('function'); + }); + + describe('.createResponse()', function() { + var response; + + before(function() { + response = mockResponse.createResponse(); + }); + + it('should return an object', function() { + expect(response).to.be.an('object'); + }); + + it('should expose Express Response methods', function() { + expect(response).to.have.property('cookie'); + expect(response.cookie).to.be.a('function'); + + expect(response).to.have.property('clearCookie'); + expect(response.clearCookie).to.be.a('function'); + + expect(response).to.have.property('status'); + expect(response.status).to.be.a('function'); + + expect(response).to.have.property('send'); + expect(response.send).to.be.a('function'); + + expect(response).to.have.property('sendStatus'); + expect(response.sendStatus).to.be.a('function'); + + expect(response).to.have.property('json'); + expect(response.json).to.be.a('function'); + + expect(response).to.have.property('contentType'); + expect(response.contentType).to.be.a('function'); + + expect(response).to.have.property('type'); + expect(response.type).to.be.a('function'); + + expect(response).to.have.property('set'); + expect(response.set).to.be.a('function'); + + expect(response).to.have.property('header'); + expect(response.header).to.be.a('function'); + + expect(response).to.have.property('get'); + expect(response.get).to.be.a('function'); + + // TODO: check origin of setEnconding() method + expect(response).to.have.property('setEncoding'); + expect(response.setEncoding).to.be.a('function'); + + expect(response).to.have.property('redirect'); + expect(response.redirect).to.be.a('function'); + + expect(response).to.have.property('render'); + expect(response.render).to.be.a('function'); + }); + + it('should expose Node OutgoingMessage methods', function() { + expect(response).to.have.property('getHeader'); + expect(response.getHeader).to.be.a('function'); + + expect(response).to.have.property('setHeader'); + expect(response.setHeader).to.be.a('function'); + + expect(response).to.have.property('removeHeader'); + expect(response.removeHeader).to.be.a('function'); + + expect(response).to.have.property('write'); + expect(response.write).to.be.a('function'); + + expect(response).to.have.property('end'); + expect(response.end).to.be.a('function'); + }); + + it('should expose Node ServerResponse methods', function() { + expect(response).to.have.property('writeHead'); + expect(response.writeHead).to.be.a('function'); + }); + + it('should expose Node WritableStream methods', function() { + + expect(response).to.have.property('destroy'); + expect(response.destroy).to.be.a('function'); + + expect(response).to.have.property('destroySoon'); + expect(response.destroySoon).to.be.a('function'); + }); + + it('should expose Node EventEmitter methods', function() { + expect(response).to.have.property('addListener'); + expect(response.addListener).to.be.a('function'); + + expect(response).to.have.property('on'); + expect(response.on).to.be.a('function'); + + expect(response).to.have.property('once'); + expect(response.once).to.be.a('function'); + + expect(response).to.have.property('removeListener'); + expect(response.removeListener).to.be.a('function'); + + expect(response).to.have.property('removeAllListeners'); + expect(response.removeAllListeners).to.be.a('function'); + + expect(response).to.have.property('setMaxListeners'); + expect(response.setMaxListeners).to.be.a('function'); + + expect(response).to.have.property('listeners'); + expect(response.listeners).to.be.a('function'); + + expect(response).to.have.property('emit'); + expect(response.emit).to.be.a('function'); + }); + + it('should expose heler methods', function() { + expect(response).to.have.property('_isEndCalled'); + expect(response._isEndCalled).to.be.a('function'); + + expect(response).to.have.property('_getHeaders'); + expect(response._getHeaders).to.be.a('function'); + + expect(response).to.have.property('_getData'); + expect(response._getData).to.be.a('function'); + + expect(response).to.have.property('_getStatusCode'); + expect(response._getStatusCode).to.be.a('function'); + + expect(response).to.have.property('_isJSON'); + expect(response._isJSON).to.be.a('function'); + + expect(response).to.have.property('_isUTF8'); + expect(response._isUTF8).to.be.a('function'); + + expect(response).to.have.property('_isDataLengthValid'); + expect(response._isDataLengthValid).to.be.a('function'); + + expect(response).to.have.property('_getRedirectUrl'); + expect(response._getRedirectUrl).to.be.a('function'); + + expect(response).to.have.property('_getRenderView'); + expect(response._getRenderView).to.be.a('function'); + + expect(response).to.have.property('_getRenderData'); + expect(response._getRenderData).to.be.a('function'); + }); + + it('shoud initialize with default options', function() { + expect(response.statusCode).to.equal(200); + expect(response.cookies).to.deep.equal({}); + }); + + }); + + describe('Express Response methods', function() { + + describe('.cookie()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should set cookie, when called with, name and value', function() { + var cookie = { + value: 'value', + options: undefined + }; + response.cookie('name', cookie.value); + expect(response.cookies.name).to.deep.equal(cookie); + }); + + it('should set cookie, when called with, name, value and options', function() { + var cookie = { + value: 'value', + options: { + domain: 'foo.bar.com', + path: '/cookie/path' + } + }; + response.cookie('name', cookie.value, cookie.options); + expect(response.cookies.name).to.deep.equal(cookie); + }); + + it('should throw and error, when called without arguments', function() { + expect(response.cookie).to.throw; + }); + + }); + + describe('.clearCookie()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should remove cookie, when called with existing cookie', function() { + response.cookie('name', 'value'); + response.clearCookie('name'); + expect(response.cookies.name).not.to.exist; + }); + + it('should return silently, when called with non-existing cookie', function() { + expect(response.clearCookie.bind(null, 'invalid')).not.to.throw; + }); + + }); + + describe('.status()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should set cookie, when called with, name and value', function() { + response.status(404); + expect(response.statusCode).to.equal(404); + }); + + it('should statusCode to undefined, when called without arguments', function() { + response.status(); + expect(response.statusCode).to.not.exist; + }); + + }); + + describe('.sendStatus()', function() { + var response; + + before(function() { + response = mockResponse.createResponse(); + sinon.spy(response, 'send'); + }); + + after(function() { + response.send.restore(); + response = null; + }); + + it('should set .statusCode, set .type to "text/plain", and call .send()', function() { + response.sendStatus(404); + expect(response.statusCode).to.equal(404); + expect(response.get('Content-Type')).to.equal('text/plain'); + expect(response.send).to.have.been.calledOnce; + }); + }); + + describe('.contentType()/.type()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + it('should set "Content-Type"', function() { + response.type('html'); + expect(response.get('Content-Type')).to.equal('text/html'); + response.contentType('txt'); + expect(response.get('Content-Type')).to.equal('text/plain'); + }); + + it('should trow an error, when called without arguments', function() { + expect(response.type).to.throw; + expect(response.contentType).to.throw; + }); + + }); + + describe('.set()/.header()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + sinon.spy(response, 'setHeader'); + }); + + afterEach(function() { + response.setHeader.restore(); + response = null; + }); + + it('should set header, when called with name and value strings', function() { + response.set('name1', 'value1'); + expect(response.setHeader).to.have.been.calledWith('name1', 'value1'); + expect(response.get('name1')).to.equal('value1'); + + response.header('name2', 'value2'); + expect(response.setHeader).to.have.been.calledWith('name2', 'value2'); + expect(response.get('name2')).to.equal('value2'); + }); + + it('should conver value to string, when called with called with non-string value', function() { + var num = 1; + var obj = { key: 'value' }; + var bool = false; + + response.set('num', num); + expect(response.setHeader).to.have.been.calledWith('num', num.toString()); + expect(response.get('num')).to.equal(num.toString()); + + response.set('obj', obj); + expect(response.setHeader).to.have.been.calledWith('obj', obj.toString()); + expect(response.get('obj')).to.equal(obj.toString()); + + response.set('bool', bool); + expect(response.setHeader).to.have.been.calledWith('bool', bool.toString()); + expect(response.get('bool')).to.equal(bool.toString()); + }); + + it('should set headers, when called with a hash of key/values', function() { + var headers = { + name1: 'value1', + name2: 'value2' + }; + + response.set(headers); + expect(response.setHeader).to.have.been.calledTwice; + expect(response.setHeader).to.have.been.calledWith('name1', 'value1'); + expect(response.setHeader).to.have.been.calledWith('name2', 'value2'); + + expect(response.get('name1')).to.equal('value1'); + expect(response.get('name2')).to.equal('value2'); + }); + + it('should throw an error when called without arguments', function() { + expect(response.set).to.throw; + expect(response.header).to.throw; + }); + + }); + + describe('.get()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should get header, when called existing header name', function() { + response.set('name1', 'value1'); + expect(response.get('name1')).to.equal('value1'); + + response.header('name2', 'value2'); + expect(response.get('name2')).to.equal('value2'); + }); + + it('should throw and error, when called without arguments', function() { + expect(response.get).to.throw; + expect(response.getHeader).to.throw; + }); + + }); + + describe('.redirect()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should mimic Express Response.redirect()'); + + it('should redirect with status 302, when not specified', function() { + var url = '/path/to/redirect'; + + response.redirect(url); + expect(response.statusCode).to.equal(302); + expect(response._getRedirectUrl()).to.equal(url); + }); + + it('should redirect with status specified status', function() { + var statusCode = 301; + var url = '/path/to/redirect'; + + response.redirect(statusCode, url); + expect(response.statusCode).to.equal(statusCode); + expect(response._getRedirectUrl()).to.equal(url); + }); + + }); + + // TODO: fix in 2.0; method should mimic Express Response.render() + describe('.render()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + sinon.spy(response, 'emit'); + }); + + afterEach(function() { + response.emit.restore(); + response = null; + }); + + it('should mimic Express Response.render()'); + + it('should accept view argument only', function() { + var view = 'view'; + + response.render(view); + expect(response._getRenderView()).to.equal(view); + expect(response._getRenderData()).to.deep.equal({}); + expect(response.emit).to.have.been.calledTwice; + expect(response.emit).to.have.been.calledWith('render'); + expect(response.emit).to.have.been.calledWith('end'); + }); + + it('should accept view and data arguments', function() { + var view = 'view'; + var data = { key: 'value' }; + + response.render(view, data); + expect(response._getRenderView()).to.equal(view); + expect(response._getRenderData()).to.deep.equal(data); + expect(response.emit).to.have.been.calledTwice; + expect(response.emit).to.have.been.calledWith('render'); + expect(response.emit).to.have.been.calledWith('end'); + }); + + }); + + // TODO: fix in 2.0; method should mimic Express Response.send() + describe('.send()', function() { + + it('should mimic Express Response.send()'); + + }); + + // TODO: fix in 2.0; method should mimic Express Response.json() + describe('.json()', function() { + + it('method should mimic Express Response.json()'); + + }); + + // TODO: fix in 2.0; method should mimic Express Response.redirect() + describe('.redirect()', function() { + + it('method should mimic Express Response.redirect()'); + + }); + + }); + + // TODO: fix in 2.0; methods should be inherited from Node ServerResponse + describe('Node ServerResponse methods', function() { + + describe('.writeHead()', function() { + + it('should inherit from ServerResponse.writeHead()'); + + }); + + }); + + // TODO: fix in 2.0; methods should be inherited from Node OutogingMessage + describe('Node OutogingMessage methods', function() { + + describe('.setHeader()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + + it('should set header, when called with name and value strings', function() { + response.setHeader('name', 'value'); + expect(response.getHeader('name')).to.equal('value'); + }); + + it('should throw an error when called without arguments', function() { + expect(response.setHeader).to.throw; + }); + + }); + + describe('.getHeader()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should get header, when called existing header', function() { + response.set('name1', 'value1'); + expect(response.getHeader('name1')).to.equal('value1'); + + response.header('name2', 'value2'); + expect(response.getHeader('name2')).to.equal('value2'); + }); + + it('should throw and error, when called without arguments', function() { + expect(response.getHeader).to.throw; + }); + + }); + + describe('.removeHeader()', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + it('should delete header, when with called existing header', function() { + response.set('namer1'); + response.removeHeader('name1'); + expect(response.getHeader('name1')).not.to.exist; + }); + + it('should exit silently, when with called non-existing header', function() { + expect(response.getHeader('name2')).not.to.exist; + response.removeHeader('name2'); + }); + + it('should throw and error, when called without arguments', function() { + expect(response.removeHeader).to.throw; + }); + + }); + + describe('.write()', function() { + + it('should inherit from Node OutogingMessage.write()'); + + }); + + describe('.end()', function() { + + it('should inherit from Node OutogingMessage.end()'); + + }); + + }); + + // TODO: fix in 2.0; methods should be inherited from Node WritableStream + describe('node WritableStream methods', function() { + + describe('.destroy()', function() { + + it('should inherit from Node WritableStream.destroy()'); + + }); + + describe('.destroySoon()', function() { + + it('should inherit from Node WritableStream.destroySoon()'); + + }); + + }); + + // TODO: fix in 2.0; methods should be inherited from Node EventEmitter + describe('node EventEmitter methods', function() { + + describe('.addListener()', function() { + + it('should inherit from Node EventEmitter.addListener()'); + + }); + + describe('.on()', function() { + + it('should inherit from Node EventEmitter.on()'); + + }); + + describe('.once()', function() { + + it('should inherit from Node EventEmitter.once()'); + + }); + + describe('.removeListener()', function() { + + it('should inherit from Node EventEmitter.removeListener()'); + + }); + + describe('.removeAllListeners()', function() { + + it('should inherit from Node EventEmitter.removeAllListeners()'); + + }); + + describe('.setMaxListeners()', function() { + + it('should inherit from Node EventEmitter.setMaxListeners()'); + + }); + + describe('.listeners()', function() { + + it('should inherit from Node EventEmitter.listeners()'); + + }); + + describe('.emit()', function() { + + it('should inherit from Node EventEmitter.emit()'); + + }); + + }); + + // TODO: deprecate helper methods in 2.0 + describe('helper methods', function() { + var response; + + beforeEach(function() { + response = mockResponse.createResponse(); + }); + + afterEach(function() { + response = null; + }); + + describe('._isEndCalled()', function() { + + it('will be deprecated in 2.0'); + + it('should return false when .end() hasn\'t been called', function() { + expect(response._isEndCalled()).to.be.false; + }); + + it('should return true when .end() has been called', function() { + response.end(); + expect(response._isEndCalled()).to.be.true; + }); + + }); + + describe('._getHeaders()', function() { + + it('will be deprecated in 2.0'); + + it('should return empty object when no headers have been set', function() { + expect(response._getHeaders()).to.deep.equal({}); + }); + + it('should return true when .end() has been called', function() { + var headers = { + 'Content-Type': 'text/plain' + }; + response.type('txt'); + expect(response._getHeaders()).to.deep.equal(headers); + }); + + }); + + describe('._getData()', function() { + + it('will be deprecated in 2.0'); + + it('should return empty string when no data has been sent', function() { + expect(response._getData()).to.equal(''); + }); + + it('should return sent data', function() { + response.send('data'); + expect(response._getData()).to.equal('data'); + }); + + }); + + describe('._getStatusCode()', function() { + + it('will be deprecated in 2.0'); + + it('should return default status code, when not set', function() { + expect(response._getStatusCode()).to.equal(200); + }); + + it('should return set status code', function() { + response.status(404); + expect(response._getStatusCode()).to.equal(404); + }); + + }); + + describe('._isJSON()', function() { + + it('will be deprecated in 2.0'); + + it('should return true, when Content-Type is JSON', function() { + response.type('json'); + expect(response._isJSON()).to.be.true; + }); + + it('should return false, when Content-Type is not JSON', function() { + response.type('html'); + expect(response._isJSON()).to.be.false; + }); + + }); + + describe('._isUTF8()', function() { + + it('will be deprecated in 2.0'); + + it('should return false, when enconding is not UTF-8', function() { + expect(response._isUTF8()).to.be.false; + }); + + it('should return true, when enconding is UTF-8', function() { + response.setEncoding('utf8'); + expect(response._isUTF8()).to.be.true; + }); + + }); + + describe('._isDataLengthValid()', function() { + + it('will be deprecated in 2.0'); + + it('should return true, when Content-Length not present', function() { + expect(response._isDataLengthValid()).to.be.true; + }); + + it('should return true, when Content-Length equals data size', function() { + response.send('data'); + response.header('Content-Length', '4'); + expect(response._isDataLengthValid()).to.be.true; + }); + + it('should return false, when Content-Length does not equal data size', function() { + response.send('data'); + response.header('Content-Length', '5'); + expect(response._isDataLengthValid()).to.be.false; + }); + + }); + + describe('._getRedirectUrl()', function() { + + it('will be deprecated in 2.0'); + + it('should return empty string, when .redirect() not called', function() { + expect(response._getRedirectUrl()).to.equal(''); + }); + + it('should return redirect url', function() { + var url = '/path/to/redirect'; + response.redirect(url); + expect(response._getRedirectUrl()).to.equal(url); + }); + + }); + + describe('._getRenderView()', function() { + + it('will be deprecated in 2.0'); + + it('should return empty string, when .render() not called', function() { + expect(response._getRenderView()).to.equal(''); + }); + + it('should return name of rendered view', function() { + var view = 'view'; + response.render(view); + expect(response._getRenderView()).to.equal(view); + }); + + }); + + describe('._getRenderData()', function() { + + it('will be deprecated in 2.0'); + + it('should return empty object, when .render() not called', function() { + expect(response._getRenderData()).to.deep.equal({}); + }); + + it('should return empty object, when .render() called without data', function() { + response.render('view'); + expect(response._getRenderData()).to.deep.equal({}); + }); + + it('should return data object, when .render() called with data', function() { + var data = { + key: 'value' + }; + response.render('view', data); + expect(response._getRenderData()).to.deep.equal(data); + }); + + }); + + }); + +}); diff --git a/test/lib/mockWritableStream.spec.js b/test/lib/mockWritableStream.spec.js new file mode 100644 index 0000000..7909b13 --- /dev/null +++ b/test/lib/mockWritableStream.spec.js @@ -0,0 +1,41 @@ +'use strict'; + +var chai = require('chai'); +var expect = chai.expect; + +var MockWritableStream = require('../../lib/mockWritableStream'); +var mockWritableStream; + +describe('mockWritableStream', function() { + + before(function() { + mockWritableStream = new MockWritableStream(); + }); + + it('should be a function', function() { + expect(MockWritableStream).to.be.a('function'); + }); + + it('should be an object factory', function() { + expect(mockWritableStream).to.be.a('object'); + expect(mockWritableStream).to.be.an.instanceof(MockWritableStream); + }); + + it('should expose "MockWritableStream" prototype', function() { + expect(mockWritableStream).to.have.property('end'); + expect(mockWritableStream.end).to.be.a('function'); + + expect(mockWritableStream).to.have.property('destroy'); + expect(mockWritableStream.destroy).to.be.a('function'); + + expect(mockWritableStream).to.have.property('destroySoon'); + expect(mockWritableStream.destroySoon).to.be.a('function'); + }); + + it('should return undefined when methods called', function() { + expect(mockWritableStream.end()).to.be.undefined; + expect(mockWritableStream.destroy()).to.be.undefined; + expect(mockWritableStream.destroySoon()).to.be.undefined; + }); + +}); diff --git a/test/mocha.opts b/test/mocha.opts new file mode 100644 index 0000000..727f2d7 --- /dev/null +++ b/test/mocha.opts @@ -0,0 +1,3 @@ +--recursive +--reporter spec +--ui bdd diff --git a/test/test-mockRequest.js b/test/test-mockRequest.js deleted file mode 100644 index ccf1452..0000000 --- a/test/test-mockRequest.js +++ /dev/null @@ -1,328 +0,0 @@ -'use strict'; - -/** - * Test: test-mockRequest - * - * Test cases for the module. - */ - -var httpMocks = require('../lib/http-mock'); - -exports['params - Simple verification'] = function(test) { - var request = httpMocks.createRequest(); - request._setParameter('id', 42); - test.equal(42, request.params.id); - test.done(); -}; - -exports['params - Unset value'] = function(test) { - var request = httpMocks.createRequest(); - test.equal(undefined, request.params.id); - test.done(); -}; - - -exports['method - Default value'] = function(test) { - var request = httpMocks.createRequest(); - test.equal('GET', request.method); - test.done(); -}; - -exports['method - Setting a POST'] = function(test) { - var request = httpMocks.createRequest(); - request._setMethod('POST'); - test.equal('POST', request.method); - test.done(); -}; - -exports['url - Default value'] = function(test) { - var request = httpMocks.createRequest(); - test.equal('', request.url); - test.done(); -}; - -exports['url - Setting a POST'] = function(test) { - var request = httpMocks.createRequest(); - var expected = 'http://localhost:5732/blah'; - request._setURL(expected); - test.equal(expected, request.url); - test.done(); -}; - -exports['originalUrl - Default value'] = function(test) { - var request = httpMocks.createRequest(); - test.equal(request.url, request.originalUrl); - test.done(); -}; - -exports['originalUrl - Default value (with url option set)'] = function(test) { - var expected = 'http://localhost:5732/blah'; - var request = httpMocks.createRequest({ url: expected }); - test.equal(request.url, request.originalUrl); - test.done(); -}; - -exports['originalUrl - Default value (with url and originalUrl options set)'] = function(test) { - var url = 'http://localhost:5732/blah'; - var originalUrl = 'http://original/blah'; - var request = httpMocks.createRequest({ url: url, originalUrl: originalUrl }); - test.equal(url, request.url); - test.equal(originalUrl, request.originalUrl); - test.done(); -}; - -exports['originalUrl - Setting using ._setOriginalUrl()'] = function(test) { - var request = httpMocks.createRequest(); - var expected = 'http://localhost:5732/blah'; - request._setOriginalUrl(expected); - test.equal(expected, request.originalUrl); - test.done(); -}; - -exports['get/header - Setting a header using options'] = function(test) { - var name = 'accept'; - var value = 'text/plain'; - var options = { headers: {} }; - options.headers[name] = value; - var request = httpMocks.createRequest(options); - test.equal(request.get(name), value); - test.done(); -}; - -exports['get/header - Setting a header using ._setHeadersVariable()'] = function(test) { - var request = httpMocks.createRequest(); - var name = 'accept'; - var value = 'text/plain'; - request._setHeadersVariable(name, value); - test.equal(request.get(name), value); - test.equal(request.header(name), value); - test.done(); -}; - -exports['get/header - Setting header "referer" edge-case'] = function(test) { - var request = httpMocks.createRequest(); - var name = 'referer'; - var value = 'http://localhost:5732/blah'; - request._setHeadersVariable(name, value); - test.equal(request.get('referer'), value); - test.equal(request.get('referrer'), value); - test.equal(request.header('referer'), value); - test.equal(request.header('referrer'), value); - test.done(); -}; - -exports['get/header - Setting header "referrer" edge-case'] = function(test) { - var request = httpMocks.createRequest(); - var name = 'referrer'; - var value = 'http://localhost:5732/blah'; - request._setHeadersVariable(name, value); - test.equal(request.get('referer'), value); - test.equal(request.get('referrer'), value); - test.equal(request.header('referer'), value); - test.equal(request.header('referrer'), value); - test.done(); -}; - -exports['addBody - Simple verification'] = function(test) { - var request = httpMocks.createRequest(); - - var username = 'bob'; - request._addBody('user', username); - test.equal(username, request.body.user); - test.done(); -}; - -exports['setBody - Simple verification'] = function(test) { - var request = httpMocks.createRequest(); - - var username = 'bob'; - request._setBody({ - 'user': username - }); - test.equal(username, request.body.user); - test.done(); -}; - -exports['Object creation - No values set'] = function(test) { - var request = httpMocks.createRequest(); - - test.equal(request.method, 'GET'); - test.equal(request.url, ''); - test.equal(request.path, ''); - test.deepEqual(request.params, {}); - test.equal(typeof request.session, 'undefined'); - test.deepEqual(request.cookies, {}); - test.equal(typeof request.signedCookies, 'undefined'); - test.deepEqual(request.headers, {}); - test.deepEqual(request.body, {}); - test.deepEqual(request.query, {}); - test.deepEqual(request.files, {}); - test.done(); -}; - - -exports['Object creation - Most values set'] = function(test) { - - var methodValue = 'PUT'; - var idValue = 34; - var urlValue = 'http://localhost:6522/blingbling'; - var usernameValue = 'mittens'; - - var request = httpMocks.createRequest({ - method: methodValue, - url: urlValue, - params: { - id: idValue, - sort: 'asc' - }, - session: {}, - cookies: { - name: 'value' - }, - signedCookies: { - name: 'value' - }, - headers: { - name: 'value' - }, - body: { - username: usernameValue, - email: 'bob@dog.com' - } - }); - - test.equal(request.method, methodValue); - test.equal(request.url, urlValue); - test.equal(request.originalUrl, urlValue); - test.equal(request.params.id, idValue); - test.deepEqual(request.session, {}); - test.equal(request.cookies.name, 'value'); - test.equal(request.signedCookies.name, 'value'); - test.equal(request.header('name'), 'value'); - test.equal(request.body.username, usernameValue); - test.done(); -}; - -exports['.param - returns value from params if exists'] = function(test) { - var request = httpMocks.createRequest(); - request._setParameter('paramitem', 'abc'); - - test.equal(request.param('paramitem'), 'abc'); - test.done(); -}; - -exports['.param - returns value in body if exists'] = function(test) { - var request = httpMocks.createRequest(); - request._addBody('bodyitem', 'abc'); - - test.equal(request.param('bodyitem'), 'abc'); - test.done(); -}; - -exports['.param - returns value in querystring if exists'] = function(test) { - var request = httpMocks.createRequest({ - query: { - 'queryitem': 'abc' - } - }); - - test.equal(request.param('queryitem'), 'abc'); - test.done(); -}; - - -exports['.param - returns value in correct order (params)'] = function(test) { - var request = httpMocks.createRequest({ - query: { - 'thing': '3' - } - }); - - request._setParameter('thing', '1'); - request._addBody('thing', '2'); - - test.equal(request.param('thing'), '1'); - test.done(); -}; - -exports['.param - returns value in correct order (body)'] = function(test) { - var request = httpMocks.createRequest({ - query: { - 'thing': '2' - } - }); - - request._addBody('thing', '1'); - - test.equal(request.param('thing'), '1'); - test.done(); -}; - -exports['.param - returns value in correct order (query)'] = function(test) { - var request = httpMocks.createRequest({ - query: { - 'thing': '1' - } - }); - - test.equal(request.param('thing'), '1'); - - test.done(); -}; - -exports['query object is parsed from url query string'] = function(test) { - var request = httpMocks.createRequest({ - url: 'http://www.whatever.com?a=1&b=2&c=3' - }); - - test.equal(request.query.a, '1'); - test.equal(request.query.b, '2'); - test.equal(request.query.c, '3'); - - test.done(); -}; - -exports['query object is parsed from supplied options if provided'] = function(test) { - var request = httpMocks.createRequest({ - url: 'http://www.whatever.com?a=1&b=2&c=3', - query: { - 'a': '7', - 'b': '8', - 'c': '9' - } - }); - - test.equal(request.query.a, '7'); - test.equal(request.query.b, '8'); - test.equal(request.query.c, '9'); - - test.done(); -}; - -exports['path(pathname) has to be parsed from url'] = function(test) { - var request = httpMocks.createRequest({ - url: 'http://www.whatever.com/iamthepath?a=1&b=2&c=3' - }); - - test.equal(request.path, '/iamthepath'); - - test.done(); -}; - -exports['session - setting session variable using _setSessionVariable()'] = function(test) { - var request = httpMocks.createRequest(); - request._setSessionVariable('name', 'value'); - - test.equal(request.session.name, 'value'); - - test.done(); -}; - -exports['signedCookies - setting signed cookies variable using _setSignedCookiesVariable()'] = function(test) { - var request = httpMocks.createRequest(); - request._setSignedCookiesVariable('name', 'value'); - - test.equal(request.signedCookies.name, 'value'); - - test.done(); -}; diff --git a/test/test-mockResponse.js b/test/test-mockResponse.js deleted file mode 100644 index 60ccabd..0000000 --- a/test/test-mockResponse.js +++ /dev/null @@ -1,432 +0,0 @@ -'use strict'; - -/** - * Test: test-mockResponse - * - * Test cases for the module. - */ - -var httpMocks = require('../lib/http-mock'); -var EventEmitter = require('events').EventEmitter; - -exports['object - Simple verification'] = function (test) { - var response = httpMocks.createResponse(); - - response.send('Hello', 'utf8'); - response.send('World'); - test.equal('HelloWorld', response._getData()); - test.ok(response._isUTF8()); - test.ok(!response._isEndCalled()); - test.done(); -}; - -exports['object - Data Initialization'] = function (test) { - var response = httpMocks.createResponse(); - test.equal(200, response.statusCode); - test.equal('', response._getData()); - test.ok(!response._isUTF8()); - test.ok(!response._isEndCalled()); - test.done(); -}; - -exports['end - Simple Verification'] = function (test) { - var response = httpMocks.createResponse(); - - response.send('Hello'); - response.end('World'); - - test.equal('HelloWorld', response._getData()); - - test.ok(response._isEndCalled()); - test.done(); -}; - -exports['end - No Data Called'] = function (test) { - var response = httpMocks.createResponse(); - - response.end('Hello World'); - - test.equal('Hello World', response._getData()); - - test.ok(response._isEndCalled()); - test.done(); -}; - -exports['write - Simple verification'] = function (test) { - var response = httpMocks.createResponse(); - - response.write('Hello', 'utf8'); - response.end('World'); - - test.equal('HelloWorld', response._getData()); - - test.ok(response._isUTF8()); - test.ok(response._isEndCalled()); - test.done(); -}; - -exports['setHeader - Simple verification'] = function (test) { - var response = httpMocks.createResponse(); - - response.setHeader('foo', 'bar'); - response.setHeader('bling', 'blang'); - - test.equal('bar', response.getHeader('foo')); - test.equal('blang', response.getHeader('bling')); - - response.removeHeader('bling'); - test.ok(!response.getHeader('bling')); - - test.done(); -}; - -exports['setHeader - Can not call after end'] = function (test) { - var response = httpMocks.createResponse(); - - var body = 'hello world'; - response.end(body); - - test.throws(function () { - response.setHead('Content-Length', body.length); - }); - test.done(); -}; - -exports['set - Can set multiple headers with object'] = function (test) { - var response = httpMocks.createResponse(); - - response.set({ - 'foo': 'bar', - 'bling': 'blang' - }); - - test.equal('bar', response.getHeader('foo')); - test.equal('blang', response.getHeader('bling')); - - test.done(); -}; - -exports['writeHead - Simple verification'] = function (test) { - var response = httpMocks.createResponse(); - - var body = 'hello world'; - response.writeHead(200, { - 'Content-Length': body.length, - 'Content-Type': 'text/plain' - }); - response.end(body); - - test.equal(200, response._getStatusCode()); - test.equal(body, response._getData()); - test.ok(response._isDataLengthValid()); - test.ok(response._isEndCalled()); - test.ok(!response._isJSON()); - test.done(); -}; - -exports['writeHead - Can not call after end'] = function (test) { - var response = httpMocks.createResponse(); - - var body = 'hello world'; - response.end(body); - - test.throws(function () { - response.writeHead(200, { - 'Content-Length': body.length, - 'Content-Type': 'text/plain' - }); - }); - test.done(); -}; - -exports['status - Set the status code'] = function (test) { - var response = httpMocks.createResponse(); - response.status(401); - test.equal(401, response._getStatusCode()); - test.done(); -}; - -exports['send - Status code at the beginning'] = function (test) { - var s = 123; - var t = 'This is a weird status code'; - - var response = httpMocks.createResponse(); - response.send(s, t); - - test.equal(s, response._getStatusCode()); - test.equal(t, response._getData()); - test.done(); -}; - -exports['send - Status code at the end'] = function (test) { - var s = 543; - var t = 'This is a weird status code'; - - var response = httpMocks.createResponse(); - response.send(t, s); - - test.equal(s, response._getStatusCode()); - test.equal(t, response._getData()); - test.done(); -}; - -exports['implement - WriteableStream'] = function (test) { - var response = httpMocks.createResponse(); - test.equal(typeof (response.writable), 'function'); - test.equal(typeof (response.destroy), 'function'); - test.equal(typeof (response.destroySoon), 'function'); - test.done(); -}; - -exports['implement - EventEmitter'] = function (test) { - var response = httpMocks.createResponse(); - test.equal(typeof (response.addListener), 'function'); - test.equal(typeof (response.on), 'function'); - test.equal(typeof (response.once), 'function'); - test.equal(typeof (response.removeListener), 'function'); - test.equal(typeof (response.removeAllListeners), 'function'); - test.equal(typeof (response.setMaxListeners), 'function'); - test.equal(typeof (response.listeners), 'function'); - test.equal(typeof (response.emit), 'function'); - test.done(); -}; - -exports['cookies - Cookies creation'] = function (test) { - var response = httpMocks.createResponse(); - test.deepEqual(response.cookies, {}); - test.done(); -}; - -exports['cookies - Cookies assignment'] = function (test) { - var response = httpMocks.createResponse(); - response.cookie('egg', 'chicken', { - maxAge: 1000 - }); - test.deepEqual(response.cookies, { - egg: { - value: 'chicken', - options: { - maxAge: 1000 - } - } - }); - test.done(); -}; - -exports['cookies - Cookie deletion'] = function (test) { - var response = httpMocks.createResponse(); - response.cookie('egg', 'chicken', { - maxAge: 1000 - }); - response.clearCookie('egg'); - test.deepEqual(response.cookies, {}); - test.done(); -}; - -exports['redirect - Redirect to a url with response code'] = function (test) { - var response = httpMocks.createResponse(); - var url = '/index'; - var responseCode = 200; - response.redirect(responseCode, url); - test.equal(response._getRedirectUrl(), url); - test.equal(response._getStatusCode(), responseCode); - test.done(); -}; - -exports['redirect - Redirect to a url without response code'] = function (test) { - var response = httpMocks.createResponse(); - var url = '/index'; - response.redirect(url); - test.equal(response._getRedirectUrl(), url); - test.equal(response._getStatusCode(), 302); - test.done(); -}; - -exports['render - Render to a view with data'] = function (test) { - var response = httpMocks.createResponse(); - var view = 'index'; - var data = { - 'name': 'bob' - }; - var callback = function () {}; - response.render(view, data, callback); - test.equal(response._getRenderView(), view); - test.deepEqual(response._getRenderData(), data); - test.done(); -}; - -exports['render - Render to a view without data'] = function (test) { - var response = httpMocks.createResponse(); - var view = 'index'; - var callback = function () {}; - response.render(view, callback); - test.equal(response._getRenderView(), view); - test.done(); -}; - -exports['json - Without status code'] = function (test) { - var response = httpMocks.createResponse(), - data = { - hello: 'there' - }; - - response.json(data); - test.equal(response._isJSON(), true); - test.equal(response._getData(), JSON.stringify(data)); - test.equal(response.statusCode, 200); - test.done(); -}; - -exports['json - With status code'] = function (test) { - var response = httpMocks.createResponse(), - data = { - hello: 'there' - }; - response.json(201, data); - test.equal(response._isJSON(), true); - test.equal(response._getData(), JSON.stringify(data)); - test.equal(response.statusCode, 201); - test.done(); -}; - -exports['json - With status code reverse'] = function (test) { - var response = httpMocks.createResponse(), - data = { - hello: 'there' - }; - - response.json(data, 201); - test.equal(response._isJSON(), true); - test.equal(response._getData(), JSON.stringify(data)); - test.equal(response.statusCode, 201); - test.done(); -}; - -exports['json - With status code - chain'] = function (test) { - var response = httpMocks.createResponse(), - data = { - hello: 'there' - }; - response.status(201).json(data); - test.equal(response._isJSON(), true); - test.equal(response._getData(), JSON.stringify(data)); - test.equal(response.statusCode, 201); - test.done(); -}; - -exports['events - end'] = function (test) { - var response = httpMocks.createResponse({ - eventEmitter: EventEmitter - }); - - response.on('end', function () { - test.ok(response._isEndCalled()); - test.done(); - }); - - response.end(); -}; - -exports['events - send'] = function (test) { - var response = httpMocks.createResponse({ - eventEmitter: EventEmitter - }); - - response.on('send', function () { - test.equal(response.statusCode, 200); - test.done(); - }); - - response.send(200); -}; - -exports['events - render'] = function (test) { - var response = httpMocks.createResponse({ - eventEmitter: EventEmitter - }); - var view = 'index'; - var data = { - 'name': 'bob' - }; - var callback = function () {}; - - response.on('render', function () { - test.equal(response._getRenderView(), view); - test.deepEqual(response._getRenderData(), data); - test.done(); - }); - - response.render(view, data, callback); -}; - - -exports['send - sending response objects a.k.a restifyError with statusCode'] = function(test) { - var errors = require('node-restify-errors'); - var response = httpMocks.createResponse(); - response.send(409, new errors.InvalidArgumentError('I just dont like you')); - - test.equal(409, response._getStatusCode()); - test.equal('InvalidArgument', response._getData().code); - test.equal('I just dont like you', response._getData().message); - test.done(); -}; - -exports['send - sending response objects a.k.a restifyError without statusCode'] = function(test) { - var errors = require('node-restify-errors'); - var response = httpMocks.createResponse(); - response.send(new errors.InvalidArgumentError('I just dont like you')); - - test.equal(409, response._getStatusCode()); - test.equal('InvalidArgument', response._getData().code); - test.equal('I just dont like you', response._getData().message); - test.done(); -}; - -exports['type - set "Content-Type" response header with .type()'] = function(test) { - var response = httpMocks.createResponse(); - - response.type('.html'); - test.equal('text/html', response.getHeader('Content-Type')); - response.type('html'); - test.equal('text/html', response.getHeader('Content-Type')); - response.type('json'); - test.equal('application/json', response.getHeader('Content-Type')); - response.type('application/json'); - test.equal('application/json', response.getHeader('Content-Type')); - response.type('png'); - test.equal('image/png', response.getHeader('Content-Type')); - test.done(); -}; - -exports['type - set "Content-Type" response header with .contentType()'] = function(test) { - var response = httpMocks.createResponse(); - - response.contentType('.html'); - test.equal('text/html', response.getHeader('Content-Type')); - response.contentType('html'); - test.equal('text/html', response.getHeader('Content-Type')); - response.contentType('json'); - test.equal('application/json', response.getHeader('Content-Type')); - response.contentType('application/json'); - test.equal('application/json', response.getHeader('Content-Type')); - response.contentType('png'); - test.equal('image/png', response.getHeader('Content-Type')); - test.done(); -}; - -exports['send - response "Status Code" with .sendStatus()'] = function(test) { - var http = require('../lib/node/http'); - var response, statusCode; - - for (statusCode in http.STATUS_CODES) { - response = httpMocks.createResponse(); - response.sendStatus(statusCode); - test.equal(response._getStatusCode(), statusCode); - test.equal(response._getData(), http.STATUS_CODES[statusCode]); - // uncomment after fixing bug in .send() - // test.ok(response._isEndCalled()); - response = null; - } - - test.done(); -};