• 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

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
public class HttpRequest extends ReceivedHttpMessage {
33

34
    /** The protocol. */
35
    private String _protocol;
36

37
    /** The command. */
38
    private String _command;
39

40
    /** The uri. */
41
    private String _uri;
42

43
    /** The parameters. */
44
    private Hashtable _parameters;
45

46
    /**
47
     * Instantiates a new http request.
48
     *
49
     * @param inputStream
50
     *            the input stream
51
     *
52
     * @throws IOException
53
     *             Signals that an I/O exception has occurred.
54
     */
55
    HttpRequest(InputStream inputStream) throws IOException {
56
        super(inputStream);
1✔
57
    }
1✔
58

59
    @Override
60
    void interpretMessageHeader(String messageHeader) {
61
        StringTokenizer st = new StringTokenizer(messageHeader);
1✔
62
        _command = st.nextToken();
1✔
63
        _uri = st.nextToken();
1✔
64
        _protocol = st.nextToken();
1✔
65
    }
1✔
66

67
    @Override
68
    void appendMessageHeader(StringBuilder sb) {
69
        sb.append(_command).append(' ').append(_uri).append(' ').append(_protocol);
1✔
70
    }
1✔
71

72
    /**
73
     * Returns the command associated with this request.
74
     *
75
     * @return the command
76
     */
77
    public String getCommand() {
78
        return _command;
1✔
79
    }
80

81
    /**
82
     * Returns the URI specified in the message header for this request.
83
     *
84
     * @return the uri
85
     */
86
    public String getURI() {
87
        return _uri;
1✔
88
    }
89

90
    /**
91
     * Returns the protocol string specified in the message header for this request.
92
     *
93
     * @return the protocol
94
     */
95
    public String getProtocol() {
96
        return _protocol;
1✔
97
    }
98

99
    /**
100
     * Returns the parameter with the specified name. If no such parameter exists, will return null.
101
     *
102
     * @param name
103
     *            the name
104
     *
105
     * @return the parameter
106
     */
107
    public String[] getParameter(String name) {
108
        if (_parameters == null) {
1!
109
            if (_command.equalsIgnoreCase("GET") || _command.equalsIgnoreCase("HEAD")) {
1!
110
                _parameters = readParameters(getParameterString(_uri));
1✔
111
            } else {
112
                _parameters = readParameters(new String(getBody()));
1✔
113
            }
114
        }
115
        return (String[]) _parameters.get(name);
1✔
116
    }
117

118
    /**
119
     * Gets the parameter string.
120
     *
121
     * @param uri
122
     *            the uri
123
     *
124
     * @return the parameter string
125
     */
126
    private String getParameterString(String uri) {
127
        return uri.indexOf('?') < 0 ? "" : uri.substring(uri.indexOf('?') + 1);
1!
128
    }
129

130
    /**
131
     * Wants keep alive.
132
     *
133
     * @return true, if successful
134
     */
135
    boolean wantsKeepAlive() {
136
        if ("Keep-alive".equalsIgnoreCase(getConnectionHeader())) {
1✔
137
            return true;
1✔
138
        }
139
        if (_protocol.equals("HTTP/1.1")) {
1✔
140
            return !"Close".equalsIgnoreCase(getConnectionHeader());
1✔
141
        }
142
        return false;
1✔
143
    }
144

145
    /**
146
     * Read parameters.
147
     *
148
     * @param content
149
     *            the content
150
     *
151
     * @return the hashtable
152
     */
153
    private Hashtable readParameters(String content) {
154
        Hashtable parameters = new Hashtable<>();
1✔
155
        if (content == null || content.trim().isEmpty()) {
1!
156
            return parameters;
×
157
        }
158

159
        for (String spec : content.split("&")) {
1✔
160
            String[] split = spec.split("=");
1✔
161
            addParameter(parameters, HttpUnitUtils.decode(split[0]),
1✔
162
                    split.length < 2 ? null : HttpUnitUtils.decode(split[1]));
1✔
163
        }
164
        return parameters;
1✔
165
    }
166

167
    /**
168
     * Adds the parameter.
169
     *
170
     * @param parameters
171
     *            the parameters
172
     * @param name
173
     *            the name
174
     * @param value
175
     *            the value
176
     */
177
    private void addParameter(Hashtable parameters, String name, String value) {
178
        String[] oldValues = (String[]) parameters.get(name);
1✔
179
        if (oldValues == null) {
1!
180
            parameters.put(name, new String[] { value });
1✔
181
        } else {
182
            String[] values = new String[oldValues.length + 1];
×
183
            System.arraycopy(oldValues, 0, values, 0, oldValues.length);
×
184
            values[oldValues.length] = value;
×
185
            parameters.put(name, values);
×
186
        }
187
    }
1✔
188

189
    /**
190
     * Gets the connection header.
191
     *
192
     * @return the connection header
193
     */
194
    private String getConnectionHeader() {
195
        return getHeader("Connection");
1✔
196
    }
197

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