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

     1  /* eslint new-cap: 0, no-new: 0 */
     2  /* jshint ignore: start */
     3  'use strict';
     4  
     5  var expect = require('expect.js')
     6    , SockJS = require('../../lib/entry')
     7    ;
     8  
     9  describe('SockJS', function() {
    10    describe('Constructor', function () {
    11      it('should support construction without new', function () {
    12        var s = SockJS('http://localhost');
    13        expect(s).to.be.a(SockJS);
    14        s.close();
    15      });
    16  
    17      it('create a valid WebSocket object', function () {
    18        var s = new SockJS('http://localhost');
    19        expect(s).to.have.property('url', 'http://localhost');
    20        expect(s).to.have.property('readyState', SockJS.CONNECTING);
    21        expect(s).to.have.property('extensions', '');
    22        expect(s).to.have.property('protocol', '');
    23        s.close();
    24      });
    25  
    26      it('should not remove basic authentication credentials (1/2)', function () {
    27        var s = new SockJS('http://user@localhost');
    28        expect(s).to.have.property('url', 'http://user@localhost');
    29        s.close();
    30      });
    31  
    32      it('should not remove basic authentication credentials (2/2)', function () {
    33        var s = new SockJS('http://user:password@localhost');
    34        expect(s).to.have.property('url', 'http://user:password@localhost');
    35        s.close();
    36      });
    37  
    38      describe('WebSocket specification step #1', function () {
    39        it('should throw TypeError for a null url', function () {
    40          expect(function () {
    41            new SockJS();
    42          }).to.throwException(function (e) {
    43            expect(e).to.be.a(TypeError);
    44          });
    45        });
    46  
    47        it('should throw SyntaxError when the url contains a fragment', function () {
    48          expect(function () {
    49            new SockJS('http://localhost/#test');
    50          }).to.throwException(function (e) {
    51            expect(e).to.be.a(SyntaxError);
    52          });
    53        });
    54  
    55        it('should throw SyntaxError for an invalid protocol', function () {
    56          expect(function () {
    57            new SockJS('ftp://localhost');
    58          }).to.throwException(function (e) {
    59            expect(e).to.be.a(SyntaxError);
    60          });
    61        });
    62      });
    63  
    64      describe('WebSocket specification step #5', function () {
    65        it('should throw SyntaxError for duplicated protocols', function () {
    66          expect(function () {
    67            new SockJS('http://localhost', ['test', 'test']);
    68          }).to.throwException(function (e) {
    69            expect(e).to.be.a(SyntaxError);
    70          });
    71        });
    72      });
    73  
    74      it('should generate 8-character-long session ids by default', function () {
    75        var s = SockJS('http://localhost');
    76        expect(s._generateSessionId().length).to.be(8);
    77        s.close();
    78      });
    79  
    80      it('should generate N-character-long session ids', function () {
    81        for (var i = 1; i <= 100; i++) {
    82          var s = SockJS('http://localhost', null, {sessionId: i});
    83          expect(s._generateSessionId().length).to.be(i);
    84          s.close();
    85        }
    86      });
    87  
    88      it('should generate sessionIds using the custom generator function', function () {
    89        var f = function() {
    90          return 'this_is_not_random';
    91        };
    92        var s = SockJS('http://localhost', null, {sessionId: f});
    93        expect(s._generateSessionId).to.be(f);
    94        s.close();
    95      });
    96  
    97      it('should throw TypeError if sessionId is neither a number nor a function', function () {
    98          expect(function () {
    99            new SockJS('http://localhost', null, {sessionId: 'this is wrong'});
   100          }).to.throwException(function (e) {
   101            expect(e).to.be.a(TypeError);
   102          });
   103      });
   104    });
   105  });