• 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

69.23
/src/main/java/com/meterware/pseudoserver/HttpUserAgentTest.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.pseudoserver;
21

22
import static org.junit.jupiter.api.Assertions.assertEquals;
23
import static org.junit.jupiter.api.Assertions.fail;
24

25
import java.io.IOException;
26
import java.util.ArrayList;
27
import java.util.Collections;
28
import java.util.Enumeration;
29
import java.util.List;
30
import java.util.StringTokenizer;
31

32
import org.junit.jupiter.api.extension.RegisterExtension;
33

34
/**
35
 * A base class for test cases that use the pseudo server.
36
 **/
37
public class HttpUserAgentTest {
1✔
38

39
    /** The Constant testSupport. */
40
    @RegisterExtension
41
    public static final PseudoServerTestSupport testSupport = new PseudoServerTestSupport();
1✔
42

43
    /**
44
     * Define resource.
45
     *
46
     * @param resourceName
47
     *            the resource name
48
     * @param servlet
49
     *            the servlet
50
     */
51
    protected void defineResource(String resourceName, PseudoServlet servlet) {
52
        testSupport.defineResource(resourceName, servlet);
1✔
53
    }
1✔
54

55
    /**
56
     * Define resource.
57
     *
58
     * @param resourceName
59
     *            the resource name
60
     * @param value
61
     *            the value
62
     */
63
    protected void defineResource(String resourceName, String value) {
64
        testSupport.defineResource(resourceName, value);
1✔
65
    }
1✔
66

67
    /**
68
     * Define resource.
69
     *
70
     * @param resourceName
71
     *            the resource name
72
     * @param value
73
     *            the value
74
     * @param contentType
75
     *            the content type
76
     */
77
    protected void defineResource(String resourceName, byte[] value, String contentType) {
78
        testSupport.defineResource(resourceName, value, contentType);
1✔
79
    }
1✔
80

81
    /**
82
     * Define resource.
83
     *
84
     * @param resourceName
85
     *            the resource name
86
     * @param value
87
     *            the value
88
     * @param statusCode
89
     *            the status code
90
     */
91
    protected void defineResource(String resourceName, String value, int statusCode) {
92
        testSupport.defineResource(resourceName, value, statusCode);
1✔
93
    }
1✔
94

95
    /**
96
     * Define resource.
97
     *
98
     * @param resourceName
99
     *            the resource name
100
     * @param value
101
     *            the value
102
     * @param contentType
103
     *            the content type
104
     */
105
    protected void defineResource(String resourceName, String value, String contentType) {
106
        testSupport.defineResource(resourceName, value, contentType);
1✔
107
    }
1✔
108

109
    /**
110
     * Adds the resource header.
111
     *
112
     * @param resourceName
113
     *            the resource name
114
     * @param header
115
     *            the header
116
     */
117
    protected void addResourceHeader(String resourceName, String header) {
118
        testSupport.addResourceHeader(resourceName, header);
1✔
119
    }
1✔
120

121
    /**
122
     * Sets the resource char set.
123
     *
124
     * @param resourceName
125
     *            the resource name
126
     * @param setName
127
     *            the set name
128
     * @param reportCharSet
129
     *            the report char set
130
     */
131
    protected void setResourceCharSet(String resourceName, String setName, boolean reportCharSet) {
132
        testSupport.setResourceCharSet(resourceName, setName, reportCharSet);
1✔
133
    }
1✔
134

135
    /**
136
     * define a Web Page with the given page name and boy adding the html and body tags with pageName as the title of
137
     * the page use the given xml names space if it is not null.
138
     *
139
     * @param xmlns
140
     *            the xmlns
141
     * @param pageName
142
     *            the page name
143
     * @param body
144
     *            the body
145
     */
146
    protected void defineWebPage(String xmlns, String pageName, String body) {
147
        testSupport.defineWebPage(xmlns, pageName, body);
1✔
148
    }
1✔
149

150
    /**
151
     * define a Web Page with the given page name and boy adding the html and body tags with pageName as the title of
152
     * the page.
153
     *
154
     * @param pageName
155
     *            the page name
156
     * @param body
157
     *            the body
158
     */
159
    protected void defineWebPage(String pageName, String body) {
160
        testSupport.defineWebPage(pageName, body);
1✔
161
    }
1✔
162

163
    /**
164
     * Map to classpath.
165
     *
166
     * @param directory
167
     *            the directory
168
     */
169
    protected void mapToClasspath(String directory) {
170
        testSupport.mapToClasspath(directory);
1✔
171
    }
1✔
172

173
    /**
174
     * Gets the server.
175
     *
176
     * @return the server
177
     */
178
    protected PseudoServer getServer() {
179
        return testSupport.getServer();
×
180
    }
181

182
    /**
183
     * Sets the server debug.
184
     *
185
     * @param enabled
186
     *            the new server debug
187
     */
188
    protected void setServerDebug(boolean enabled) {
189
        testSupport.setServerDebug(enabled);
×
190
    }
×
191

192
    /**
193
     * Gets the host path.
194
     *
195
     * @return the host path
196
     */
197
    protected String getHostPath() {
198
        return testSupport.getHostPath();
1✔
199
    }
200

201
    /**
202
     * Gets the host port.
203
     *
204
     * @return the host port
205
     *
206
     * @throws IOException
207
     *             Signals that an I/O exception has occurred.
208
     */
209
    protected int getHostPort() throws IOException {
210
        return testSupport.getHostPort();
1✔
211
    }
212

213
    /**
214
     * Assert equal queries.
215
     *
216
     * @param query1
217
     *            the query 1
218
     * @param query2
219
     *            the query 2
220
     */
221
    protected void assertEqualQueries(String query1, String query2) {
222
        assertEquals(new QuerySpec(query1), new QuerySpec(query2));
1✔
223
    }
1✔
224

225
    /**
226
     * Assert implement.
227
     *
228
     * @param comment
229
     *            the comment
230
     * @param objects
231
     *            the objects
232
     * @param expectedClass
233
     *            the expected class
234
     */
235
    protected void assertImplement(String comment, Object[] objects, Class expectedClass) {
236
        if (objects.length == 0) {
1!
237
            fail("No " + comment + " found.");
×
238
        }
239
        for (Object object : objects) {
1✔
240
            assertImplements(comment, object, expectedClass);
1✔
241
        }
242
    }
1✔
243

244
    /**
245
     * Assert implements.
246
     *
247
     * @param comment
248
     *            the comment
249
     * @param object
250
     *            the object
251
     * @param expectedClass
252
     *            the expected class
253
     */
254
    protected void assertImplements(String comment, Object object, Class expectedClass) {
255
        if (object == null) {
1!
256
            fail(comment + " should be of class " + expectedClass.getName() + " but is null");
×
257
        } else if (!expectedClass.isInstance(object)) {
1!
258
            fail(comment + " should be of class " + expectedClass.getName() + " but is " + object.getClass().getName());
×
259
        }
260
    }
1✔
261

262
    /**
263
     * Assert matching set.
264
     *
265
     * @param comment
266
     *            the comment
267
     * @param expected
268
     *            the expected
269
     * @param found
270
     *            the found
271
     */
272
    protected void assertMatchingSet(String comment, Object[] expected, Enumeration found) {
273
        List foundItems = new ArrayList<>();
1✔
274
        while (found.hasMoreElements()) {
1✔
275
            foundItems.add(found.nextElement());
1✔
276
        }
277

278
        assertMatchingSet(comment, expected, foundItems);
1✔
279
    }
1✔
280

281
    /**
282
     * Assert matching set.
283
     *
284
     * @param comment
285
     *            the comment
286
     * @param expected
287
     *            the expected
288
     * @param foundItems
289
     *            the found items
290
     */
291
    private void assertMatchingSet(String comment, Object[] expected, List foundItems) {
292
        List expectedItems = new ArrayList<>();
1✔
293
        for (Object element : expected) {
1✔
294
            expectedItems.add(element);
1✔
295
        }
296
        for (Object element : expected) {
1✔
297
            if (!foundItems.contains(element)) {
1!
298
                fail(comment + ": expected " + asText(expected) + " but missing " + element);
×
299
            } else {
300
                foundItems.remove(element);
1✔
301
            }
302
        }
303

304
        if (!foundItems.isEmpty()) {
1!
305
            fail(comment + ": expected " + asText(expected) + " but found superfluous" + foundItems);
×
306
        }
307
    }
1✔
308

309
    /**
310
     * Assert matching set.
311
     *
312
     * @param comment
313
     *            the comment
314
     * @param expected
315
     *            the expected
316
     * @param found
317
     *            the found
318
     */
319
    public static void assertMatchingSet(String comment, Object[] expected, Object[] found) {
320
        List foundItems = new ArrayList<>();
1✔
321
        for (Object element : found) {
1✔
322
            foundItems.add(element);
1✔
323
        }
324

325
        List expectedItems = new ArrayList<>();
1✔
326

327
        for (Object element : expected) {
1✔
328
            expectedItems.add(element);
1✔
329
        }
330

331
        for (Object element : expected) {
1✔
332
            if (!foundItems.contains(element)) {
1!
333
                fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
334
            } else {
335
                foundItems.remove(element);
1✔
336
            }
337
        }
338

339
        for (Object element : found) {
1✔
340
            if (!expectedItems.contains(element)) {
1!
341
                fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
342
            } else {
343
                expectedItems.remove(element);
1✔
344
            }
345
        }
346

347
        if (!foundItems.isEmpty()) {
1!
348
            fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
349
        }
350
    }
1✔
351

352
    /**
353
     * As text.
354
     *
355
     * @param args
356
     *            the args
357
     *
358
     * @return the string
359
     */
360
    public static String asText(Object[] args) {
361
        StringBuilder sb = new StringBuilder("{");
×
362
        for (int i = 0; i < args.length; i++) {
×
363
            if (i != 0) {
×
364
                sb.append(",");
×
365
            }
366
            sb.append('"').append(args[i]).append('"');
×
367
        }
368
        sb.append("}");
×
369
        return sb.toString();
×
370
    }
371

372
    /**
373
     * As bytes.
374
     *
375
     * @param s
376
     *            the s
377
     *
378
     * @return the string
379
     */
380
    protected String asBytes(String s) {
381
        StringBuilder sb = new StringBuilder();
×
382
        char[] chars = s.toCharArray();
×
383
        for (char element : chars) {
×
384
            sb.append(Integer.toHexString(element)).append(" ");
×
385
        }
386
        return sb.toString();
×
387
    }
388

389
    /**
390
     * The Class QuerySpec.
391
     */
392
    static class QuerySpec {
393

394
        /**
395
         * Instantiates a new query spec.
396
         *
397
         * @param urlString
398
         *            the url string
399
         */
400
        QuerySpec(String urlString) {
1✔
401
            if (urlString.indexOf('?') < 0) {
1!
402
                _path = urlString;
×
403
            } else {
404
                _path = urlString.substring(0, urlString.indexOf('?'));
1✔
405
            }
406
            _fullString = urlString;
1✔
407

408
            StringTokenizer st = new StringTokenizer(urlString.substring(urlString.indexOf('?') + 1), "&");
1✔
409
            while (st.hasMoreTokens()) {
1✔
410
                _parameters.add(st.nextToken());
1✔
411
            }
412
        }
1✔
413

414
        @Override
415
        public String toString() {
416
            return _fullString;
×
417
        }
418

419
        @Override
420
        public boolean equals(Object o) {
421
            return getClass().equals(o.getClass()) && equals((QuerySpec) o);
1!
422
        }
423

424
        @Override
425
        public int hashCode() {
426
            return _path.hashCode() ^ _parameters.size();
×
427
        }
428

429
        /** The path. */
430
        private String _path;
431

432
        /** The full string. */
433
        private String _fullString;
434

435
        /** The parameters. */
436
        private List _parameters = new ArrayList<>();
1✔
437

438
        /**
439
         * Equals.
440
         *
441
         * @param o
442
         *            the o
443
         *
444
         * @return true, if successful
445
         */
446
        private boolean equals(QuerySpec o) {
447
            if (!_path.equals(o._path) || _parameters.size() != o._parameters.size()) {
1!
448
                return false;
×
449
            }
450
            for (Enumeration e = Collections.enumeration(o._parameters); e.hasMoreElements();) {
1✔
451
                if (!_parameters.contains(e.nextElement())) {
1!
452
                    return false;
×
453
                }
454
            }
455
            return true;
1✔
456
        }
457
    }
458
}
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