Mock Network Services 4 Java
:http server

This object provides an service that waits for http connections and delivers documents in a http stream to the caller. There is a default document that will be delivered for any URL given. You may change this default document by invoking addRecource(String pathOfUrl, String documentContent, String mimeTyp) or addRecource(String pathOfUrl, File document, String mimeTyp).
on the created instance of de.tt.mns4j.http.MockHttpServer with pathOfUrl = "/".

Of cause you can have unique documents for certain URL. To do this have your unittest call either addRecource(String pathOfUrl, String documentContent, String mimeTyp) or addRecource(String pathOfUrl, File document, String mimeTyp).

The parameters are:
String pathOfUrl
the virtual absolute path of the resource on this MockHttpServer instance.
String documentContent
a String that represents the stream of bytes the service shall return to the function in test for the pathOfUrl, or
File document
a file handle to the document that containes the stream of bytes to be delivered to the function in test.
String mimeType
a String with the mime type that the service will present for the document. For your convinience there are (a very few) constants defined in de.tt.mns4j.http.MockHttpServer.

The resource will be added to a map with the pathName as key ande the resorce as value. If you gave different resources for the same path a request against the mock server instance will return the last one.

Its always important to test failure conditions on your code. But for instance how do you force an apache http server to deliver a http 500 error when your unittest needs it? With mns4j this is easy: just let your unittest call addRecourceFailing(String pathOfUrl, int errorNumber) on the de.tt.mns4j.http.MockHttpServer object. This will force the the mock server instance to send a HTTP-error errorNumber to the client that requested pathOfUrl.

The parameters are:
String pathOfUrl
This is the virtual absolute path of the resource on this MockHttpServer instance.
int errorNumber
the number of the error that the mock server will pretend to occure. Reffer de.tt.mns4j.http.MockHttpServer for some predefined constants.
boolean withErrorPage
This parameter is optional. This means there is another method with this name but without this parameter, setting it to a default (which is true). The value true means, that the MockHttpServer will return an error page along with the HTTP error message which could be displayed by a client. Giving the value false will force the Mock Http Server to sent only the HTTP error message without an error page.

Note that if you add a path here as well as with addResource() you will allways get the error returned.

You can get the current resources map when you call getResources(). The same is true for the errorRequests map with getErrorRequests(). later you can put back this maps using setResources(Map recourcesMap) or setErrorRequests(Map recourcesMap) respectively.

In case your code builds the path of the URLs dynamically and you want to check them you can get a list of requests received by the the mock server instance and process it with assertations. After processing this list you can reset it by calling clearRequestList().