Skip to main content

Using Fiddler to mock web responses

Introduction

I recently had a need to debug an issue that a customer was having, but it seemed as though it was related to their data. I had two options, take a copy of their data and configuration files and set them up in my development environment, or find another way.

"The Other Way"

What I really needed to be able to do was to return specific responses to certain requests, but allow other requests to go through unhindered, thus spoofing the client into believing I had the customer's data set up. That's when I remembered that Telerik Fiddler can be used as a proxy and has something called an AutoResponder which sounds like it could do what I wanted.

AutoResponder


The way the AutoResponder works is by matching the URL with a known string, and then performing an action if it matches (see the AutoResponder documentation for more info). This could be as simple as returning the response from a file, setting a header, or performing a redirect. The benefit to this is that it's very easy to set up, but unfortunately for me, it wasn't the solution I needed. The AutoResponder is ideal for data which can be returned verbatim (XML, JSON), but the data I was working with (JSONP) meant I needed to modify the response before it was sent back to the client, and I couldn't see a way to get the AutoResponder to do this (jQuery gives each JSONP request a dynamic function name to use, and waits for that function to be called when the response arrives).

FiddlerScript

So I started investigating FiddlerScript which, as it's name suggests, is Fiddler's scripting language. The FiddlerScript documentation is a bit sparse, but luckily the in-built editor has autocompletion which makes things a bit easier. The language is basically JavaScript which means it should be fairly easy to pick up, but it's based on JScript.NET which may or may not have some of the more advanced JavaScript features, so I found it best to stick to the tried and tested methods. The way FiddlerScript works is that there are several functions which are called at certain times during the request (like events), and we can add code to those functions to do what we need at the right time.

OnBeforeRequest

In this function, you can modify the request before the server sees it, so things like adding a request header, modifying the request data etc. can all be done here. You cannot modify the response as at this point, the server hasn't seen the request, so the response hasn't been generated yet.

OnBeforeResponse

This function happens after the server has seen the request and then generated the response for the client. This is where you need to put your code if you want to modify the data going back to the client (as I did). You can modify the original request, but as the server has already processed it, it will make no difference.

The Solution

The way I accomplished this was to add the following code to the OnBeforeResponse function, which retrieves the function name from the request object, then appends that to the specially crafted contents of a file on disk, then allow that to be returned to the client.

The way I accomplished this was to add the following code to the OnBeforeResponse function, which retrieves the function name from the request object, then appends that to the specially crafted contents of a file on disk, then allow that to be returned to the client.

The Code

if (oSession.uriContains('Layers=some_council_data')) {
    var data = File.ReadAllText("C:\\somedata.json");
    var url = oSession.fullUrl;
    var found = url.match(/jQuery\d{1,}_\d{1,}/i);
    if (found) {
        oSession.responseCode = 200;
        oSession.oResponse["Content-Type"] = 'application/json';
        oSession.utilSetResponseBody(data.replace(/\*\*id\*\*/gi, found[0]));
    }
}

The Original Data

jQuery164022858497596061111_1518526135138([
   {
      "type":"FeatureCollection",
      "features":[
         {
            "type":"Feature",
            "id":"1",
            "geometry":{
               "type":"Polygon",
               "coordinates":[
                  [ 389161.500000, 284626.300000 ],
                  [ 389161.500000, 286392.800000 ],
                  [ 392691.300000, 286392.800000 ],
                  [ 392691.300000, 284626.300000 ],
                  [ 389161.500000, 284626.300000 ]
               ]
            },
            "properties":{
               "fields":{ "Ward_Name_and_Councillor_Link":"Amber Nectar Ward" },
               "links":{ "Ward_Name_and_Councillor_Link":"http://councillors.acouncil.gov.uk/borough-wards/ambernectar" },
               "html":"<div class=\"infoResult\"> <p> <strong>Ward Name and Councillor Link:</strong> <strong><a title=\"Click here to go to the related web page\" target=\"_top\" href=\"http://councillors.acouncil.gov.uk/borough-wards/ambernectar\">Amber Nectar Ward</a></strong> </p> </div>"
            }
         }
      ],
      "properties":{
         "layer":"Electoral Wards",
         "layerName":"wards",
         "uniqueField":"",
         "uniqueFieldSelected":false,
         "htmlHeader":"<div class=\"contentDisplay popup-Electoral-Wards\"><h3>Electoral Wards</h3>",
         "htmlFooter":"</div>"
      }
   }
]);

The Modified Data

**id**([
   {
      "type":"FeatureCollection",
      "features":[
         {
            "type":"Feature",
            "id":"1",
            "geometry":{
               "type":"Polygon",
               "coordinates":[
                  [ 389161.500000, 284626.300000 ],
                  [ 389161.500000, 286392.800000 ],
                  [ 392691.300000, 286392.800000 ],
                  [ 392691.300000, 284626.300000 ],
                  [ 389161.500000, 284626.300000 ]
               ]
            },
            "properties":{
               "fields":{ "Ward_Name_and_Councillor_Link":"Amber Nectar Ward" },
               "links":{ "Ward_Name_and_Councillor_Link":"http://councillors.acouncil.gov.uk/borough-wards/ambernectar" },
               "html":"<div class=\"infoResult\"> <p> <strong>Ward Name and Councillor Link:</strong> <strong><a title=\"Click here to go to the related web page\" target=\"_top\" href=\"http://councillors.acouncil.gov.uk/borough-wards/ambernectar\">Amber Nectar Ward</a></strong> </p> </div>"
            }
         }
      ],
      "properties":{
         "layer":"Electoral Wards",
         "layerName":"wards",
         "uniqueField":"",
         "uniqueFieldSelected":false,
         "htmlHeader":"<div class=\"contentDisplay popup-Electoral-Wards\"><h3>Electoral Wards</h3>",
         "htmlFooter":"</div>"
      }
   }
]);

Conclusion

The result is that when the request is made by the client that matches the criteria, it loads the file from disk, replaces the ID in the file data, and then returns the response to the client. You can see this response as shown in the Chrome Developer Tools network tab below.



Comments

Popular posts from this blog

G-Cloud 11 Update

Astun Technology have been a Crown Commercial Service (CCS) Approved Supplier for a number of years now, with our solutions and services available via their Digital Marketplace.

Our customers have acquired our solutions via both the G-Cloud and Local Authority Software Applications (LASA) frameworks.

G-Cloud 10 was replaced by G-Cloud 11 on 2 July 2019 and I am delighted to say that all of our solutions and services are now available for purchase under this new framework. This framework makes buying cloud solutions and services faster and cheaper than entering into individual procurement contracts, so switching to Cloud based GIS has just got easier.

Our solutions are available under the Cloud Software and Cloud Support lots as follows:

Cloud Software
iShare In the CloudQGIS in the CloudAstun Data ServicesGet INSPIREdGet INSPIREd and Enterprise Metadata for iShareGet INSPIREd Enterprise
Cloud Support Geospatial Cloud Consultancy ServicesGeospatial Open Source Support PackagesGeospatial O…

Making home working work!

I was chatting to a personal acquaintance recently about work and we got on to the subject of home working. The organisation she worked for was going through a period of change and they were planning on moving some of their office staff to home working on a permanent basis. She was after my views on how we manage to make that work effectively at Astun. This blog, which is very much a manager's perspective, is what I told her!

Before we get on to to the two key themes of this blog - the Technical and Cultural - a quick bit about Astun first! 1. Astun's circumstances We employ around 21 people, of which 6 or so work from an office in Epsom, and the rest work from home on a permanent basis. From my home in Bristol, I line manage 13 of those people, 3 of which are office based, the rest primarily work in the UK, as far north as Tyneside & Lancaster and as far south as Southampton. I have one member of the team who is based in Seville in Spain.
2. Technical Use of technology is k…

Reflections on FOSS4G UK Edinburgh 2019

Six members of the Astun team recently headed en mass to the Free and Open Source Software for Geo (FOSS4G) UK conference in Edinburgh - and we came back with a 7th!

FOSS4G is a great forum to find out all the latest on what is going on in the OSGeo community and to spread the word via talks and workshops about some of the exciting work we’ve been involved in. Check out the post to find out about our own personal reflections of the event … and find out who our latest recruit is in the final post!

Dan The FOSS4G UK conference just seems to get better and better each year, and this year was no exception. As the baton gets passed annually to a new organising team, the advice and guidance on running a great event goes with it. This year’s team led by co-chairs Tom Armitage and Ross McDonald did an amazing job. The venue was breathtaking, the agenda was packed with outstanding talks and workshops and the atmosphere both during the day and in the evening social just had a great buzz to it. …