• 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

72.97
/src/main/java/com/meterware/servletunit/ServletUnitClient.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.servletunit;
9

10
import com.meterware.httpunit.FrameSelector;
11
import com.meterware.httpunit.GetMethodWebRequest;
12
import com.meterware.httpunit.HttpInternalErrorException;
13
import com.meterware.httpunit.WebClient;
14
import com.meterware.httpunit.WebRequest;
15
import com.meterware.httpunit.WebResponse;
16

17
import jakarta.servlet.ServletException;
18
import jakarta.servlet.http.HttpSession;
19

20
import java.io.ByteArrayOutputStream;
21
import java.io.IOException;
22
import java.net.MalformedURLException;
23

24
import org.xml.sax.SAXException;
25

26
/**
27
 * A client for use with the servlet runner class, allowing the testing of servlets without an actual servlet container.
28
 * Testing can be done in one of two ways. End-to-end testing works much like the HttpUnit package, except that only
29
 * servlets actually registered with the ServletRunner will be invoked. It is also possible to test servlets 'from the
30
 * inside' by creating a ServletInvocationContext and then calling any servlet methods which may be desired. Even in
31
 * this latter mode, end-to-end testing is supported, but requires a call to this class's getResponse method to update
32
 * its cookies and frames.
33
 **/
34
public class ServletUnitClient extends WebClient {
35

36
    /**
37
     * Instantiates a new servlet unit client.
38
     */
39
    private ServletUnitClient() {
×
40
        throw new RuntimeException("ServletUnitClient constructor needs InvocationContextFactory parameter");
×
41
    }
42

43
    /**
44
     * Creates and returns a new servlet unit client instance.
45
     *
46
     * @param factory
47
     *            the factory
48
     *
49
     * @return the servlet unit client
50
     */
51
    public static ServletUnitClient newClient(InvocationContextFactory factory) {
52
        return new ServletUnitClient(factory);
1✔
53
    }
54

55
    /**
56
     * Specifies a proxy server to use for requests from this client.
57
     */
58
    @Override
59
    public void setProxyServer(String proxyHost, int proxyPort) {
60
        // not implemented
61
    }
×
62

63
    /**
64
     * Creates and returns a new invocation context from a GET request.
65
     *
66
     * @param requestString
67
     *            the request string
68
     *
69
     * @return the invocation context
70
     *
71
     * @throws IOException
72
     *             Signals that an I/O exception has occurred.
73
     * @throws MalformedURLException
74
     *             the malformed URL exception
75
     */
76
    public InvocationContext newInvocation(String requestString) throws IOException, MalformedURLException {
77
        return newInvocation(new GetMethodWebRequest(requestString));
1✔
78
    }
79

80
    /**
81
     * Creates and returns a new invocation context to test calling of servlet methods.
82
     *
83
     * @param request
84
     *            the request
85
     *
86
     * @return the invocation context
87
     *
88
     * @throws IOException
89
     *             Signals that an I/O exception has occurred.
90
     * @throws MalformedURLException
91
     *             the malformed URL exception
92
     */
93
    public InvocationContext newInvocation(WebRequest request) throws IOException, MalformedURLException {
94
        return newInvocation(request, FrameSelector.TOP_FRAME);
1✔
95
    }
96

97
    /**
98
     * get a new Invocation Context based on a request and a frame.
99
     *
100
     * @param request
101
     *            the request
102
     * @param frame
103
     *            the frame
104
     *
105
     * @return the invocation context
106
     *
107
     * @throws IOException
108
     *             Signals that an I/O exception has occurred.
109
     * @throws MalformedURLException
110
     *             the malformed URL exception
111
     */
112
    InvocationContext newInvocation(WebRequest request, FrameSelector frame) throws IOException, MalformedURLException {
113
        ByteArrayOutputStream baos = getMessageBody(request);
1✔
114
        if (_invocationContextFactory == null) {
1!
115
            throw new RuntimeException("newInvocation called with null _invocationContextFactory");
×
116
        }
117
        return _invocationContextFactory.newInvocation(this, frame, request, getHeaderFields(request.getURL()),
1✔
118
                baos.toByteArray());
1✔
119
    }
120

121
    /**
122
     * Gets the message body.
123
     *
124
     * @param request
125
     *            the request
126
     *
127
     * @return the message body
128
     *
129
     * @throws IOException
130
     *             Signals that an I/O exception has occurred.
131
     */
132
    ByteArrayOutputStream getMessageBody(WebRequest request) throws IOException {
133
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
1✔
134
        writeMessageBody(request, baos);
1✔
135
        return baos;
1✔
136
    }
137

138
    /**
139
     * Updates this client and returns the response which would be displayed by the user agent. Note that this will
140
     * typically be the same as that returned by the servlet invocation unless that invocation results in a redirect
141
     * request.
142
     *
143
     * @param invocation
144
     *            the invocation
145
     *
146
     * @return the response
147
     *
148
     * @throws MalformedURLException
149
     *             the malformed URL exception
150
     * @throws IOException
151
     *             Signals that an I/O exception has occurred.
152
     * @throws SAXException
153
     *             the SAX exception
154
     */
155
    public WebResponse getResponse(InvocationContext invocation)
156
            throws MalformedURLException, IOException, SAXException {
157
        updateMainWindow(invocation.getFrame(), invocation.getServletResponse());
1✔
158
        return getFrameContents(invocation.getFrame());
1✔
159
    }
160

161
    /**
162
     * Returns the session that would be used by the next request (if it asks for one).
163
     *
164
     * @param create
165
     *            if true, will create a new session if no valid session is defined.
166
     *
167
     * @return the session
168
     */
169
    public HttpSession getSession(boolean create) {
170
        HttpSession session = _invocationContextFactory
1✔
171
                .getSession(getCookieValue(ServletUnitHttpSession.SESSION_COOKIE_NAME), create);
1✔
172
        if (session != null) {
1!
173
            putCookie(ServletUnitHttpSession.SESSION_COOKIE_NAME, session.getId());
1✔
174
        }
175
        return session;
1✔
176
    }
177

178
    // -------------------------------- WebClient methods --------------------------------------
179

180
    /**
181
     * Creates a web response object which represents the response to the specified web request.
182
     **/
183
    @Override
184
    protected WebResponse newResponse(WebRequest request, FrameSelector targetFrame)
185
            throws MalformedURLException, IOException {
186

187
        try {
188
            InvocationContext invocation = newInvocation(request, targetFrame);
1✔
189
            invocation.service();
1✔
190
            return invocation.getServletResponse();
1✔
191
        } catch (ServletException e) {
×
192
            throw new HttpInternalErrorException(request.getURL(), e);
×
193
        }
194

195
    }
196

197
    // -------------------------- private members -----------------------------------
198

199
    /** The invocation context factory. */
200
    private InvocationContextFactory _invocationContextFactory;
201

202
    // --------------------------------- package methods ---------------------------------------
203

204
    /**
205
     * Instantiates a new servlet unit client.
206
     *
207
     * @param factory
208
     *            the factory
209
     */
210
    private ServletUnitClient(InvocationContextFactory factory) {
1✔
211
        if (factory == null) {
1!
212
            throw new RuntimeException("constructor for ServletUnitClient called with null factory parameter");
×
213
        }
214
        _invocationContextFactory = factory;
1✔
215
    }
1✔
216
}
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