Skip to content

How to mock a [web] server – the easy way

March 15, 2011

Many a times we need isolation. Isolate your client app from the server – that will show the people who develop the server!  Testing a client has one major head shake –  how can one test the behavior without controlling the input data? Think accènts, empty titles, long titles, bad response format, 404, 504, 500, 502 etc. You could ask the people that maintain the server to drop the data you need, but that’s a dependency you can’t control nor bribe (well, sometimes you can bribe them with beer, but if they are married it won’t work often…).

So – mock that server!

You can argue that mocking the entire server is too much. Why not just mock the data layer accessing the network? Well, 3 reasons:

  • the data layer can and will be changed a lot
  • not all flows go through the same data layer, there’s always some authentication call that makes a weird detour
  • you’ll be testing the real app, not a hacked and detoured version of it

Well, if you are still reading this, you might already know Fiddler. It’s the single most useful tool for client / server apps. Ever. It proxies the data transfers and matches the requests with the responses, it has visualizers for XML, HTML, images. Heck, it can even read HTTPS (really, it installs a fake certificate on your machine so it becomes a man in the middle). You can set breakpoints before a request or an answer. It has scripting support and an API in C#. It’s god’s gift to all testers! And, it’s free.

Well, Fiddler has a nice feature called AutoResponder:

Fiddler AutoResponder UI

Fiddler AutoResponder UI

The UI is pretty much self explanatory – you can build up a list of rules where a rule is (URI_TEMPLATE, ACTION_TO_PERFORM). You are free to use wildcards for templates. As with most access lists, rules are picked top to bottom. The AutoResponder has a feature to route URIs to the real server, acting as a, well, a router :). And the coolest of them all, you can send responses from a files, inject delays and web errors:

Fiddler AutoResponder predefined responses

Predefined Responses

Oh, in case you are wondering, *bpu and *bpafter refer to breaking the flow before / after response / requests (it works like visual studio breakpoints so you can make manual modifications to the data). Fiddler is extendable so you can add more rules!

Conclusion

There you have it, these are all the elements needed to create a mock server. Fiddler can load and save your list of rules. Test case automation? Save all requests for a flow to files, mock them and have one request return an error. You’ll get a test suite of number_of_requests x number_of_possible_errors. Managers will love you!


Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: