• 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

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

10
import com.meterware.httpunit.protocol.MessageBody;
11
import com.meterware.httpunit.protocol.ParameterCollection;
12

13
import java.io.IOException;
14
import java.io.InputStream;
15
import java.io.OutputStream;
16
import java.net.URL;
17
import java.net.URLConnection;
18

19
/**
20
 * A web request which contains a non-empty message body. Note that such requests <em>must</em> use the
21
 * <code>http</code> or <code>https</code> protocols.
22
 **/
23
public abstract class MessageBodyWebRequest extends WebRequest {
24

25
    /** The body. */
26
    protected MessageBody _body;
27

28
    /** The mime encoded. */
29
    private boolean _mimeEncoded;
30

31
    /**
32
     * Constructs a web request using a specific absolute url string.
33
     *
34
     * @param urlString
35
     *            the url string
36
     * @param mimeEncoded
37
     *            the mime encoded
38
     */
39
    protected MessageBodyWebRequest(String urlString, boolean mimeEncoded) {
40
        super(urlString);
1✔
41
        _mimeEncoded = mimeEncoded;
1✔
42
    }
1✔
43

44
    /**
45
     * Constructs a web request using a specific absolute url string.
46
     *
47
     * @param urlString
48
     *            the url string
49
     * @param messageBody
50
     *            the message body
51
     */
52
    protected MessageBodyWebRequest(String urlString, MessageBody messageBody) {
53
        super(urlString);
1✔
54
        _body = messageBody;
1✔
55
    }
1✔
56

57
    /**
58
     * Constructs a web request with a specific target.
59
     *
60
     * @param urlBase
61
     *            the url base
62
     * @param urlString
63
     *            the url string
64
     * @param target
65
     *            the target
66
     * @param mimeEncoded
67
     *            the mime encoded
68
     */
69
    protected MessageBodyWebRequest(URL urlBase, String urlString, String target, boolean mimeEncoded) {
70
        super(urlBase, urlString, target);
×
71
        _mimeEncoded = mimeEncoded;
×
72
    }
×
73

74
    /**
75
     * Constructs a web request for a form submitted via a button.
76
     *
77
     * @param sourceForm
78
     *            the source form
79
     * @param parameterHolder
80
     *            the parameter holder
81
     * @param button
82
     *            the button
83
     * @param x
84
     *            the x
85
     * @param y
86
     *            the y
87
     */
88
    protected MessageBodyWebRequest(WebForm sourceForm, ParameterHolder parameterHolder, SubmitButton button, int x,
89
            int y) {
90
        super(sourceForm, parameterHolder, button, x, y);
1✔
91
        _mimeEncoded = parameterHolder.isSubmitAsMime();
1✔
92
        setHeaderField(REFERER_HEADER_NAME, sourceForm.getBaseURL().toExternalForm());
1✔
93
    }
1✔
94

95
    /**
96
     * Constructs a web request for a form submitted via script.
97
     *
98
     * @param sourceForm
99
     *            the source form
100
     */
101
    protected MessageBodyWebRequest(WebForm sourceForm) {
102
        super(sourceForm, WebRequest.newParameterHolder(sourceForm));
1✔
103
        _mimeEncoded = sourceForm.isSubmitAsMime();
1✔
104
        setHeaderField(REFERER_HEADER_NAME, sourceForm.getBaseURL().toExternalForm());
1✔
105
    }
1✔
106

107
    /**
108
     * Subclasses may override this method to provide a message body for the request.
109
     *
110
     * @return the message body
111
     */
112
    protected MessageBody getMessageBody() {
113
        return _body;
1✔
114
    }
115

116
    // ---------------------------------- WebRequest methods --------------------------------
117

118
    @Override
119
    protected void writeMessageBody(OutputStream stream) throws IOException {
120
        getMessageBody().writeTo(stream, getParameterHolder());
1✔
121
    }
1✔
122

123
    /**
124
     * Performs any additional processing necessary to complete the request.
125
     **/
126
    @Override
127
    protected void completeRequest(URLConnection connection) throws IOException {
128
        super.completeRequest(connection);
1✔
129
        connection.setDoInput(true);
1✔
130
        connection.setDoOutput(true);
1✔
131

132
        OutputStream stream = connection.getOutputStream();
1✔
133
        writeMessageBody(stream);
1✔
134
        stream.flush();
1✔
135
        stream.close();
1✔
136
    }
1✔
137

138
    @Override
139
    protected String getContentType() {
140
        return getMessageBody().getContentType();
1✔
141
    }
142

143
    @Override
144
    public boolean isMimeEncoded() {
145
        return _mimeEncoded;
1✔
146
    }
147

148
    // ============================= class InputStreamMessageBody ======================================
149

150
    /**
151
     * A method request message body read directly from an input stream.
152
     **/
153
    public static class InputStreamMessageBody extends MessageBody {
154

155
        /**
156
         * Instantiates a new input stream message body.
157
         *
158
         * @param source
159
         *            the source
160
         * @param contentType
161
         *            the content type
162
         */
163
        public InputStreamMessageBody(InputStream source, String contentType) {
164
            super(null);
1✔
165
            _source = source;
1✔
166
            _contentType = contentType;
1✔
167
        }
1✔
168

169
        /**
170
         * Returns the content type of this message body.
171
         *
172
         * @return the content type
173
         */
174
        @Override
175
        public String getContentType() {
176
            return _contentType;
1✔
177
        }
178

179
        /**
180
         * Transmits the body of this request as a sequence of bytes.
181
         *
182
         * @param outputStream
183
         *            the output stream
184
         * @param parameters
185
         *            the parameters
186
         *
187
         * @throws IOException
188
         *             if the tranmission fails
189
         */
190
        @Override
191
        public void writeTo(OutputStream outputStream, ParameterCollection parameters) throws IOException {
192
            if (_source.markSupported()) {
1!
193
                mark();
1✔
194
            }
195
            byte[] buffer = new byte[8 * 1024];
1✔
196
            int count = 0;
1✔
197
            do {
198
                outputStream.write(buffer, 0, count);
1✔
199
                count = _source.read(buffer, 0, buffer.length);
1✔
200
            } while (count != -1);
1✔
201

202
            written = true;
1✔
203
        }
1✔
204

205
        /**
206
         * Mark.
207
         *
208
         * @throws IOException
209
         *             Signals that an I/O exception has occurred.
210
         */
211
        public void mark() throws IOException {
212
            if (written) {
1✔
213
                _source.reset();
1✔
214
            } else {
215
                // amount of bytes to be read after mark gets invalid
216
                int readlimit = 1024 * 1024; // ! MByte
1✔
217
                // Marks the current position in this input stream.
218
                // A subsequent call to the reset method repositions
219
                // this stream at the last marked position so that subsequent reads re-read the same bytes.
220
                _source.mark(readlimit);
1✔
221
            }
222
        }
1✔
223

224
        /** The written. */
225
        private boolean written = false;
1✔
226

227
        /** The source. */
228
        private InputStream _source;
229

230
        /** The content type. */
231
        private String _contentType;
232
    }
233
}
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