Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
1
/*!
2
* Copyright (c) 2015, Salesforce.com, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
*
11
* 2. Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
*
15
* 3. Neither the name of Salesforce.com nor the names of its contributors may
16
* be used to endorse or promote products derived from this software without
17
* specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
* POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
'use strict';
33
var vows = require('vows');
34
var assert = require('assert');
35
var fs = require('fs');
36
var path = require('path');
37
var url = require('url');
38
var tough = require('../lib/cookie');
39
var Cookie = tough.Cookie;
40
var CookieJar = tough.CookieJar;
41
42
function readJson(filePath) {
43
filePath = path.join(__dirname, filePath);
44
return JSON.parse(fs.readFileSync(filePath).toString());
45
}
46
47
function setGetCookieVows() {
48
var theVows = {};
49
var data = readJson('./ietf_data/parser.json');
50
51
data.forEach(function (testCase) {
52
theVows[testCase.test] = function () {
53
var jar = new CookieJar();
54
var expected = testCase['sent']
55
var sentFrom = 'http://home.example.org/cookie-parser?' + testCase.test;
56
var sentTo = testCase['sent-to'] ?
57
url.resolve('http://home.example.org', testCase['sent-to']) :
58
'http://home.example.org/cookie-parser-result?' + testCase.test;
59
60
testCase['received'].forEach(function (cookieStr) {
61
jar.setCookieSync(cookieStr, sentFrom, {ignoreError: true});
62
});
63
64
var actual = jar.getCookiesSync(sentTo);
65
actual = actual.sort(tough.cookieCompare);
66
67
assert.strictEqual(actual.length, expected.length);
68
69
actual.forEach(function (actualCookie, idx) {
70
var expectedCookie = expected[idx];
71
assert.strictEqual(actualCookie.key, expectedCookie.name);
72
assert.strictEqual(actualCookie.value, expectedCookie.value);
73
});
74
};
75
});
76
77
return {'Set/get cookie tests': theVows};
78
}
79
80
function dateVows() {
81
var theVows = {};
82
83
[
84
'./ietf_data/dates/bsd-examples.json',
85
'./ietf_data/dates/examples.json'
86
].forEach(function (filePath) {
87
var data = readJson(filePath);
88
var fileName = path.basename(filePath);
89
90
data.forEach(function (testCase) {
91
theVows[fileName + ' : ' + testCase.test] = function () {
92
var actual = tough.parseDate(testCase.test);
93
actual = actual ? actual.toUTCString() : null;
94
assert.strictEqual(actual, testCase.expected);
95
};
96
});
97
});
98
99
return {'Date': theVows};
100
}
101
102
vows
103
.describe('IETF http state tests')
104
.addBatch(setGetCookieVows())
105
.addBatch(dateVows())
106
.export(module);
107
108