One of the things I've recently been working on is a large HTML5 website for a client. This site communicates with a WCF-based REST backend, using a Knockout plugin for the calls that, in the end, basically uses the usual $.ajax call from jQuery.

As I started the development of the proof of concept, I came to a point in which I wanted to display some dummy data in the UI without hard-coding into the source, as some fellow developers were to take over this codebase and write the actual production website. What I needed was a fast way to deploy some mock API calls and responses. If you deploy automated testing using a framework like Selenium WebDriver, which I also used for this project, this can be very useful for UI testing.

I was deploying this website using Internet Information Services. IIS has a very nice feature, on which you can pretty much configure all of the website using web.config files. This has de advantage that if you include this in a Team Foundation Server build which automatically deploys the website, then shipping these web.config files means wherever you run it, it will always be correctly configured. Also, web.config is accepted not only at the root of the website but anywhere.

Now, my API has a URI format of

http://example.local/<service_group>/<service>[query param...]

If you make the root of the API configurable, which is always a good idea anyway so you can switch between e.g. productive and testing servers, you can easily point this to another server with a similar API structure. So, the solution to my problem was easy!

I created an api/ folder in my proof of concept and placed a web.config file there specifying that all the files in that folder and respective children should have the application/json MIME type. Then I simply create a service group folder and a file named service in it, and in this file I place the mock reply. If the URI takes parameters, e.g. a GET, I keep creating folders and the last part of the URL is the file. Easy.

Now, if you try to do this from the IIS management console, it will ask you for an extension. Of course you can say that the extension is '*' to match all files, but this will only match files with extensions, which was not what I want. A workaround is to specify an extension of '.' without the quotes. The important part is that dot. It tells IIS that the files with no extension need to be served with application/json MIME type.

Take into account that if you are serving the API files from the same website, you won't be testing against cross origin requests, so if when changing to the real API server no call goes through, remember this.

And you're done! Pointing your AJAX calls to the local URL will get you the mock replies. While you're at it, stick another web.config file into the root to disable caching. Saves you a ton of time debugging!