I want to make tests where I can call the following without actually spinning up a web server:
MyServletHandler.handle("", baseRequest, request, resp)
MyServletHandler implements org.eclipse.jetty.server.handler.AbstractHandler
Then I perform tests on the data in the response, database changes, exceptions thrown, etc. It's really nice to be able to do end-to-end unit tests like this without spinning up a webserver and loading my app for each one.
I also do tests of individual screens by sending the fakeRequest to them directly without involving the ServletHandler.
The basic FakeHttpServletRequest was pretty straightforward except for file uploads. I got it to work in Jetty 11 / Servlet 5, but there are changes in version 12/6. Is there a simple way to get the Jetty 12 / Servlet 6 classes to fake a file upload?
Here is more of the use case for Jetty 11. `req` is a builder for the fake request. I mock the HttpChannel because I don't care about it, it just has to be there for Jetty to be happy.
val resp = FakeHttpServletResponse()
val request: HttpServletRequest = req.toReq()
val mockHttpChannel: HttpChannel = mock { }
val baseRequest = FakeJettyRequest(request, mockHttpChannel)
MyServletHandler.handle("", baseRequest, request, resp)
class FakeJettyRequest(
private val httpReq: HttpServletRequest,
channel: HttpChannel,
) : Request(channel, null) {
override fun getRemoteAddr(): String? = httpReq.remoteAddr
override fun getHeader(name: String?): String? = httpReq.getHeader(name)
}
Thanks again for many great years with Jetty so far!