Created
July 7, 2019 16:24
-
-
Save singh1114/61495aff847d0a527cb039aaf8ffa408 to your computer and use it in GitHub Desktop.
Tests for request validation middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sinon = require('sinon'); | |
const should = require('should'); | |
describe('validate Params middleware', () => { | |
it('When param is required', () => { | |
let nextSpy = sinon.spy(); | |
let response = function () {}; | |
response.send = sinon.spy(); | |
validateParams([{ | |
type: 'string', | |
required: true, | |
param_key: 'param_1', | |
validator_functions: [] | |
}])(sinon.useFakeXMLHttpRequest(), response, nextSpy); | |
response.send.args[0][0].should.equal(400); | |
response.send.args[0][1].result.should.equal( | |
'Missing Parameter param_1'); | |
}); | |
it('When param type is different', () => { | |
let nextSpy = sinon.spy(); | |
let response = function () {}; | |
response.send = sinon.spy(); | |
let request = sinon.useFakeXMLHttpRequest(); | |
request.params = { | |
param_1: 908998989 | |
}; | |
validateParams([{ | |
type: 'string', | |
required: true, | |
param_key: 'param_1', | |
validator_functions: [] | |
}])(request, response, nextSpy); | |
response.send.args[0][0].should.equal(400); | |
response.send.args[0][1].result.should.equal( | |
'param_1 is of type Number but should be string'); | |
}); | |
it('When validation test passes', () => { | |
let nextSpy = sinon.spy(); | |
let response = function () {}; | |
response.send = sinon.spy(); | |
let request = sinon.useFakeXMLHttpRequest(); | |
request.params = { | |
param_1: '9089980989' | |
}; | |
validateParams([{ | |
type: 'string', | |
required: true, | |
param_key: 'param_1', | |
validator_functions: [] | |
}])(request, response, nextSpy); | |
nextSpy.calledOnce.should.be.true(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment