Best JavaScript code snippet using mountebank
smtp-server-tests.ts
Source:smtp-server-tests.ts
1import { AddressInfo } from 'net';2import { SMTPServer, SMTPServerAddress, SMTPServerAuthentication, SMTPServerAuthenticationResponse, SMTPServerDataStream, SMTPServerOptions, SMTPServerSession } from 'smtp-server';3import { Readable } from 'stream';4function test_with_handlers_as_options() {5 const options: SMTPServerOptions = {6 hideSTARTTLS: true,7 onConnect,8 onMailFrom,9 onRcptTo,10 onData,11 onClose,12 };13 const port = 2525;14 function onConnect(session: SMTPServerSession, callback: (err?: Error) => void): void {15 console.log(`[${session.id}] onConnect`);16 callback();17 }18 function onAuth(auth: SMTPServerAuthentication, session: SMTPServerSession, callback: (err: Error | undefined, response?: SMTPServerAuthenticationResponse) => void): void {19 if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {20 callback(undefined, { user: auth.username });21 } else {22 callback(new Error('Invalid username or password'));23 }24 }25 function onMailFrom(from: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {26 console.log(`[${session.id}] onMailFrom ${from.address}`);27 if (from.address.split('@')[1] === 'spammer.com') {28 // code 421 disconnects SMTP session immediately29 callback(Object.assign(new Error('we do not like spam!'), { responseCode: 421 }));30 } else {31 callback();32 }33 }34 function onRcptTo(to: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {35 console.log(`[${session.id}] onRcptTo ${to.address}`);36 callback();37 }38 function onData(stream: SMTPServerDataStream, session: SMTPServerSession, callback: (err?: Error) => void): void {39 console.log(`[${session.id}] onData started`);40 if (stream.sizeExceeded) {41 callback(new Error('Message too big'));42 return;43 }44 let messageLength = 0;45 stream.on('data', (chunk: Buffer) => {46 console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`);47 messageLength += chunk.length;48 });49 stream.once('end', () => {50 console.log(`[${session.id}] onData finished after reading ${messageLength} bytes`);51 callback();52 });53 }54 function onClose(session: SMTPServerSession) {55 console.log(`[${session.id}] onClose`);56 }57 const server = new SMTPServer(options);58 server.on('error', (err) => {59 console.log(`Server got error:`, err);60 });61 server.on('close', () => {62 console.log('Server closed');63 });64 server.listen(port, () => {65 const address = server.server.address() as AddressInfo;66 console.log(`Listening on [${address.address}]:${address.port}`);67 });68 server.close(() => {69 console.log('Server closed');70 });71}72function test_with_handlers_in_subclass() {73 class MySMTPServer extends SMTPServer {74 onConnect(session: SMTPServerSession, callback: (err?: Error) => void): void {75 console.log(`[${session.id}] onConnect`);76 callback();77 }78 onAuth(auth: SMTPServerAuthentication, session: SMTPServerSession, callback: (err: Error | null | undefined, response?: SMTPServerAuthenticationResponse) => void): void {79 if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {80 callback(undefined, { user: auth.username });81 } else {82 callback(new Error('Invalid username or password'));83 }84 }85 onMailFrom(from: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {86 console.log(`[${session.id}] onMailFrom ${from.address}`);87 if (from.address.split('@')[1] === 'spammer.com') {88 // code 421 disconnects SMTP session immediately89 callback(Object.assign(new Error('we do not like spam!'), { responseCode: 421 }));90 } else {91 callback();92 }93 }94 onRcptTo(to: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {95 console.log(`[${session.id}] onRcptTo ${to.address}`);96 callback();97 }98 onData(stream: Readable, session: SMTPServerSession, callback: (err?: Error) => void): void {99 console.log(`[${session.id}] onData started`);100 let messageLength = 0;101 stream.on('data', (chunk: Buffer) => {102 console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`);103 messageLength += chunk.length;104 });105 stream.once('end', () => {106 console.log(`[${session.id}] onData finished after reading ${messageLength} bytes`);107 callback();108 });109 }110 onClose(session: SMTPServerSession) {111 console.log(`[${session.id}] onClose`);112 }113 }114 const options: SMTPServerOptions = {115 hideSTARTTLS: true116 };117 const port = 2525;118 const server = new MySMTPServer(options);119 server.on('error', (err) => {120 console.log(`Server got error:`, err);121 });122 server.on('close', () => {123 console.log('Server closed');124 });125 server.listen(port, () => {126 const address = server.server.address() as AddressInfo;127 console.log(`Listening on [${address.address}]:${address.port}`);128 });129 server.close(() => {130 console.log('Server closed');131 });...
http-proxy-client-test.js
Source:http-proxy-client-test.js
1/* eslint no-unused-expressions:0, prefer-arrow-callback: 0 */2/* globals beforeEach, describe, it */3'use strict';4const http = require('http');5const proxy = require('proxy');6const httpProxyClient = require('../../lib/smtp-connection/http-proxy-client');7const SMTPServer = require('smtp-server').SMTPServer;8const chai = require('chai');9const expect = chai.expect;10chai.config.includeStack = true;11const PROXY_PORT = 3128;12const TARGET_PORT = 3129;13describe('HTTP Proxy Client Tests', function() {14 it('should connect to a socket through proxy', function(done) {15 let smtpServer = new SMTPServer({16 logger: false17 });18 smtpServer.listen(TARGET_PORT, () => {19 let proxyServer = proxy(http.createServer());20 proxyServer.listen(PROXY_PORT, () => {21 httpProxyClient('http://localhost:' + PROXY_PORT, TARGET_PORT, '127.0.0.1', (err, socket) => {22 expect(err).to.not.exist;23 socket.once('data', chunk => {24 expect(/^220[ -]/.test(chunk.toString())).to.be.true;25 socket.end();26 socket.on('close', () => {27 socket.destroy();28 smtpServer.close(() => setImmediate(done) && proxyServer.close());29 });30 });31 });32 });33 });34 });35 it('should connect to a socket through proxy with auth', function(done) {36 let smtpServer = new SMTPServer({37 logger: false38 });39 smtpServer.listen(TARGET_PORT, () => {40 let proxyServer = proxy(http.createServer());41 proxyServer.authenticate = (req, cb) => {42 cb(null, req.headers['proxy-authorization'] === 'Basic dGVzdDpwZXN0');43 };44 proxyServer.listen(PROXY_PORT, () => {45 httpProxyClient('http://test:pest@localhost:' + PROXY_PORT, TARGET_PORT, '127.0.0.1', (err, socket) => {46 expect(err).to.not.exist;47 socket.once('data', chunk => {48 expect(/^220[ -]/.test(chunk.toString())).to.be.true;49 socket.end();50 socket.on('close', () => {51 socket.destroy();52 smtpServer.close(() => setImmediate(done) && proxyServer.close());53 });54 });55 });56 });57 });58 });59 it('should should fail auth', function(done) {60 let smtpServer = new SMTPServer({61 logger: false62 });63 smtpServer.listen(TARGET_PORT, () => {64 let proxyServer = proxy(http.createServer());65 proxyServer.authenticate = (req, cb) => {66 cb(null, req.headers['proxy-authorization'] === 'Basic dGVzdDpwZXN0');67 };68 proxyServer.listen(PROXY_PORT, () => {69 httpProxyClient('http://test:kest@localhost:' + PROXY_PORT, TARGET_PORT, '127.0.0.1', (err, socket) => {70 expect(err).to.exist;71 expect(socket).to.not.exist;72 smtpServer.close(() => setImmediate(done) && proxyServer.close());73 });74 });75 });76 });...
Using AI Code Generation
1var request = require('request');2var options = {3 headers: {4 },5 body: JSON.stringify({6 {7 {8 is: {9 headers: {10 },11 }12 }13 }14 })15};16request(options, function (error, response, body) {17 if (error) {18 console.log(error);19 } else {20 console.log(body);21 }22});23var request = require('request');24var options = {25 headers: {26 },27 body: JSON.stringify({28 {29 {30 is: {31 headers: {32 },33 }34 }35 }36 })37};38request(options, function (error, response, body) {39 if (error) {40 console.log(error);41 } else {42 console.log(body);43 }44});45var request = require('request');46var options = {47 headers: {48 },49 body: JSON.stringify({50 {51 {52 is: {53 headers: {54 },55 }56 }57 }58 })59};60request(options, function (error, response, body) {61 if (error) {62 console.log(error);63 } else {64 console.log(body);65 }66});
Using AI Code Generation
1var smtpServer = require('smtp-server').SMTPServer;2var server = new smtpServer({3 onData: function(stream, session, callback) {4 stream.on('data', function() {});5 stream.on('end', function() {6 });7 }8});9server.listen(2525, function() {10 console.log('SMTP Server is running on port 2525');11});12var imposter = {13 stubs: [{14 responses: [{15 is: {16 }17 }]18 }]19};20var mb = require('mountebank');21mb.start({22}).then(function() {23 return mb.post('/imposters', imposter);24}).then(function(response) {25});26var nodemailer = require('nodemailer');27var transporter = nodemailer.createTransport({28});29var mailOptions = {
Using AI Code Generation
1var smtpServer = require('smtp-server').SMTPServer;2var server = new smtpServer({3 onAuth(auth, session, callback) {4 if (auth.username !== 'user' || auth.password !== 'pass') {5 return callback(new Error('Invalid username or password'));6 }7 },8 onData(stream, session, callback) {9 stream.on('data', function () {10 });11 }12});13server.listen(2525);14var nodemailer = require('nodemailer');15var transporter = nodemailer.createTransport({16});17let info = await transporter.sendMail({
Using AI Code Generation
1var smtpServer = require('smtp-server').SMTPServer;2var server = new smtpServer({3 onData: function (stream, session, callback) {4 stream.on('data', function () {5 });6 stream.on('end', callback);7 }8});9server.listen(2525, 'localhost', function () {10 console.log('SMTP Server is listening on port 2525');11});
Using AI Code Generation
1var mb = require('mountebank');2mb.create({3}, function (error, server) {4 console.log('mb server is up and running');5});6var nodemailer = require('nodemailer');7var transporter = nodemailer.createTransport({8});9var mailOptions = {
Using AI Code Generation
1const mb = require('mountebank');2const smtpServer = mb.createSmtpServer(2525);3smtpServer.then(server => {4 server.on('data', (connection, chunk) => {5 console.log(chunk.toString());6 });7});8const mb = require('mountebank');9const httpServer = mb.createHttpServer(2525);10httpServer.then(server => {11 server.on('request', (request, response) => {12 console.log(request);13 console.log(response);14 });15});16const mb = require('mountebank');17const httpsServer = mb.createHttpsServer(2525);18httpsServer.then(server => {19 server.on('request', (request, response) => {20 console.log(request);21 console.log(response);22 });23});24const mb = require('mountebank');25const tcpServer = mb.createTcpServer(2525);26tcpServer.then(server => {27 server.on('data', (connection, chunk) => {28 console.log(chunk.toString());29 });30});31const mb = require('mountebank');32const tcpServer = mb.createTcpServer(2525);33tcpServer.then(server => {34 server.on('data', (connection, chunk) => {35 console.log(chunk.toString());36 });37});38const mb = require('mountebank');39const tcpServer = mb.createTcpServer(2525);40tcpServer.then(server => {41 server.on('data', (connection, chunk) => {42 console.log(chunk.toString());43 });44});45const mb = require('mountebank');46const tcpServer = mb.createTcpServer(2525);47tcpServer.then(server => {48 server.on('data', (connection, chunk) => {49 console.log(chunk.toString());50 });51});52const mb = require('mountebank');53const tcpServer = mb.createTcpServer(2525
Using AI Code Generation
1const mb = require('mountebank');2const mbHelper = require('mountebank-helper');3const port = 2525;4const host = 'localhost';5const mbConfig = {6};7 {8 {9 {10 is: {11 }12 }13 }14 }15];16const mbServer = mb.create(mbConfig, imposters);17mbServer.start()18 .then(() => {19 console.log('mountebank started');20 return mbHelper.mbHelper.getImposterList(mbConfig);21 })22 .then((imposters) => {23 console.log('imposters: ', imposters);24 return mbHelper.mbHelper.getImposter(mbConfig, imposters[0].port);25 })26 .then((imposter) => {27 console.log('imposter: ', imposter);28 return mbHelper.mbHelper.verifyImposter(mbConfig, imposter.port);29 })30 .then((imposter) => {31 console.log('imposter: ', imposter);32 return mbHelper.mbHelper.resetImposter(mbConfig, imposter.port);33 })34 .then((imposter) => {35 console.log('imposter: ', imposter);36 return mbHelper.mbHelper.deleteImposter(mbConfig, imposter.port);37 })38 .then((imposter) => {39 console.log('imposter: ', imposter);40 return mbServer.stop();41 })42 .then(() => {43 console.log('mountebank stopped');44 })45 .catch((e) => {46 console.error(e);47 });48var mb = require('mountebank');49var mbHelper = require('mountebank-helper');50var port = 2525;51var host = 'localhost';52var mbConfig = {
Using AI Code Generation
1var mb = require('mountebank');2var smtpServer = mb.createSmtpServer(2525, 'localhost');3smtpServer.then(function (server) {4 console.log('SMTP server running at ' + server.url);5 server.on('data', function (request, response) {6 console.log('Received from: ' + request.from);7 console.log('Received to: ' + request.to);8 console.log('Received data: ' + request.data);9 response.resolve();10 });11});12var mb = require('mountebank');13var smtpServer = mb.createSmtpServer(2525, 'localhost');14smtpServer.then(function (server) {15 console.log('SMTP server running at ' + server.url);16 server.on('data', function (request, response) {17 console.log('Received from: ' + request.from);18 console.log('Received to: ' + request.to);19 console.log('Received data: ' + request.data);20 response.resolve();21 });22});23var mb = require('mountebank');24var smtpServer = mb.createSmtpServer(2525, 'localhost');25smtpServer.then(function (server) {26 console.log('SMTP server running at ' + server.url);27 server.on('data', function (request, response) {28 console.log('Received from: ' + request.from);29 console.log('Received to: ' + request.to);30 console.log('Received data: ' + request.data);31 response.resolve();32 });33});34var mb = require('mountebank');35var smtpServer = mb.createSmtpServer(2525, 'localhost');36smtpServer.then(function (server) {37 console.log('SMTP server running at ' + server.url);38 server.on('data', function (request, response) {39 console.log('Received from: ' + request.from);40 console.log('Received to: ' + request.to);41 console.log('Received data:
Using AI Code Generation
1var port = 2525;2var smtpServer = require('smtp-server').SMTPServer;3var server = new smtpServer({4});5server.on('error', function(err) {6 console.log('Error %s', err.message);7});8server.listen(port, function() {9 console.log('SMTP Server is listening on port %s', port);10});11server.close(function() {12 console.log('SMTP Server closed');13});14after(function() {15 server.close(function() {16 console.log('SMTP Server closed');17 });18});
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!!