-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdate.test.js
More file actions
119 lines (101 loc) · 4.83 KB
/
date.test.js
File metadata and controls
119 lines (101 loc) · 4.83 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
/**!
* hessian.js - test/date.test.js
*
* Copyright(c) 2014
* MIT Licensed
*
* Authors:
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
"use strict";
/**
* Module dependencies.
*/
var should = require('should');
var hessian = require('../');
var utils = require('./utils');
describe('date.test.js', function () {
var dateBuffer = new Buffer(['d'.charCodeAt(0), 0x00, 0x00, 0x00, 0xd0, 0x4b, 0x92, 0x84, 0xb8]);
it('should read date 2:51:31 May 8, 1998', function () {
var d = hessian.decode(dateBuffer);
d.should.be.an.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
hessian.decode(dateBuffer, true).should.eql({
$class: 'java.util.Date',
$: new Date(894621091000)
});
});
it('should write date 2:51:31 May 8, 1998', function () {
hessian.encode(new Date(894621091000)).should.eql(dateBuffer);
hessian.encode({$class: 'java.util.Date', $:new Date(894621091000)}).should.eql(dateBuffer);
});
it('should write date 0 and read', function () {
hessian.encode(new Date(0)).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
hessian.encode({$class: 'java.util.Date', $: new Date(0)}).should.eql(new Buffer(['d'.charCodeAt(0), 0, 0, 0, 0, 0, 0, 0, 0]));
});
it('should read date 09:51:31 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v1/date/894621091000'), '1.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
hessian.decode(utils.bytes('v1/date/894621091000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
});
it('should read date 09:51:00 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v1/date/894621060000'), '1.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621060000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');
hessian.decode(utils.bytes('v1/date/894621060000'), '1.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
});
it('should write date', function () {
var now = new Date(1398280514000);
hessian.encode(now, '1.0').should.eql(utils.bytes('v1/date/now'));
hessian.encode({$class: 'java.util.Date', $: now}, '1.0').should.eql(utils.bytes('v1/date/now'));
// read it
hessian.decode(utils.bytes('v1/date/now'), '1.0').should.eql(now);
hessian.decode(utils.bytes('v1/date/now'), '1.0', true).should.eql({$class: 'java.util.Date', $: now});
});
describe('hessian 2.0', function () {
it('should read date 09:51:31 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v2/date/894621091000'), '2.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621091000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:31 GMT');
d.toISOString().should.equal('1998-05-08T09:51:31.000Z');
hessian.decode(utils.bytes('v2/date/894621091000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621091000)});
});
it('should read Compact: date in minutes, 09:51:00 May 8, 1998 UTC', function () {
var d = hessian.decode(utils.bytes('v2/date/894621060000'), '2.0');
d.should.be.a.Date;
d.getFullYear().should.equal(1998);
d.getTime().should.equal(894621060000);
d.toUTCString().should.equal('Fri, 08 May 1998 09:51:00 GMT');
d.toISOString().should.equal('1998-05-08T09:51:00.000Z');
hessian.decode(utils.bytes('v2/date/894621060000'), '2.0', true).should.eql({$class: 'java.util.Date', $: new Date(894621060000)});
});
it('should write and read date', function () {
var now = new Date(1398280514000);
hessian.encode(now, '2.0').should.eql(utils.bytes('v2/date/now'));
hessian.encode({$class: 'java.util.Date', $: now}, '2.0').should.eql(utils.bytes('v2/date/now'));
// read it
hessian.decode(utils.bytes('v2/date/now'), '2.0').should.eql(now);
hessian.decode(utils.bytes('v2/date/now'), '2.0', true).should.eql({$class: 'java.util.Date', $: now});
});
// it('should read 1.0 format', function () {
// hessian.decode(utils.bytes('v1/date/894621091000'), '2.0').getTime()
// .should.equal(894621091000);
// hessian.decode(utils.bytes('v1/date/894621060000'), '2.0').getTime()
// .should.equal(894621060000);
// hessian.decode(utils.bytes('v1/date/now'), '2.0').getTime()
// .should.equal(1398280514000);
// });
});
});