github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/themes/wind/static/libs/sockjs-client-1.1.0/tests/lib/end-to-end.js (about)

     1  'use strict';
     2  
     3  var expect = require('expect.js')
     4    , testUtils = require('./test-utils')
     5    , XDR = require('../../lib/transport/sender/xdr')
     6    , XHRCors = require('../../lib/transport/sender/xhr-cors')
     7    , InfoIframe = require('../../lib/info-iframe')
     8    ;
     9  
    10  describe('End to End', function () {
    11    // selenium needs a long time to timeout
    12    this.timeout(30000);
    13  
    14    describe('Connection Errors', function () {
    15      it('invalid url 404', function (done) {
    16        var test = this.runnable();
    17        var sjs = testUtils.newSockJs('/invalid_url', 'jsonp-polling');
    18        expect(sjs).to.be.ok();
    19        sjs.onopen = sjs.onmessage = function () {
    20          done(new Error('Open/Message event should not fire for an invalid url'));
    21          sjs.close();
    22        };
    23        sjs.onclose = function (e) {
    24          if (test.timedOut || test.duration) {
    25            return;
    26          }
    27  
    28          try {
    29            expect(e.code).to.equal(1002);
    30            expect(e.reason).to.equal('Cannot connect to server');
    31            expect(e.wasClean).to.equal(false);
    32          } catch (err) {
    33            done(err);
    34            return;
    35          }
    36  
    37          done();
    38        };
    39      });
    40  
    41      // TODO this isn't a great way to disable this test
    42      if (!XHRCors.enabled && !XDR.enabled && !InfoIframe.enabled()) {
    43        // CORS unsupported, won't actually hit info server
    44        it('invalid url port [unsupported]');
    45        return;
    46      } else {
    47        it('invalid url port', function (done) {
    48          var test = this.runnable();
    49          var badUrl;
    50          if (global.location) {
    51            badUrl = global.location.protocol + '//' + global.location.hostname + ':1079';
    52          } else {
    53            badUrl = 'http://localhost:1079';
    54          }
    55  
    56          var sjs = testUtils.newSockJs(badUrl, 'jsonp-polling');
    57          expect(sjs).to.be.ok();
    58          sjs.onopen = sjs.onmessage = function () {
    59            done(new Error('Open/Message event should not fire for an invalid port'));
    60            sjs.close();
    61          };
    62          sjs.onclose = function (e) {
    63            if (test.timedOut || test.duration) {
    64              return;
    65            }
    66  
    67            try {
    68              expect(e.code).to.equal(1002);
    69              expect(e.reason).to.equal('Cannot connect to server');
    70              expect(e.wasClean).to.equal(false);
    71            } catch (err) {
    72              done(err);
    73              return;
    74            }
    75  
    76            done();
    77          };
    78        });
    79      }
    80  
    81      it('disabled websocket test', function (done) {
    82        var test = this.runnable();
    83        var sjs = testUtils.newSockJs('/disabled_websocket_echo', 'websocket');
    84        expect(sjs).to.be.ok();
    85        sjs.onopen = sjs.onmessage = function () {
    86          done(new Error('Open/Message event should not fire for disabled websockets'));
    87          sjs.close();
    88        };
    89        sjs.onclose = function (e) {
    90          if (test.timedOut || test.duration) {
    91            return;
    92          }
    93  
    94          try {
    95            expect(e.code).to.equal(2000);
    96            expect(e.reason).to.equal('All transports failed');
    97            expect(e.wasClean).to.equal(false);
    98          } catch (err) {
    99            done(err);
   100            return;
   101          }
   102          done();
   103        };
   104      });
   105  
   106      it('close on close', function (done) {
   107        var test = this.runnable();
   108        var sjs = testUtils.newSockJs('/close');
   109        expect(sjs).to.be.ok();
   110        sjs.onopen = function () {
   111          expect(true).to.be.ok();
   112        };
   113        sjs.onmessage = function () {
   114          done(new Error('Message should not be emitted'));
   115          sjs.close();
   116        };
   117        sjs.onclose = function (e) {
   118          if (test.timedOut || test.duration) {
   119            return;
   120          }
   121  
   122          try {
   123            expect(e.code).to.equal(3000);
   124            expect(e.reason).to.equal('Go away!');
   125            expect(e.wasClean).to.equal(true);
   126          } catch (err) {
   127            done(err);
   128            return;
   129          }
   130  
   131          sjs.onclose = function () {
   132            done(new Error());
   133          };
   134          sjs.close();
   135          setTimeout(function () {
   136            done();
   137          }, 10);
   138        };
   139      });
   140    });
   141  });