views:

1195

answers:

5

Hello,

I would like to write tests for my client code, which accesses HTTP Server. I am looking for simple HTTP Server, which would simulate responses from real server.

Such HTTP Server (mock server :-)) should

  1. verify all requests really came from my client code
  2. verify that requests had all required parameters
  3. send response, ideally based on parameters from requests
  4. it should also support sending error codes, or multiple responses for multiple requests on one URI

My goal is to verify that 1) client code is working OK from server's point of view, sending all requests it should, with valid parameters, using correct method (GET/POST), and 2) client can process responses, and it can even handle some error conditions.

I am using jUnit 4 for my tests. For now, I use embedded Jetty HTTP Server as my 'mock server', but I would like to avoid writing support for above requirements. Do you know any library, which would act as http server and helping me with above-described testing?

Thank you.

+1  A: 

Stick with Jetty. From the Jetty wiki:

Jetty is an open-source, standards-based, full-featured web server and servlet container implemented entirely in Java.

Especially your points 2 and 3 make me think you should deploy some servlets in Jetty that provide the desired behaviour.

eljenso
Hello. Thank you for your answer. This is what I am doing already at the moment, but I'd like to avoid developing it myself, and would prefer existing solution, if one exists. My goal is not to write another library :-) I just want to test my code.
Peter Štibraný
I honestly don't know if there is a framework/library that would somehow allow you to mock server responses on a "higher" level than you can do with Jetty and servlets. Maybe setting it up would be more hassle than what you are doing now. So I stick by my recommendation :)
eljenso
I ended up with Jetty and custom servlet, which checks for prerequsites, and sends prepared response. I combined Jetty, Hamcrest (with few request matchers) for specifying request/response pairs, and jUnit.Not what I was looking for, as I wanted to avoid writing this... but it works like expected.
Peter Štibraný
A: 

If you want to test the client code properly, then you should interact with real world servers as there might be tiny differences between implementation and your client may not work with very common servers (Apache, IIS, lighttpd).

If you can aford doing the verification manually, have a look at a proxy (Charles Proxy for example) that can show you the full trace of your requests.

Vincent
+3  A: 

Have a look at Simple. It is an embeddable HTTP server. You could debug all incoming requests in a much smaller scale than jetty.

And its much faster than Jetty
ng
I use embedded Jetty for my application and Simple for testing. It's a nice combination.
jdigital
+1  A: 

NanoHTTPD is another option. It is a single-file HTTP server written in Java.

Features:

  • Only one Java file
  • No fixed config files, logging, authorization etc.
  • Supports parameter parsing of GET and POST methods
  • Supports both dynamic content and file serving
  • Default code serves files and shows all HTTP parameters and headers
  • File server supports directory listing, index.html and index.htm
  • File server uses current directory as a web root

I have used it successfuly for unit-testing some HTTP client code - since it's a single .java file you can put it anywhere you need it, without any classpath headaches and even keep it in the release builds.

TomA
+1  A: 

There's a really neat MockHttpServer in the Wink project. See http://incubator.apache.org/wink/1.1.1/api/org/apache/wink/client/MockHttpServer.html.

Viktor Nordling
Thanks, I'll check it out.
Peter Štibraný