Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] Trouble making FakeHttpServletRequest implement getParts() in Jetty 12

Sorry for the late reply.  I really had to think, and then I was working on other things.

I made a wrapper class for request parameters (called RequestParams) that:
  • Returns parameters in a single format, regardless of whether those parameters were in the query string, request body, or headers of that request.  Code using RequestParams asks for a string, list-of-strings, or file by parameter name.  It has a few methods for parsing decimals and booleans too.
  • Actually, it doesn't return Strings, it returns TaintedStrings so the type system can be used to enforce that user-supplied input is not trusted.  We save and read TaintedStrings to and from the database and have wrappers for each output type that encode that data safely for each type (HTML, JSON, CSV, PDF, etc.)
  • It also converts everything to Unicode Normalized Form C.
In order to test this RequestParams class, I needed to be certain that my test harness worked the way my web server worked.  I had to make some minor tweaks to RequestParams when I moved from Tomcat to Jetty.

I really didn't know exactly how each kind of request parameter was handled, so with Jetty 9 and then 11, I was able to sort of pick apart the internals to let Jetty parse the raw contents I captured from actual HTTP requests.  Now that I had working tests from that experience, I was able to replace all that with FakeParts (for file uploads) or Strings passed through the FakeRequest.  It doesn't use Jetty at all and doesn't parse anything.  It just implements the Servlet6 interfaces.  So that seemed like a big improvement.

Although now I'm questioning whether it needs to take the Servlet6 Request or a Jetty12 Request, but that's another email.

On Fri, Sep 6, 2024 at 12:48 PM Simone Bordet <sbordet@xxxxxxxxxxx> wrote:
Hi,

On Fri, Sep 6, 2024 at 5:02 PM Simone Bordet <sbordet@xxxxxxxxxxx> wrote:
>
> Hi,
>
> On Fri, Sep 6, 2024 at 4:13 PM Glen Peterson via jetty-users
> <jetty-users@xxxxxxxxxxx> wrote:
> >
> > Thanks for Jetty.  I've been enjoying it for years now!
> >
> > I'm trying to make a FakeHttpServletRequest with Jetty 12 for unit-testing and integration-testing my web applications.  I had one that worked with Jetty 11, but some things changed.
>
> Considering it is probably fewer lines of code to set up a real
> server, rather than try to mock everything, can you not consider just
> doing it "normally", rather than trying to construct internal Jetty
> classes that will be subject to backward incompatible changes?

And if you're worried about using the network (you should not), we
have plenty of memory-only solutions like MemoryTransport and/or
LocalConnector.

--
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.

Back to the top