• 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

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

22
import com.meterware.httpunit.FrameSelector;
23
import com.meterware.httpunit.GetMethodWebRequest;
24
import com.meterware.httpunit.HttpInternalErrorException;
25
import com.meterware.httpunit.WebClient;
26
import com.meterware.httpunit.WebRequest;
27
import com.meterware.httpunit.WebResponse;
28

29
import java.io.ByteArrayOutputStream;
30
import java.io.IOException;
31
import java.net.MalformedURLException;
32

33
import javax.servlet.ServletException;
34
import javax.servlet.http.HttpSession;
35

36
import org.xml.sax.SAXException;
37

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

48
    /**
49
     * Instantiates a new servlet unit client.
50
     */
51
    private ServletUnitClient() {
×
52
        throw new RuntimeException("ServletUnitClient constructor needs InvocationContextFactory parameter");
×
53
    }
54

55
    /**
56
     * Creates and returns a new servlet unit client instance.
57
     *
58
     * @param factory
59
     *            the factory
60
     *
61
     * @return the servlet unit client
62
     */
63
    public static ServletUnitClient newClient(InvocationContextFactory factory) {
64
        return new ServletUnitClient(factory);
1✔
65
    }
66

67
    /**
68
     * Specifies a proxy server to use for requests from this client.
69
     */
70
    @Override
71
    public void setProxyServer(String proxyHost, int proxyPort) {
72
        // not implemented
73
    }
×
74

75
    /**
76
     * Creates and returns a new invocation context from a GET request.
77
     *
78
     * @param requestString
79
     *            the request string
80
     *
81
     * @return the invocation context
82
     *
83
     * @throws IOException
84
     *             Signals that an I/O exception has occurred.
85
     * @throws MalformedURLException
86
     *             the malformed URL exception
87
     */
88
    public InvocationContext newInvocation(String requestString) throws IOException, MalformedURLException {
89
        return newInvocation(new GetMethodWebRequest(requestString));
1✔
90
    }
91

92
    /**
93
     * Creates and returns a new invocation context to test calling of servlet methods.
94
     *
95
     * @param request
96
     *            the request
97
     *
98
     * @return the invocation context
99
     *
100
     * @throws IOException
101
     *             Signals that an I/O exception has occurred.
102
     * @throws MalformedURLException
103
     *             the malformed URL exception
104
     */
105
    public InvocationContext newInvocation(WebRequest request) throws IOException, MalformedURLException {
106
        return newInvocation(request, FrameSelector.TOP_FRAME);
1✔
107
    }
108

109
    /**
110
     * get a new Invocation Context based on a request and a frame.
111
     *
112
     * @param request
113
     *            the request
114
     * @param frame
115
     *            the frame
116
     *
117
     * @return the invocation context
118
     *
119
     * @throws IOException
120
     *             Signals that an I/O exception has occurred.
121
     * @throws MalformedURLException
122
     *             the malformed URL exception
123
     */
124
    InvocationContext newInvocation(WebRequest request, FrameSelector frame) throws IOException, MalformedURLException {
125
        ByteArrayOutputStream baos = getMessageBody(request);
1✔
126
        if (_invocationContextFactory == null) {
1!
127
            throw new RuntimeException("newInvocation called with null _invocationContextFactory");
×
128
        }
129
        return _invocationContextFactory.newInvocation(this, frame, request, getHeaderFields(request.getURL()),
1✔
130
                baos.toByteArray());
1✔
131
    }
132

133
    /**
134
     * Gets the message body.
135
     *
136
     * @param request
137
     *            the request
138
     *
139
     * @return the message body
140
     *
141
     * @throws IOException
142
     *             Signals that an I/O exception has occurred.
143
     */
144
    ByteArrayOutputStream getMessageBody(WebRequest request) throws IOException {
145
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
1✔
146
        writeMessageBody(request, baos);
1✔
147
        return baos;
1✔
148
    }
149

150
    /**
151
     * Updates this client and returns the response which would be displayed by the user agent. Note that this will
152
     * typically be the same as that returned by the servlet invocation unless that invocation results in a redirect
153
     * request.
154
     *
155
     * @param invocation
156
     *            the invocation
157
     *
158
     * @return the response
159
     *
160
     * @throws MalformedURLException
161
     *             the malformed URL exception
162
     * @throws IOException
163
     *             Signals that an I/O exception has occurred.
164
     * @throws SAXException
165
     *             the SAX exception
166
     */
167
    public WebResponse getResponse(InvocationContext invocation)
168
            throws MalformedURLException, IOException, SAXException {
169
        updateMainWindow(invocation.getFrame(), invocation.getServletResponse());
1✔
170
        return getFrameContents(invocation.getFrame());
1✔
171
    }
172

173
    /**
174
     * Returns the session that would be used by the next request (if it asks for one).
175
     *
176
     * @param create
177
     *            if true, will create a new session if no valid session is defined.
178
     *
179
     * @return the session
180
     */
181
    public HttpSession getSession(boolean create) {
182
        HttpSession session = _invocationContextFactory
1✔
183
                .getSession(getCookieValue(ServletUnitHttpSession.SESSION_COOKIE_NAME), create);
1✔
184
        if (session != null) {
1!
185
            putCookie(ServletUnitHttpSession.SESSION_COOKIE_NAME, session.getId());
1✔
186
        }
187
        return session;
1✔
188
    }
189

190
    // -------------------------------- WebClient methods --------------------------------------
191

192
    /**
193
     * Creates a web response object which represents the response to the specified web request.
194
     **/
195
    @Override
196
    protected WebResponse newResponse(WebRequest request, FrameSelector targetFrame)
197
            throws MalformedURLException, IOException {
198

199
        try {
200
            InvocationContext invocation = newInvocation(request, targetFrame);
1✔
201
            invocation.service();
1✔
202
            return invocation.getServletResponse();
1✔
203
        } catch (ServletException e) {
×
204
            throw new HttpInternalErrorException(request.getURL(), e);
×
205
        }
206

207
    }
208

209
    // -------------------------- private members -----------------------------------
210

211
    /** The invocation context factory. */
212
    private InvocationContextFactory _invocationContextFactory;
213

214
    // --------------------------------- package methods ---------------------------------------
215

216
    /**
217
     * Instantiates a new servlet unit client.
218
     *
219
     * @param factory
220
     *            the factory
221
     */
222
    private ServletUnitClient(InvocationContextFactory factory) {
1✔
223
        if (factory == null) {
1!
224
            throw new RuntimeException("constructor for ServletUnitClient called with null factory parameter");
×
225
        }
226
        _invocationContextFactory = factory;
1✔
227
    }
1✔
228
}
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