-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCookie.js
167 lines (138 loc) · 3.1 KB
/
Cookie.js
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
var
url = require('url'),
Cookie;
/**
* @param {Object} data
* @param {String} data.name The name of the cookie.
* @param {String} data.value The cookie value.
* @param {String} data.path The cookie path. Defaults to '/'.
* @param {String} data.domain The domain the cookie is visible to. Defaults to current domain.
* @param {Boolean} data.secure Whether the cookie is a secure cookie. Defaults to false.
* @param {Number} data.expiry When the cookie expires, specified in seconds since midnight, January 1, 1970 UTC.
* @param {WebDriver} wd
*
* @constructor
*/
Cookie = function (data, wd) {
var name, value, path, domain, secure, expiry;
/**
* @returns {String}
*/
this.getName = function () {
return name;
};
/**
* @param {String} name_
*/
this.setName = function (name_) {
if (typeof name_ !== 'string' || !name_) {
throw new TypeError('Cookie name should be a non-empty string');
}
name = name_;
return this;
};
/**
* @returns {String}
*/
this.getValue = function () {
return data.value;
};
/**
* @param {String} value_
*/
this.setValue = function (value_) {
if (value_ === undefined) {
throw new TypeError('Cookie value is not defined');
}
value = value_;
return this;
};
/**
* @returns {String}
*/
this.getPath = function () {
return path;
};
/**
* @param {String|undefined} path_
*/
this.setPath = function (path_) {
path = path_ || '/';
return this;
};
/**
* @returns {String}
*/
this.getDomain = function () {
return domain;
};
/**
* @param {String|undefined} domain_
*/
this.setDomain = function (domain_) {
domain = domain_ || url.parse(wd.getCurrentUrl()).hostname;
return this;
};
/**
* @returns {Boolean}
*/
this.getSecure = function () {
return secure;
};
/**
* @param {Boolean} secure_
*/
this.setSecure = function (secure_) {
secure = !!secure_;
return this;
};
/**
* @returns {Number}
*/
this.getExpiry = function () {
return expiry;
};
/**
* @param {Number} expiry_
*/
this.setExpiry = function (expiry_) {
expiry_ = parseInt(expiry_, 10);
if (expiry_ < 0) {
expiry_ = 0;
}
expiry = expiry_;
return this;
};
/**
* @param {Number} hours
*/
this.setExpiryInHours = function (hours) {
expiry = ((new Date).getTime() + hours * 3600) / 1000;
return this;
};
/**
* @returns {{name: String, value: String, path: String, domain: String, secure: Boolean, expiry: Number}}
*/
this.toObject = function () {
return {
name: this.getName(),
value: this.getValue(),
path: this.getPath(),
domain: this.getDomain(),
secure: this.getSecure(),
expiry: this.getExpiry()
};
};
data = data || {};
this.setName(data.name);
this.setValue(data.value);
this.setPath(data.path);
this.setDomain(data.domain);
this.setSecure(data.secure);
if (data.expiryHours) {
this.setExpiryInHours(data.expiryHours);
} else {
this.setExpiry(data.expiry);
}
};
module.exports = Cookie;