Blame view

node_modules/object-hash/test/object-classes.js 3.12 KB
ce4c83ff   wxy   初始提交
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  'use strict';
  
  var assert = require('assert');
  var crypto = require('crypto');
  var stream = require('stream');
  var hash = require('../index');
  var validSha1 = /^[0-9a-f]{40}$/i;
  
  describe('hash() objects with custom class names', function() {
    var builtinToString;
    beforeEach(function() {
      builtinToString = Object.prototype.toString;
      Object.prototype.toString = function() {
        if (this && typeof this.__className !== 'undefined') {
          return this.__className;
        }
        
        return builtinToString.apply(this, arguments);
      };
    });
    
    afterEach(function() {
      Object.prototype.toString = builtinToString;
    });
    
    it('should throw when trying to hash an unknown object', function() {
      assert.throws(function() {
        hash({a:1, __className: '[object Foo]'});
      }, /Unknown object type "foo"/);
      
      assert.throws(function() {
        hash({a:1, __className: 'Foo'});
      }, /Unknown object type/);
    });
  
    it('should not throw when trying to hash an unknown object with ignoreUnknown', function() {
      var opt = {ignoreUnknown: true};
      
      assert.ok(validSha1.test(hash({a:1, __className: '[object Foo]'}, opt)));
    });
  
    it('should not throw when trying to hash a weirdly-named object with ignoreUnknown', function() {
      var opt = {ignoreUnknown: true};
      
      assert.ok(validSha1.test(hash({a:1, __className: 'Foo'}, opt)));
    });
    
    it('should not delve further into a number of native types', function() {
      var nativeTypes = [
        'domwindow',
        'process', 'timer', 'pipe', 'tcp', 'udp', 'tty', 'statwatcher',
        'securecontext', 'connection', 'zlib', 'context', 'nodescript',
        'httpparser', 'dataview', 'signal', 'fsevent', 'tlswrap'
      ];
      
      for (var i = 0; i < nativeTypes.length; i++) {
        var obj = { foobar: 1, __className: '[object ' + nativeTypes[i] + ']' };
        var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
        assert.strictEqual(serialized, nativeTypes[i]);
      }
    });
    
    it('should hash xml based on its string representation', function() {
      var obj = {
        __className: '[object xml]',
        toString: function() { return 'Bananä' }
      };
      
      var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
      assert.strictEqual(serialized, 'xml:Bananä');
    });
    
    it('should hash URLs based on its string representation', function() {
      var obj = {
        __className: '[object url]',
        toString: function() { return 'https://example.com/' }
      };
      
      var serialized = hash(obj, { algorithm: 'passthrough', encoding: 'utf8' });
      assert.strictEqual(serialized, 'url:https://example.com/');
    });
    
    it('should not hash blobs without ignoreUnknown', function() {
      var obj = {
        __className: '[object blob]'
      };
      
      assert.throws(function() {
        hash(obj);
      }, /not supported/);
    });
    
    it('should ignore blobs with ignoreUnknown', function() {
      var obj = {
        __className: '[object blob]'
      };
      
      var serialized = hash(obj, {
        algorithm: 'passthrough',
        encoding: 'utf8',
        ignoreUnknown: true
      });
      
      assert.strictEqual(serialized, '[blob]');
    });
  });