How to mock Web API with Fiddler’s AutoResponder

As developers, we often find ourselves in need to mock Web APIs.

For instance, let us assume that we need to consume a Web API that points to http://raymartsamples.com/orderservice. Then due to some server issues, this endpoint is going to be inaccessible for the next three days or so according to the web service provider. Needless to say, we cannot let it block our development, so the solution is to mock it.

First, we need to download and install Fiddler. It is a network tool used primarily to inspect and alter Http and Https traffic. But this tool has more to offer, one of which is the AutoResponder feature, which allows developers to specify rules that return mocked data. In this article, we are going to set up a rule that returns JSON data when we send a request to http://raymartsamples.com/orderservice.

Next, we need to create a .dat file that will return the JSON data as a result of the Web API call. This is the content of the .dat file, copy-paste and save it.

{“AutoRespondText”:”Hello World!”,”AutoRespondDescription”:”It works!”}

Now, we need to setup Fiddler:

  • Open Fiddler
  • On the right panel, click the AutoResponder tab
  • Click Add Rule button
  • Check the Unmatched requests passthrough checkbox
  • On the Rule Editor, we need to specify the details:
  • First, we need to tell AutoResponder the endpoint through Regular Expression matching. Yes, we need to specify an endpoint expression that the tool has to respond to. For our example, our expression is going to be like this: regex:(^http:\/\/+[a-zA-Z0–9]*\.com\/orderservice$). Brief explanation of this expression is it would match any string that starts with “http:” then followed by alpha-numeric characters then a “.com” string that is followed by “/orderservice”. You can use some online tools like regexr to build your regular expression.
  • Next, we need choose the .dat file that we created earlier. This is the JSON data that Autoresponder will return when we send a request to the endpoint that we specified.

Finally, we need to test these altogether using a simple program.

Here is the code I’ve written in C# with Visual Studio. As you can see, I have an AutoRespondResult class which is used to Deserialize our Web API response, and I used Newtonsoft.Json Nuget package for the Deserialization.

Now, all that is left to do is run the simple program and see the results.

Cheers!

I’m an MCSD, I’ve been developing applications for 10 years now, It’s my profession and hobby. Hoping to contribute to the community :)