Best JavaScript code snippet using mountebank
httpStubTest.js
Source:httpStubTest.js
1'use strict';2const assert = require('assert'),3 api = require('../../api').create(),4 BaseHttpClient = require('../../baseHttpClient'),5 port = api.port + 1,6 timeout = parseInt(process.env.MB_SLOW_TEST_TIMEOUT || 2000);7function merge (defaults, overrides) {8 const result = JSON.parse(JSON.stringify(defaults));9 Object.keys(overrides).forEach(key => {10 if (typeof overrides[key] === 'object' && overrides[key] !== null) {11 result[key] = merge(result[key] || {}, overrides[key]);12 }13 else {14 result[key] = overrides[key];15 }16 });17 return result;18}19['http', 'https'].forEach(protocol => {20 const client = BaseHttpClient.create(protocol);21 describe(`${protocol} imposter`, function () {22 this.timeout(timeout);23 afterEach(async function () {24 await api.del('/imposters');25 });26 describe('POST /imposters with stubs', function () {27 it('should return stubbed response', async function () {28 const stub = {29 responses: [{30 is: {31 statusCode: 400,32 headers: { 'X-Test': 'test header' },33 body: 'test body',34 query: {35 key: true36 }37 }38 }]39 },40 request = { protocol, port, stubs: [stub] };41 await api.createImposter(request);42 const response = await client.get('/test?key=true', port);43 assert.strictEqual(response.statusCode, 400);44 assert.strictEqual(response.body, 'test body');45 assert.strictEqual(response.headers['x-test'], 'test header');46 });47 it('should allow a sequence of stubs as a circular buffer', async function () {48 const stub = { responses: [{ is: { statusCode: 400 } }, { is: { statusCode: 405 } }] },49 request = { protocol, port, stubs: [stub] };50 await api.createImposter(request);51 const first = await client.get('/test', port);52 assert.strictEqual(first.statusCode, 400);53 const second = await client.get('/test', port);54 assert.strictEqual(second.statusCode, 405);55 const third = await client.get('/test', port);56 assert.strictEqual(third.statusCode, 400);57 const fourth = await client.get('/test', port);58 assert.strictEqual(fourth.statusCode, 405);59 });60 it('should only return stubbed response if matches complex predicate', async function () {61 const spec = {62 path: '/test?key=value&next=true',63 port,64 method: 'POST',65 headers: {66 'X-One': 'Test',67 'X-Two': 'Test',68 'Content-Type': 'text/plain'69 }70 },71 stub = {72 responses: [{ is: { statusCode: 400 } }],73 predicates: [74 { equals: { path: '/test', method: 'POST' } },75 { equals: { query: { key: 'value' } } },76 { exists: { headers: { 'X-One': true } } },77 { exists: { headers: { 'X-Two': true } } },78 { equals: { headers: { 'X-Two': 'Test' } } },79 { exists: { headers: { 'X-Three': false } } },80 { not: { exists: { headers: { 'X-Four': true } } } },81 { startsWith: { body: 'T' } },82 { contains: { body: 'ES' } },83 { endsWith: { body: 'T' } },84 { matches: { body: '^TEST$' } },85 { equals: { body: 'TEST' } },86 { exists: { body: true } }87 ]88 },89 request = { protocol, port, stubs: [stub] };90 await api.createImposter(request);91 const first = await client.responseFor(merge(spec, { path: '/', body: 'TEST' }));92 assert.strictEqual(first.statusCode, 200, 'should not have matched; wrong path');93 const second = await client.responseFor(merge(spec, { path: '/test?key=different', body: 'TEST' }));94 assert.strictEqual(second.statusCode, 200, 'should not have matched; wrong query');95 const third = await client.responseFor(merge(spec, { method: 'PUT', body: 'TEST' }));96 assert.strictEqual(third.statusCode, 200, 'should not have matched; wrong method');97 const missingHeaderOptions = merge(spec, { body: 'TEST' });98 delete missingHeaderOptions.headers['X-One'];99 const fourth = await client.responseFor(missingHeaderOptions);100 assert.strictEqual(fourth.statusCode, 200, 'should not have matched; missing header');101 const fifth = await client.responseFor(merge(spec, { headers: { 'X-Two': 'Testing', body: 'TEST' } }));102 assert.strictEqual(fifth.statusCode, 200, 'should not have matched; wrong value for header');103 const sixth = await client.responseFor(merge(spec, { body: 'TESTing' }));104 assert.strictEqual(sixth.statusCode, 200, 'should not have matched; wrong value for body');105 const seventh = await client.responseFor(merge(spec, { body: 'TEST' }));106 assert.strictEqual(seventh.statusCode, 400, 'should have matched');107 });108 it('should correctly handle deepEquals object predicates', async function () {109 const stubWithEmptyObjectPredicate = {110 responses: [{ is: { body: 'first stub' } }],111 predicates: [{ deepEquals: { query: {} } }]112 },113 stubWithPredicateKeywordInObject = {114 responses: [{ is: { body: 'second stub' } }],115 predicates: [{ deepEquals: { query: { equals: 1 } } }]116 },117 stubWithTwoKeywordsInObject = {118 responses: [{ is: { body: 'third stub' } }],119 predicates: [{ deepEquals: { query: { equals: 'true', contains: false } } }]120 },121 stubs = [stubWithEmptyObjectPredicate, stubWithPredicateKeywordInObject, stubWithTwoKeywordsInObject],122 request = { protocol, port, stubs: stubs };123 await api.createImposter(request);124 const first = await client.get('/', port);125 assert.strictEqual(first.body, 'first stub');126 const second = await client.get('/?equals=something', port);127 assert.strictEqual(second.body, '');128 const third = await client.get('/?equals=1', port);129 assert.strictEqual(third.body, 'second stub');130 const fourth = await client.get('/?contains=false&equals=true', port);131 assert.strictEqual(fourth.body, 'third stub');132 const fifth = await client.get('/?contains=false&equals=true&matches=yes', port);133 assert.strictEqual(fifth.body, '');134 });135 it('should support sending binary response', async function () {136 const buffer = Buffer.from([0, 1, 2, 3]),137 stub = { responses: [{ is: { body: buffer.toString('base64'), _mode: 'binary' } }] },138 request = { protocol, port, stubs: [stub] };139 await api.createImposter(request);140 const response = await client.responseFor({ method: 'GET', port, path: '/', mode: 'binary' });141 assert.deepEqual(response.body.toJSON().data, [0, 1, 2, 3]);142 });143 it('should support JSON bodies', async function () {144 const stub = {145 responses: [146 {147 is: {148 body: {149 key: 'value',150 sub: {151 'string-key': 'value'152 },153 arr: [1, 2]154 }155 }156 },157 {158 is: {159 body: {160 key: 'second request'161 }162 }163 }164 ]165 },166 request = { protocol, port, stubs: [stub] };167 await api.createImposter(request);168 const first = await client.get('/', port);169 assert.deepEqual(JSON.parse(first.body), {170 key: 'value',171 sub: {172 'string-key': 'value'173 },174 arr: [1, 2]175 });176 const second = await client.get('/', port);177 assert.deepEqual(JSON.parse(second.body), { key: 'second request' });178 });179 it('should support treating the body as a JSON object for predicate matching', async function () {180 const stub = {181 responses: [{ is: { body: 'SUCCESS' } }],182 predicates: [183 { equals: { body: { key: 'value' } } },184 { equals: { body: { arr: 3 } } },185 { deepEquals: { body: { key: 'value', arr: [2, 1, 3] } } },186 { matches: { body: { key: '^v' } } }187 ]188 },189 request = { protocol, port, stubs: [stub] };190 await api.createImposter(request);191 const response = await client.post('/', '{"key": "value", "arr": [3,2,1]}', port);192 assert.strictEqual(response.body, 'SUCCESS');193 });194 it('should support changing default response for stub', async function () {195 const stub = {196 responses: [197 { is: { body: 'Wrong address' } },198 { is: { statusCode: 500 } }199 ],200 predicates: [{ equals: { path: '/' } }]201 },202 defaultResponse = { statusCode: 404, body: 'Not found' },203 request = { protocol, port, defaultResponse: defaultResponse, stubs: [stub] };204 await api.createImposter(request);205 const first = await client.get('/', port);206 assert.strictEqual(first.statusCode, 404);207 assert.strictEqual(first.body, 'Wrong address');208 const second = await client.get('/', port);209 assert.strictEqual(second.statusCode, 500);210 assert.strictEqual(second.body, 'Not found');211 const third = await client.get('/differentStub', port);212 assert.strictEqual(third.statusCode, 404);213 assert.strictEqual(third.body, 'Not found');214 const imposter = await api.get(`/imposters/${port}`);215 assert.deepEqual(imposter.body.defaultResponse, defaultResponse);216 });217 it('should support keepalive connections', async function () {218 const stub = { responses: [{ is: { body: 'Success' } }] },219 defaultResponse = { headers: { CONNECTION: 'Keep-Alive' } }, // tests case-sensitivity of header match220 request = { protocol, port, defaultResponse: defaultResponse, stubs: [stub] };221 await api.createImposter(request);222 const response = await client.get('/', port);223 assert.strictEqual(response.body, 'Success');224 assert.strictEqual(response.headers.connection, 'Keep-Alive');225 });226 it('should support sending multiple values back for same header', async function () {227 const stub = { responses: [{ is: { headers: { 'Set-Cookie': ['first', 'second'] } } }] },228 request = { protocol, port, stubs: [stub] };229 await api.createImposter(request);230 const response = await client.get('/', port);231 assert.deepEqual(response.headers['set-cookie'], ['first', 'second']);232 });233 it('should support sending JSON bodies with _links field for canned responses', async function () {234 const stub = { responses: [{ is: {235 headers: { 'Content-Type': 'application/json' },236 body: { _links: { self: { href: '/products/123' } } }237 } }] },238 request = { protocol, port, stubs: [stub] };239 await api.createImposter(request);240 const response = await client.get('/', port);241 assert.deepEqual(response.body, { _links: { self: { href: '/products/123' } } });242 });243 it('should correctly set content-length for binary data', async function () {244 // https://github.com/bbyars/mountebank/issues/204245 const stub = {246 responses: [{247 is: {248 headers: { 'Content-Length': 852 },249 body: 'H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/In7xR8Xso0cfzab3p/vn053t/NPZbHt/cn5/++D+5N72pwefTnd2JtP8/CD7aPRR02btuqH2zXo6zZuGPpplbfbRo1/80arMlviZXWZFmU2Ksmiv8XdbLPIfVMucXsqb9vfPZy29VC3LAh/94o8WFb91XlcLarFz/9HODn3fVvTH3h7++CX015qbbmxzldMwbmjTztc3tjmvixvbNFnrt3mIj02bXfyBNutgXJE2v4RagWi//zRftnVWonlZXVALmpNFdpH//uuaPvxo3rar5tHdu9NFsz3LL8fL7JJIOivejafV4m5z3bT54u55UebN3d27/GIzXi0vqDei8VsPCMEI3gWadd5U65qm8qNH3/vFHy2zBVH6o5d1dVnM8jp9WtT5tK3qawLWg7PM2zH9n6C4F+dZvcim1+//YlUW0yJv0l+YUufTfLYmxG757vG6nVd18YOsLapl+rxowGC3efFZVS/WZXZ7JA1ZXuXneZ0vpzmh+0oJmH6+pu9ui/MJTU0xzUqM9qLOFrd9z9I1rc7Tb+dZ2c4BgtFq0mKZvsiv0t+nqt9uhPd9YnMa+8Cc5wugtJoX0/RsiXZC2Ff5L1qTAKeg2kboDtuTqqpnxVLeJ4Sf5Mv8vGib94JRZsXivd54WefbJ3ndFudEYe76xpeJINNqdV0XF3OSbPd72rR1sbxIdyGtv+T/AdOWKsArBQAA',250 _mode: 'binary'251 }252 }]253 },254 request = { protocol, port, stubs: [stub] };255 await api.createImposter(request);256 const response = await client.get('/', port);257 assert.deepEqual(response.headers['content-length'], 639);258 });259 it('should correctly set content-length for binary data when using multiline base64', async function () {260 const stub = {261 responses: [{262 is: {263 headers: { 'Content-Length': 274 },264 body: 'iVBORw0KGgoAAAANSUhEUgAAAGQAAAAyBAMAAABYG2ONAAAAFVBMVEUAAAD///9/f39fX1+fn58f\nHx8/Pz8rYiDqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAo0lEQVRIie2Qyw7CIBBFb2DwO5q+1o0L\n1w0NrrHRPQnV//8EAUl0ga1ujIs5CZAz4YYZAIZhmN/QQOkjzq3LLuv6xUrQHmTJGphcEGE9rUQ3\nY4bqqrDjAlgQoJK9Z8YBmFy8Gp8DeSeTfRSBCf2I6/JN5ORiRfrNiIfqh9S9SVPL27A1C0G4EX2e\nJR7J1iI7rbG0Vf4x0UwPW0Uh3i0bwzD/yR11mBj1DIKiVwAAAABJRU5ErkJggg==\n',265 _mode: 'binary'266 }267 }]268 },269 request = { protocol, port, stubs: [stub] };270 await api.createImposter(request);271 const response = await client.get('/', port);272 assert.deepEqual(response.headers['content-length'], 274);273 });274 it('should handle JSON null values', async function () {275 // https://github.com/bbyars/mountebank/issues/209276 const stub = { responses: [{ is: { body: { name: 'test', type: null } } }] },277 request = { protocol, port, stubs: [stub] };278 await api.createImposter(request);279 const response = await client.get('/', port);280 assert.deepEqual(JSON.parse(response.body), { name: 'test', type: null });281 });282 it('should handle null values in deepEquals predicate (issue #229)', async function () {283 const stub = {284 predicates: [{ deepEquals: { body: { field: null } } }],285 responses: [{ is: { body: 'SUCCESS' } }]286 },287 request = { protocol, port, stubs: [stub] };288 await api.createImposter(request);289 const response = await client.post('/', { field: null }, port);290 assert.strictEqual(response.body, 'SUCCESS');291 });292 it('should support array predicates with xpath', async function () {293 const stub = {294 responses: [{ is: { body: 'SUCCESS' } }],295 predicates: [{296 equals: { body: ['first', 'third', 'second'] },297 xpath: { selector: '//value' }298 }]299 },300 xml = '<values><value>first</value><value>second</value><value>third</value></values>',301 request = { protocol, port, stubs: [stub] };302 await api.createImposter(request);303 const response = await client.post('/', xml, port);304 assert.strictEqual(response.body, 'SUCCESS');305 });306 it('should support matches predicate on uppercase JSON key (issue #228)', async function () {307 const stub = {308 predicates: [{ matches: { body: { Key: '^Value' } } }],309 responses: [{ is: { body: 'SUCCESS' } }]310 },311 request = { protocol, port, stubs: [stub] };312 await api.createImposter(request);313 const response = await client.post('/', { Key: 'Value' }, port);314 assert.strictEqual(response.body, 'SUCCESS');315 });316 it('should support predicate matching with null value (issue #262)', async function () {317 const stub = {318 predicates: [{ equals: { body: { version: null } } }],319 responses: [{ is: { body: 'SUCCESS' } }]320 },321 request = { protocol, port, stubs: [stub] };322 await api.createImposter(request);323 const response = await client.post('/', { version: null }, port);324 assert.strictEqual(response.body, 'SUCCESS');325 });326 it('should support predicate form matching', async function () {327 const spec = {328 path: '/',329 port,330 method: 'POST',331 headers: {332 'Content-Type': 'application/x-www-form-urlencoded'333 },334 body: 'firstname=ruud&lastname=mountebank'335 },336 stub = {337 predicates: [{ deepEquals: { form: { firstname: 'ruud', lastname: 'mountebank' } } }],338 responses: [{ is: { body: 'SUCCESS' } }]339 },340 request = { protocol, port, stubs: [stub] };341 await api.createImposter(request);342 const response = await client.responseFor(spec);343 assert.strictEqual(response.body, 'SUCCESS');344 });345 it('should support predicate from gzipped request (issue #499)', async function () {346 const zlib = require('zlib'),347 spec = {348 path: '/',349 port,350 method: 'POST',351 headers: {352 'Content-Encoding': 'gzip'353 },354 mode: 'binary',355 body: zlib.gzipSync('{"key": "value", "arr": [3,2,1]}')356 },357 stub = {358 responses: [{ is: { body: 'SUCCESS' } }],359 predicates: [360 { equals: { body: { key: 'value' } } },361 { equals: { body: { arr: 3 } } },362 { deepEquals: { body: { key: 'value', arr: [2, 1, 3] } } },363 { matches: { body: { key: '^v' } } }364 ]365 },366 request = { protocol, port, stubs: [stub] };367 await api.createImposter(request);368 const response = await client.responseFor(spec);369 assert.strictEqual(response.body.toString(), 'SUCCESS');370 });371 it('should support overwriting the stubs without restarting the imposter', async function () {372 const stub = { responses: [{ is: { body: 'ORIGINAL' } }] },373 request = { protocol, port, stubs: [stub] };374 await api.createImposter(request);375 const putResponse = await api.put(`/imposters/${port}/stubs`, {376 stubs: [377 { responses: [{ is: { body: 'FIRST' } }] },378 { responses: [{ is: { body: 'ORIGINAL' } }] },379 { responses: [{ is: { body: 'THIRD' } }] }380 ]381 });382 assert.strictEqual(putResponse.statusCode, 200);383 assert.deepEqual(putResponse.body.stubs, [384 {385 responses: [{ is: { body: 'FIRST' } }],386 _links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }387 },388 {389 responses: [{ is: { body: 'ORIGINAL' } }],390 _links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }391 },392 {393 responses: [{ is: { body: 'THIRD' } }],394 _links: { self: { href: `${api.url}/imposters/${port}/stubs/2` } }395 }396 ]);397 const getResponse = await client.get('/', port);398 assert.strictEqual(getResponse.body, 'FIRST');399 });400 it('should support overwriting a single stub without restarting the imposter', async function () {401 const request = {402 protocol,403 port,404 stubs: [405 { responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }] },406 { responses: [{ is: { body: 'SECOND' } }] },407 { responses: [{ is: { body: 'third' } }] }408 ]409 },410 changedStub = { responses: [{ is: { body: 'CHANGED' } }] };411 await api.createImposter(request);412 const putResponse = await api.put(`/imposters/${port}/stubs/1`, changedStub);413 assert.strictEqual(putResponse.statusCode, 200, JSON.stringify(putResponse.body));414 assert.deepEqual(putResponse.body.stubs, [415 {416 responses: [{ is: { body: 'first' } }],417 predicates: [{ equals: { path: '/first' } }],418 _links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }419 },420 {421 responses: [{ is: { body: 'CHANGED' } }],422 _links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }423 },424 {425 responses: [{ is: { body: 'third' } }],426 _links: { self: { href: `${api.url}/imposters/${port}/stubs/2` } }427 }428 ]);429 const getResponse = await client.get('/', port);430 assert.strictEqual(getResponse.body, 'CHANGED');431 });432 it('should support deleting single stub without restarting the imposter', async function () {433 const request = {434 protocol,435 port,436 stubs: [437 { responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }] },438 { responses: [{ is: { body: 'SECOND' } }] },439 { responses: [{ is: { body: 'third' } }] }440 ]441 };442 await api.createImposter(request);443 const deleteResponse = await api.del(`/imposters/${port}/stubs/1`);444 assert.strictEqual(deleteResponse.statusCode, 200);445 assert.deepEqual(deleteResponse.body.stubs, [446 {447 responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }],448 _links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }449 },450 {451 responses: [{ is: { body: 'third' } }],452 _links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }453 }454 ]);455 const getResponse = await client.get('/', port);456 assert.strictEqual(getResponse.body, 'third');457 });458 it('should support adding single stub without restarting the imposter', async function () {459 const request = {460 protocol,461 port,462 stubs: [463 { responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }] },464 { responses: [{ is: { body: 'third' } }] }465 ]466 },467 newStub = { responses: [{ is: { body: 'SECOND' } }] };468 await api.createImposter(request);469 const postResponse = await api.post(`/imposters/${port}/stubs`, { index: 1, stub: newStub });470 assert.strictEqual(postResponse.statusCode, 200);471 assert.deepEqual(postResponse.body.stubs, [472 {473 responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }],474 _links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }475 },476 {477 responses: [{ is: { body: 'SECOND' } }],478 _links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }479 },480 {481 responses: [{ is: { body: 'third' } }],482 _links: { self: { href: `${api.url}/imposters/${port}/stubs/2` } }483 }484 ]);485 const getResponse = await client.get('/', port);486 assert.strictEqual(getResponse.body, 'SECOND');487 });488 it('should provide a good error message when adding stub with missing information', async function () {489 const request = {490 protocol,491 port,492 stubs: [{ responses: [{ is: { body: 'first' } }] }]493 },494 newStub = { responses: [{ is: { body: 'SECOND' } }] },495 errorBody = { index: 1, STUBS: newStub };496 await api.createImposter(request);497 const postResponse = await api.post(`/imposters/${port}/stubs`, errorBody);498 assert.strictEqual(postResponse.statusCode, 400);499 assert.deepStrictEqual(postResponse.body.errors,500 [{ code: 'bad data', message: "must contain 'stub' field" }]);501 });502 it('should support adding single stub at end without index ', async function () {503 const request = {504 protocol,505 port,506 stubs: [507 { responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }] },508 { responses: [{ is: { body: 'third' } }] }509 ]510 },511 newStub = { responses: [{ is: { body: 'LAST' } }] };512 await api.createImposter(request);513 const postResponse = await api.post(`/imposters/${port}/stubs`, { stub: newStub });514 assert.strictEqual(postResponse.statusCode, 200);515 assert.deepEqual(postResponse.body.stubs, [516 {517 responses: [{ is: { body: 'first' } }], predicates: [{ equals: { path: '/first' } }],518 _links: { self: { href: `${api.url}/imposters/${port}/stubs/0` } }519 },520 {521 responses: [{ is: { body: 'third' } }],522 _links: { self: { href: `${api.url}/imposters/${port}/stubs/1` } }523 },524 {525 responses: [{ is: { body: 'LAST' } }],526 _links: { self: { href: `${api.url}/imposters/${port}/stubs/2` } }527 }528 ]);529 const getResponse = await client.get('/', port);530 assert.strictEqual(getResponse.body, 'third');531 });532 it('should support matching cirillic characters (issue #477)', async function () {533 const request = {534 port,535 protocol,536 stubs: [{537 predicates: [{ deepEquals: { body: { ÑеÑÑ: '2' } } }],538 responses: [{ is: { body: 'Matched' } }]539 }]540 };541 await api.createImposter(request);542 const response = await client.post('/', '{ "ÑеÑÑ": "2" }', port);543 assert.strictEqual(response.body, 'Matched');544 });545 });546 });...
Using AI Code Generation
1var mb = require('mountebank');2var assert = require('assert');3mb.create({4}, function(err) {5 assert.ifError(err);6 console.log('mountebank started');7 mb.get('/imposters', function(err, res) {8 assert.ifError(err);9 console.log(res.body);10 });11});12{13 "scripts": {14 },15 "dependencies": {16 }17}18{19 {20 {21 "is": {22 "headers": {23 },24 }25 }26 }27}28info: [mb:2525] ::1:52141 responded with status 200 (0ms)29info: [mb:2525] ::1:52142 responded with status 200 (0ms)30info: [mb:2525] ::1:52143 responded with status 200 (0ms)31info: [mb:2525] ::1:52144 responded with status 200 (0ms)
Using AI Code Generation
1const mb = require('mountebank');2mb.start({3}).then(() => {4 return mb.post('/imposters', {5 stubs: [{6 responses: [{7 is: {8 headers: {9 }10 }11 }]12 }]13 });14}).then(() => {15 return mb.get('/imposters/3000');16}).then(response => {17 console.log(response.body);18 return mb.del('/imposters/3000');19}).then(() => {20 return mb.stop();21}).then(() => {22 console.log('All done');23}).catch(error => {24 console.error(error);25});26const mb = require('mountebank');27mb.start({28}).then(() => {29 return mb.post('/imposters', {30 stubs: [{31 responses: [{32 is: {33 headers: {34 }35 }36 }]37 }]38 });39}).then(() => {40 return mb.get('/imposters/3000');41}).then(response => {42 console.log(response.body);43 return mb.del('/imposters/3000');44}).then(() => {45 return mb.stop();46}).then(() => {47 console.log('All done');48}).catch(error => {49 console.error(error);50});51const mb = require('mountebank');52mb.start({53}).then(() => {54 return mb.post('/imposters', {
Using AI Code Generation
1const mb = require('mountebank');2const mbHelper = require('mountebank-helper');3const imposter = mbHelper.imposter;4const stub = mbHelper.stub;5const predicate = mbHelper.predicate;6const response = mbHelper.response;7const port = 4545;8const protocol = 'http';9const path = '/test';10const method = 'POST';11const headers = {12};13const body = {14};15const predicateObj = predicate.equals({ method, path, headers, body });16const responseObj = response.with({ statusCode: 200 });17const stubObj = stub.with({ predicates: [predicateObj], responses: [responseObj] });18const imposterObj = imposter.with({ protocol, port, stubs: [stubObj] });19mb.createImposter(imposterObj).then(() => {20 return mb.getImposters();21}).then((imposters) => {22 console.log(imposters);23}).catch((error) => {24 console.log(error);25});
Using AI Code Generation
1var mb = require('mountebank');2mb.create({ port: 2525, pidfile: 'mb.pid' }, function (error, mb) {3 if (error) {4 console.log(error);5 }6 else {7 console.log('Mountebank started');8 mb.post('/imposters', {9 {10 {11 is: {12 headers: {13 },14 body: JSON.stringify({ message: 'Hello, world!' })15 }16 }17 }18 }, function (error, response) {19 if (error) {20 console.log(error);21 }22 else {23 console.log('Imposter created');24 }25 });26 }27});28var mb = require('mountebank');29mb.create({ port: 2525, pidfile: 'mb.pid' }, function (error, mb) {30 if (error) {31 console.log(error);32 }33 else {34 console.log('Mountebank started');35 mb.post('/imposters', {36 {37 {38 is: {39 headers: {40 },41 body: JSON.stringify({ message: 'Hello, world!' })42 }43 }44 }45 }, function (error, response) {46 if (error) {47 console.log(error);48 }49 else {50 console.log('Imposter created');51 }52 });53 }54});55var mb = require('mountebank');56mb.create({ port: 2525, pidfile: 'mb.pid' }, function (error, mb) {57 if (error) {58 console.log(error);59 }60 else {61 console.log('Mountebank started');62 mb.post('/imposters', {63 {64 {65 is: {66 headers: {
Using AI Code Generation
1const mb = require('mountebank');2const mbHelper = require('mountebank-helper');3const mbPort = 2525;4const mbServer = mb.create({5});6mbServer.then(() => {7 console.log('mb server started');8 const options = {9 {10 {11 is: {12 headers: {13 },14 body: JSON.stringify({15 })16 }17 }18 }19 };20 const mbHelperInstance = new mbHelper(mbUrl);21 mbHelperInstance.missingHeaderOptions(options, 'Content-Type').then((response) => {22 console.log('response', response);23 return mbHelperInstance.createImposter(3000, options);24 }).then((response) => {25 console.log('response', response);26 }).catch((error) => {27 console.log('error', error);28 });29}).catch((error) => {30 console.log('error', error);31});32{33 "scripts": {34 },35 "dependencies": {36 }37}
Using AI Code Generation
1var mb = require('mountebank');2var port = 2525;3var options = {4};5mb.create(options)6.then(function (imposter) {7 imposter.post('/test', function (request, response) {8 response.statusCode = 200;9 response.send('test');10 });11 console.log('imposter is ready');12})13.catch(function (error) {14 console.error('error creating imposter', error);15});16var mb = require('mountebank');17var port = 2525;18var options = {19};20mb.create(options)21.then(function (imposter) {22 imposter.post('/test', function (request, response) {23 response.statusCode = 200;24 response.send('test');25 });26 console.log('imposter is ready');27})28.catch(function (error) {29 console.error('error creating imposter', error);30});31var mb = require('mountebank');32var port = 2525;33var options = {34};35mb.create(options)36.then(function (imposter) {37 imposter.post('/test', function (request, response) {38 response.statusCode = 200;39 response.send('test');40 });41 console.log('imposter is ready');42})43.catch(function (error) {44 console.error('error creating imposter', error);45});46var mb = require('mountebank');47var port = 2525;48var options = {49};50mb.create(options)51.then(function (imposter) {52 imposter.post('/test', function (request, response) {53 response.statusCode = 200;54 response.send('test');55 });56 console.log('imposter is ready');57})58.catch(function (error) {59 console.error('error creating imposter', error);60});61var mb = require('mountebank');62var port = 2525;63var options = {64};65mb.create(options)66.then(function (imposter) {
Using AI Code Generation
1var mb = require('mountebank');2var mbHelper = require('mountebank-helper');3var mbInstance = mbHelper.create({port: 2525});4mbInstance.addImposter({port: 2525, protocol: 'http', stubs: [{responses: [{is: {body: 'hello world'}}]}]});5mbInstance.missingHeaderOptions('Content-Type').then(function (options) {6 console.log(options);7});
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!!