How should I write `HttpServletRequest`, which are sent to` doPost` in `HttpServlet` for later playback?

I want to dump some requests (like javax.servlet.http.HttpServletRequest ) into a file and then play them from the file so that I can test future changes in the HttpServlet . What is the best way to accomplish this?

So far, I am trying to extract data from the input stream associated with the request and save this binary data to a file. Ultimately, this may require something like storing bytes in front of each stored input stream, so I know where one request ends and another begins.

Is there an easier way to do this?

** EDIT: for clarification, these are not browser related queries. So far, none of the answers solve my specific problem, which, I believe, comes down to serializing and deserializing the HttpServletRequest. I tried just pulling bytes from the input stream returned by request.getInputStream (). Unfortunately, if I turn this into a string, it seems that the resulting bytes cannot be parsed by Message.Builder.mergeFrom (bytes).

I offer generosity to everyone who knows how to solve my problem.

+8
java servlets
source share
3 answers

In my specific case, I ended up CodedOutputStream problem using CodedOutputStream to write com.google.protobuf.GeneratedMessage ( foo below):

 ByteArrayOutputStream bos = new ByteArrayOutputStream(); CodedOutputStream cos = CodedOutputStream.newInstance(bos); foo.writeTo(cos); 

This can be read when using CodedInputStream with something like:

 byte[] ba = readBytesFromOutputFile(); CodedInputStream cis = CodedInputStream.newInstance(ba); 

CodedInputStream can then CodedInputStream to the message developer to complete deserialization.

+1
source

Maybe you should take a look at Selenium ( http://seleniumhq.org/ ).

I say this because I assume that the reason you want to play the queries is for testing. Selenium records what you do in your browser and then play it.

If you are trying to do something else, perhaps you could explain what you are ultimately trying to do.

+2
source

There are many local reverse proxy tools that can record and play back an HTTP session. Usually, you configure your browser to use the localhost proxy, follow the steps, save the session, and then play it back. JMeter and Charles are two tools that are Java Based.

Another option would be to use HttpClient to programmatically implement your servlet. Using JUnit to run tests has the advantage of making it easier for you to verify that you are getting the "right" answer.

+1
source

All Articles