• 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

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 jakarta.servlet.ServletException;
30
import jakarta.servlet.http.HttpSession;
31

32
import java.io.ByteArrayOutputStream;
33
import java.io.IOException;
34
import java.net.MalformedURLException;
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
    private ServletUnitClient() {
×
49
        throw new RuntimeException("ServletUnitClient constructor needs InvocationContextFactory parameter");
×
50
    }
51

52
    /**
53
     * Creates and returns a new servlet unit client instance.
54
     **/
55
    public static ServletUnitClient newClient(InvocationContextFactory factory) {
56
        return new ServletUnitClient(factory);
1✔
57
    }
58

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

67
    /**
68
     * Creates and returns a new invocation context from a GET request.
69
     **/
70
    public InvocationContext newInvocation(String requestString) throws IOException, MalformedURLException {
71
        return newInvocation(new GetMethodWebRequest(requestString));
1✔
72
    }
73

74
    /**
75
     * Creates and returns a new invocation context to test calling of servlet methods.
76
     **/
77
    public InvocationContext newInvocation(WebRequest request) throws IOException, MalformedURLException {
78
        return newInvocation(request, FrameSelector.TOP_FRAME);
1✔
79
    }
80

81
    /**
82
     * get a new Invocation Context based on a request and a frame
83
     *
84
     * @param request
85
     * @param frame
86
     *
87
     * @return
88
     *
89
     * @throws IOException
90
     * @throws MalformedURLException
91
     */
92
    InvocationContext newInvocation(WebRequest request, FrameSelector frame) throws IOException, MalformedURLException {
93
        ByteArrayOutputStream baos = getMessageBody(request);
1✔
94
        if (_invocationContextFactory == null) {
1!
95
            throw new RuntimeException("newInvocation called with null _invocationContextFactory");
×
96
        }
97
        return _invocationContextFactory.newInvocation(this, frame, request, getHeaderFields(request.getURL()),
1✔
98
                baos.toByteArray());
1✔
99
    }
100

101
    ByteArrayOutputStream getMessageBody(WebRequest request) throws IOException {
102
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
1✔
103
        writeMessageBody(request, baos);
1✔
104
        return baos;
1✔
105
    }
106

107
    /**
108
     * Updates this client and returns the response which would be displayed by the user agent. Note that this will
109
     * typically be the same as that returned by the servlet invocation unless that invocation results in a redirect
110
     * request.
111
     **/
112
    public WebResponse getResponse(InvocationContext invocation)
113
            throws MalformedURLException, IOException, SAXException {
114
        updateMainWindow(invocation.getFrame(), invocation.getServletResponse());
1✔
115
        return getFrameContents(invocation.getFrame());
1✔
116
    }
117

118
    /**
119
     * Returns the session that would be used by the next request (if it asks for one).
120
     *
121
     * @param create
122
     *            if true, will create a new session if no valid session is defined.
123
     */
124
    public HttpSession getSession(boolean create) {
125
        HttpSession session = _invocationContextFactory
1✔
126
                .getSession(getCookieValue(ServletUnitHttpSession.SESSION_COOKIE_NAME), create);
1✔
127
        if (session != null) {
1!
128
            putCookie(ServletUnitHttpSession.SESSION_COOKIE_NAME, session.getId());
1✔
129
        }
130
        return session;
1✔
131
    }
132

133
    // -------------------------------- WebClient methods --------------------------------------
134

135
    /**
136
     * Creates a web response object which represents the response to the specified web request.
137
     **/
138
    @Override
139
    protected WebResponse newResponse(WebRequest request, FrameSelector targetFrame)
140
            throws MalformedURLException, IOException {
141

142
        try {
143
            InvocationContext invocation = newInvocation(request, targetFrame);
1✔
144
            invocation.service();
1✔
145
            return invocation.getServletResponse();
1✔
146
        } catch (ServletException e) {
×
147
            throw new HttpInternalErrorException(request.getURL(), e);
×
148
        }
149

150
    }
151

152
    // -------------------------- private members -----------------------------------
153

154
    private InvocationContextFactory _invocationContextFactory;
155

156
    // --------------------------------- package methods ---------------------------------------
157

158
    private ServletUnitClient(InvocationContextFactory factory) {
1✔
159
        if (factory == null) {
1!
160
            throw new RuntimeException("constructor for ServletUnitClient called with null factory parameter");
×
161
        }
162
        _invocationContextFactory = factory;
1✔
163
    }
1✔
164
}
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