Best JavaScript code snippet using apimocker
test-functional.js
Source:test-functional.js
...42 });43 });44 req.end();45}46function verifyResponseBody(httpReqOptions, postData, expected, done) {47 var req = http.request(httpReqOptions, function(res) {48 res.setEncoding('utf8');49 res.on('data', function (chunk) {50 expect(JSON.parse(chunk)).to.deep.equal(expected);51 // console.log(chunk);52 if (done) {53 done();54 }55 });56 });57 if (postData) {58 req.write(postData);59 }60 req.end();61}62describe('Functional tests using an http client to test "end-to-end": ', function() {63 describe('apimocker server:', function() {64 before(function startMockerForFuncTests(done) {65 var options = {66 quiet: true,67 proxyURL: 'http://localhost:' + MOCK_PORT68 };69 mocker = apiMocker.createServer(options).setConfigFile('test/test-config.json');70 mocker.start(null, done);71 });72 before(function (done) {73 testEndpoint = http.createServer(function (req, res) {74 if (req.url === '/non-mocked') {75 res.writeHead(200, {'Content-Type': 'application/json'});76 res.end(JSON.stringify({data: 'real'}));77 } else {78 res.writeHead(404);79 res.end();80 }81 }).listen(MOCK_PORT, done);82 });83 after(function (done) {84 mocker.stop(done);85 });86 describe('basic requests: ', function() {87 it('returns correct data for basic get request', function(done) {88 var reqOptions = httpReqOptions('/first');89 verifyResponseBody(reqOptions, null, {'king': 'greg'}, done);90 });91 it('returns correct data for basic post request', function(done) {92 var reqOptions = httpReqOptions('/nested/ace');93 reqOptions.method = 'POST';94 verifyResponseBody(reqOptions, null, {'ace': 'greg'}, done);95 });96 it('returns correct data for post to path with mockFile varying on verb', function(done) {97 var reqOptions = httpReqOptions('/royals');98 reqOptions.method = 'POST';99 verifyResponseBody(reqOptions, null, {'king': 'greg'}, done);100 });101 it('returns correct data for get to path with mockFile varying on verb', function(done) {102 var reqOptions = httpReqOptions('/royals');103 verifyResponseBody(reqOptions, null, {'ace': 'greg'}, done);104 });105 it('Returns data in template from the route', function(done){106 var reqOptions = httpReqOptions('/template/john/4');107 verifyResponseBody(reqOptions, null, {'name':'john', 'number':4}, done);108 });109 it('returns correct data for get to templateSwitch substituting GET params into mockFile ', function(done) {110 var reqOptions = httpReqOptions('/templateSwitchGetParams?appID=123456789&appName=myAppName&userName=MyName&userAge=21');111 var expected = {'appID': 123456789,112 'appName': 'myAppName',113 'userName': 'MyName',114 'userAge': 21115 };116 verifyResponseBody(reqOptions, null, expected, done);117 });118 it('returns correct data for post to templateSwitch substituting POST data parsed using jsonPath into mockFile', function(done) {119 var postData = '{ "data": { "appID": 123456789, "appName": "myAppName", "user": { "userName": "MyName", "userAge": 21 } } }',120 postOptions = httpPostOptions('/templateSwitchPostJsonPath', postData),121 expected = {'appID': 123456789,122 'appName': 'myAppName',123 'userName': 'MyName',124 'userAge': 21125 };126 verifyResponseBody(postOptions, postData, expected, done);127 });128 it('returns correct data for an alternate path', function (done) {129 var reqOptions = httpReqOptions('/1st');130 verifyResponseBody(reqOptions, null, {'king': 'greg'}, done);131 });132 });133 describe('content type: ', function() {134 it('returns a custom content type', function(done) {135 var reqOptions = httpReqOptions('/first');136 verifyResponseHeaders(reqOptions, {'content-type': 'foobar'}, done);137 });138 it('returns correct content-type for json response, with nested path', function(done) {139 var reqOptions = httpReqOptions('/nested/ace');140 verifyResponseHeaders(reqOptions, {'content-type': 'application/json'}, done);141 });142 it('returns correct content-type for xml response', function(done) {143 var reqOptions = httpReqOptions('/var/123');144 verifyResponseHeaders(reqOptions, {'content-type': 'application/xml'}, done);145 });146 });147 describe('switch on request param: ', function() {148 it('returns correct file for switch param in json request', function(done) {149 var postData = '{"customerId": 1234}',150 postOptions = httpPostOptions('/nested/ace', postData),151 expected = {152 'ace': 'greg',153 'note': 'request contained customerId = 1234'154 };155 verifyResponseBody(postOptions, postData, expected, done);156 });157 it('returns base file when no match for switch param in json request', function(done) {158 var postData = '{"customerId": 124}',159 postOptions = httpPostOptions('/nested/ace', postData),160 expected = {161 'ace': 'greg'162 };163 verifyResponseBody(postOptions, postData, expected, done);164 });165 it('returns base file when no switch param passed in json request', function(done) {166 var postData = '{"phonenumber": 124}',167 postOptions = httpPostOptions('/nested/ace', postData),168 expected = {169 'ace': 'greg'170 };171 verifyResponseBody(postOptions, postData, expected, done);172 });173 it('returns correct file for switch param in query string', function(done) {174 var reqOptions = httpReqOptions('/nested/ace?customerId=1234'),175 expected = {176 'ace': 'greg',177 'note': 'request contained customerId = 1234'178 };179 verifyResponseBody(reqOptions, null, expected, done);180 });181 it('returns correct httpStatus when switches match', function(done) {182 stRequest.post('/login')183 .set('Content-Type', 'application/json')184 .send('{"userId": "user1", "password": "good"}')185 .expect(200, done);186 });187 it('returns correct httpStatus when switch does not match, with contentType set', function(done) {188 stRequest.post('/login')189 .set('Content-Type', 'application/json')190 .send('{"userId": "user1", "password": "bad"}')191 .expect(401, done);192 });193 it('returns correct httpStatus when switch does not match', function(done) {194 stRequest.post('/login')195 .send('{"userId": "user1", "password": "bad"}')196 .expect(401, done);197 });198 it('returns 404 when switch does not match and no httpStatus was set', function(done) {199 stRequest.post('/verify')200 .send('{"foo": "bar"}')201 .expect(404, done);202 });203 });204 describe('http status: ', function() {205 it('returns 404 for incorrect path', function(done) {206 stRequest.get('/badurl')207 .expect(404)208 .end(function(err) {209 // console.log('got a 404 as expected');210 done();211 });212 });213 it('returns httpStatus of 200 if not set', function(done) {214 stRequest.get('/first').expect(200, done);215 });216 it('returns httpStatus specified in config file, when contentType is passed in', function(done) {217 stRequest.put('/protected').expect(403, done);218 });219 it('returns httpStatus 204 specified in config file', function(done) {220 stRequest.delete('/second')221 .expect(204, done);222 });223 it('returns httpStatus 404 if no mockFile is set for a web service', function(done) {224 stRequest.get('/noMockFile').expect(404, done);225 });226 it('returns specified httpStatus even if mockFile is set incorrectly and no contentType is configured', function(done) {227 stRequest.get('/missingMockFile').expect(203, done);228 });229 });230 describe('http headers: ', function() {231 it('returns the headers as specified in the config file', function(done) {232 var reqOptions = httpReqOptions('/firstheaders');233 verifyResponseHeaders(reqOptions, {'x-requested-by': '4c2df03a17a803c063f21aa86a36f6f55bdde1f85b89e49ee1b383f281d18c09c2ba30654090df3531cd2318e3c', 'dummyheader': 'dummyvalue', 'content-type': 'foobar'}, done);234 });235 it('allows domains specified in config file', function(done) {236 var reqOptions = httpReqOptions('/first');237 verifyResponseHeaders(reqOptions, {'access-control-allow-origin': 'abc'}, done);238 });239 it('allows headers as specified in config file', function(done) {240 var reqOptions = httpReqOptions('/first');241 verifyResponseHeaders(reqOptions, {'access-control-allow-headers': 'Content-Type,my-custom-header'}, done);242 });243 it('sets Access-Control-Allow-Credentials header if corsCredentials option is set', function(done) {244 var reqOptions = httpReqOptions('/first');245 verifyResponseHeaders(reqOptions, {'access-control-allow-credentials': 'true'}, done);246 });247 });248 describe('proxy: ', function () {249 it('forwards get request to non-mocked endpoint', function (done) {250 stRequest.get('/non-mocked')251 .expect(200, {data: 'real'}, done);252 });253 it('forwards post request to non-mocked endpoint', function (done) {254 stRequest.post('/non-mocked')255 .set('Content-Type', 'application/json')256 .send({foo: 'bar'})257 .expect(200, {data: 'real'}, done);258 });259 });260 describe('admin functions for on-the-fly configuration', function() {261 // function reloadConfigFile(mocker, done) {262 // mocker.setConfigFile("test/test-config.json");263 // var req, reqOptions = httpReqOptions();264 // reqOptions.path = "/admin/reload";265 // req = http.request(reqOptions, function(res) {266 // res.setEncoding('utf8');267 // res.on('data', function () {268 // expect(res.statusCode).to.equal(200);269 // if (done) {270 // done();271 // }272 // });273 // });274 // req.end();275 // }276 it('returns correct mock file after admin/setMock was called', function(done) {277 var postData = {'verb':'get', 'serviceUrl':'third', 'mockFile':'king.json'},278 postOptions = httpPostOptions('/admin/setMock', postData),279 expected = {280 'verb':'get',281 'serviceUrl':'third',282 'mockFile':'king.json',283 'httpStatus': 200284 };285 // verifyResponseBody(postOptions, postData, expected);286 // verifyResponseBody(httpReqOptions('/third'), null, {king: 'greg'}, done);287 stRequest.post('/admin/setMock')288 .set('Content-Type', 'application/json')289 .send(postData)290 .expect(200, function() {291 stRequest.get('/third')292 .expect(200, {king: 'greg'}, done);293 });294 });295 it('returns correct mock file with http status code after admin/setMock was called', function(done) {296 var postData = {'verb':'post', 'serviceUrl':'third', 'mockFile':'king.json', 'httpStatus': 201},297 postOptions = httpPostOptions('/admin/setMock', postData),298 expected = {299 'verb':'post',300 'serviceUrl':'third',301 'mockFile':'king.json',302 'httpStatus': 201303 };304 stRequest.post('/admin/setMock')305 .set('Content-Type', 'application/json')306 .send(postData)307 .expect(200, function() {308 stRequest.post('/third')309 .expect(201, {king: 'greg'}, done);310 });311 });312 // it('returns 404 for incorrect path after reload was called', function(done) {313 // verifyResponseBody(postOptions, postData, expected);314 // verifyResponseBody(httpReqOptions('/third'), null, {king: 'greg'});315 // reloadConfigFile(mocker);316 // verifyResponseStatus(httpReqOptions('/third'), null, 404, done);317 // });318 // TODO: Fix this test... it fails intermittently, due to timing problems.319 xit('returns correct mock file after admin/setMock was called twice', function(done) {320 // verifyResponseBody(postOptions, postData, expected);321 // verifyResponseBody(httpReqOptions('/third'), null, {king: 'greg'});322 // // change route, and verify again323 // verifyResponseBody(postOptions, postData, expected);324 // verifyResponseBody(httpReqOptions('/third'), null, {ace: 'greg'}, done);325 stRequest.post('/admin/setMock')326 .set('Content-Type', 'application/json')327 .send(postData)328 .expect(200, function() {329 stRequest.get('/third')330 .expect(200, {kingyy: 'greg'}, function() {331 stRequest.post('/admin/setMock')332 .set('Content-Type', 'application/json')333 .send({'verb':'get', 'serviceUrl':'third', 'mockFile':'king.json'})334 .expect(200, function() {335 stRequest.get('/third')336 .expect(200, {ace: 'greg'}, done);337 });338 });...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!