Best JavaScript code snippet using mountebank
impostersControllerTest.js
Source:impostersControllerTest.js
1'use strict';2var assert = require('assert'),3 mock = require('../mock').mock,4 Controller = require('../../src/controllers/impostersController'),5 FakeResponse = require('../fakes/fakeResponse'),6 Q = require('q'),7 promiseIt = require('../testHelpers').promiseIt;8describe('ImpostersController', function () {9 var response;10 beforeEach(function () {11 response = FakeResponse.create();12 });13 describe('#get', function () {14 it('should send an empty array if no imposters', function () {15 var controller = Controller.create({}, {});16 controller.get({ url: '/imposters' }, response);17 assert.deepEqual(response.body, { imposters: [] });18 });19 it('should send list JSON for all imposters by default', function () {20 var firstImposter = { toJSON: mock().returns('firstJSON') },21 secondImposter = { toJSON: mock().returns('secondJSON') },22 controller = Controller.create({}, { 1: firstImposter, 2: secondImposter });23 controller.get({ url: '/imposters' }, response);24 assert.deepEqual(response.body, { imposters: ['firstJSON', 'secondJSON'] });25 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: false, removeProxies: false, list: true }), firstImposter.toJSON.message());26 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: false, removeProxies: false, list: true }), secondImposter.toJSON.message());27 });28 it('should send replayable JSON for all imposters if querystring present', function () {29 var firstImposter = { toJSON: mock().returns('firstJSON') },30 secondImposter = { toJSON: mock().returns('secondJSON') },31 controller = Controller.create({}, { 1: firstImposter, 2: secondImposter });32 controller.get({ url: '/imposters?replayable=true' }, response);33 assert.deepEqual(response.body, { imposters: ['firstJSON', 'secondJSON'] });34 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: false, list: false }), firstImposter.toJSON.message());35 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: false, list: false }), secondImposter.toJSON.message());36 });37 it('should send replayable and removeProxies JSON for all imposters if querystring present', function () {38 var firstImposter = { toJSON: mock().returns('firstJSON') },39 secondImposter = { toJSON: mock().returns('secondJSON') },40 controller = Controller.create({}, { 1: firstImposter, 2: secondImposter });41 controller.get({ url: '/imposters?replayable=true&removeProxies=true' }, response);42 assert.deepEqual(response.body, { imposters: ['firstJSON', 'secondJSON'] });43 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: true, list: false }), firstImposter.toJSON.message());44 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: true, list: false }), secondImposter.toJSON.message());45 });46 });47 describe('#post', function () {48 var request, Imposter, imposter, imposters, Protocol, controller, logger;49 beforeEach(function () {50 request = { body: {}, socket: { remoteAddress: 'host', remotePort: 'port' } };51 imposter = {52 url: mock().returns('imposter-url'),53 toJSON: mock().returns('JSON')54 };55 Imposter = {56 create: mock().returns(Q(imposter))57 };58 imposters = {};59 Protocol = {60 name: 'http',61 Validator: {62 create: mock().returns({ validate: mock().returns(Q({ isValid: true })) })63 }64 };65 logger = { debug: mock(), warn: mock() };66 controller = Controller.create({ http: Protocol }, imposters, Imposter, logger);67 });68 promiseIt('should return a 201 with the Location header set', function () {69 request.body = { port: 3535, protocol: 'http' };70 return controller.post(request, response).then(function () {71 assert(response.headers.Location, 'http://localhost/servers/3535');72 assert.strictEqual(response.statusCode, 201);73 });74 });75 promiseIt('should return imposter JSON', function () {76 request.body = { port: 3535, protocol: 'http' };77 return controller.post(request, response).then(function () {78 assert.strictEqual(response.body, 'JSON');79 });80 });81 promiseIt('should add new imposter to list of all imposters', function () {82 imposter.port = 3535;83 request.body = { protocol: 'http' };84 return controller.post(request, response).then(function () {85 assert.deepEqual(imposters, { 3535: imposter });86 });87 });88 promiseIt('should return a 400 for a floating point port', function () {89 request.body = { protocol: 'http', port: '123.45' };90 return controller.post(request, response).then(function () {91 assert.strictEqual(response.statusCode, 400);92 assert.deepEqual(response.body, {93 errors: [{94 code: 'bad data',95 message: "invalid value for 'port'"96 }]97 });98 });99 });100 promiseIt('should return a 400 for a missing protocol', function () {101 request.body = { port: 3535 };102 return controller.post(request, response).then(function () {103 assert.strictEqual(response.statusCode, 400);104 assert.deepEqual(response.body, {105 errors: [{106 code: 'bad data',107 message: "'protocol' is a required field"108 }]109 });110 });111 });112 promiseIt('should return a 400 for unsupported protocols', function () {113 request.body = { port: 3535, protocol: 'unsupported' };114 return controller.post(request, response).then(function () {115 assert.strictEqual(response.statusCode, 400);116 assert.strictEqual(response.body.errors.length, 1);117 assert.strictEqual(response.body.errors[0].code, 'bad data');118 });119 });120 promiseIt('should aggregate multiple errors', function () {121 request.body = { port: -1, protocol: 'invalid' };122 return controller.post(request, response).then(function () {123 assert.strictEqual(response.body.errors.length, 2, response.body.errors);124 });125 });126 promiseIt('should return a 403 for insufficient access', function () {127 Imposter.create = mock().returns(Q.reject({128 code: 'insufficient access',129 key: 'value'130 }));131 request.body = { port: 3535, protocol: 'http' };132 return controller.post(request, response).then(function () {133 assert.strictEqual(response.statusCode, 403);134 assert.deepEqual(response.body, {135 errors: [{136 code: 'insufficient access',137 key: 'value'138 }]139 });140 });141 });142 promiseIt('should return a 400 for other protocol creation errors', function () {143 Imposter.create = mock().returns(Q.reject('ERROR'));144 request.body = { port: 3535, protocol: 'http' };145 return controller.post(request, response).then(function () {146 assert.strictEqual(response.statusCode, 400);147 assert.deepEqual(response.body, { errors: ['ERROR'] });148 });149 });150 promiseIt('should not call protocol validation if there are common validation failures', function () {151 Protocol.Validator = { create: mock() };152 request.body = { protocol: 'invalid' };153 return controller.post(request, response).then(function () {154 assert.ok(!Protocol.Validator.create.wasCalled());155 });156 });157 promiseIt('should validate with Protocol if there are no common validation failures', function () {158 Protocol.Validator = {159 create: mock().returns({160 validate: mock().returns(Q({ isValid: false, errors: 'ERRORS' }))161 })162 };163 request.body = { port: 3535, protocol: 'http' };164 return controller.post(request, response).then(function () {165 assert.strictEqual(response.statusCode, 400);166 assert.deepEqual(response.body, { errors: 'ERRORS' });167 });168 });169 });170 describe('#del', function () {171 function stopMock () {172 return mock().returns(Q(true));173 }174 promiseIt('should delete all imposters', function () {175 var firstImposter = { stop: stopMock(), toJSON: mock().returns('firstJSON') },176 secondImposter = { stop: stopMock(), toJSON: mock().returns('secondJSON') },177 imposters = { 1: firstImposter, 2: secondImposter },178 controller = Controller.create({}, imposters, {}, {});179 return controller.del({ url: '/imposters' }, response).then(function () {180 assert.deepEqual(imposters, {});181 });182 });183 promiseIt('should call stop on all imposters', function () {184 var firstImposter = { stop: stopMock(), toJSON: mock().returns('firstJSON') },185 secondImposter = { stop: stopMock(), toJSON: mock().returns('secondJSON') },186 imposters = { 1: firstImposter, 2: secondImposter },187 controller = Controller.create({}, imposters, {}, {});188 return controller.del({ url: '/imposters' }, response).then(function () {189 assert(firstImposter.stop.wasCalled());190 assert(secondImposter.stop.wasCalled());191 });192 });193 promiseIt('should send replayable JSON for all imposters by default', function () {194 var firstImposter = { stop: stopMock(), toJSON: mock().returns('firstJSON') },195 secondImposter = { stop: stopMock(), toJSON: mock().returns('secondJSON') },196 imposters = { 1: firstImposter, 2: secondImposter },197 controller = Controller.create({}, imposters, {}, {});198 return controller.del({ url: '/imposters' }, response).then(function () {199 assert.deepEqual(response.body, { imposters: ['firstJSON', 'secondJSON'] });200 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: false }), firstImposter.toJSON.message());201 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: false }), secondImposter.toJSON.message());202 });203 });204 promiseIt('should send default JSON for all imposters if replayable is false on querystring', function () {205 var firstImposter = { stop: stopMock(), toJSON: mock().returns('firstJSON') },206 secondImposter = { stop: stopMock(), toJSON: mock().returns('secondJSON') },207 controller = Controller.create({}, { 1: firstImposter, 2: secondImposter });208 return controller.del({ url: '/imposters?replayable=false' }, response).then(function () {209 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: false, removeProxies: false }), firstImposter.toJSON.message());210 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: false, removeProxies: false }), secondImposter.toJSON.message());211 });212 });213 promiseIt('should send removeProxies JSON for all imposters if querystring present', function () {214 var firstImposter = { stop: stopMock(), toJSON: mock().returns('firstJSON') },215 secondImposter = { stop: stopMock(), toJSON: mock().returns('secondJSON') },216 controller = Controller.create({}, { 1: firstImposter, 2: secondImposter });217 return controller.del({ url: '/imposters?removeProxies=true' }, response).then(function () {218 assert.ok(firstImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: true }), firstImposter.toJSON.message());219 assert.ok(secondImposter.toJSON.wasCalledWith({ replayable: true, removeProxies: true }), secondImposter.toJSON.message());220 });221 });222 });223 describe('#put', function () {224 var request, logger, Protocol;225 beforeEach(function () {226 request = { body: {}, socket: { remoteAddress: 'host', remotePort: 'port' } };227 logger = { debug: mock(), warn: mock() };228 Protocol = {229 name: 'http',230 Validator: {231 create: mock().returns({ validate: mock().returns(Q({ isValid: true, errors: [] })) })232 }233 };234 });235 promiseIt('should return a 400 if the "imposters" key is not present', function () {236 var existingImposter = { stop: mock() },237 imposters = { 0: existingImposter },238 controller = Controller.create({ http: Protocol }, imposters, {}, logger);239 request.body = {};240 return controller.put(request, response).then(function () {241 assert.strictEqual(response.statusCode, 400);242 assert.deepEqual(response.body, {243 errors: [{244 code: 'bad data',245 message: "'imposters' is a required field"246 }]247 });248 assert.deepEqual(imposters, { 0: existingImposter });249 });250 });251 promiseIt('should return an empty array if no imposters provided', function () {252 var existingImposter = { stop: mock() },253 imposters = { 0: existingImposter },254 controller = Controller.create({ http: Protocol }, imposters, {}, logger);255 request.body = { imposters: [] };256 return controller.put(request, response).then(function () {257 assert.deepEqual(response.body, { imposters: [] });258 assert.deepEqual(imposters, {});259 });260 });261 promiseIt('should return imposter list JSON for all imposters', function () {262 var firstImposter = { toJSON: mock().returns({ first: true }) },263 secondImposter = { toJSON: mock().returns({ second: true }) },264 imposters = [firstImposter, secondImposter],265 creates = 0,266 Imposter = {267 create: function () {268 var result = imposters[creates];269 creates += 1;270 return result;271 }272 },273 controller = Controller.create({ http: Protocol }, {}, Imposter, logger);274 request.body = { imposters: [{ protocol: 'http' }, { protocol: 'http' }] };275 return controller.put(request, response).then(function () {276 assert.deepEqual(response.body, { imposters: [{ first: true }, { second: true }] });277 assert.ok(firstImposter.toJSON.wasCalledWith({ list: true }), firstImposter.toJSON.message());278 assert.ok(secondImposter.toJSON.wasCalledWith({ list: true }), secondImposter.toJSON.message());279 });280 });281 promiseIt('should replace imposters list', function () {282 var oldImposter = { stop: mock() },283 imposters = { 0: oldImposter },284 firstImposter = { toJSON: mock().returns({ first: true }), port: 1 },285 secondImposter = { toJSON: mock().returns({ second: true }), port: 2 },286 impostersToCreate = [firstImposter, secondImposter],287 creates = 0,288 Imposter = {289 create: function () {290 var result = impostersToCreate[creates];291 creates += 1;292 return result;293 }294 },295 controller = Controller.create({ http: Protocol }, imposters, Imposter, logger);296 request.body = { imposters: [{ protocol: 'http' }, { protocol: 'http' }] };297 return controller.put(request, response).then(function () {298 assert.deepEqual(imposters, { 1: firstImposter, 2: secondImposter });299 assert.ok(firstImposter.toJSON.wasCalledWith({ list: true }), firstImposter.toJSON.message());300 assert.ok(secondImposter.toJSON.wasCalledWith({ list: true }), secondImposter.toJSON.message());301 });302 });303 promiseIt('should return a 400 for any invalid imposter', function () {304 var controller = Controller.create({ http: Protocol }, {}, {}, logger);305 request.body = { imposters: [{ protocol: 'http' }, {}] };306 return controller.put(request, response).then(function () {307 assert.strictEqual(response.statusCode, 400);308 assert.deepEqual(response.body, {309 errors: [{310 code: 'bad data',311 message: "'protocol' is a required field"312 }]313 });314 });315 });316 promiseIt('should return a 403 for insufficient access on any imposter', function () {317 var creates = 0,318 Imposter = {319 create: function () {320 creates += 1;321 if (creates === 2) {322 return Q.reject({323 code: 'insufficient access',324 key: 'value'325 });326 }327 else {328 return Q({});329 }330 }331 },332 controller = Controller.create({ http: Protocol }, {}, Imposter, logger);333 request.body = { imposters: [{ protocol: 'http' }, { protocol: 'http' }] };334 return controller.put(request, response).then(function () {335 assert.strictEqual(response.statusCode, 403);336 assert.deepEqual(response.body, {337 errors: [{338 code: 'insufficient access',339 key: 'value'340 }]341 });342 });343 });344 });...
Using AI Code Generation
1var mb = require('mountebank');2mb.create(function (error, mb) {3 mb.post('/imposters', {4 }, function (error, response) {5 mb.get('/imposters/3000', function (error, response) {6 console.log(response.body);7 });8 });9});10var mb = require('mountebank');11mb.create(function (error, mb) {12 mb.post('/imposters', {13 }, function (error, response) {14 mb.get('/imposters/3000', function (error, response) {15 console.log(response.body);16 });17 });18});19var mb = require('mountebank');20mb.create(function (error, mb) {21 mb.post('/imposters', {22 }, function (error, response) {23 mb.get('/imposters/3000', function (error, response) {24 console.log(response.body);25 });26 });27});28var mb = require('mountebank');29mb.create(function (error, mb) {30 mb.post('/imposters', {31 }, function (error, response) {32 mb.get('/imposters/3000', function (error, response) {33 console.log(response.body);34 });35 });36});37var mb = require('mountebank');38mb.create(function (error, mb) {39 mb.post('/imposters', {40 }, function (error, response) {41 mb.get('/imposters/3000', function (error, response) {42 console.log(response.body);43 });44 });45});46var mb = require('mountebank
Using AI Code Generation
1var mb = require('mountebank');2var imposter = {3 stubs: [{4 responses: [{5 is: { body: 'Hello World!' }6 }]7 }]8};9mb.create(imposter).then(function (result) {10 console.log(result);11});12var mb = require('mountebank');13var imposter = {14 stubs: [{15 responses: [{16 is: { body: 'Hello World!' }17 }]18 }]19};20mb.createImposter(4545, imposter).then(function (result) {21 console.log(result);22});23var mb = require('mountebank');24mb.removeImposter(4545).then(function (result) {25 console.log(result);26});27var mb = require('mountebank');28mb.getImposter(4545).then(function (result) {29 console.log(result);30});31var mb = require('mountebank');32mb.getImposters().then(function (result) {33 console.log(result);34});35var mb = require('mountebank');36mb.getLogs().then(function (result) {37 console.log(result);38});39var mb = require('mountebank');40mb.clearLogs().then(function (result) {41 console.log(result);42});43var mb = require('mountebank');44mb.deleteAllImposters().then(function (result) {45 console.log(result);46});47var mb = require('mountebank');48mb.getStats().then(function (result) {49 console.log(result);50});51var mb = require('mountebank');52mb.resetStats().then(function (result) {53 console.log(result);54});55var mb = require('mountebank');
Using AI Code Generation
1var mb = require('mountebank');2var port = 2525;3var imposter = mb.create({4 {5 {6 equals: {7 }8 }9 {10 is: {11 }12 }13 }14});15imposter.then(function (imposter) {16 imposter.get('/test').then(function (response) {17 });18});19var mb = require('mountebank');20var port = 2525;21var imposter = mb.create({22 {23 {24 equals: {25 }26 }27 {28 is: {29 }30 }31 }32});33imposter.then(function (imposter) {34 imposter.get('/test').then(function (response) {35 });36});37var mb = require('mountebank');38var port = 2525;39var imposter = mb.create({40 {41 {42 equals: {43 }44 }45 {46 is: {47 }48 }49 }50});51imposter.then(function (imposter) {52 imposter.get('/test').then(function (response) {
Using AI Code Generation
1const mb = require('mountebank');2const imposter = { port: 3000, protocol: 'http' };3mb.create().then(mb => mb.post('/imposters', imposter)).then(response => {4 const imposter = response.body;5 console.log(`Imposter created with name ${imposter.name}`);6});7var mb = require('mountebank');8var imposter = { port: 3000, protocol: 'http' };9mb.create().then(mb => mb.createImposter(imposter)).then(response => {10 const imposter = response.body;11 console.log(`Imposter created with name ${imposter.name}`);12});13var mb = require('mountebank');14var imposter = { port: 3000, protocol: 'http' };15mb.create().then(mb => mb.createImposter(imposter)).then(response => {16 const imposter = response.body;17 console.log(`Imposter created with name ${imposter.name}`);18});19var mb = require('mountebank');20var imposter = { port: 3000, protocol: 'http' };21mb.create().then(mb => mb.createImposter(imposter)).then(response => {22 const imposter = response.body;23 console.log(`Imposter created with name ${imposter.name}`);24});25var mb = require('mountebank');26var imposter = { port: 3000, protocol: 'http' };27mb.create().then(mb => mb.createImposter(imposter)).then(response => {28 const imposter = response.body;29 console.log(`Imposter created with name ${imposter.name}`);30});31var mb = require('mountebank');32var imposter = { port: 3000, protocol: 'http' };33mb.create().then(mb => mb.createImposter(imposter)).then(response => {34 const imposter = response.body;35 console.log(`Imposter created with name ${imposter.name}`);36});37var mb = require('mountebank');38var imposter = { port: 3000, protocol: 'http' };39mb.create().then(mb => mb
Using AI Code Generation
1var mb = require('mountebank');2var imposter = mb.create({port: 2525, name: 'imposter1'});3imposter.addStub({4 {5 is: {6 }7 }8});9imposter.save();10imposter.start();11var mb = require('mountebank');12var imposter = mb.create({port: 2525, name: 'imposter1'});13imposter.addStub({14 {15 is: {16 }17 }18});19imposter.save();20imposter.start();21mb.create({port: 2525, name: 'imposter1'}).then(function (imposter) {22 imposter.addStub({23 {24 is: {25 }26 }27 });28 imposter.save();29 imposter.start();30});31var mb = require('mountebank');32var imposter = mb.create({port: 2525, name: 'imposter1'});33imposter.addStub({34 {35 is: {36 }37 }38});39imposter.save();40imposter.start();41var mb = require('mountebank');42var imposter = mb.create({port: 2525, name: 'imposter1'});43imposter.addStub({44 {45 is: {46 }47 }48});49imposter.save();50imposter.start();51mb.create({port: 2525, name: 'imposter1'}).then(function (imposter) {52 imposter.addStub({53 {54 is: {55 }56 }57 });58 imposter.save();59 imposter.start();60});
Using AI Code Generation
1var mb = require('mountebank');2var imposter = mb.createImposter(2525, 'http');3imposter.addStub({4 { is: { body: 'Hello from mountebank!' } }5}).then(function () {6 imposter.start();7});8var mb = require('mountebank');9mb.createImposter(2525, 'http', {10 { responses: [{ is: { body: 'Hello from mountebank!' } }] }11}).then(function (imposter) {12 imposter.start();13});
Using AI Code Generation
1var mb = require('mountebank');2var fs = require('fs');3var imposter = fs.readFileSync('imposter.json', 'utf8');4var imposterJson = JSON.parse(imposter);5var options = {6};7mb.create(options, function (error, imposter) {8 console.log(imposter.port);9});10{11 {12 {13 "is": {14 "headers": {15 },16 "body": {17 }18 }19 }20 }21}
Using AI Code Generation
1var mb = require('mountebank');2var imposter = mb.create();3imposter.get('/test', function (request, response) {4 response.send({key: 'value'});5});6imposter.start();7var mb = require('mountebank');8var imposter = mb.create();9imposter.get('/test', function (request, response) {10 response.send({key: 'value'});11});12imposter.start();13var mb = require('mountebank');14var imposter = mb.create();15imposter.get('/test', function (request, response) {16 response.send({key: 'value'});17});18imposter.start();19var mb = require('mountebank');20var imposter = mb.create();21imposter.get('/test', function (request, response) {22 response.send({key: 'value'});23});24imposter.start();25var mb = require('mountebank');26var imposter = mb.create();27imposter.get('/test', function (request, response) {28 response.send({key: 'value'});29});30imposter.start();31var mb = require('mountebank');32var imposter = mb.create();33imposter.get('/test', function (request, response) {34 response.send({key: 'value'});35});36imposter.start();37var mb = require('mountebank');38var imposter = mb.create();
Using AI Code Generation
1var mb = require('mountebank');2var options = {3};4mb.create(options).then(function (imposter) {5 imposter.addStub({6 {7 is: {8 }9 }10 });11 imposter.save();12});13{ [Error: connect ECONNREFUSED
Using AI Code Generation
1const mb = require('mountebank')2const path = require('path')3const fs = require('fs')4 {5 {6 equals: {7 }8 }9 {10 is: {11 headers: { 'Content-Type': 'application/json' },12 body: { test: 'test' }13 }14 }15 }16const options = {17}18mb.create(options)19 .then(() => {20 console.log('Imposter created')21 return mb.get(port)22 })23 .then(imposter => {24 console.log('Imposter retrieved')25 return mb.put(port, imposter)26 })27 .then(() => {28 console.log('Imposter updated')29 return mb.del(port)30 })31 .then(() => {32 console.log('Imposter deleted')33 return mb.stop()34 })35 .then(() => {36 console.log('Mountebank stopped')37 })38const mb = require('mountebank')39const path = require('path')40const fs = require('fs')41 {42 {43 equals: {44 }45 }46 {47 is: {48 headers: { 'Content-Type': 'application/json' },49 body: { test: 'test' }50 }51 }52 }53const options = {54}55mb.create(options)56 .then(() => {57 console.log('Imposter created')58 return mb.get(port)59 })60 .then(imposter => {61 console.log('Imposter retrieved')62 return mb.put(port, imposter)63 })64 .then(() => {65 console.log('Imposter updated')66 return mb.del(port)
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!!