Steven Edouard

Developer Advocate. Tech Enthusiast. Life Enthusiast.

NAVIGATION - SEARCH

Testing in Isolation: Mocking an Http service

If you’re a web developer, you probably know about the awesome web debugging tool Fiddler. It was a project originally created by a Microsoft employee that eventually brought the project over to Telerik. Any good tester knows the awesome value of testing in isolation and this is one problem that I had with connected apps.

 

Fiddler is the next-best-thing to sliced bread for testers of these types of applications. Fiddler actually exposes a core api set that allows you to quickly write a system proxy server that can programagically manipulate http and even https traffic on your box. 

 

I had the pleasure of tinkering around with this for a quick little project. Using a Mock web service (or client) can address two important issues:

 

  1. Test reliability can be questionable because a malfunction in one piece (server or client) causes the other piece to misbehave.

 

  1. Noise in network traffic causes noise in performance testing.

 

Now, Fiddler's out-of-the box UI has an "AutoResponder" feature:

 

 

 

Unfortunately its way too tedious to use on any application s you have to add these rules manually.

 

 

What if we could simply "record" all the requests from a particular target process, and then just respond later with a saved version of that message? Or even respond with a slightly modified version if we recognize a pattern?

 

Fiddler actually exposes an API which makes this really easy to do. In fact I have an example that does in a pretty small amount of code. You can find my sample here.

 

The Fiddler api feels a bit dated compared to modern .NET apis (no async/await for example). Essentially the entire api is exposed as a static class FiddlerApplication. Which is probably done this way since there can only be 1 proxy for the system at a time.

 

The api has two very important events to register to:

 

 

//This event is fired after the client sends the HTTP/S request but before the message actually arrives to the server
Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)

//This event is fired after the server sends the HTTP/S request but before the message actually arrives to the client
Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS)





These API's allow you be the "middle man" between the client and the server. When your "middle man" has understood all the questions that would be asked by the client or server, you can start to respond to the questions with previously recorded responses.

 

In my example, the BeforeRequest handler filters for a process of interest, (so that you don't block traffic for the entire system) and decides to either let the request go to the server, or to bypass the server with its own "Mocked" response:

 

Fiddler.FiddlerApplication.BeforeRequest += delegate(Fiddler.Session oS)
            {
                Console.WriteLine("Before request for:\t" + oS.fullUrl);
                // In order to enable response tampering, buffering mode must
                // be enabled; this allows FiddlerCore to permit modification of
                // the response in the BeforeResponse handler rather than streaming
                // the response to the client as the response comes in.
                oS.bBufferResponse = true;
                bool isProcessOfInterest = false;
                string processName = null;
                foreach (Process p in Process.GetProcesses())
                {
                    if (p.Id == oS.LocalProcessID)
                    {
                        var results = from x in processNames where Path.GetFileNameWithoutExtension(x).Equals(p.ProcessName, StringComparison.CurrentCultureIgnoreCase) select x;
                        int count = 0;
                        foreach(string s in results)
                        {
                            count++;
                            processName = s;
                            break;
                        }
                        if(count > 0)
                        {
                            isProcessOfInterest = true;
                            break;
                        }
                    }
                }

                if (isProcessOfInterest && !record && mSnoops.ContainsKey((KeyString(oS)).GetHashCode()))
                {
                    oS.utilCreateResponseAndBypassServer();
                    //UpdateTimeStamp(Path.Combine(storagePath, processName) + (KeyString(oS)).GetHashCode().ToString() + ".txt");
                    oS.LoadResponseFromFile(Path.Combine(storagePath, processName) + (KeyString(oS)).GetHashCode().ToString() + ".txt");
                    Console.WriteLine("MOCKED RESPONSE FOR: " + KeyString(oS) + " with " + Path.Combine(storagePath, processName) + (KeyString(oS)).GetHashCode().ToString() + ".txt");
                    return;
                }

                int questionMark = oS.fullUrl.IndexOf("?");
                if (!record && questionMark > 0 && isProcessOfInterest)
                {
                    var result = from HttpSnoop x in mSnoops.Values where x.FileName.StartsWith(oS.RequestMethod+"."+oS.fullUrl.Substring(0, questionMark).Replace("/",".")) select x;
                    foreach (var snoop in result)
                    {
                        oS.utilCreateResponseAndBypassServer();
                        oS.LoadResponseFromFile(Path.Combine(storagePath, processName) + (snoop.FileName).GetHashCode().ToString() + ".txt");
                        Console.WriteLine("MOCKED !!!SIMILAR!!! RESPONSE FOR: " + KeyString(oS) + " with " + Path.Combine(storagePath, processName) + (snoop.FileName).GetHashCode().ToString() + ".txt");
                        oS.SaveResponse(Path.Combine(storagePath, processName) + (snoop.FileName).GetHashCode().ToString() + ".txt", false);
                        return;
                    }
                }
                if(isProcessOfInterest && !record)
                {
                    Console.WriteLine("Error, no response for request: " + KeyString(oS));
                    oS.utilCreateResponseAndBypassServer();
                    return;
                }
            };

 

 

If you notice, I'm actually doing something funky here; in the "replay" leg of this handler, if I don't have an exact matching recorded request URL I will respond to the request with a "similar" url, where in this case the "similar" url is simply anything with the same url root (everything before the where "?" character). This worked well for me in the scenario of an app doing a random query on an endpoint. Obviously this may not work in all scenarios.

 

Another really handy API to know is:

 

//tells Fiddler that this message's response will originate from the proxy server.
Fiddler.Session.utilCreateResponseAndBypassServer();

Calling this keeps your application of interest from ever hitting the target server, guaranteeing that that app never talks to the server. This is very handy since you don't want to block up traffic on the entire machine.

 

Using Fiddler.Session.LoadResponseFromFile/Fiddler.Session.SaveResponseToFile makes it really easy to manage the storage of http message bodies. In my example I used a Dictionary to map the request method and URL to the the file that contained the recorded response for the request.

The BeforeResponse event handler is similar, but opposite of the BeforeRequestHandler. Note that you have to lock around shared resources as many different threads will be invoking the events. Here we simply save the response if it is something we haven't seen before:

 

 
Fiddler.FiddlerApplication.BeforeResponse += delegate(Fiddler.Session oS)
            {
                
                bool isProcessOfInterest = false;
                string processName = null;
                foreach (Process p in Process.GetProcesses())
                {
                    if (p.Id == oS.LocalProcessID)
                    {
                        var results = from x in processNames where Path.GetFileNameWithoutExtension(x).Equals(p.ProcessName, StringComparison.CurrentCultureIgnoreCase) select x;
                        int count = 0;
                        foreach (string s in results)
                        {
                            count++;
                            processName = s;
                            break;
                        }
                        if (count > 0)
                        {
                            isProcessOfInterest = true;
                        }
                    }
                }
                
                if (!record || !isProcessOfInterest)
                {
                    return;
                }

                
                lock (mapLock)
                {
                    if (!mSnoops.ContainsKey((KeyString(oS)).GetHashCode()))
                    {
                        try
                        {
                            //Save the response to a text file
                            oS.SaveResponse(Path.Combine(storagePath, processName) + (KeyString(oS)).GetHashCode().ToString() + ".txt", false);
                            Console.WriteLine("CACHED RESPONSE FOR: " + KeyString(oS));
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("ERROR: Could not save response - {0}", processName + (KeyString(oS)).GetHashCode().ToString() + ".txt -" + e.ToString());
                        }
                        //Add the mapping of Request.Uri -> file name
                        mSnoops.Add((KeyString(oS)).GetHashCode(), new HttpSnoop()
                            {
                                FileName = KeyString(oS)
                            });
                    }
                }
};




After these two event handlers are implemented you have a very basic but functional "Mock" web service where you can test your client or your server in isolation.

The full Gist post of the implementaiton is here.

Happy testing!