Best JavaScript code snippet using appium-base-driver
nodeserver.spec.js
Source:nodeserver.spec.js
...35 var _server = NodeServer.createServer(null, true);36 var c = 0;37 var address = NodeServer.getIpAddress('127');38 var serverSocket = _server.createServer(address[0].address, 6323, () => {39 _server.close();40 });41 assert.ok(serverSocket);42 _server.close();43 serverSocket.close();44 done();45 });46 it('can listen to a socket', (done) => {47 var _server = NodeServer.createServer(null, true);48 var _server2 = NodeServer.createServer(null, true);49 // _server.proxy = true;50 // _server2.proxy = true;51 var c = 0;52 var address = NodeServer.getIpAddress('127');53 var ss2 = _server2.createServer(address[0].address, 1423 + c, () => {54 console.log('ready to connect');55 var serverSocket = _server.connectSocket(address[0].address, 1423 + c, (res) => {56 _server2.close();57 _server.close();58 done();59 });60 assert.ok(serverSocket);61 });62 });63 it('can send messages over a socket', (done) => {64 var address = NodeServer.getIpAddress('127');65 var _server2 = NodeServer.createServer(null, true);66 var _server = NodeServer.createServer(null, true);67 var received = false;68 var onReceived = ((address, port, message) => {69 received = message;70 console.log(address)71 console.log(port);72 assert.ok(received);73 console.log(message);74 _server.close();75 _server2.close();76 done();77 });78 _server.onReceived = onReceived;79 _server2.onReceived = onReceived;80 console.log(address);81 var port = 1243;82 _server.createServer(address[0].iface.address, port, res => {83 console.log('created server')84 });85 var serverSocket = _server2.connectSocket(address[0].iface.address, port, res => {86 console.log('connected to socket')87 serverSocket.send({ sending: 'a message' });88 });89 });90 it('can send messages over a socket 2', (done) => {91 var address = NodeServer.getIpAddress('127');92 var _server2 = NodeServer.createServer(null, true);93 var _server = NodeServer.createServer(null, true);94 var received = false;95 var onReceived = ((address, port, message) => {96 received = message;97 console.log(address)98 console.log(port);99 assert.ok(received);100 console.log(message);101 _server.close();102 _server2.close();103 done();104 });105 _server.onReceived = onReceived;106 _server2.onReceived = onReceived;107 console.log(address);108 var port = 1243;109 _server.createServer(address[0].iface.address, port, res => {110 console.log('created server')111 });112 var serverSocket = _server2.connectSocket(address[0].iface.address, port, res => {113 console.log('connected to socket')114 _server2.send(address[0].iface.address, port, { sending: 'a message' });115 });116 });117 it('can send messages over a socket 3', (done) => {118 var address = NodeServer.getIpAddress('127');119 var _server2 = NodeServer.createServer(null, true);120 var _server = NodeServer.createServer(null, true);121 var received = false;122 var onReceived = ((address, port, message) => {123 received = message;124 console.log(address)125 console.log(port);126 assert.ok(received);127 console.log(message);128 _server.close();129 _server2.close();130 done();131 });132 _server.onReceived = onReceived;133 _server2.onReceived = onReceived;134 console.log(address);135 var port = 1244;136 var p = new Promise((resolve) => {137 _server.createServer(address[0].iface.address, port, res => {138 console.log('created server')139 resolve();140 });141 }).then(() => {142 return new Promise((resolve) => {143 _server2.connectSocket(address[0].iface.address, port, res => {144 console.log('connected to socket')145 resolve();146 });147 });148 });149 Promise.all([p]).then(() => {150 setTimeout(() => {151 _server.send(address[0].iface.address, port, { sending: 'a message' });152 }, 1000);153 });154 });155 it('can use a child process to excute everything', (done) => {156 var address = NodeServer.getIpAddress('127');157 var _server2 = NodeServer.proxyServer();158 var _server = NodeServer.createServer(null, true);159 var received = false;160 var onReceived = ((address, port, message) => {161 received = message;162 console.log(address)163 console.log(port);164 assert.ok(received);165 console.log(message);166 _server.close();167 _server2.close();168 done();169 });170 _server.onReceived = onReceived;171 _server2.onReceived = onReceived;172 console.log(address);173 var port = 1243;174 _server.createServer(address[0].iface.address, port, res => {175 console.log('created server')176 });177 _server2.connectSocket(address[0].iface.address, port, res => {178 console.log('connected to socket')179 _server2.send(address[0].iface.address, port, { sending: 'a message' });180 });181 });182 it('can use a child process to excute everything 2', (done) => {183 var address = NodeServer.getIpAddress('127');184 console.log(address);185 var _server2 = NodeServer.createServer(null, true);186 var _server = NodeServer.proxyServer();187 var received = false;188 var onReceived = ((address, port, message) => {189 received = message;190 console.log(address)191 console.log(port);192 assert.ok(received);193 console.log(message);194 _server.close();195 _server2.close();196 done();197 });198 _server.onReceived = onReceived;199 _server2.onReceived = onReceived;200 console.log(address);201 var port = 5243;202 _server.createServer(address[0].iface.address, port, res => {203 console.log('created server')204 _server2.connectSocket(address[0].iface.address, port, res => {205 console.log('connected to socket')206 _server2.send(address[0].iface.address, port, { sending: 'a message' });207 });208 });209 });210 it('can create server', () => {211 var address = NodeServer.getIpAddress('127');212 var server = NodeServer.createHttpServer({213 address: address.address,214 port: 9142215 });216 assert.ok(server);217 server.close();218 });219 it('can accept a call', (done) => {220 var address = NodeServer.getIpAddress('127')[0];221 var port = 8812;222 console.log(address);223 var server = NodeServer.createHttpServer({224 address: address.address,225 port: port226 }, () => {227 console.log('listening')228 const postData = JSON.stringify({229 'msg': 'Hello World!'230 });231 const options = {232 hostname: address.address,233 port: port,234 path: '',235 method: 'POST',236 headers: {237 'Content-Type': 'application/x-www-form-urlencoded',238 'Content-Length': Buffer.byteLength(postData)239 }240 };241 const req = http.request(options, (res) => {242 console.log(`STATUS: ${res.statusCode}`);243 console.log(`HEADERS: ${JSON.stringify(res.headers)}`);244 res.setEncoding('utf8');245 res.on('data', (chunk) => {246 console.log(`BODY: ${chunk}`);247 });248 res.on('end', () => {249 console.log('No more data in response.');250 server.close();251 done();252 });253 });254 req.on('error', (e) => {255 console.error(`problem with request: ${e.message}`);256 });257 // write data to request body258 req.write(postData);259 req.end();260 });261 assert.ok(server);262 // server.close();263 });264 it('can add a handler', (done) => {265 var address = NodeServer.getIpAddress('127')[0];266 var port = 8814;267 var handled = false;268 console.log(address);269 var path = '/get/lines';270 var server = NodeServer.createHttpServer({271 address: address.address,272 port: port273 }, () => {274 server.addHandler((headers, method, url) => {275 console.log('check handler')276 if (method === 'POST' && url === path) {277 return true;278 }279 return false;280 }, () => {281 handled = true;282 });283 console.log('listening')284 const postData = JSON.stringify({285 'msg': 'Hello World!'286 });287 const options = {288 hostname: address.address,289 port: port,290 path: path,291 method: 'POST',292 headers: {293 'Content-Type': 'application/x-www-form-urlencoded',294 'Content-Length': Buffer.byteLength(postData)295 }296 };297 const req = http.request(options, (res) => {298 console.log(`STATUS: ${res.statusCode}`);299 console.log(`HEADERS: ${JSON.stringify(res.headers)}`);300 res.setEncoding('utf8');301 res.on('data', (chunk) => {302 console.log(`BODY: ${chunk}`);303 });304 res.on('end', () => {305 console.log('No more data in response.');306 server.close();307 assert.ok(handled);308 done();309 });310 });311 req.on('error', (e) => {312 console.error(`problem with request: ${e.message}`);313 });314 // write data to request body315 req.write(postData);316 req.end();317 });318 assert.ok(server);319 // server.close();320 });321 it('add redhash controller', (done) => {322 var address = NodeServer.getIpAddress('127')[0];323 var server = NodeServer.createHttpServer({324 address: address.address,325 port: 14812326 }, () => {327 server.addController(new RedHashController());328 server.close();329 done();330 });331 });332 it('can handle a request controller', (done) => {333 var address = NodeServer.getIpAddress('127')[0];334 var server = NodeServer.createHttpServer({335 address: address.address,336 port: 14812337 }, () => {338 var controller = new RedHashController();339 var controller2 = new RedHashController();340 server.addController(controller);341 var server2 = NodeServer.createHttpServer({342 address: address.address,343 port: 14861344 }, () => {345 server2.addController(controller2);346 server2.proxy = true;347 console.log('create server 2');348 server.proxy = true;349 controller.sendHttp({350 body: {351 line: 'line',352 address: address.address,353 port: 12000354 },355 path: RHS.REQUEST_CONNECTION_PATH,356 address: address.address,357 method: 'POST',358 port: 14861359 }).then(res => {360 assert.ok(res);361 assert.ok(res.ok);362 assert.ok(controller2.pendingRequests);363 assert.ok(controller2.pendingRequests.length === 1);364 assert.ok(controller.pendingRequests);365 assert.ok(controller.pendingRequests.length === 0);366 server.close();367 server2.close();368 done();369 });370 });371 });372 });373 it('can handle a request controller, then process requests', (done) => {374 var address = NodeServer.getIpAddress('127')[0];375 var server2port = 14239;376 var socketport = 12001;377 var server = NodeServer.createHttpServer({378 address: address.address,379 port: 14812380 }, () => {381 var controller = new RedHashController();382 var controller2 = new RedHashController();383 server.addController(controller);384 var server2 = NodeServer.createHttpServer({385 address: address.address,386 port: server2port387 }, () => {388 server2.addController(controller2);389 server2.proxy = true;390 console.log('create server 2');391 server.proxy = true;392 server.createServer(address.address, socketport, () => {393 console.log('server created, send http request')394 controller.sendHttp({395 body: {396 line: 'line',397 address: address.address,398 port: socketport,399 id: 'me'400 },401 path: RHS.REQUEST_CONNECTION_PATH,402 address: address.address,403 method: 'POST',404 port: server2port405 }).then(res => {406 assert.ok(res);407 assert.ok(res.ok);408 assert.ok(controller2.pendingRequests);409 assert.ok(controller2.pendingRequests.length === 1);410 assert.ok(controller.pendingRequests);411 assert.ok(controller.pendingRequests.length === 0);412 return controller2.processRequests().then(() => {413 assert.ok(controller2.pendingRequests);414 assert.ok(controller2.pendingRequests.length === 0);415 server.close();416 server2.close();417 done();418 });419 });420 });421 });422 });423 });424 it('can use a child process to excute everything 3', (done) => {425 var address = NodeServer.getIpAddress('127');426 var _server2 = NodeServer.proxyServer();427 var _server = NodeServer.proxyServer();428 var received = false;429 var onReceived = ((address, port, message) => {430 received = message;431 console.log(address)432 console.log(port);433 assert.ok(received);434 console.log(message);435 done();436 });437 var r1;438 var r2;439 var p1 = new Promise((resolve) => {440 r1 = resolve;441 });442 var p2 = new Promise((resolve) => {443 r2 = resolve;444 });445 _server.onReceived = (address, port, message) => {446 received = message;447 console.log(address)448 console.log(port);449 assert.ok(received);450 console.log(message);451 r1();452 // _server.close();453 // _server2.close();454 // done();455 };456 _server2.onReceived = (address, port, message) => {457 received = message;458 console.log(address)459 console.log(port);460 assert.ok(received);461 console.log(message);462 r2();463 // _server.close();464 // _server2.close();465 // done();466 };;467 Promise.all([r1, r2]).then(() => {468 _server.close();469 _server2.close();470 done();471 });472 console.log(address);473 var port = 1243;474 _server.createServer(address[0].iface.address, port, res => {475 console.log('created server')476 });477 _server2.connectSocket(address[0].iface.address, port, res => {478 console.log('connected to socket')479 _server2.send(address[0].iface.address, port, { sending: 'a message' });480 _server.send(address[0].iface.address, port, { sending: 'a message' });481 });482 });...
lib-http-proxy-test.js
Source:lib-http-proxy-test.js
...43 var source = http.createServer(function(req, res) {44 expect(req.method).to.eql('GET');45 expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);46 source.close();47 proxy._server.close();48 done();49 });50 source.listen(ports.source);51 http.request('http://127.0.0.1:' + ports.proxy, function() {}).end();52 })53 });54 describe('#createProxyServer using the web-incoming passes', function () {55 it('should make the request on pipe and finish it', function(done) {56 var ports = { source: gen.port, proxy: gen.port };57 var proxy = httpProxy.createProxyServer({58 target: 'http://127.0.0.1:' + ports.source59 }).listen(ports.proxy);60 var source = http.createServer(function(req, res) {61 expect(req.method).to.eql('POST');62 expect(req.headers['x-forwarded-for']).to.eql('127.0.0.1');63 expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);64 source.close();65 proxy._server.close();66 done();67 });68 source.listen(ports.source);69 70 http.request({71 hostname: '127.0.0.1',72 port: ports.proxy,73 method: 'POST',74 headers: {75 'x-forwarded-for': '127.0.0.1'76 } 77 }, function() {}).end();78 });79 });80 describe('#createProxyServer using the web-incoming passes', function () {81 it('should make the request, handle response and finish it', function(done) {82 var ports = { source: gen.port, proxy: gen.port };83 var proxy = httpProxy.createProxyServer({84 target: 'http://127.0.0.1:' + ports.source85 }).listen(ports.proxy);86 var source = http.createServer(function(req, res) {87 expect(req.method).to.eql('GET');88 expect(req.headers.host.split(':')[1]).to.eql(ports.proxy);89 res.writeHead(200, {'Content-Type': 'text/plain'})90 res.end('Hello from ' + source.address().port);91 });92 source.listen(ports.source);93 94 http.request({95 hostname: '127.0.0.1',96 port: ports.proxy,97 method: 'GET'98 }, function(res) {99 expect(res.statusCode).to.eql(200);100 res.on('data', function (data) {101 expect(data.toString()).to.eql('Hello from ' + ports.source);102 });103 res.on('end', function () { 104 source.close();105 proxy._server.close();106 done();107 });108 }).end();109 });110 });111 describe('#createProxyServer() method with error response', function () {112 it('should make the request and emit the error event', function(done) {113 var ports = { source: gen.port, proxy: gen.port };114 var proxy = httpProxy.createProxyServer({115 target: 'http://127.0.0.1:' + ports.source116 });117 proxy.on('error', function (err) {118 expect(err).to.be.an(Error);119 expect(err.code).to.be('ECONNREFUSED');120 proxy._server.close();121 done();122 })123 proxy.listen(ports.proxy);124 125 http.request({126 hostname: '127.0.0.1',127 port: ports.proxy,128 method: 'GET',129 }, function() {}).end();130 });131 });132 describe('#createProxyServer setting the correct timeout value', function () {133 it('should hang up the socket at the timeout', function (done) {134 this.timeout(30);135 var ports = { source: gen.port, proxy: gen.port };136 var proxy = httpProxy.createProxyServer({137 target: 'http://127.0.0.1:' + ports.source,138 timeout: 3139 }).listen(ports.proxy);140 var source = http.createServer(function(req, res) {141 setTimeout(function () {142 res.end('At this point the socket should be closed');143 }, 5)144 });145 source.listen(ports.source);146 var testReq = http.request({147 hostname: '127.0.0.1',148 port: ports.proxy,149 method: 'GET',150 }, function() {});151 testReq.on('error', function (e) {152 expect(e).to.be.an(Error);153 expect(e.code).to.be.eql('ECONNRESET');154 proxy._server.close();155 source.close();156 done();157 });158 testReq.end();159 })160 })161 // describe('#createProxyServer using the web-incoming passes', function () {162 // it('should emit events correclty', function(done) {163 // var proxy = httpProxy.createProxyServer({164 // target: 'http://127.0.0.1:8080'165 // }),166 // proxyServer = proxy.listen('8081'),167 // source = http.createServer(function(req, res) {168 // expect(req.method).to.eql('GET');169 // expect(req.headers.host.split(':')[1]).to.eql('8081');170 // res.writeHead(200, {'Content-Type': 'text/plain'})171 // res.end('Hello from ' + source.address().port);172 // }),173 // events = [];174 // source.listen('8080');175 // proxy.ee.on('http-proxy:**', function (uno, dos, tres) {176 // events.push(this.event);177 // })178 179 // http.request({180 // hostname: '127.0.0.1',181 // port: '8081',182 // method: 'GET',183 // }, function(res) {184 // expect(res.statusCode).to.eql(200);185 // res.on('data', function (data) {186 // expect(data.toString()).to.eql('Hello from 8080');187 // });188 // res.on('end', function () {189 // expect(events).to.contain('http-proxy:outgoing:web:begin');190 // expect(events).to.contain('http-proxy:outgoing:web:end');191 // source.close();192 // proxyServer._server.close();193 // done();194 // });195 // }).end();196 // });197 // });198 describe('#createProxyServer using the ws-incoming passes', function () {199 it('should proxy the websockets stream', function (done) {200 var ports = { source: gen.port, proxy: gen.port };201 var proxy = httpProxy.createProxyServer({202 target: 'ws://127.0.0.1:' + ports.source,203 ws: true204 }),205 proxyServer = proxy.listen(ports.proxy),206 destiny = new ws.Server({ port: ports.source }, function () {207 var client = new ws('ws://127.0.0.1:' + ports.proxy);208 client.on('open', function () {209 client.send('hello there');210 });211 client.on('message', function (msg) {212 expect(msg).to.be('Hello over websockets');213 client.close();214 proxyServer._server.close();215 destiny.close();216 done();217 });218 });219 destiny.on('connection', function (socket) {220 socket.on('message', function (msg) {221 expect(msg).to.be('hello there');222 socket.send('Hello over websockets');223 });224 });225 });226 it('should emit error on proxy error', function (done) {227 var ports = { source: gen.port, proxy: gen.port };228 var proxy = httpProxy.createProxyServer({229 // note: we don't ever listen on this port230 target: 'ws://127.0.0.1:' + ports.source,231 ws: true232 }),233 proxyServer = proxy.listen(ports.proxy),234 client = new ws('ws://127.0.0.1:' + ports.proxy);235 client.on('open', function () {236 client.send('hello there');237 });238 proxy.on('error', function (err) {239 expect(err).to.be.an(Error);240 expect(err.code).to.be('ECONNREFUSED');241 proxyServer._server.close();242 done();243 });244 });245 it('should proxy a socket.io stream', function (done) {246 var ports = { source: gen.port, proxy: gen.port };247 var proxy = httpProxy.createProxyServer({248 target: 'ws://127.0.0.1:' + ports.source,249 ws: true250 }),251 proxyServer = proxy.listen(ports.proxy),252 destiny = io.listen(ports.source, function () {253 var client = ioClient.connect('ws://127.0.0.1:' + ports.proxy);254 client.on('connect', function () {255 client.emit('incoming', 'hello there');256 });257 client.on('outgoing', function (data) {258 expect(data).to.be('Hello over websockets');259 proxyServer._server.close();260 destiny.server.close();261 done();262 });263 });264 destiny.sockets.on('connection', function (socket) {265 socket.on('incoming', function (msg) {266 expect(msg).to.be('hello there');267 socket.emit('outgoing', 'Hello over websockets');268 });269 })270 });271 })...
test.js
Source:test.js
...15 it('should construct normally', function (done) {16 testBridge = flic.createBridge(function () {17 assert(testBridge instanceof Bridge)18 assert.equal(testBridge._config.port, 8221)19 testBridge._server.close(done)20 })21 testBridge._server.unref()22 })23 it('should allow setting of the listening port', function (done) {24 testBridge = flic.createBridge({25 port: 900326 }, function (err) {27 assert.ifError(err)28 assert.equal(testBridge._config.port, 9003)29 testBridge._server.close(done)30 })31 testBridge._server.unref()32 })33 it('should pass any errors to a listener callback', function (done) {34 testBridge = flic.createBridge(function () {35 var dupeBridge = flic.createBridge(function (err) {36 assert(err)37 testBridge._server.close(done)38 })39 dupeBridge._server.unref()40 })41 testBridge._server.unref()42 })43 })44 describe('close', function () {45 var b, goodNodes46 goodNodes = []47 before(function (done) {48 done = _.after(2, done)49 b = flic.createBridge()50 b._server.unref()51 goodNodes.push(flic.createNode({52 id: 'node_1',53 connect_callback: function (err) {54 assert.ifError(err)55 done()56 }57 }))58 goodNodes.push(flic.createNode({59 id: 'node_2',60 connect_callback: function (err) {61 assert.ifError(err)62 done()63 }64 }))65 })66 after(function () {67 b._server && b._server._handle && b._server.close()68 })69 it('should send a close message to all connected nodes', function (done) {70 async.parallel([71 function (cb) {72 goodNodes[0].on('close', function (partingData) {73 assert.equal(partingData, 'live long and prosper')74 cb()75 })76 },77 function (cb) {78 goodNodes[1].on('close', function (partingData) {79 assert.equal(partingData, 'live long and prosper')80 cb()81 })82 }83 ], done)84 b.close(['live long and prosper'])85 })86 })87})88describe('Node', function () {89 describe('construction', function () {90 var b, b291 before(function () {92 b2 = flic.createBridge({93 port: 982394 })95 b = flic.createBridge()96 b._server.unref()97 b2._server.unref()98 })99 after(function () {100 b._server && b._server._handle && b._server.close()101 b2._server && b2._server._handle && b2._server.close()102 })103 it('can still be accessed through flic.node', function () {104 assert(Node === flic.node)105 })106 it('should be backwards compatible with v1.x', function (done) {107 done = _.after(2, done)108 var node = new Node('node_22', function (err) {109 assert.ifError(err)110 done()111 })112 assert(node instanceof Node)113 var node2 = flic.createNode('node_34', function (err) {114 assert.ifError(err)115 done()116 })117 assert(node2 instanceof Node)118 })119 it('should construct normally and callback when connected to the bridge', function (done) {120 flic.createNode({121 id: 'node_1',122 connect_callback: function (err) {123 assert.ifError(err)124 done()125 }126 })127 })128 it('should fail because the name is taken', function (done) {129 flic.createNode({130 id: 'node_1',131 connect_callback: function (err) {132 assert(err)133 done()134 }135 })136 })137 it('should fail because there is no bridge listening on specified port', function (done) {138 this.timeout(3000)139 flic.createNode({140 id: 'muh_dumb_node',141 port: 9999,142 connect_callback: function (err) {143 assert(err)144 done()145 }146 })147 })148 it('should be able to connect normally to a server on a different port', function (done) {149 flic.createNode({150 id: 'node_3',151 port: 9823,152 connect_callback: function (err) {153 assert.ifError(err)154 done()155 }156 })157 })158 })159 describe('tell', function () {160 var b, goodNodes161 goodNodes = []162 before(function (done) {163 done = _.after(2, done)164 b = new Bridge()165 b._server.unref()166 goodNodes.push(flic.createNode({167 id: 'node_1',168 connect_callback: function (err) {169 assert.ifError(err)170 done()171 }172 }))173 goodNodes.push(flic.createNode({174 id: 'node_2',175 connect_callback: function (err) {176 assert.ifError(err)177 done()178 }179 }))180 })181 after(function () {182 b._server && b._server._handle && b._server.close()183 })184 afterEach(function () {185 goodNodes.forEach(function (n) {186 n.removeAllListeners()187 })188 })189 it('should be able to contact another node', function (done) {190 goodNodes[0].on('test_event', function (param1) {191 assert.equal(param1, 'test_param')192 done()193 })194 goodNodes[1].tell('node_1:test_event', 'test_param')195 })196 it('should be able to contact another node and send callbacks', function (done) {197 goodNodes[0].on('test_event', function (param1, ack) {198 assert.equal(param1, 'test_param')199 ack('my-reply')200 })201 goodNodes[1].tell('node_1:test_event', 'test_param', function (a) {202 assert.equal(a, 'my-reply')203 done()204 })205 })206 it("should fail if the who and what format is not valid ('node_name:remote_event')", function () {207 assert.throws(function () {208 goodNodes[1].tell('node_1-what-the-hell')209 })210 assert.throws(function () {211 goodNodes[1].tell('node_1:too:many:colons')212 })213 })214 it('should fail if the node being told does not exist', function (done) {215 goodNodes[0].tell('me-no-exist:remote_event', function (err) {216 assert.equal(err, 'unknown-node')217 done()218 })219 })220 })221 describe('shout', function () {222 var b, goodNodes223 goodNodes = []224 before(function (done) {225 b = flic.createBridge()226 async.parallel([227 function (cb) {228 goodNodes.push(flic.createNode({id: 'node_1', connect_callback: cb}))229 },230 function (cb) {231 goodNodes.push(flic.createNode({id: 'node_2', connect_callback: cb}))232 },233 function (cb) {234 goodNodes.push(flic.createNode({id: 'node_3', connect_callback: cb}))235 }236 ], function (err) {237 assert.ifError(err)238 done()239 })240 })241 after(function () {242 b._server && b._server._handle && b._server.close()243 })244 afterEach(function () {245 goodNodes.forEach(function (n) {246 n.removeAllListeners()247 })248 })249 it('should send events to all connected nodes', function (done) {250 async.parallel([251 function (done) {252 goodNodes[0].on('shout_eve', function (param1) {253 assert.equal(param1, 'test-param')254 done()255 })256 },257 function (done) {258 goodNodes[1].on('shout_eve', function (param1) {259 assert.equal(param1, 'test-param')260 done()261 })262 }263 ], done)264 goodNodes[2].shout('shout_eve', 'test-param')265 })266 it('should catch events shouted very quickly', function (done) {267 async.parallel([268 function (done) {269 goodNodes[0].on('shout_eve', function (param1) {270 var isParamOk = param1 === 'test-param 1' ||271 param1 === 'test-param 2' ||272 param1 === 'test-param 3'273 assert.ok(isParamOk)274 done()275 })276 }277 ], done)278 goodNodes[2].shout('shout_eve', 'test-param 1')279 goodNodes[2].shout('shout_eve', 'test-param 2')280 goodNodes[2].shout('shout_eve', 'test-param 3')281 })282 })283 describe('leave', function () {284 var b285 before(function () {286 b = flic.createBridge()287 b._server.unref()288 })289 after(function () {290 b._server && b._server._handle && b._server.close()291 })292 it('should tell the bridge that it is leaving', function (done) {293 var x = flic.createNode({294 id: 'leaving_node',295 connect_callback: function (err) {296 assert.ifError(err)297 x.leave()298 setTimeout(function () {299 assert.ok(!b._sockets.hasOwnProperty('leaving_node'), b._sockets)300 done()301 }, 20)302 }303 })304 })...
file-allow.js
Source:file-allow.js
...35 done();36 });37 });38 after(function() {39 app._server.close();40 });41 it('should upload a file to an allowed path', function(done) {42 var r = request({43 method: 'POST',44 url: base + '/allowed/',45 json: true46 }, function(err, res, d) {47 assert.ok(d);48 assert.ok(d.body);49 assert.equal(d.body.foobar, 1);50 assert.ok(d.files);51 assert.ok(d.files['the-file']);52 assert.ok(d.files['the-file'].file);53 assert.ok(fs.existsSync(d.files['the-file'].file));54 done();55 });56 var form = r.form();57 form.append('foobar', 1);58 form.append('the-file', fs.createReadStream(__filename));59 });60 61});62describe('express-busboy: file upload - allowedPath function', function() {63 var app;64 var base = 'http://127.0.0.1:';65 before(function(done) {66 app = express();67 bb.extend(app, {68 upload: true,69 allowedPath: function(url) {70 return url.indexOf('allowed') > -1;71 }72 });73 portfinder.getPort(function(e, port) {74 setup(app, port);75 base += port;76 done();77 });78 });79 after(function() {80 app._server.close();81 });82 it('should upload a file to an allowed path', function(done) {83 var r = request({84 method: 'POST',85 url: base + '/allowed/',86 json: true87 }, function(err, res, d) {88 assert.ok(d);89 assert.ok(d.body);90 assert.equal(d.body.foobar, 1);91 assert.ok(d.files);92 assert.ok(d.files['the-file']);93 assert.ok(d.files['the-file'].file);94 assert.ok(fs.existsSync(d.files['the-file'].file));95 done();96 });97 var form = r.form();98 form.append('foobar', 1);99 form.append('the-file', fs.createReadStream(__filename));100 });101 102});103describe('express-busboy: file upload - allowedPath string', function() {104 var app;105 var base = 'http://127.0.0.1:';106 before(function(done) {107 app = express();108 bb.extend(app, {109 upload: true,110 allowedPath: '/allowed/'111 });112 portfinder.getPort(function(e, port) {113 setup(app, port);114 base += port;115 done();116 });117 });118 after(function() {119 app._server.close();120 });121 it('should upload a file to an allowed path', function(done) {122 var r = request({123 method: 'POST',124 url: base + '/allowed/',125 json: true126 }, function(err, res, d) {127 assert.ok(d);128 assert.ok(d.body);129 assert.equal(d.body.foobar, 1);130 assert.ok(d.files);131 assert.ok(d.files['the-file']);132 assert.ok(d.files['the-file'].file);133 assert.ok(fs.existsSync(d.files['the-file'].file));134 done();135 });136 var form = r.form();137 form.append('foobar', 1);138 form.append('the-file', fs.createReadStream(__filename));139 });140 141});142describe('express-busboy: NO file upload - allowedPath regex', function() {143 var app;144 var base = 'http://127.0.0.1:';145 before(function(done) {146 app = express();147 bb.extend(app, {148 upload: true,149 allowedPath: /allowed/150 });151 portfinder.getPort(function(e, port) {152 setup(app, port);153 base += port;154 done();155 });156 });157 after(function() {158 app._server.close();159 });160 it('should NOT upload a file to an allowed path', function(done) {161 var r = request({162 method: 'POST',163 url: base + '/upload/',164 json: true165 }, function(err, res, d) {166 assert.ok(d);167 assert.ok(d.body);168 assert.equal(d.body.foobar, 1);169 assert.ok(d.files);170 assert.equal(d.files['the-file'], undefined);171 done();172 });173 var form = r.form();174 form.append('foobar', 1);175 form.append('the-file', fs.createReadStream(__filename));176 });177 178});179describe('express-busboy: NO file upload - allowedPath function', function() {180 var app;181 var base = 'http://127.0.0.1:';182 before(function(done) {183 app = express();184 bb.extend(app, {185 upload: true,186 allowedPath: function(url) {187 return url.indexOf('allowed') > -1;188 }189 });190 portfinder.getPort(function(e, port) {191 setup(app, port);192 base += port;193 done();194 });195 });196 after(function() {197 app._server.close();198 });199 it('should NOT upload a file to an allowed path', function(done) {200 var r = request({201 method: 'POST',202 url: base + '/upload/',203 json: true204 }, function(err, res, d) {205 assert.ok(d);206 assert.ok(d.body);207 assert.equal(d.body.foobar, 1);208 assert.ok(d.files);209 assert.equal(d.files['the-file'], undefined);210 done();211 });212 var form = r.form();213 form.append('foobar', 1);214 form.append('the-file', fs.createReadStream(__filename));215 });216 217});218describe('express-busboy: allowed mime type', function() {219 var app;220 var base = 'http://127.0.0.1:';221 before(function(done) {222 app = express();223 bb.extend(app, {224 upload: true,225 mimeTypeLimit: ['application/javascript']226 });227 portfinder.getPort(function(e, port) {228 setup(app, port);229 base += port;230 done();231 });232 });233 after(function() {234 app._server.close();235 });236 it('should upload a file with a valid mime type', function(done) {237 var r = request({238 method: 'POST',239 url: base + '/upload/',240 json: true241 }, function(err, res, d) {242 assert.ok(d);243 assert.ok(d.body);244 assert.equal(d.body.foobar, 1);245 assert.ok(d.files);246 assert.ok(d.files['the-file']);247 assert.ok(d.files['the-file'].file);248 assert.ok(fs.existsSync(d.files['the-file'].file));249 done();250 });251 var form = r.form();252 form.append('foobar', 1);253 form.append('the-file', fs.createReadStream(__filename));254 });255 256});257describe('express-busboy: NOT allowed mime type', function() {258 var app;259 var base = 'http://127.0.0.1:';260 before(function(done) {261 app = express();262 bb.extend(app, {263 upload: true,264 mimeTypeLimit: 'application/javascript'265 });266 portfinder.getPort(function(e, port) {267 setup(app, port);268 base += port;269 done();270 });271 });272 after(function() {273 app._server.close();274 });275 276 it('should NOT upload a file with an invalid mime type', function(done) {277 var r = request({278 method: 'POST',279 url: base + '/upload/',280 json: true281 }, function(err, res, d) {282 assert.ok(d);283 assert.ok(d.body);284 assert.equal(d.body.foobar, 1);285 assert.ok(d.files);286 assert.equal(d.files['the-file'], undefined);287 done();...
transports.websocket.js
Source:transports.websocket.js
...32 , trips = 2;33 34 function close(){35 _client.close();36 _server.close();37 };38 39 listen(_server, function(){40 var messages = 0;41 _client = client(_server);42 _client.onopen = function(){43 _client.send(encode('from client'));44 };45 _client.onmessage = function(ev){46 if (++messages == 2){ // first message is the session id47 assert.ok(decode(ev.data), 'from server');48 --trips || close();49 }50 };51 });52 53 _socket.on('connection', function(conn){54 assert.ok(conn instanceof Client);55 conn56 .on('message', function(msg){57 assert.ok(msg == 'from client');58 --trips || close();59 })60 .send('from server');61 });62 },63 64 'test clients tracking': function(assert){65 var _server = server()66 , _socket = socket(_server);67 68 listen(_server, function(){69 var _client = client(_server);70 _client.onopen = function(){71 assert.ok(Object.keys(_socket.clients).length == 1);72 73 var _client2 = client(_server);74 _client2.onopen = function(){75 assert.ok(Object.keys(_socket.clients).length == 2);76 77 _client.close();78 _client2.close();79 _server.close();80 };81 }82 });83 },84 85 'test buffered messages': function(assert){86 var _server = server()87 , _socket = socket(_server, {88 transportOptions: {89 websocket: {90 closeTimeout: 500091 }92 }93 });94 95 listen(_server, function(){96 var _client = client(_server);97 98 _client.onopen = function(){99 assert.ok(Object.keys(_socket.clients).length == 1);100 var sessionid = Object.keys(_socket.clients)[0]101 , runOnce = false;102 103 _socket.clients[sessionid].connection.addListener('end', function(){104 if (!runOnce){105 assert.ok(_socket.clients[sessionid]._open == false);106 assert.ok(_socket.clients[sessionid].connected);107 _socket.clients[sessionid].send('should get this');108 109 var _client2 = client(_server, sessionid);110 _client2.onmessage = function(ev){111 assert.ok(Object.keys(_socket.clients).length == 1);112 assert.ok(decode(ev.data), 'should get this');113 _socket.clients[sessionid].options.closeTimeout = 0;114 _client2.close();115 _server.close();116 };117 runOnce = true;118 }119 });120 121 _client.close();122 };123 });124 },125 126 'test json encoding': function(assert){127 var _server = server()128 , _socket = socket(_server)129 , _client130 , trips = 2;131 132 function close(){133 _server.close();134 _client.close();135 };136 137 listen(_server, function(){138 _socket.on('connection', function(conn){139 conn.on('message', function(msg){140 assert.ok(msg.from == 'client');141 --trips || close();142 });143 conn.send({ from: 'server' });144 });145 146 var messages = 0;147 148 _client = client(_server);149 _client.onmessage = function(ev){150 if (++messages == 2){151 assert.ok(decode(ev.data)[0].substr(0, 3) == '~j~');152 assert.ok(JSON.parse(decode(ev.data)[0].substr(3)).from == 'server');153 _client.send(encode({ from: 'client' }));154 --trips || close();155 }156 };157 });158 },159 160 'test hearbeat timeout': function(assert){161 var _server = server()162 , _socket = socket(_server, {163 transportOptions: {164 websocket: {165 timeout: 100,166 heartbeatInterval: 1167 }168 }169 });170 171 listen(_server, function(){172 var _client = client(_server)173 , messages = 0;174 _client.onmessage = function(ev){175 ++messages;176 if (decode(ev.data)[0].substr(0, 3) == '~h~'){177 assert.ok(messages === 2);178 assert.ok(Object.keys(_socket.clients).length == 1);179 setTimeout(function(){180 assert.ok(Object.keys(_socket.clients).length == 0);181 _client.close();182 _server.close();183 }, 150);184 }185 };186 });187 },188 189 'test client broadcast': function(assert){190 var _server = server()191 , _socket = socket(_server);192 193 listen(_server, function(){194 var _client = client(_server)195 , _client2196 , _client3197 , _first198 , _connections = 0;199 200 _client.onmessage = function(ev){201 if (!('messages' in _client)) _client.messages = 0;202 if (++_client.messages == 2){203 assert.ok(decode(ev.data)[0] == 'not broadcasted');204 _client.close();205 _client2.close();206 _client3.close();207 _server.close();208 }209 };210 211 _client.onopen = function(){212 _client2 = client(_server);213 _client2.onmessage = function(ev){214 if (!('messages' in _client2)) _client2.messages = 0;215 if (++_client2.messages == 2)216 assert.ok(decode(ev.data)[0] == 'broadcasted')217 };218 _client2.onopen = function(){219 _client3 = client(_server);220 _client3.onmessage = function(ev){221 if (!('messages' in _client3)) _client3.messages = 0;...
transports.jsonp-polling.js
Source:transports.jsonp-polling.js
...65 _socket.on('connection', function(client){66 assert.ok(client instanceof Polling);67 client.on('message', function(msg){68 assert.ok(msg == 'from client');69 --trips || _server.close();70 });71 });72 listen(_server, function(){73 get(client(_server), '/socket.io/jsonp-polling/', function(data){74 var sessid = decode(data);75 assert.ok(Object.keys(_socket.clients).length == 1);76 assert.ok(sessid == Object.keys(_socket.clients)[0]);77 assert.ok(_socket.clients[sessid] instanceof Polling);78 79 _socket.clients[sessid].send('from server');80 81 get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(data){82 assert.ok(decode(data), 'from server');83 --trips || _server.close();84 });85 86 post(client(_server), '/socket.io/jsonp-polling/' + sessid + '/send//0', {data: encode('from client')});87 });88 });89 },90 91 'test clients tracking': function(assert){92 var _server = server()93 , _socket = socket(_server);94 95 listen(_server, function(){96 get(client(_server), '/socket.io/jsonp-polling/', function(){97 assert.ok(Object.keys(_socket.clients).length == 1);98 get(client(_server), '/socket.io/jsonp-polling/', function(){99 assert.ok(Object.keys(_socket.clients).length == 2);100 _server.close();101 });102 });103 });104 },105 106 'test buffered messages': function(assert){107 var _server = server()108 , _socket = socket(_server, { transportOptions: { 109 'jsonp-polling': {110 closeTimeout: 100,111 duration: 200112 }113 } });114 115 listen(_server, function(){116 get(client(_server), '/socket.io/jsonp-polling/', function(data){117 var sessid = decode(data);118 assert.ok(_socket.clients[sessid]._open === false);119 assert.ok(_socket.clients[sessid].connected);120 _socket.clients[sessid].send('from server');121 get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(data){122 var durationCheck;123 assert.ok(decode(data) == 'from server');124 setTimeout(function(){125 assert.ok(_socket.clients[sessid]._open);126 assert.ok(_socket.clients[sessid].connected);127 durationCheck = true;128 }, 50);129 get(client(_server), '/socket.io/jsonp-polling/' + sessid, function(){130 assert.ok(durationCheck);131 _server.close();132 });133 });134 });135 });136 }137 ...
transports.xhr-polling.js
Source:transports.xhr-polling.js
...56 _socket.on('connection', function(client){57 assert.ok(client instanceof Polling);58 client.on('message', function(msg){59 assert.ok(msg == 'from client');60 --trips || _server.close();61 });62 });63 listen(_server, function(){64 get(client(_server), '/socket.io/xhr-polling', function(data){65 var sessid = decode(data);66 assert.ok(Object.keys(_socket.clients).length == 1);67 assert.ok(sessid == Object.keys(_socket.clients)[0]);68 assert.ok(_socket.clients[sessid] instanceof Polling);69 70 _socket.clients[sessid].send('from server');71 72 get(client(_server), '/socket.io/xhr-polling/' + sessid, function(data){73 assert.ok(decode(data), 'from server');74 --trips || _server.close();75 });76 77 post(client(_server), '/socket.io/xhr-polling/' + sessid + '/send', {data: encode('from client')});78 });79 });80 },81 82 'test clients tracking': function(assert){83 var _server = server()84 , _socket = socket(_server);85 86 listen(_server, function(){87 get(client(_server), '/socket.io/xhr-polling', function(){88 assert.ok(Object.keys(_socket.clients).length == 1);89 get(client(_server), '/socket.io/xhr-polling', function(){90 assert.ok(Object.keys(_socket.clients).length == 2);91 _server.close();92 });93 });94 });95 },96 97 'test buffered messages': function(assert){98 var _server = server()99 , _socket = socket(_server, { transportOptions: { 100 'xhr-polling': {101 closeTimeout: 100,102 duration: 200103 }104 } });105 106 listen(_server, function(){107 get(client(_server), '/socket.io/xhr-polling', function(data){108 var sessid = decode(data);109 assert.ok(_socket.clients[sessid]._open === false);110 assert.ok(_socket.clients[sessid].connected);111 _socket.clients[sessid].send('from server');112 get(client(_server), '/socket.io/xhr-polling/' + sessid, function(data){113 var durationCheck;114 assert.ok(decode(data) == 'from server');115 setTimeout(function(){116 assert.ok(_socket.clients[sessid]._open);117 assert.ok(_socket.clients[sessid].connected);118 durationCheck = true;119 }, 100);120 get(client(_server), '/socket.io/xhr-polling/' + sessid, function(){121 assert.ok(durationCheck);122 _server.close();123 });124 });125 });126 });127 }128 ...
shortlife.js
Source:shortlife.js
...3console.log(process.pid);4process.on("SIGTERM", () => {5 console.info("SIGTERM signal received.");6 console.log("Closing http server.");7 expressApp._server.close(() => {8 console.log("Http server closed.");9 process.exit(0);10 });11});12setTimeout(() => {13 //console.warn("closing....");14 expressApp._server.close(() => {15 console.log("Http server closed.");16 process.exit(0);17 });18 //expressApp._server.close();19}, 1500);...
Using AI Code Generation
1const wd = require('wd');2const { server } = require('appium');3const assert = require('assert');4const chai = require('chai');5const chaiAsPromised = require('chai-as-promised');6chai.use(chaiAsPromised);7chai.should();8const PORT = 4723;9const caps = {10};11let driver = wd.promiseChainRemote(SERVER);12let serverInstance;13before(async () =>
Using AI Code Generation
1const { AppiumDriver } = require('appium-base-driver');2AppiumDriver.prototype.close = function () {3 return this._server.close();4};5const { startServer } = require('appium');6startServer({7 capabilities: [{8 }],9}).then((server) => {10 server.close();11});
Using AI Code Generation
1const wd = require('wd');2const { Server } = require('appium');3const { startServer } = require('appium/build/lib/main');4const { execSync } = require('child_process');5const { getSimulator } = require('./simulator');6const PORT = 4723;7const UDID = 'F9A3D3C8-2B87-4E7C-8B6D-8C1D9E9E7F9B';8const BUNDLE_ID = 'com.apple.Preferences';9const server = new Server();10let driver = null;11(async () => {12 await server.start();13 driver = wd.promiseChainRemote('localhost', PORT);14 await driver.init({15 });16 const sim = await getSimulator(UDID);17 const pid = await sim.getPid(BUNDLE_ID);18 console.log(pid);19 await execSync(`kill -9 ${pid}`);20 await driver.quit();21 await server.close();22})();23const { execSync } = require('child_process');24async function getSimulator (udid) {25 const { devices } = require('node-simctl');26 const { Simulator } = require('node-simctl/lib/simulator');27 const device = await devices({ udid });28 return new Simulator(device[0]);29}30async function getPid (bundleId) {31 const { pidOf } = require('node-simctl/lib/simulator-xcode-6');32 return await pidOf(bundleId);33}34module.exports = {35};36info: [debug] [BaseDriver] Event 'quitSessionRequested' logged at 1593227513288 (12:45:13 GMT+0530 (India Standard Time))
Using AI Code Generation
1var appium = require('appium');2var appiumServer = appium();3appiumServer.start(4723, function(err) {4 if (err) {5 console.log('Appium server failed to start');6 } else {7 console.log('Appium server started');8 appiumServer.close(function(err) {9 if (err) {10 console.log('Appium server failed to close');11 } else {12 console.log('Appium server closed');13 }14 });15 }16});17I have tried to use the _server.close() method, but it did not work. I am assuming that I am missing something in the code. Can anyone please help me with this?18Thanks for the reply. I am using a different port number (4723) in my code. I am not sure why the error is still occurring. I have tried to close the server using the _server.close() method, but it did not work. I am assuming that I am missing something in the code. Can anyone please help me with this?19Thanks for the reply. I am using a different port number (4723) in my code. I am not sure why the error is still occurring. I have tried to close the server using the _server.close() method, but it did not work. I am assuming that I
Using AI Code Generation
1const { AppiumServer } = require('appium-base-driver');2const { server } = require('appium-express');3const { startServer } = require('appium');4const _server = server;5const _appiumServer = AppiumServer;6const _startServer = startServer;7async function test() {8 const server = await _startServer({9 });10 const appiumServer = new _appiumServer(server);11 await appiumServer.start();12 console.log('appium server started');13 setTimeout(async () => {14 await appiumServer.close();15 console.log('appium server closed');16 }, 10000);17}18test();
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!!