• 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

59.78
/src/main/java/com/meterware/httpunit/WebConversation.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 java.io.IOException;
23
import java.net.HttpURLConnection;
24
import java.net.MalformedURLException;
25
import java.net.URL;
26
import java.net.URLConnection;
27
import java.util.Dictionary;
28
import java.util.Enumeration;
29
import java.util.Properties;
30

31
/**
32
 * The context for a series of HTTP requests. This class manages cookies used to maintain session context, computes
33
 * relative URLs, and generally emulates the browser behavior needed to build an automated test of a web site.
34
 **/
35
public class WebConversation extends WebClient {
36

37
    /** The proxy host. */
38
    private String _proxyHost;
39

40
    /** The proxy port. */
41
    private int _proxyPort;
42

43
    /** The connect timeout. */
44
    private int _connectTimeout = -1;
1✔
45

46
    /** The read timeout. */
47
    private int _readTimeout = -1;
1✔
48

49
    /**
50
     * Creates a new web conversation.
51
     **/
52
    public WebConversation() {
1✔
53
    }
1✔
54

55
    // ---------------------------------- protected members --------------------------------
56

57
    /**
58
     * Creates a web response object which represents the response to the specified web request.
59
     **/
60
    @Override
61
    protected WebResponse newResponse(WebRequest request, FrameSelector targetFrame)
62
            throws MalformedURLException, IOException {
63
        Properties savedProperties = (Properties) System.getProperties().clone();
1✔
64
        try {
65
            if (_proxyHost != null) {
1✔
66
                System.setProperty("proxyHost", _proxyHost);
1✔
67
                System.setProperty("proxyPort", Integer.toString(_proxyPort));
1✔
68
            }
69
            URLConnection connection = openConnection(getRequestURL(request));
1✔
70
            // [ 1518901 ] enable http connect and read timeouts (needs JDK 1.5)
71
            // comment the next two line if you do not need this and have JDK <1.5
72
            if (_connectTimeout >= 0) {
1!
73
                connection.setConnectTimeout(_connectTimeout);
×
74
            }
75
            if (_readTimeout >= 0) {
1!
76
                connection.setReadTimeout(_readTimeout);
×
77
            }
78
            if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
79
                String urlString = request.getURLString();
×
80
                System.out.println("\nConnecting to " + request.getURL().getHost());
×
81
                System.out.println("Sending:: " + request.getMethod() + " " + urlString);
×
82
            }
83
            sendHeaders(connection, getHeaderFields(request.getURL()));
1✔
84
            sendHeaders(connection, request.getHeaderDictionary());
1✔
85
            request.completeRequest(connection);
1✔
86
            return new HttpWebResponse(this, targetFrame, request, connection, getExceptionsThrownOnErrorStatus());
1✔
87
        } finally {
88
            System.setProperties(savedProperties);
1✔
89
        }
90
    }
91

92
    @Override
93
    public void clearProxyServer() {
94
        _proxyHost = null;
1✔
95
    }
1✔
96

97
    /**
98
     * set the proxy server to the given proxyHost with the given proxy Port
99
     *
100
     * @param proxyHost
101
     *            - the hostname of the proxy e.g. proxy.somedomain.org
102
     * @param proxyPort
103
     *            - the number of the port to use e.g. 8080
104
     */
105
    @Override
106
    public void setProxyServer(String proxyHost, int proxyPort) {
107
        _proxyHost = proxyHost;
1✔
108
        _proxyPort = proxyPort;
1✔
109
    }
1✔
110

111
    /**
112
     * Gets the connect timeout.
113
     *
114
     * @return the _connectTimeout -1 means it is not set (the default)
115
     */
116
    public int get_connectTimeout() {
117
        return _connectTimeout;
×
118
    }
119

120
    /**
121
     * set the connectionTimout -1 means it is not set (the default).
122
     *
123
     * @param timeout
124
     *            the _connectTimeout to set
125
     */
126
    public void set_connectTimeout(int timeout) {
127
        _connectTimeout = timeout;
×
128
    }
×
129

130
    /**
131
     * Gets the read timeout.
132
     *
133
     * @return the _readTimeout -1 means it is not set (the default)
134
     */
135
    public int get_readTimeout() {
136
        return _readTimeout;
×
137
    }
138

139
    /**
140
     * Sets the read timeout.
141
     *
142
     * @param timeout
143
     *            the _readTimeout to set -1 means it is not set (the default)
144
     */
145
    public void set_readTimeout(int timeout) {
146
        _readTimeout = timeout;
×
147
    }
×
148

149
    /**
150
     * get the Uniform Resource Locator for this request.
151
     *
152
     * @param request
153
     *            the request
154
     *
155
     * @return the URL
156
     *
157
     * @throws MalformedURLException
158
     *             the malformed URL exception
159
     */
160
    private URL getRequestURL(WebRequest request) throws MalformedURLException {
161
        DNSListener dnsListener = getClientProperties().getDnsListener();
1✔
162
        if (dnsListener == null) {
1!
163
            return request.getURL();
1✔
164
        }
165

166
        String hostName = request.getURL().getHost();
×
167
        String portPortion = request.getURL().getPort() == -1 ? "" : ":" + request.getURL().getPort();
×
168
        setHeaderField("Host", hostName + portPortion);
×
169
        String actualHost = dnsListener.getIpAddress(hostName);
×
170
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
×
171
            System.out.println("Rerouting request to :: " + actualHost);
×
172
        }
173
        return new URL(request.getURL().getProtocol(), actualHost, request.getURL().getPort(),
×
174
                request.getURL().getFile());
×
175
    }
176

177
    // ---------------------------------- private members --------------------------------
178

179
    /**
180
     * open a connection for the given uniform resource locator.
181
     *
182
     * @param url
183
     *            - the url to use
184
     *
185
     * @return the URL connection
186
     *
187
     * @throws MalformedURLException
188
     *             the malformed URL exception
189
     * @throws IOException
190
     *             Signals that an I/O exception has occurred.
191
     */
192
    private URLConnection openConnection(URL url) throws MalformedURLException, IOException {
193
        URLConnection connection = url.openConnection();
1✔
194
        if (connection instanceof HttpURLConnection) {
1✔
195
            ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
1✔
196
        }
197
        connection.setUseCaches(false);
1✔
198
        return connection;
1✔
199
    }
200

201
    /**
202
     * send the headers for the given connection based on the given Dictionary of headers.
203
     *
204
     * @param connection
205
     *            the connection
206
     * @param headers
207
     *            the headers
208
     */
209
    private void sendHeaders(URLConnection connection, Dictionary headers) {
210
        boolean sendReferer = getClientProperties().isSendReferer();
1✔
211
        for (Enumeration e = headers.keys(); e.hasMoreElements();) {
1✔
212
            String key = (String) e.nextElement();
1✔
213
            if (sendReferer || !"referer".equalsIgnoreCase(key)) {
1✔
214
                connection.setRequestProperty(key, (String) headers.get(key));
1✔
215
                if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
216
                    if (key.equalsIgnoreCase("authorization") || key.equalsIgnoreCase("proxy-authorization")) {
×
217
                        System.out.println("Sending:: " + key + ": " + headers.get(key));
×
218
                    } else {
219
                        System.out.println("Sending:: " + key + ": " + connection.getRequestProperty(key));
×
220
                    }
221
                }
222
            } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
223
                System.out.println("Blocked sending referer:: " + connection.getRequestProperty(key));
×
224
            }
225
        } // for
1✔
226
    }
1✔
227
}
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