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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.cookies;
21

22
import java.net.URL;
23
import java.text.DateFormat;
24
import java.text.ParseException;
25
import java.text.SimpleDateFormat;
26
import java.util.Iterator;
27
import java.util.Locale;
28
import java.util.Map;
29
import java.util.TimeZone;
30

31
/**
32
 * An HTTP client-side cookie.
33
 **/
34
public class Cookie {
35

36
    /** The name. */
37
    private String _name;
38

39
    /** The value. */
40
    private String _value;
41

42
    /** The path. */
43
    private String _path;
44

45
    /** The domain. */
46
    private String _domain;
47

48
    /** The expired time. */
49
    private long _expiredTime;
50

51
    /**
52
     * Gets the expired time.
53
     *
54
     * @return the _expiredTime in milliseconds
55
     */
56
    public long getExpiredTime() {
57
        return _expiredTime;
1✔
58
    }
59

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

64
    static {
65
        originalCookieFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
1✔
66
    }
1✔
67

68
    /**
69
     * Constructs a cookie w/o any domain or path restrictions.
70
     *
71
     * @param name
72
     *            - the name of the cookie
73
     * @param value
74
     *            - the value of the cookie
75
     */
76
    Cookie(String name, String value) {
1✔
77
        _name = name;
1✔
78
        _value = value;
1✔
79
    }
1✔
80

81
    /**
82
     * construct a cookie with domain and path restrictions.
83
     *
84
     * @param name
85
     *            the name
86
     * @param value
87
     *            the value
88
     * @param domain
89
     *            the domain
90
     * @param path
91
     *            the path
92
     */
93
    Cookie(String name, String value, String domain, String path) {
94
        this(name, value);
1✔
95
        _path = path;
1✔
96
        _domain = domain;
1✔
97
    }
1✔
98

99
    /**
100
     * Constructs a cookie w/o any domain or path restrictions.
101
     *
102
     * @param name
103
     *            - the name of the cookie
104
     * @param value
105
     *            - the value of the cookie
106
     * @param attributes
107
     *            - a map of attributes for the cookie
108
     */
109
    Cookie(String name, String value, Map attributes) {
110
        this(name, value);
1✔
111
        for (Iterator iterator = attributes.keySet().iterator(); iterator.hasNext();) {
1✔
112
            String key = (String) iterator.next();
1✔
113
            String attributeValue = (String) attributes.get(key);
1✔
114
            if (key.equalsIgnoreCase("path")) {
1✔
115
                _path = attributeValue;
1✔
116
            } else if (key.equalsIgnoreCase("domain")) {
1✔
117
                _domain = attributeValue;
1✔
118
            } else if (key.equalsIgnoreCase("max-age")) {
1✔
119
                _expiredTime = System.currentTimeMillis() + getAgeInMsec(attributeValue);
1✔
120
            } else if (key.equalsIgnoreCase("expires")) {
1!
121
                _expiredTime = getAgeInMsecFromDate(attributeValue);
1✔
122
            }
123
        }
1✔
124
    }
1✔
125

126
    /**
127
     * get the age of the cookie in Milliseconds from a string representaiton in seconds.
128
     *
129
     * @param maxAgeValue
130
     *            - the string with the age in seconds
131
     *
132
     * @return - the integer millisecond value or 0 if conversion fails
133
     */
134
    private int getAgeInMsec(String maxAgeValue) {
135
        try {
136
            return 1000 * Integer.parseInt(maxAgeValue);
1✔
137
        } catch (NumberFormatException e) {
×
138
            return 0;
×
139
        }
140
    }
141

142
    /**
143
     * return the age of a cookie in milliesconds from a string formatted date value.
144
     *
145
     * @param dateValue
146
     *            - the string to parse
147
     *
148
     * @return - milliseconds as integer or 0 if parsing fails
149
     */
150
    private long getAgeInMsecFromDate(String dateValue) {
151
        try {
152
            // SimpleDateFormat isn't thread-safe
153
            synchronized (originalCookieFormat) {
1✔
154
                return originalCookieFormat.parse(dateValue).getTime();
1✔
155
            }
156
        } catch (ParseException e) {
1✔
157
            return 0;
1✔
158
        }
159
    }
160

161
    /**
162
     * Returns the name of this cookie.
163
     *
164
     * @return the name
165
     */
166
    public String getName() {
167
        return _name;
1✔
168
    }
169

170
    /**
171
     * Returns the value associated with this cookie.
172
     *
173
     * @return the value
174
     */
175
    public String getValue() {
176
        return _value;
1✔
177
    }
178

179
    /**
180
     * Sets the value associated with this cookie.
181
     *
182
     * @param value
183
     *            the new value
184
     */
185
    public void setValue(String value) {
186
        _value = value;
1✔
187
    }
1✔
188

189
    /**
190
     * Returns the path to which this cookie is restricted.
191
     *
192
     * @return the path
193
     */
194
    public String getPath() {
195
        return _path;
1✔
196
    }
197

198
    /**
199
     * Returns the domain to which this cookie may be sent.
200
     *
201
     * @return the domain
202
     */
203
    public String getDomain() {
204
        return _domain;
1✔
205
    }
206

207
    /**
208
     * Sets the path.
209
     *
210
     * @param path
211
     *            the new path
212
     */
213
    void setPath(String path) {
214
        _path = path;
1✔
215
    }
1✔
216

217
    /**
218
     * Sets the domain.
219
     *
220
     * @param domain
221
     *            the new domain
222
     */
223
    void setDomain(String domain) {
224
        _domain = domain;
1✔
225
    }
1✔
226

227
    @Override
228
    public int hashCode() {
229
        int hashCode = _name.hashCode();
×
230
        if (_domain != null) {
×
231
            hashCode ^= _domain.hashCode();
×
232
        }
233
        if (_path != null) {
×
234
            hashCode ^= _path.hashCode();
×
235
        }
236
        return hashCode;
×
237
    }
238

239
    @Override
240
    public boolean equals(Object obj) {
241
        return obj.getClass() == getClass() && equals((Cookie) obj);
1!
242
    }
243

244
    /**
245
     * Equals.
246
     *
247
     * @param other
248
     *            the other
249
     *
250
     * @return true, if successful
251
     */
252
    private boolean equals(Cookie other) {
253
        return _name.equalsIgnoreCase(other._name) && equalProperties(getDomain(), other.getDomain())
1✔
254
                && equalProperties(getPath(), other.getPath());
1!
255
    }
256

257
    /**
258
     * Equal properties.
259
     *
260
     * @param first
261
     *            the first
262
     * @param second
263
     *            the second
264
     *
265
     * @return true, if successful
266
     */
267
    private boolean equalProperties(String first, String second) {
268
        return first == second || first != null && first.equals(second);
1!
269
    }
270

271
    /**
272
     * check whether the cookie is expired.
273
     *
274
     * @return true if the _expiredTime is higher than the current System time
275
     */
276
    public boolean isExpired() {
277
        return _expiredTime != 0 && _expiredTime <= System.currentTimeMillis();
1✔
278
    }
279

280
    /**
281
     * may this cookie be sent to the given url?.
282
     *
283
     * @param url
284
     *            - the unform resource locator to check
285
     *
286
     * @return true if the cookie is not expired and the path is accepted if a domain is set
287
     */
288
    public boolean mayBeSentTo(URL url) {
289
        if (getDomain() == null) {
1✔
290
            return true;
1✔
291
        }
292
        if (isExpired()) {
1✔
293
            return false;
1✔
294
        }
295

296
        return acceptHost(getDomain(), url.getHost()) && acceptPath(getPath(), url.getPath());
1✔
297
    }
298

299
    /**
300
     * accept path for the given hostpath.
301
     *
302
     * @param pathPattern
303
     *            the path pattern
304
     * @param hostPath
305
     *            the host path
306
     *
307
     * @return true - either if PathMatching is not strict or the hostpath starts with the given path pattern
308
     */
309
    private boolean acceptPath(String pathPattern, String hostPath) {
310
        return !CookieProperties.isPathMatchingStrict() || hostPath.startsWith(pathPattern);
1✔
311
    }
312

313
    /**
314
     * accept host if the given hostName fits to the given hostPattern.
315
     *
316
     * @param hostPattern
317
     *            the host pattern
318
     * @param hostName
319
     *            the host name
320
     *
321
     * @return true if there is a fit
322
     */
323
    private static boolean acceptHost(String hostPattern, String hostName) {
324
        return hostPattern.equalsIgnoreCase(hostName) || hostPattern.startsWith(".") && hostName.endsWith(hostPattern);
1✔
325
    }
326

327
}
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