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

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

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
    private String _proxyHost;
38
    private int _proxyPort;
39
    private int _connectTimeout = -1;
1✔
40
    private int _readTimeout = -1;
1✔
41

42
    /**
43
     * Creates a new web conversation.
44
     **/
45
    public WebConversation() {
1✔
46
    }
1✔
47

48
    // ---------------------------------- protected members --------------------------------
49

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

85
    @Override
86
    public void clearProxyServer() {
87
        _proxyHost = null;
1✔
88
    }
1✔
89

90
    /**
91
     * set the proxy server to the given proxyHost with the given proxy Port
92
     *
93
     * @param proxyHost
94
     *            - the hostname of the proxy e.g. proxy.somedomain.org
95
     * @param proxyPort
96
     *            - the number of the port to use e.g. 8080
97
     */
98
    @Override
99
    public void setProxyServer(String proxyHost, int proxyPort) {
100
        _proxyHost = proxyHost;
1✔
101
        _proxyPort = proxyPort;
1✔
102
    }
1✔
103

104
    /**
105
     * @return the _connectTimeout -1 means it is not set (the default)
106
     */
107
    public int get_connectTimeout() {
108
        return _connectTimeout;
×
109
    }
110

111
    /**
112
     * set the connectionTimout -1 means it is not set (the default)
113
     *
114
     * @param timeout
115
     *            the _connectTimeout to set
116
     */
117
    public void set_connectTimeout(int timeout) {
118
        _connectTimeout = timeout;
×
119
    }
×
120

121
    /**
122
     * @return the _readTimeout -1 means it is not set (the default)
123
     */
124
    public int get_readTimeout() {
125
        return _readTimeout;
×
126
    }
127

128
    /**
129
     * @param timeout
130
     *            the _readTimeout to set -1 means it is not set (the default)
131
     */
132
    public void set_readTimeout(int timeout) {
133
        _readTimeout = timeout;
×
134
    }
×
135

136
    /**
137
     * get the Uniform Resource Locator for this request
138
     *
139
     * @param request
140
     *
141
     * @return the URL
142
     *
143
     * @throws MalformedURLException
144
     */
145
    private URL getRequestURL(WebRequest request) throws MalformedURLException {
146
        DNSListener dnsListener = getClientProperties().getDnsListener();
1✔
147
        if (dnsListener == null) {
1!
148
            return request.getURL();
1✔
149
        }
150

151
        String hostName = request.getURL().getHost();
×
152
        String portPortion = request.getURL().getPort() == -1 ? "" : ":" + request.getURL().getPort();
×
153
        setHeaderField("Host", hostName + portPortion);
×
154
        String actualHost = dnsListener.getIpAddress(hostName);
×
155
        if (HttpUnitOptions.isLoggingHttpHeaders()) {
×
156
            System.out.println("Rerouting request to :: " + actualHost);
×
157
        }
158
        return new URL(request.getURL().getProtocol(), actualHost, request.getURL().getPort(),
×
159
                request.getURL().getFile());
×
160
    }
161

162
    // ---------------------------------- private members --------------------------------
163

164
    /**
165
     * open a connection for the given uniform resource locator
166
     *
167
     * @param url
168
     *            - the url to use
169
     */
170
    private URLConnection openConnection(URL url) throws MalformedURLException, IOException {
171
        URLConnection connection = url.openConnection();
1✔
172
        if (connection instanceof HttpURLConnection) {
1✔
173
            ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
1✔
174
        }
175
        connection.setUseCaches(false);
1✔
176
        return connection;
1✔
177
    }
178

179
    /**
180
     * send the headers for the given connection based on the given Dictionary of headers
181
     *
182
     * @param connection
183
     * @param headers
184
     */
185
    private void sendHeaders(URLConnection connection, Dictionary headers) {
186
        boolean sendReferer = getClientProperties().isSendReferer();
1✔
187
        for (Enumeration e = headers.keys(); e.hasMoreElements();) {
1✔
188
            String key = (String) e.nextElement();
1✔
189
            if (sendReferer || !"referer".equalsIgnoreCase(key)) {
1✔
190
                connection.setRequestProperty(key, (String) headers.get(key));
1✔
191
                if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
192
                    if (key.equalsIgnoreCase("authorization") || key.equalsIgnoreCase("proxy-authorization")) {
×
193
                        System.out.println("Sending:: " + key + ": " + headers.get(key));
×
194
                    } else {
195
                        System.out.println("Sending:: " + key + ": " + connection.getRequestProperty(key));
×
196
                    }
197
                }
198
            } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
199
                System.out.println("Blocked sending referer:: " + connection.getRequestProperty(key));
×
200
            }
201
        } // for
1✔
202
    }
1✔
203
}
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