File: date_test.js

package info (click to toggle)
node-tough-cookie 2.3.4+dfsg-1~bpo9+1
  • links: PTS, VCS
  • area: main
  • in suites: stretch-backports
  • size: 608 kB
  • sloc: makefile: 8; sh: 2
file content (199 lines) | stat: -rw-r--r-- 7,391 bytes parent folder | download | duplicates (2)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*!
 * Copyright (c) 2015, Salesforce.com, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * 3. Neither the name of Salesforce.com nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

'use strict';
var vows = require('vows');
var assert = require('assert');
var tough = require('../lib/cookie');
require('string.prototype.repeat'); // polyfill

function dateVows(table) {
  var theVows = {};
  Object.keys(table).forEach(function (date) {
    var expect = table[date];
    theVows[date] = function () {
      var got = tough.parseDate(date) ? true : false;
      if (expect && !got) {
        assert.ok(false, "expected valid date but was invalid");
      } else if (!expect && got) {
        assert.ok(false, "expected invalid date but was valid");
      } else {
        assert.ok(true);
      }
    };
  });
  return {"date parsing": theVows};
}

function equivalenceVows(table) {
  var theVows = {};
  Object.keys(table).forEach(function (thisDate) {
    var sameAs = table[thisDate];
    var label = "'"+thisDate+"' parses the same as '"+sameAs+"'";
    theVows[label] = function () {
      var expected = tough.parseDate(sameAs);
      var actual = tough.parseDate(thisDate);
      if (!expected && !actual) {
        assert.ok(false, "both dates failed to parse!");
      }
      assert.equal(actual.toString(), expected.toString());
    };
  });
  return {"equivalence parsing": theVows};
}

var TOO_MANY_XS = String("x").repeat(65535);

vows
  .describe('Date')
  .addBatch(dateVows({
    "Wed, 09 Jun 2021 10:18:14 GMT": true,
    "Wed, 09 JUN 2021 10:18:14 GMT": true,
    "Wed, 09 Jun 2021 22:18:14 GMT": true,
    "Tue, 18 Oct 2011 07:42:42.123 GMT": true,
    "18 Oct 2011 07:42:42 GMT": true,
    "8 Oct 2011 7:42:42 GMT": true,
    "8 Oct 2011 7:2:42 GMT": true,
    "8 Oct 2011 7:2:2 GMT": true,
    "Oct 18 2011 07:42:42 GMT": true,
    "Tue Oct 18 2011 07:05:03 GMT+0000 (GMT)": true,
    "09 Jun 2021 10:18:14 GMT": true,
    "99 Jix 3038 48:86:72 ZMT": false,
    '01 Jan 1970 00:00:00 GMT': true,
    '01 Jan 1600 00:00:00 GMT': false, // before 1601
    '01 Jan 1601 00:00:00 GMT': true,
    '10 Feb 81 13:00:00 GMT': true, // implicit year
    'Thu, 17-Apr-2014 02:12:29 GMT': true, // dashes
    'Thu, 17-Apr-2014 02:12:29 UTC': true,  // dashes and UTC

    // garbage after parts:
    "Wedxxx, 09 Jun 2021 10:18:14 GMT": true, // day of week doesn't matter
    "Wed, 09e9 Jun 2021 10:18:14 GMT": true, // garbage after day ignored
    "Wed, 09 Junxxx 2021 10:18:14 GMT": true, // prefix match on month
    "Wed, 09 Jun 2021e9 10:18:14 GMT": true, // garbage after year OK
    "Wed, 09 Jun 2021 10e9:18:14 GMT": false, // can't have garbage after HH
    "Wed, 09 Jun 2021 10:18e9:14 GMT": false, // can't have garbage after MM
    "Wed, 09 Jun 2021 10:18:14e9 GMT": true, // garbage after SS ignored

    // extra digit in time parts:
    "Thu, 01 Jan 1970 000:00:01 GMT": false,
    "Thu, 01 Jan 1970 00:000:01 GMT": false,
    "Thu, 01 Jan 1970 00:00:010 GMT": false,

    // hex in time
    "Wed, 09 Jun 2021 1a:33:44 GMT": false,
    "Wed, 09 Jun 2021 a1:33:44 GMT": false,
    "Wed, 09 Jun 2021 11:f3:44 GMT": false,
    "Wed, 09 Jun 2021 11:3f:44 GMT": false,
    "Wed, 09 Jun 2021 11:33:e4 GMT": false,
    "Wed, 09 Jun 2021 11:33:4e GMT": true, // garbage after seconds is OK

    // negatives in time
    "Wed, 09 Jun 2021 -1:33:44 GMT": true, // parses as 1:33; - is a delimiter
    "Wed, 09 Jun 2021 11:-3:44 GMT": false,
    "Wed, 09 Jun 2021 11:33:-4 GMT": false,

    "": false
  }))
  .addBatch({
    "reDos hr": {
      topic: function () {
        var str = "Wed, 09 Jun 2021 10" + TOO_MANY_XS + ":18:14 GMT";
        return tough.parseDate(str, true) ? true : false;
      },
      "invalid": function (date) {
        assert.equal(date, false);
      }
    },
    "reDos min": {
      topic: function () {
        var str = "Wed, 09 Jun 2021 10:18" + TOO_MANY_XS + ":14 GMT";
        return tough.parseDate(str, true) ? true : false;
      },
      "invalid": function (date) {
        assert.equal(date, false);
      }
    },
    "reDos sec": {
      topic: function () {
        var str = "Wed, 09 Jun 2021 10:18:14" + TOO_MANY_XS + " GMT";
        return tough.parseDate(str, true) ? true : false;
      },
      "valid": function (date) {
        assert.equal(date, true);
      }
    }
  })
  .addBatch(equivalenceVows({
    // milliseconds ignored
    "Tue, 18 Oct 2011 07:42:42.123 GMT": "Tue, 18 Oct 2011 07:42:42 GMT",

    // shorter HH:MM:SS works how you'd expect:
    "8 Oct 2011 7:32:42 GMT": "8 Oct 2011 07:32:42 GMT",
    "8 Oct 2011 7:2:42 GMT":  "8 Oct 2011 07:02:42 GMT",
    "8 Oct 2011 7:2:2 GMT":   "8 Oct 2011 07:02:02 GMT",

    // MDY versus DMY:
    "Oct 18 2011 07:42:42 GMT": "18 Oct 2011 07:42:42 GMT",

    // some other messy auto format
    "Tue Oct 18 2011 07:05:03 GMT+0000 (GMT)": "Tue, 18 Oct 2011 07:05:03 GMT", 

    // short year
    '10 Feb 81 13:00:00 GMT': '10 Feb 1981 13:00:00 GMT', 
    '10 Feb 17 13:00:00 GMT': '10 Feb 2017 13:00:00 GMT', 

    // dashes
    'Thu, 17-Apr-2014 02:12:29 GMT': 'Thu, 17 Apr 2014 02:12:29 GMT',
    // dashes and "UTC" (timezone is always ignored)
    'Thu, 17-Apr-2014 02:12:29 UTC': 'Thu, 17 Apr 2014 02:12:29 GMT', 

    // no weekday
    "09 Jun 2021 10:18:14 GMT": "Wed, 09 Jun 2021 10:18:14 GMT",

    // garbage after seconds is OK
    "Wed, 09 Jun 2021 11:33:4e GMT": "Wed, 09 Jun 2021 11:33:04 GMT", 

    // - is delimiter in this position
    "Wed, 09 Jun 2021 -1:33:44 GMT": "Wed, 09 Jun 2021 01:33:44 GMT",

    // prefix match on month
    "Wed, 09 Junxxx 2021 10:18:14 GMT": "Wed, 09 Jun 2021 10:18:14 GMT",
    "09 November 2021 10:18:14 GMT": "09 Nov 2021 10:18:14 GMT",

    // case of Month
    "Wed, 09 JUN 2021 10:18:14 GMT": "Wed, 09 Jun 2021 10:18:14 GMT",
    "Wed, 09 jUN 2021 10:18:14 GMT": "Wed, 09 Jun 2021 10:18:14 GMT",

    // test the framework :wink:
    "Wed, 09 Jun 2021 10:18:14 GMT": "Wed, 09 Jun 2021 10:18:14 GMT" 
  }))
  .export(module);