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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

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
    private String _name;
37

38
    private String _value;
39

40
    private String _path;
41

42
    private String _domain;
43

44
    private long _expiredTime;
45

46
    /**
47
     * @return the _expiredTime in milliseconds
48
     */
49
    public long getExpiredTime() {
50
        return _expiredTime;
1✔
51
    }
52

53
    /**
54
     * DateFormat to be used to format original Netscape cookies
55
     */
56
    private static final DateFormat originalCookieFormat = new SimpleDateFormat("EEE,dd-MMM-yyyy HH:mm:ss z",
1✔
57
            Locale.US);
58

59
    static {
60
        originalCookieFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
1✔
61
    }
1✔
62

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

76
    /**
77
     * construct a cookie with domain and path restrictions
78
     *
79
     * @param name
80
     * @param value
81
     * @param domain
82
     * @param path
83
     */
84
    Cookie(String name, String value, String domain, String path) {
85
        this(name, value);
1✔
86
        _path = path;
1✔
87
        _domain = domain;
1✔
88
    }
1✔
89

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

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

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

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

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

166
    /**
167
     * Sets the value associated with this cookie.
168
     */
169
    public void setValue(String value) {
170
        _value = value;
1✔
171
    }
1✔
172

173
    /**
174
     * Returns the path to which this cookie is restricted.
175
     */
176
    public String getPath() {
177
        return _path;
1✔
178
    }
179

180
    /**
181
     * Returns the domain to which this cookie may be sent.
182
     */
183
    public String getDomain() {
184
        return _domain;
1✔
185
    }
186

187
    void setPath(String path) {
188
        _path = path;
1✔
189
    }
1✔
190

191
    void setDomain(String domain) {
192
        _domain = domain;
1✔
193
    }
1✔
194

195
    @Override
196
    public int hashCode() {
197
        int hashCode = _name.hashCode();
×
198
        if (_domain != null) {
×
199
            hashCode ^= _domain.hashCode();
×
200
        }
201
        if (_path != null) {
×
202
            hashCode ^= _path.hashCode();
×
203
        }
204
        return hashCode;
×
205
    }
206

207
    @Override
208
    public boolean equals(Object obj) {
209
        return obj.getClass() == getClass() && equals((Cookie) obj);
1!
210
    }
211

212
    private boolean equals(Cookie other) {
213
        return _name.equalsIgnoreCase(other._name) && equalProperties(getDomain(), other.getDomain())
1✔
214
                && equalProperties(getPath(), other.getPath());
1!
215
    }
216

217
    private boolean equalProperties(String first, String second) {
218
        return first == second || first != null && first.equals(second);
1!
219
    }
220

221
    /**
222
     * check whether the cookie is expired
223
     *
224
     * @return true if the _expiredTime is higher than the current System time
225
     */
226
    public boolean isExpired() {
227
        return _expiredTime != 0 && _expiredTime <= System.currentTimeMillis();
1✔
228
    }
229

230
    /**
231
     * may this cookie be sent to the given url?
232
     *
233
     * @param url
234
     *            - the unform resource locator to check
235
     *
236
     * @return true if the cookie is not expired and the path is accepted if a domain is set
237
     */
238
    public boolean mayBeSentTo(URL url) {
239
        if (getDomain() == null) {
1✔
240
            return true;
1✔
241
        }
242
        if (isExpired()) {
1✔
243
            return false;
1✔
244
        }
245

246
        return acceptHost(getDomain(), url.getHost()) && acceptPath(getPath(), url.getPath());
1✔
247
    }
248

249
    /**
250
     * accept path for the given hostpath
251
     *
252
     * @param pathPattern
253
     * @param hostPath
254
     *
255
     * @return true - either if PathMatching is not strict or the hostpath starts with the given path pattern
256
     */
257
    private boolean acceptPath(String pathPattern, String hostPath) {
258
        return !CookieProperties.isPathMatchingStrict() || hostPath.startsWith(pathPattern);
1✔
259
    }
260

261
    /**
262
     * accept host if the given hostName fits to the given hostPattern
263
     *
264
     * @param hostPattern
265
     * @param hostName
266
     *
267
     * @return true if there is a fit
268
     */
269
    private static boolean acceptHost(String hostPattern, String hostName) {
270
        return hostPattern.equalsIgnoreCase(hostName) || hostPattern.startsWith(".") && hostName.endsWith(hostPattern);
1✔
271
    }
272

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