• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.84
/src/main/java/com/meterware/httpunit/cookies/Cookie.java
1
/*
2
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit.cookies;
9

10
import java.net.URL;
11
import java.text.DateFormat;
12
import java.text.ParseException;
13
import java.text.SimpleDateFormat;
14
import java.util.Iterator;
15
import java.util.Locale;
16
import java.util.Map;
17
import java.util.TimeZone;
18

19
/**
20
 * An HTTP client-side cookie.
21
 **/
22
public class Cookie {
23

24
    /** The name. */
25
    private String _name;
26

27
    /** The value. */
28
    private String _value;
29

30
    /** The path. */
31
    private String _path;
32

33
    /** The domain. */
34
    private String _domain;
35

36
    /** The expired time. */
37
    private long _expiredTime;
38

39
    /**
40
     * Gets the expired time.
41
     *
42
     * @return the _expiredTime in milliseconds
43
     */
44
    public long getExpiredTime() {
45
        return _expiredTime;
1✔
46
    }
47

48
    /** DateFormat to be used to format original Netscape cookies. */
49
    private static final DateFormat originalCookieFormat = new SimpleDateFormat("EEE,dd-MMM-yyyy HH:mm:ss z",
1✔
50
            Locale.US);
51

52
    static {
53
        originalCookieFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
1✔
54
    }
1✔
55

56
    /**
57
     * Constructs a cookie w/o any domain or path restrictions.
58
     *
59
     * @param name
60
     *            - the name of the cookie
61
     * @param value
62
     *            - the value of the cookie
63
     */
64
    Cookie(String name, String value) {
1✔
65
        _name = name;
1✔
66
        _value = value;
1✔
67
    }
1✔
68

69
    /**
70
     * construct a cookie with domain and path restrictions.
71
     *
72
     * @param name
73
     *            the name
74
     * @param value
75
     *            the value
76
     * @param domain
77
     *            the domain
78
     * @param path
79
     *            the path
80
     */
81
    Cookie(String name, String value, String domain, String path) {
82
        this(name, value);
1✔
83
        _path = path;
1✔
84
        _domain = domain;
1✔
85
    }
1✔
86

87
    /**
88
     * Constructs a cookie w/o any domain or path restrictions.
89
     *
90
     * @param name
91
     *            - the name of the cookie
92
     * @param value
93
     *            - the value of the cookie
94
     * @param attributes
95
     *            - a map of attributes for the cookie
96
     */
97
    Cookie(String name, String value, Map attributes) {
98
        this(name, value);
1✔
99
        for (Iterator iterator = attributes.keySet().iterator(); iterator.hasNext();) {
1✔
100
            String key = (String) iterator.next();
1✔
101
            String attributeValue = (String) attributes.get(key);
1✔
102
            if (key.equalsIgnoreCase("path")) {
1✔
103
                _path = attributeValue;
1✔
104
            } else if (key.equalsIgnoreCase("domain")) {
1✔
105
                _domain = attributeValue;
1✔
106
            } else if (key.equalsIgnoreCase("max-age")) {
1✔
107
                _expiredTime = System.currentTimeMillis() + getAgeInMsec(attributeValue);
1✔
108
            } else if (key.equalsIgnoreCase("expires")) {
1!
109
                _expiredTime = getAgeInMsecFromDate(attributeValue);
1✔
110
            }
111
        }
1✔
112
    }
1✔
113

114
    /**
115
     * get the age of the cookie in Milliseconds from a string representaiton in seconds.
116
     *
117
     * @param maxAgeValue
118
     *            - the string with the age in seconds
119
     *
120
     * @return - the integer millisecond value or 0 if conversion fails
121
     */
122
    private int getAgeInMsec(String maxAgeValue) {
123
        try {
124
            return 1000 * Integer.parseInt(maxAgeValue);
1✔
125
        } catch (NumberFormatException e) {
×
126
            return 0;
×
127
        }
128
    }
129

130
    /**
131
     * return the age of a cookie in milliesconds from a string formatted date value.
132
     *
133
     * @param dateValue
134
     *            - the string to parse
135
     *
136
     * @return - milliseconds as integer or 0 if parsing fails
137
     */
138
    private long getAgeInMsecFromDate(String dateValue) {
139
        try {
140
            // SimpleDateFormat isn't thread-safe
141
            synchronized (originalCookieFormat) {
1✔
142
                return originalCookieFormat.parse(dateValue).getTime();
1✔
143
            }
144
        } catch (ParseException e) {
1✔
145
            return 0;
1✔
146
        }
147
    }
148

149
    /**
150
     * Returns the name of this cookie.
151
     *
152
     * @return the name
153
     */
154
    public String getName() {
155
        return _name;
1✔
156
    }
157

158
    /**
159
     * Returns the value associated with this cookie.
160
     *
161
     * @return the value
162
     */
163
    public String getValue() {
164
        return _value;
1✔
165
    }
166

167
    /**
168
     * Sets the value associated with this cookie.
169
     *
170
     * @param value
171
     *            the new value
172
     */
173
    public void setValue(String value) {
174
        _value = value;
1✔
175
    }
1✔
176

177
    /**
178
     * Returns the path to which this cookie is restricted.
179
     *
180
     * @return the path
181
     */
182
    public String getPath() {
183
        return _path;
1✔
184
    }
185

186
    /**
187
     * Returns the domain to which this cookie may be sent.
188
     *
189
     * @return the domain
190
     */
191
    public String getDomain() {
192
        return _domain;
1✔
193
    }
194

195
    /**
196
     * Sets the path.
197
     *
198
     * @param path
199
     *            the new path
200
     */
201
    void setPath(String path) {
202
        _path = path;
1✔
203
    }
1✔
204

205
    /**
206
     * Sets the domain.
207
     *
208
     * @param domain
209
     *            the new domain
210
     */
211
    void setDomain(String domain) {
212
        _domain = domain;
1✔
213
    }
1✔
214

215
    @Override
216
    public int hashCode() {
217
        int hashCode = _name.hashCode();
×
218
        if (_domain != null) {
×
219
            hashCode ^= _domain.hashCode();
×
220
        }
221
        if (_path != null) {
×
222
            hashCode ^= _path.hashCode();
×
223
        }
224
        return hashCode;
×
225
    }
226

227
    @Override
228
    public boolean equals(Object obj) {
229
        return obj.getClass() == getClass() && equals((Cookie) obj);
1!
230
    }
231

232
    /**
233
     * Equals.
234
     *
235
     * @param other
236
     *            the other
237
     *
238
     * @return true, if successful
239
     */
240
    private boolean equals(Cookie other) {
241
        return _name.equalsIgnoreCase(other._name) && equalProperties(getDomain(), other.getDomain())
1✔
242
                && equalProperties(getPath(), other.getPath());
1!
243
    }
244

245
    /**
246
     * Equal properties.
247
     *
248
     * @param first
249
     *            the first
250
     * @param second
251
     *            the second
252
     *
253
     * @return true, if successful
254
     */
255
    private boolean equalProperties(String first, String second) {
256
        return first == second || first != null && first.equals(second);
1!
257
    }
258

259
    /**
260
     * check whether the cookie is expired.
261
     *
262
     * @return true if the _expiredTime is higher than the current System time
263
     */
264
    public boolean isExpired() {
265
        return _expiredTime != 0 && _expiredTime <= System.currentTimeMillis();
1✔
266
    }
267

268
    /**
269
     * may this cookie be sent to the given url?.
270
     *
271
     * @param url
272
     *            - the unform resource locator to check
273
     *
274
     * @return true if the cookie is not expired and the path is accepted if a domain is set
275
     */
276
    public boolean mayBeSentTo(URL url) {
277
        if (getDomain() == null) {
1✔
278
            return true;
1✔
279
        }
280
        if (isExpired()) {
1✔
281
            return false;
1✔
282
        }
283

284
        return acceptHost(getDomain(), url.getHost()) && acceptPath(getPath(), url.getPath());
1✔
285
    }
286

287
    /**
288
     * accept path for the given hostpath.
289
     *
290
     * @param pathPattern
291
     *            the path pattern
292
     * @param hostPath
293
     *            the host path
294
     *
295
     * @return true - either if PathMatching is not strict or the hostpath starts with the given path pattern
296
     */
297
    private boolean acceptPath(String pathPattern, String hostPath) {
298
        return !CookieProperties.isPathMatchingStrict() || hostPath.startsWith(pathPattern);
1✔
299
    }
300

301
    /**
302
     * accept host if the given hostName fits to the given hostPattern.
303
     *
304
     * @param hostPattern
305
     *            the host pattern
306
     * @param hostName
307
     *            the host name
308
     *
309
     * @return true if there is a fit
310
     */
311
    private static boolean acceptHost(String hostPattern, String hostName) {
312
        return hostPattern.equalsIgnoreCase(hostName) || hostPattern.startsWith(".") && hostName.endsWith(hostPattern);
1✔
313
    }
314

315
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc