• 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

92.86
/src/main/java/com/meterware/httpunit/MessageBodyWebRequest.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;
21

22
import com.meterware.httpunit.protocol.MessageBody;
23
import com.meterware.httpunit.protocol.ParameterCollection;
24

25
import java.io.IOException;
26
import java.io.InputStream;
27
import java.io.OutputStream;
28
import java.net.URL;
29
import java.net.URLConnection;
30

31
/**
32
 * A web request which contains a non-empty message body. Note that such requests <em>must</em> use the
33
 * <code>http</code> or <code>https</code> protocols.
34
 **/
35
public abstract class MessageBodyWebRequest extends WebRequest {
36

37
    /** The body. */
38
    protected MessageBody _body;
39

40
    /** The mime encoded. */
41
    private boolean _mimeEncoded;
42

43
    /**
44
     * Constructs a web request using a specific absolute url string.
45
     *
46
     * @param urlString
47
     *            the url string
48
     * @param mimeEncoded
49
     *            the mime encoded
50
     */
51
    protected MessageBodyWebRequest(String urlString, boolean mimeEncoded) {
52
        super(urlString);
1✔
53
        _mimeEncoded = mimeEncoded;
1✔
54
    }
1✔
55

56
    /**
57
     * Constructs a web request using a specific absolute url string.
58
     *
59
     * @param urlString
60
     *            the url string
61
     * @param messageBody
62
     *            the message body
63
     */
64
    protected MessageBodyWebRequest(String urlString, MessageBody messageBody) {
65
        super(urlString);
1✔
66
        _body = messageBody;
1✔
67
    }
1✔
68

69
    /**
70
     * Constructs a web request with a specific target.
71
     *
72
     * @param urlBase
73
     *            the url base
74
     * @param urlString
75
     *            the url string
76
     * @param target
77
     *            the target
78
     * @param mimeEncoded
79
     *            the mime encoded
80
     */
81
    protected MessageBodyWebRequest(URL urlBase, String urlString, String target, boolean mimeEncoded) {
82
        super(urlBase, urlString, target);
×
83
        _mimeEncoded = mimeEncoded;
×
84
    }
×
85

86
    /**
87
     * Constructs a web request for a form submitted via a button.
88
     *
89
     * @param sourceForm
90
     *            the source form
91
     * @param parameterHolder
92
     *            the parameter holder
93
     * @param button
94
     *            the button
95
     * @param x
96
     *            the x
97
     * @param y
98
     *            the y
99
     */
100
    protected MessageBodyWebRequest(WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x,
101
            int y) {
102
        super(sourceForm, parameterHolder, button, x, y);
1✔
103
        _mimeEncoded = parameterHolder.isSubmitAsMime();
1✔
104
        setHeaderField(REFERER_HEADER_NAME, sourceForm.getBaseURL().toExternalForm());
1✔
105
    }
1✔
106

107
    /**
108
     * Constructs a web request for a form submitted via script.
109
     *
110
     * @param sourceForm
111
     *            the source form
112
     */
113
    protected MessageBodyWebRequest(WebForm sourceForm) {
114
        super(sourceForm, WebRequest.newParameterHolder(sourceForm));
1✔
115
        _mimeEncoded = sourceForm.isSubmitAsMime();
1✔
116
        setHeaderField(REFERER_HEADER_NAME, sourceForm.getBaseURL().toExternalForm());
1✔
117
    }
1✔
118

119
    /**
120
     * Subclasses may override this method to provide a message body for the request.
121
     *
122
     * @return the message body
123
     */
124
    protected MessageBody getMessageBody() {
125
        return _body;
1✔
126
    }
127

128
    // ---------------------------------- WebRequest methods --------------------------------
129

130
    @Override
131
    protected void writeMessageBody(OutputStream stream) throws IOException {
132
        getMessageBody().writeTo(stream, getParameterHolder());
1✔
133
    }
1✔
134

135
    /**
136
     * Performs any additional processing necessary to complete the request.
137
     **/
138
    @Override
139
    protected void completeRequest(URLConnection connection) throws IOException {
140
        super.completeRequest(connection);
1✔
141
        connection.setDoInput(true);
1✔
142
        connection.setDoOutput(true);
1✔
143

144
        OutputStream stream = connection.getOutputStream();
1✔
145
        writeMessageBody(stream);
1✔
146
        stream.flush();
1✔
147
        stream.close();
1✔
148
    }
1✔
149

150
    @Override
151
    protected String getContentType() {
152
        return getMessageBody().getContentType();
1✔
153
    }
154

155
    @Override
156
    public boolean isMimeEncoded() {
157
        return _mimeEncoded;
1✔
158
    }
159

160
    // ============================= class InputStreamMessageBody ======================================
161

162
    /**
163
     * A method request message body read directly from an input stream.
164
     **/
165
    public static class InputStreamMessageBody extends MessageBody {
166

167
        /**
168
         * Instantiates a new input stream message body.
169
         *
170
         * @param source
171
         *            the source
172
         * @param contentType
173
         *            the content type
174
         */
175
        public InputStreamMessageBody(InputStream source, String contentType) {
176
            super(null);
1✔
177
            _source = source;
1✔
178
            _contentType = contentType;
1✔
179
        }
1✔
180

181
        /**
182
         * Returns the content type of this message body.
183
         *
184
         * @return the content type
185
         */
186
        @Override
187
        public String getContentType() {
188
            return _contentType;
1✔
189
        }
190

191
        /**
192
         * Transmits the body of this request as a sequence of bytes.
193
         *
194
         * @param outputStream
195
         *            the output stream
196
         * @param parameters
197
         *            the parameters
198
         *
199
         * @throws IOException
200
         *             if the tranmission fails
201
         */
202
        @Override
203
        public void writeTo(OutputStream outputStream, ParameterCollection parameters) throws IOException {
204
            if (_source.markSupported()) {
1!
205
                mark();
1✔
206
            }
207
            byte[] buffer = new byte[8 * 1024];
1✔
208
            int count = 0;
1✔
209
            do {
210
                outputStream.write(buffer, 0, count);
1✔
211
                count = _source.read(buffer, 0, buffer.length);
1✔
212
            } while (count != -1);
1✔
213

214
            written = true;
1✔
215
        }
1✔
216

217
        /**
218
         * Mark.
219
         *
220
         * @throws IOException
221
         *             Signals that an I/O exception has occurred.
222
         */
223
        public void mark() throws IOException {
224
            if (written) {
1✔
225
                _source.reset();
1✔
226
            } else {
227
                // amount of bytes to be read after mark gets invalid
228
                int readlimit = 1024 * 1024; // ! MByte
1✔
229
                // Marks the current position in this input stream.
230
                // A subsequent call to the reset method repositions
231
                // this stream at the last marked position so that subsequent reads re-read the same bytes.
232
                _source.mark(readlimit);
1✔
233
            }
234
        }
1✔
235

236
        /** The written. */
237
        private boolean written = false;
1✔
238

239
        /** The source. */
240
        private InputStream _source;
241

242
        /** The content type. */
243
        private String _contentType;
244
    }
245
}
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