Faking Web service calls with *IX file systems

Yes, it’s a hack!

March 25, 2017

Note: A version of this was originally written in 2010. I have modified it somewhat, and am republishing it so I remember the technique.

Every once in a while it is nice to be able to mock up something like a Web service without having to write code first. Perhaps you just don’t want to spend the time writing the service yet, no matter how trivial. Maybe you don’t have the appropriate software installed or configured. Whatever. If your local Web server or your ISP is running on a *IX/BSD OS, then you can use native “UNIX” file systems to mock up Web service “GET” methods, including parameterized URLs.

Note: The following technique will not work for FAT*/NTFS file systems due to the restrictions on special characters like “?” in file names.

Let’s say your client code is going to want to build URLs of the type:

http://yoursite.com/objectname?querystring

For example:

http://yoursite.com/customers?Dick%20Tracy

You can simply create test files in any format you want (XML, JSON, etc.) and push them to the root of your host and name them accordingly. For example, you could create a file named customers?Dick%20Tracy, e.g.,

$ touch 'customers?Dick%20Tracy'
$ ls
customers?Dick%20Tracy

The single quotes are required.

Now you should be able to do something like the following (Objective-C code, map the example to your own programming language to make it work for you).

NSURL *url = [NSURL URLWithString:@"http://localhost:8000/customers%3fDick%20Tracy"];
NSDictionary *customer = [NSDictionary dictionaryWithContentsOfURL:url];

Works great! And because there are so few special character restrictions in file names on *IX file systems, you can name your files to represent multiple query string parameters. The above file just as easily could have been named:

customers?lastname=Tracy&firstname=Dick&middlename=

…allowing HTTP “GET” access via:

http://localhost:8000/customers?lastname=Tracy&firstname=Dick&middlename=

It is then easy enough to serve such “requests” using something like:

python -m SimpleHTTPServer 8000

Yeah, this is a hack, but it’s saved me time by allowing me to quickly get something I can access using the HTTP protocol’s GET method that ultimately mimics the URLs I will be building and sending to a real Web service later. A few minutes of file editing and I have a “Web service” mockup ready for testing from the client prototype, at least for GETs.

Cool.