Best JavaScript code snippet using mountebank
predicates.js
Source:predicates.js
...29 if (Array.isArray(obj[key])) {30 result[key] = obj[key].map(forceStrings);31 }32 else if (typeof obj[key] === 'object') {33 result[key] = forceStrings(obj[key]);34 }35 else if (['boolean', 'number'].indexOf(typeof obj[key]) >= 0) {36 result[key] = obj[key].toString();37 }38 else {39 result[key] = obj[key];40 }41 return result;42 }, {});43 }44}45function select (type, selectFn, encoding) {46 if (encoding === 'base64') {47 var errors = require('../util/errors');48 throw errors.ValidationError('the ' + type + ' predicate parameter is not allowed in binary mode');49 }50 var nodeValues = selectFn();51 // Return either a string if one match or array if multiple52 // This matches the behavior of node's handling of query parameters,53 // which allows us to maintain the same semantics between deepEquals54 // (all have to match, passing in an array if necessary) and the other55 // predicates (any can match)56 if (nodeValues && nodeValues.length === 1) {57 return nodeValues[0];58 }59 else {60 return nodeValues;61 }62}63function orderIndependent (possibleArray) {64 var util = require('util');65 if (util.isArray(possibleArray)) {66 return possibleArray.sort();67 }68 else {69 return possibleArray;70 }71}72function selectXPath (config, caseTransform, encoding, text) {73 var xpath = require('./xpath'),74 combinators = require('../util/combinators'),75 ns = normalize(config.ns, {}, 'utf8'),76 selectFn = combinators.curry(xpath.select, caseTransform(config.selector), ns, text);77 return orderIndependent(select('xpath', selectFn, encoding));78}79function selectJSONPath (config, caseTransform, encoding, text) {80 var jsonpath = require('./jsonpath'),81 combinators = require('../util/combinators'),82 selectFn = combinators.curry(jsonpath.select, caseTransform(config.selector), text);83 return orderIndependent(select('jsonpath', selectFn, encoding));84}85function normalize (obj, config, encoding, withSelectors) {86 /* eslint complexity: [2, 8] */87 var combinators = require('../util/combinators'),88 lowerCaser = function (text) { return text.toLowerCase(); },89 caseTransform = config.caseSensitive ? combinators.identity : lowerCaser,90 keyCaseTransform = config.keyCaseSensitive === false ? lowerCaser : caseTransform,91 exceptRegexOptions = config.caseSensitive ? 'g' : 'gi',92 exceptionRemover = function (text) { return text.replace(new RegExp(config.except, exceptRegexOptions), ''); },93 exceptTransform = config.except ? exceptionRemover : combinators.identity,94 encoder = function (text) { return new Buffer(text, 'base64').toString(); },95 encodeTransform = encoding === 'base64' ? encoder : combinators.identity,96 xpathSelector = combinators.curry(selectXPath, config.xpath, caseTransform, encoding),97 xpathTransform = withSelectors && config.xpath ? xpathSelector : combinators.identity,98 jsonPathSelector = combinators.curry(selectJSONPath, config.jsonpath, caseTransform, encoding),99 jsonPathTransform = withSelectors && config.jsonpath ? jsonPathSelector : combinators.identity,100 transform = combinators.compose(jsonPathTransform, xpathTransform, exceptTransform, caseTransform, encodeTransform),101 transformAll = function (o) {102 if (!o) {103 return o;104 }105 if (Array.isArray(o)) {106 // sort to provide deterministic comparison for deepEquals,107 // where the order in the array for multi-valued querystring keys108 // and xpath selections isn't important109 return o.map(transformAll).sort(sortObjects);110 }111 else if (typeof o === 'object') {112 return Object.keys(o).reduce(function (result, key) {113 result[keyCaseTransform(key)] = transformAll(o[key]);114 return result;115 }, {});116 }117 else if (typeof o === 'string') {118 return transform(o);119 }120 return o;121 };122 return transformAll(obj);123}124function tryJSON (value) {125 try {126 return JSON.parse(value);127 }128 catch (e) {129 return value;130 }131}132function predicateSatisfied (expected, actual, predicate) {133 if (!actual) {134 return false;135 }136 return Object.keys(expected).every(function (fieldName) {137 var helpers = require('../util/helpers');138 var test = function (value) {139 if (!helpers.defined(value)) {140 value = '';141 }142 if (typeof expected[fieldName] === 'object') {143 return predicateSatisfied(expected[fieldName], value, predicate);144 }145 else {146 return predicate(expected[fieldName], value);147 }148 };149 // Support predicates that reach into fields encoded in JSON strings (e.g. HTTP bodies)150 if (!helpers.defined(actual[fieldName]) && typeof actual === 'string') {151 actual = tryJSON(actual);152 }153 if (Array.isArray(actual[fieldName])) {154 return actual[fieldName].some(test);155 }156 else if (!helpers.defined(actual[fieldName]) && Array.isArray(actual)) {157 // support array of objects in JSON158 return actual.some(function (element) {159 return predicateSatisfied(expected, element, predicate);160 });161 }162 else if (typeof expected[fieldName] === 'object') {163 return predicateSatisfied(expected[fieldName], actual[fieldName], predicate);164 }165 else {166 return test(actual[fieldName]);167 }168 });169}170function create (operator, predicateFn) {171 return function (predicate, request, encoding) {172 var expected = normalize(predicate[operator], predicate, encoding, false),173 actual = normalize(request, predicate, encoding, true);174 return predicateSatisfied(expected, actual, predicateFn);175 };176}177function deepEquals (predicate, request, encoding) {178 var expected = normalize(forceStrings(predicate.deepEquals), predicate, encoding, false),179 actual = normalize(forceStrings(request), predicate, encoding, true),180 stringify = require('json-stable-stringify');181 return Object.keys(expected).every(function (fieldName) {182 // Support predicates that reach into fields encoded in JSON strings (e.g. HTTP bodies)183 if (typeof expected[fieldName] === 'object' && typeof actual[fieldName] === 'string') {184 actual[fieldName] = normalize(forceStrings(tryJSON(actual[fieldName])), predicate, encoding, false);185 }186 return stringify(expected[fieldName]) === stringify(actual[fieldName]);187 });188}189function matches (predicate, request, encoding) {190 // We want to avoid the lowerCase transform on values so we don't accidentally butcher191 // a regular expression with upper case metacharacters like \W and \S192 // However, we need to maintain the case transform for keys like http header names (issue #169)193 // eslint-disable-next-line no-unneeded-ternary194 var caseSensitive = predicate.caseSensitive ? true : false, // convert to boolean even if undefined195 helpers = require('../util/helpers'),196 clone = helpers.merge(predicate, { caseSensitive: true, keyCaseSensitive: caseSensitive }),197 expected = normalize(predicate.matches, clone, encoding, false),198 actual = normalize(request, clone, encoding, true),...
Using AI Code Generation
1const mb = require('mountebank');2mb.create({3}).then(function(){4 console.log('Imposter created');5}).catch(function(err){6 console.log('Imposter creation failed', err);7});8const imposter = {9 {10 {11 equals: {12 }13 }14 {15 is: {16 headers: {17 },18 body: {19 }20 }21 }22 }23};24mb.post('/imposters', imposter).then(function(response){25 console.log('Imposter posted');26}).catch(function(err){27 console.log('Imposter posting failed', err);28});29mb.get('/imposters').then(function(response){30 console.log('Imposter get');31}).catch(function(err){32 console.log('Imposter get failed', err);33});34mb.del('/imposters/2525').then(function(response){35 console.log('Imposter deleted');36}).catch(function(err){37 console.log('Imposter deletion failed', err);38});39mb.del('/imposters').then(function(response){40 console.log('Imposter deleted');41}).catch(function(err){42 console.log('Imposter deletion failed', err);43});44mb.del('/imposters').then(function(response){45 console.log('Imposter deleted');46}).catch(function(err){47 console.log('Imposter deletion failed', err);48});49mb.delete().then(function(){50 console.log('Imposter deleted');51}).catch(function(err){52 console.log('Imposter deletion failed', err);53});54mb.delete().then(function(){55 console.log('Imposter deleted');56}).catch(function(err){57 console.log('Imposter deletion failed', err);58});
Using AI Code Generation
1var mb = require('mountebank');2var imposter = {3 {4 {5 is: {6 }7 }8 }9};10mb.create(imposter).then(function (imposter) {11 console.log(imposter.port);12 return imposter.get('/', {headers: {'Accept': 'text/html'}});13}).then(function (response) {14 console.log(response.body);15});
Using AI Code Generation
1const mb = require('mountebank');2mb.create({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.post('/imposters', {16 stubs: [{17 responses: [{18 is: {19 headers: {20 }21 }22 }]23 }]24 });25}).then(() => {26 return mb.post('/imposters', {27 stubs: [{28 responses: [{29 is: {30 headers: {31 }32 }33 }]34 }]35 });36}).then(() => {37 return mb.post('/imposters', {38 stubs: [{39 responses: [{40 is: {41 headers: {42 }43 }44 }]45 }]46 });47}).then(() => {48 return mb.forceStrings();49}).then(() => {50 return mb.post('/imposters', {51 stubs: [{52 responses: [{53 is: {54 headers: {55 }56 }57 }]58 }]59 });60}).then(() => {61 return mb.forceStrings(false);62}).then(() => {63 return mb.post('/imposters', {64 stubs: [{65 responses: [{66 is: {67 headers: {
Using AI Code Generation
1var mb = require('mountebank');2mb.create({ port: 2525, allowInjection: true }, function (error, mb) {3 if (error) {4 console.error(error);5 }6 else {7 console.log('mountebank is listening on port 2525');8 }9});10var mb = require('mountebank');11mb.create({ port: 2525, allowInjection: true }, function (error, mb) {12 if (error) {13 console.error(error);14 }15 else {16 console.log('mountebank is listening on port 2525');17 }18});19var mb = require('mountebank');20mb.create({ port: 2525, allowInjection: true }, function (error, mb) {21 if (error) {22 console.error(error);23 }24 else {25 console.log('mountebank is listening on port 2525');26 }27});28var mb = require('mountebank');29mb.create({ port: 2525, allowInjection: true }, function (error, mb) {30 if (error) {31 console.error(error);32 }33 else {34 console.log('mountebank is listening on port 2525');35 }36});37var mb = require('mountebank');38mb.create({ port: 2525, allowInjection: true }, function (error, mb) {39 if (error) {40 console.error(error);41 }42 else {43 console.log('mountebank is listening on port 2525');44 }45});46var mb = require('mountebank');47mb.create({ port: 2525, allowInjection: true }, function (error, mb) {48 if (error) {49 console.error(error);50 }51 else {52 console.log('mountebank is listening on port 2525');53 }54});55var mb = require('mountebank');56mb.create({ port: 2525, allowInjection: true }, function (error, mb) {57 if (error) {58 console.error(error);
Using AI Code Generation
1var mb = require('mountebank');2var imposters = mb.create();3var options = {4 stubs: [{5 responses: [{6 is: {7 headers: {8 }9 }10 }]11 }]12};13imposters.add(options, function (error, result) {14 if (error) {15 console.log(error);16 } else {17 console.log(result);18 }19});20imposters.forceString(function (error, result) {21 if (error) {22 console.log(error);23 } else {24 console.log(result);25 }26});27imposters.stop(function (error, result) {28 if (error) {29 console.log(error);30 } else {31 console.log(result);32 }33});34{ port: 2525, protocol: 'http', stubs: [ { responses: [ [Object] ] } ] }35{ imposters: [ { port: 2525, protocol: 'http', stubs: [ [Object] ] } ] }
Using AI Code Generation
1const mb = require('mountebank');2const imposters = mb.create({port: 2525, pidfile: 'mb.pid', logfile: 'mb.log'});3const imposters = mb.create({port: 2525});4imposters.post('/imposters', {5 stubs: [{6 responses: [{7 is: {8 }9 }]10 }]11});12imposters.get('/imposters/5000');13const mb = require('mountebank');14const imposters = mb.create({port: 2525});15imposters.post('/imposters', {16 stubs: [{17 responses: [{18 is: {19 }20 }]21 }]22});23imposters.get('/imposters/5000');24const mb = require('mountebank');25const imposters = mb.create({port: 2525});26imposters.post('/imposters', {27 stubs: [{28 responses: [{29 is: {30 }31 }]32 }]33});34imposters.get('/imposters/5000');35const mb = require('mountebank');36const imposters = mb.create({port: 2525});37imposters.post('/imposters', {38 stubs: [{39 responses: [{40 is: {
Using AI Code Generation
1const request = require('request');2const mb = require('mountebank');3const port = 2525;4const protocol = 'http';5const host = 'localhost';6const imposterPort = 3000;7const imposterProtocol = 'http';8const imposterHost = 'localhost';9const imposterPath = '/test';10const imposterName = 'test';11const imposter = {12 stubs: [{13 responses: [{14 is: {15 }16 }]17 }]18};19const options = {20};21mb.create(options)22 .then(mb => {23 mb.post('/imposters', imposter)24 .then(() => {25 const forceStringOptions = {26 uri: `${imposterUrl}/forceString`,27 body: {28 }29 };30 request(forceStringOptions, (error, response, body) => {31 if (error) {32 console.log(error);33 }34 else {35 request(imposterUrl, (error, response, body) => {36 if (error) {37 console.log(error);38 }39 else {40 console.log(body);41 }42 });43 }44 });45 });46 });
Using AI Code Generation
1const mb = require('mountebank');2mb.forceStrings();3const stub = {4 { equals: { method: 'GET' } }5 { is: { statusCode: 200, body: 'Hello World' } }6};7mb.create({ port: 2525, allowInjection: true }, () => {8 mb.post('/imposters', { imposters: [{ port: 3000, protocol: 'http', stubs: [stub] }] }, () => {9 console.log('Mock server ready');10 });11});12const request = require('request');13 console.log(body);14});15mb.stop(() => {16 console.log('Mock server stopped');17});
Using AI Code Generation
1const mb = require('mountebank');2const port = 2525;3const imposterPort = 2526;4const imposterProtocol = 'http';5const imposterName = 'testImposter';6const imposterStub = {7 responses: [{8 is: {9 body: {10 }11 }12 }]13};14mb.start({15}, function (error) {16 if (error) {17 console.error(error);18 } else {19 console.log('Mountebank started on port ' + port);20 mb.create({21 }, function (error, imposter) {22 if (error) {23 console.error(error);24 } else {25 console.log('Imposter started on port ' + imposterPort);26 console.log('Imposter name: ' + imposter.name);27 console.log('Imposter protocol: ' + imposter.protocol);28 console.log('Imposter stubs: ' + imposter.stubs);29 mb.forceString(imposterPort, imposterName, imposterProtocol, ['message'], function (error) {30 if (error) {31 console.error(error);32 } else {33 console.log('Imposter forced to return string');34 }35 });36 }37 });38 }39});
Using AI Code Generation
1const mb = require('mountebank');2const port = 2525;3const imposterPort = 3000;4const imposterName = "imposter";5const protocol = "http";6const stub = {7 {8 is: {9 }10 }11};12const imposter = {13};14mb.create(port, imposter)15 .then(() => {16 return mb.get(port, imposterName);17 })18 .then(imposter => {19 console.log(imposter);20 })21 .catch(error => {22 console.error(error);23 });24const mb = require('mountebank');25const port = 2525;26const imposterPort = 3000;27const imposterName = "imposter";28const protocol = "http";29const stub = {30 {31 is: {32 }33 }34};35const imposter = {36};37mb.create(port, imposter)38 .then(() => {39 return mb.get(port, imposterName);40 })41 .then(imposter => {42 console.log(imposter);43 })44 .catch(error => {45 console.error(error);46 });47const mb = require('mountebank');48const port = 2525;49const imposterPort = 3000;50const imposterName = "imposter";51const protocol = "http";52const stub = {53 {54 is: {55 }56 }57};58const imposter = {59};60mb.create(port, imposter)61 .then(() => {62 return mb.get(port, imposterName);63 })64 .then(imposter => {
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!!