github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/public/libs/sockjs-client-1.1.0/tests/lib/transports.js (about)

     1  /* eslint camelcase: 0 */
     2  'use strict';
     3  
     4  var expect = require('expect.js')
     5    , transportList = require('../../lib/transport-list')
     6    , testUtils = require('./test-utils')
     7    , echoTests = require('./echo-tests')
     8    , batchTests = require('./batch-tests')
     9    , SockJS = require('../../lib/entry')
    10    ;
    11  
    12  function userClose(url, transport) {
    13    it('user close', function (done) {
    14      var test = this.runnable();
    15      this.timeout(10000);
    16      var sjs = new SockJS(url + '/echo', null, { transports: transport });
    17      expect(sjs).to.be.ok();
    18      var counter = 0;
    19  
    20      sjs.onopen = function() {
    21        counter++;
    22        try {
    23          expect(counter).to.equal(1);
    24          sjs.close(3000, 'User message');
    25          expect(counter).to.equal(1);
    26        } catch (e) {
    27          done(e);
    28        }
    29      };
    30      sjs.onmessage = function() {
    31        done(new Error());
    32        sjs.close();
    33        counter++;
    34      };
    35      sjs.onclose = function(e) {
    36        if (test.timedOut || test.duration) {
    37          return;
    38        }
    39  
    40        counter++;
    41        try {
    42          expect(e.wasClean).to.equal(true);
    43          expect(counter).to.equal(2);
    44        } catch (err) {
    45          done(err);
    46          return;
    47        }
    48  
    49        done();
    50      };
    51    });
    52  }
    53  
    54  function serverClose(url, transport) {
    55    it('server close', function (done) {
    56      var test = this.runnable();
    57      this.timeout(10000);
    58      var sjs = new SockJS(url + '/close', null, { transports: transport });
    59      expect(sjs).to.be.ok();
    60      var i = 0;
    61      sjs.onopen = function() {
    62        i++;
    63      };
    64      sjs.onmessage = function() {
    65        done(new Error());
    66        sjs.close();
    67      };
    68      sjs.onclose = function(e) {
    69        if (test.timedOut || test.duration) {
    70          return;
    71        }
    72  
    73        try {
    74          expect(i).to.equal(1);
    75          expect(e.code).to.equal(3000);
    76          expect(e.reason).to.equal('Go away!');
    77          expect(e.wasClean).to.equal(true);
    78        } catch (err) {
    79          done(err);
    80          return;
    81        }
    82        done();
    83      };
    84    });
    85  }
    86  
    87  function runTests(url, transport) {
    88    echoTests.echoBasic(url, transport);
    89    echoTests.echoQueryString(url, transport);
    90    echoTests.echoRich(url, transport);
    91    echoTests.echoUnicode(url, transport);
    92    echoTests.echoSpecialChars(url, transport);
    93    echoTests.echoLargeMessage(url, transport);
    94    echoTests.echoUtfEncodingSimple(url, transport);
    95    echoTests.echoUtfEncoding(url, transport);
    96  
    97    batchTests.largeMessage(url, transport);
    98    batchTests.largeDownload(url, transport);
    99  
   100    userClose(url, transport);
   101    serverClose(url, transport);
   102  }
   103  
   104  describe('Transports', function () {
   105    transportList.forEach(function (Trans) {
   106      describe(Trans.transportName, function () {
   107        it('has a valid interface', function () {
   108          expect(Trans).to.be.ok();
   109          expect(Trans).to.have.property('transportName');
   110          expect(Trans.transportName.length).to.be.greaterThan(0);
   111  
   112          expect(Trans).to.have.property('roundTrips');
   113          expect(Trans.roundTrips).to.be.a('number');
   114  
   115          expect(Trans).to.have.property('enabled');
   116          expect(Trans.enabled).to.be.a('function');
   117  
   118          expect(Trans.prototype).to.have.property('send');
   119          expect(Trans.prototype.send).to.be.a('function');
   120  
   121          expect(Trans.prototype).to.have.property('close');
   122          expect(Trans.prototype.close).to.be.a('function');
   123  
   124          //var t = new Trans('http://localhost');
   125          //expect(t).to.be.an(EventEmitter);
   126          // TODO tests for event emitting
   127        });
   128  
   129        if (!Trans.enabled({ cookie_needed: false, nullOrigin: false, sameScheme: true, sameOrigin: true })) {
   130          it('[unsupported]');
   131          return;
   132        }
   133  
   134        var transport = Trans.transportName;
   135  
   136        var soUrl = testUtils.getSameOriginUrl();
   137        describe('same origin', function () {
   138          runTests(soUrl, transport);
   139        });
   140  
   141        // var corsUrl = testUtils.getCrossOriginUrl();
   142        // if (corsUrl && corsUrl !== soUrl) {
   143        //   describe('cross origin', function () {
   144        //     runTests(corsUrl, transport);
   145        //   });
   146        // }
   147      });
   148    });
   149  });