github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/tests/lib/senders.js (about) 1 'use strict'; 2 3 var expect = require('expect.js') 4 , testUtils = require('./test-utils') 5 , XhrLocal = require('../../lib/transport/sender/xhr-local') 6 , XhrCors = require('../../lib/transport/sender/xhr-cors') 7 , Xdr = require('../../lib/transport/sender/xdr') 8 ; 9 10 function ajaxSimple (Obj) { 11 it('simple', function (done) { 12 var x = new Obj('GET', testUtils.getSameOriginUrl() + '/simple.txt', null); 13 x.on('finish', function (status, text) { 14 try { 15 expect(text.length).to.equal(2051); 16 expect(text.slice(-2)).to.equal('b\n'); 17 } catch (e) { 18 done(e); 19 return; 20 } 21 done(); 22 }); 23 }); 24 } 25 26 function ajaxStreaming (Obj) { 27 if (!XhrCors.enabled) { 28 it('streaming [unsupported]'); 29 return; 30 } 31 32 it('streaming', function (done) { 33 var test = this.runnable(); 34 var x = new Obj('GET', testUtils.getSameOriginUrl() + '/streaming.txt', null); 35 var i = 0; 36 x.on('chunk', function (status/*, text*/) { 37 try { 38 expect(status).to.equal(200); 39 } catch (e) { 40 done(e); 41 x.abort(); 42 return; 43 } 44 i++; 45 // 2051 because of transparent proxies 46 //expect([2049, 2051]).to.contain(text.length); 47 }); 48 x.on('finish', function (status, text) { 49 if (test.timedOut || test.duration) { 50 return; 51 } 52 53 try { 54 expect(i).to.be.greaterThan(0); 55 expect(status).to.equal(200); 56 expect(text.slice(-4)).to.equal('a\nb\n'); 57 } catch (e) { 58 done(e); 59 return; 60 } 61 done(); 62 }); 63 }); 64 } 65 66 function wrongUrl(Obj, url, statuses) { 67 it('wrong url ' + url, function (done) { 68 var test = this.runnable(); 69 // Selenium has a long timeout for when it can't connect to the port 70 this.timeout(30000); 71 var x = new Obj('GET', url, null); 72 x.timeout = 10000; 73 x.on('chunk', function (status, text) { 74 done(new Error('No chunk should be received: ' + status + ', ' + text)); 75 x.abort(); 76 }); 77 x.on('finish', function (status, text) { 78 if (test.timedOut || test.duration) { 79 return; 80 } 81 82 try { 83 expect(statuses).to.contain(status); 84 expect(text).not.to.be(undefined); 85 } catch (e) { 86 done(e); 87 return; 88 } 89 done(); 90 }); 91 }); 92 } 93 94 function wrongPort (Obj) { 95 var badUrl; 96 if (global.location) { 97 badUrl = global.location.protocol + '//' + global.location.hostname + ':'; 98 } else { 99 badUrl = 'http://localhost:'; 100 } 101 102 var ports = [25, 8999, 65300]; 103 ports.forEach(function (port) { 104 // Sauce Labs/Selenium returns 400 when it can't connect to the port 105 wrongUrl(Obj, badUrl + port + '/wrong_url_indeed.txt', [0, 400]); 106 }); 107 } 108 109 describe('Senders', function () { 110 describe('xhr-local', function () { 111 ajaxSimple(XhrLocal); 112 ajaxStreaming(XhrLocal); 113 // TODO senders don't have a timeouts so these tests can fail 114 // BUT info-receiver has a timeout so they will never not-return 115 // wrongPort(XhrLocal); 116 wrongUrl(XhrLocal, testUtils.getSameOriginUrl() + '/wrong_url_indeed.txt', [0, 404]); 117 }); 118 119 describe('xdr', function () { 120 if (!Xdr.enabled) { 121 it('[unsupported]'); 122 return; 123 } 124 ajaxSimple(Xdr); 125 ajaxStreaming(Xdr); 126 wrongPort(Xdr); 127 wrongUrl(Xdr, testUtils.getSameOriginUrl() + '/wrong_url_indeed.txt', [0, 400]); 128 }); 129 });