• 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

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

10
import static org.junit.jupiter.api.Assertions.assertEquals;
11
import static org.junit.jupiter.api.Assertions.fail;
12

13
import java.io.IOException;
14
import java.util.ArrayList;
15
import java.util.Collections;
16
import java.util.Enumeration;
17
import java.util.List;
18
import java.util.StringTokenizer;
19

20
import org.junit.jupiter.api.extension.RegisterExtension;
21

22
/**
23
 * A base class for test cases that use the pseudo server.
24
 **/
25
public class HttpUserAgentTest {
1✔
26

27
    /** The Constant testSupport. */
28
    @RegisterExtension
29
    public static final PseudoServerTestSupport testSupport = new PseudoServerTestSupport();
1✔
30

31
    /**
32
     * Define resource.
33
     *
34
     * @param resourceName
35
     *            the resource name
36
     * @param servlet
37
     *            the servlet
38
     */
39
    protected void defineResource(String resourceName, PseudoServlet servlet) {
40
        testSupport.defineResource(resourceName, servlet);
1✔
41
    }
1✔
42

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

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

69
    /**
70
     * Define resource.
71
     *
72
     * @param resourceName
73
     *            the resource name
74
     * @param value
75
     *            the value
76
     * @param statusCode
77
     *            the status code
78
     */
79
    protected void defineResource(String resourceName, String value, int statusCode) {
80
        testSupport.defineResource(resourceName, value, statusCode);
1✔
81
    }
1✔
82

83
    /**
84
     * Define resource.
85
     *
86
     * @param resourceName
87
     *            the resource name
88
     * @param value
89
     *            the value
90
     * @param contentType
91
     *            the content type
92
     */
93
    protected void defineResource(String resourceName, String value, String contentType) {
94
        testSupport.defineResource(resourceName, value, contentType);
1✔
95
    }
1✔
96

97
    /**
98
     * Adds the resource header.
99
     *
100
     * @param resourceName
101
     *            the resource name
102
     * @param header
103
     *            the header
104
     */
105
    protected void addResourceHeader(String resourceName, String header) {
106
        testSupport.addResourceHeader(resourceName, header);
1✔
107
    }
1✔
108

109
    /**
110
     * Sets the resource char set.
111
     *
112
     * @param resourceName
113
     *            the resource name
114
     * @param setName
115
     *            the set name
116
     * @param reportCharSet
117
     *            the report char set
118
     */
119
    protected void setResourceCharSet(String resourceName, String setName, boolean reportCharSet) {
120
        testSupport.setResourceCharSet(resourceName, setName, reportCharSet);
1✔
121
    }
1✔
122

123
    /**
124
     * define a Web Page with the given page name and boy adding the html and body tags with pageName as the title of
125
     * the page use the given xml names space if it is not null.
126
     *
127
     * @param xmlns
128
     *            the xmlns
129
     * @param pageName
130
     *            the page name
131
     * @param body
132
     *            the body
133
     */
134
    protected void defineWebPage(String xmlns, String pageName, String body) {
135
        testSupport.defineWebPage(xmlns, pageName, body);
1✔
136
    }
1✔
137

138
    /**
139
     * define a Web Page with the given page name and boy adding the html and body tags with pageName as the title of
140
     * the page.
141
     *
142
     * @param pageName
143
     *            the page name
144
     * @param body
145
     *            the body
146
     */
147
    protected void defineWebPage(String pageName, String body) {
148
        testSupport.defineWebPage(pageName, body);
1✔
149
    }
1✔
150

151
    /**
152
     * Map to classpath.
153
     *
154
     * @param directory
155
     *            the directory
156
     */
157
    protected void mapToClasspath(String directory) {
158
        testSupport.mapToClasspath(directory);
1✔
159
    }
1✔
160

161
    /**
162
     * Gets the server.
163
     *
164
     * @return the server
165
     */
166
    protected PseudoServer getServer() {
167
        return testSupport.getServer();
×
168
    }
169

170
    /**
171
     * Sets the server debug.
172
     *
173
     * @param enabled
174
     *            the new server debug
175
     */
176
    protected void setServerDebug(boolean enabled) {
177
        testSupport.setServerDebug(enabled);
×
178
    }
×
179

180
    /**
181
     * Gets the host path.
182
     *
183
     * @return the host path
184
     */
185
    protected String getHostPath() {
186
        return testSupport.getHostPath();
1✔
187
    }
188

189
    /**
190
     * Gets the host port.
191
     *
192
     * @return the host port
193
     *
194
     * @throws IOException
195
     *             Signals that an I/O exception has occurred.
196
     */
197
    protected int getHostPort() throws IOException {
198
        return testSupport.getHostPort();
1✔
199
    }
200

201
    /**
202
     * Assert equal queries.
203
     *
204
     * @param query1
205
     *            the query 1
206
     * @param query2
207
     *            the query 2
208
     */
209
    protected void assertEqualQueries(String query1, String query2) {
210
        assertEquals(new QuerySpec(query1), new QuerySpec(query2));
1✔
211
    }
1✔
212

213
    /**
214
     * Assert implement.
215
     *
216
     * @param comment
217
     *            the comment
218
     * @param objects
219
     *            the objects
220
     * @param expectedClass
221
     *            the expected class
222
     */
223
    protected void assertImplement(String comment, Object[] objects, Class expectedClass) {
224
        if (objects.length == 0) {
1!
225
            fail("No " + comment + " found.");
×
226
        }
227
        for (Object object : objects) {
1✔
228
            assertImplements(comment, object, expectedClass);
1✔
229
        }
230
    }
1✔
231

232
    /**
233
     * Assert implements.
234
     *
235
     * @param comment
236
     *            the comment
237
     * @param object
238
     *            the object
239
     * @param expectedClass
240
     *            the expected class
241
     */
242
    protected void assertImplements(String comment, Object object, Class expectedClass) {
243
        if (object == null) {
1!
244
            fail(comment + " should be of class " + expectedClass.getName() + " but is null");
×
245
        } else if (!expectedClass.isInstance(object)) {
1!
246
            fail(comment + " should be of class " + expectedClass.getName() + " but is " + object.getClass().getName());
×
247
        }
248
    }
1✔
249

250
    /**
251
     * Assert matching set.
252
     *
253
     * @param comment
254
     *            the comment
255
     * @param expected
256
     *            the expected
257
     * @param found
258
     *            the found
259
     */
260
    protected void assertMatchingSet(String comment, Object[] expected, Enumeration found) {
261
        List foundItems = new ArrayList<>();
1✔
262
        while (found.hasMoreElements()) {
1✔
263
            foundItems.add(found.nextElement());
1✔
264
        }
265

266
        assertMatchingSet(comment, expected, foundItems);
1✔
267
    }
1✔
268

269
    /**
270
     * Assert matching set.
271
     *
272
     * @param comment
273
     *            the comment
274
     * @param expected
275
     *            the expected
276
     * @param foundItems
277
     *            the found items
278
     */
279
    private void assertMatchingSet(String comment, Object[] expected, List foundItems) {
280
        List expectedItems = new ArrayList<>();
1✔
281
        for (Object element : expected) {
1✔
282
            expectedItems.add(element);
1✔
283
        }
284
        for (Object element : expected) {
1✔
285
            if (!foundItems.contains(element)) {
1!
286
                fail(comment + ": expected " + asText(expected) + " but missing " + element);
×
287
            } else {
288
                foundItems.remove(element);
1✔
289
            }
290
        }
291

292
        if (!foundItems.isEmpty()) {
1!
293
            fail(comment + ": expected " + asText(expected) + " but found superfluous" + foundItems);
×
294
        }
295
    }
1✔
296

297
    /**
298
     * Assert matching set.
299
     *
300
     * @param comment
301
     *            the comment
302
     * @param expected
303
     *            the expected
304
     * @param found
305
     *            the found
306
     */
307
    public static void assertMatchingSet(String comment, Object[] expected, Object[] found) {
308
        List foundItems = new ArrayList<>();
1✔
309
        for (Object element : found) {
1✔
310
            foundItems.add(element);
1✔
311
        }
312

313
        List expectedItems = new ArrayList<>();
1✔
314

315
        for (Object element : expected) {
1✔
316
            expectedItems.add(element);
1✔
317
        }
318

319
        for (Object element : expected) {
1✔
320
            if (!foundItems.contains(element)) {
1!
321
                fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
322
            } else {
323
                foundItems.remove(element);
1✔
324
            }
325
        }
326

327
        for (Object element : found) {
1✔
328
            if (!expectedItems.contains(element)) {
1!
329
                fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
330
            } else {
331
                expectedItems.remove(element);
1✔
332
            }
333
        }
334

335
        if (!foundItems.isEmpty()) {
1!
336
            fail(comment + ": expected " + asText(expected) + " but found " + asText(found));
×
337
        }
338
    }
1✔
339

340
    /**
341
     * As text.
342
     *
343
     * @param args
344
     *            the args
345
     *
346
     * @return the string
347
     */
348
    public static String asText(Object[] args) {
349
        StringBuilder sb = new StringBuilder("{");
×
350
        for (int i = 0; i < args.length; i++) {
×
351
            if (i != 0) {
×
352
                sb.append(",");
×
353
            }
354
            sb.append('"').append(args[i]).append('"');
×
355
        }
356
        sb.append("}");
×
357
        return sb.toString();
×
358
    }
359

360
    /**
361
     * As bytes.
362
     *
363
     * @param s
364
     *            the s
365
     *
366
     * @return the string
367
     */
368
    protected String asBytes(String s) {
369
        StringBuilder sb = new StringBuilder();
×
370
        char[] chars = s.toCharArray();
×
371
        for (char element : chars) {
×
372
            sb.append(Integer.toHexString(element)).append(" ");
×
373
        }
374
        return sb.toString();
×
375
    }
376

377
    /**
378
     * The Class QuerySpec.
379
     */
380
    static class QuerySpec {
381

382
        /**
383
         * Instantiates a new query spec.
384
         *
385
         * @param urlString
386
         *            the url string
387
         */
388
        QuerySpec(String urlString) {
1✔
389
            if (urlString.indexOf('?') < 0) {
1!
390
                _path = urlString;
×
391
            } else {
392
                _path = urlString.substring(0, urlString.indexOf('?'));
1✔
393
            }
394
            _fullString = urlString;
1✔
395

396
            StringTokenizer st = new StringTokenizer(urlString.substring(urlString.indexOf('?') + 1), "&");
1✔
397
            while (st.hasMoreTokens()) {
1✔
398
                _parameters.add(st.nextToken());
1✔
399
            }
400
        }
1✔
401

402
        @Override
403
        public String toString() {
404
            return _fullString;
×
405
        }
406

407
        @Override
408
        public boolean equals(Object o) {
409
            return getClass().equals(o.getClass()) && equals((QuerySpec) o);
1!
410
        }
411

412
        @Override
413
        public int hashCode() {
414
            return _path.hashCode() ^ _parameters.size();
×
415
        }
416

417
        /** The path. */
418
        private String _path;
419

420
        /** The full string. */
421
        private String _fullString;
422

423
        /** The parameters. */
424
        private List _parameters = new ArrayList<>();
1✔
425

426
        /**
427
         * Equals.
428
         *
429
         * @param o
430
         *            the o
431
         *
432
         * @return true, if successful
433
         */
434
        private boolean equals(QuerySpec o) {
435
            if (!_path.equals(o._path) || _parameters.size() != o._parameters.size()) {
1!
436
                return false;
×
437
            }
438
            for (Enumeration e = Collections.enumeration(o._parameters); e.hasMoreElements();) {
1✔
439
                if (!_parameters.contains(e.nextElement())) {
1!
440
                    return false;
×
441
                }
442
            }
443
            return true;
1✔
444
        }
445
    }
446
}
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