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

hazendaz / httpunit / 389

12 Aug 2025 11:17PM UTC coverage: 80.48% (-0.02%) from 80.503%
389

push

github

hazendaz
Merge branch 'master' into javax

3216 of 4105 branches covered (78.34%)

Branch coverage included in aggregate %.

238 of 258 new or added lines in 68 files covered. (92.25%)

2 existing lines in 2 files now uncovered.

8254 of 10147 relevant lines covered (81.34%)

0.81 hits per line

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

82.81
/src/main/java/com/meterware/pseudoserver/HttpRequest.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 com.meterware.httpunit.HttpUnitUtils;
23

24
import java.io.IOException;
25
import java.io.InputStream;
26
import java.util.Hashtable;
27
import java.util.StringTokenizer;
28

29
/**
30
 * Represents a single HTTP request, extracted from the input stream.
31
 *
32
 * @since 1.6
33
 */
34
public class HttpRequest extends ReceivedHttpMessage {
35

36
    private String _protocol;
37
    private String _command;
38
    private String _uri;
39
    private Hashtable _parameters;
40

41
    HttpRequest(InputStream inputStream) throws IOException {
42
        super(inputStream);
1✔
43
    }
1✔
44

45
    @Override
46
    void interpretMessageHeader(String messageHeader) {
47
        StringTokenizer st = new StringTokenizer(messageHeader);
1✔
48
        _command = st.nextToken();
1✔
49
        _uri = st.nextToken();
1✔
50
        _protocol = st.nextToken();
1✔
51
    }
1✔
52

53
    @Override
54
    void appendMessageHeader(StringBuilder sb) {
55
        sb.append(_command).append(' ').append(_uri).append(' ').append(_protocol);
1✔
56
    }
1✔
57

58
    /**
59
     * Returns the command associated with this request.
60
     */
61
    public String getCommand() {
62
        return _command;
1✔
63
    }
64

65
    /**
66
     * Returns the URI specified in the message header for this request.
67
     */
68
    public String getURI() {
69
        return _uri;
1✔
70
    }
71

72
    /**
73
     * Returns the protocol string specified in the message header for this request.
74
     */
75
    public String getProtocol() {
76
        return _protocol;
1✔
77
    }
78

79
    /**
80
     * Returns the parameter with the specified name. If no such parameter exists, will return null.
81
     **/
82
    public String[] getParameter(String name) {
83
        if (_parameters == null) {
1!
84
            if (_command.equalsIgnoreCase("GET") || _command.equalsIgnoreCase("HEAD")) {
1!
85
                _parameters = readParameters(getParameterString(_uri));
1✔
86
            } else {
87
                _parameters = readParameters(new String(getBody()));
1✔
88
            }
89
        }
90
        return (String[]) _parameters.get(name);
1✔
91
    }
92

93
    private String getParameterString(String uri) {
94
        return uri.indexOf('?') < 0 ? "" : uri.substring(uri.indexOf('?') + 1);
1!
95
    }
96

97
    boolean wantsKeepAlive() {
98
        if ("Keep-alive".equalsIgnoreCase(getConnectionHeader())) {
1✔
99
            return true;
1✔
100
        }
101
        if (_protocol.equals("HTTP/1.1")) {
1✔
102
            return !"Close".equalsIgnoreCase(getConnectionHeader());
1✔
103
        }
104
        return false;
1✔
105
    }
106

107
    private Hashtable readParameters(String content) {
108
        Hashtable parameters = new Hashtable<>();
1✔
109
        if (content == null || content.trim().isEmpty()) {
1!
UNCOV
110
            return parameters;
×
111
        }
112

113
        for (String spec : content.split("&")) {
1✔
114
            String[] split = spec.split("=");
1✔
115
            addParameter(parameters, HttpUnitUtils.decode(split[0]),
1✔
116
                    split.length < 2 ? null : HttpUnitUtils.decode(split[1]));
1✔
117
        }
118
        return parameters;
1✔
119
    }
120

121
    private void addParameter(Hashtable parameters, String name, String value) {
122
        String[] oldValues = (String[]) parameters.get(name);
1✔
123
        if (oldValues == null) {
1!
124
            parameters.put(name, new String[] { value });
1✔
125
        } else {
126
            String[] values = new String[oldValues.length + 1];
×
127
            System.arraycopy(oldValues, 0, values, 0, oldValues.length);
×
128
            values[oldValues.length] = value;
×
129
            parameters.put(name, values);
×
130
        }
131
    }
1✔
132

133
    private String getConnectionHeader() {
134
        return getHeader("Connection");
1✔
135
    }
136

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