• 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

59.78
/src/main/java/com/meterware/httpunit/WebConversation.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 java.io.IOException;
11
import java.net.HttpURLConnection;
12
import java.net.MalformedURLException;
13
import java.net.URL;
14
import java.net.URLConnection;
15
import java.util.Dictionary;
16
import java.util.Enumeration;
17
import java.util.Properties;
18

19
/**
20
 * The context for a series of HTTP requests. This class manages cookies used to maintain session context, computes
21
 * relative URLs, and generally emulates the browser behavior needed to build an automated test of a web site.
22
 **/
23
public class WebConversation extends WebClient {
24

25
    /** The proxy host. */
26
    private String _proxyHost;
27

28
    /** The proxy port. */
29
    private int _proxyPort;
30

31
    /** The connect timeout. */
32
    private int _connectTimeout = -1;
1✔
33

34
    /** The read timeout. */
35
    private int _readTimeout = -1;
1✔
36

37
    /**
38
     * Creates a new web conversation.
39
     **/
40
    public WebConversation() {
1✔
41
    }
1✔
42

43
    // ---------------------------------- protected members --------------------------------
44

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

80
    @Override
81
    public void clearProxyServer() {
82
        _proxyHost = null;
1✔
83
    }
1✔
84

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

99
    /**
100
     * Gets the connect timeout.
101
     *
102
     * @return the _connectTimeout -1 means it is not set (the default)
103
     */
104
    public int get_connectTimeout() {
105
        return _connectTimeout;
×
106
    }
107

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

118
    /**
119
     * Gets the read timeout.
120
     *
121
     * @return the _readTimeout -1 means it is not set (the default)
122
     */
123
    public int get_readTimeout() {
124
        return _readTimeout;
×
125
    }
126

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

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

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

165
    // ---------------------------------- private members --------------------------------
166

167
    /**
168
     * open a connection for the given uniform resource locator.
169
     *
170
     * @param url
171
     *            - the url to use
172
     *
173
     * @return the URL connection
174
     *
175
     * @throws MalformedURLException
176
     *             the malformed URL exception
177
     * @throws IOException
178
     *             Signals that an I/O exception has occurred.
179
     */
180
    private URLConnection openConnection(URL url) throws MalformedURLException, IOException {
181
        URLConnection connection = url.openConnection();
1✔
182
        if (connection instanceof HttpURLConnection) {
1✔
183
            ((HttpURLConnection) connection).setInstanceFollowRedirects(false);
1✔
184
        }
185
        connection.setUseCaches(false);
1✔
186
        return connection;
1✔
187
    }
188

189
    /**
190
     * send the headers for the given connection based on the given Dictionary of headers.
191
     *
192
     * @param connection
193
     *            the connection
194
     * @param headers
195
     *            the headers
196
     */
197
    private void sendHeaders(URLConnection connection, Dictionary headers) {
198
        boolean sendReferer = getClientProperties().isSendReferer();
1✔
199
        for (Enumeration e = headers.keys(); e.hasMoreElements();) {
1✔
200
            String key = (String) e.nextElement();
1✔
201
            if (sendReferer || !"referer".equalsIgnoreCase(key)) {
1✔
202
                connection.setRequestProperty(key, (String) headers.get(key));
1✔
203
                if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
204
                    if (key.equalsIgnoreCase("authorization") || key.equalsIgnoreCase("proxy-authorization")) {
×
205
                        System.out.println("Sending:: " + key + ": " + headers.get(key));
×
206
                    } else {
207
                        System.out.println("Sending:: " + key + ": " + connection.getRequestProperty(key));
×
208
                    }
209
                }
210
            } else if (HttpUnitOptions.isLoggingHttpHeaders()) {
1!
211
                System.out.println("Blocked sending referer:: " + connection.getRequestProperty(key));
×
212
            }
213
        } // for
1✔
214
    }
1✔
215
}
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