views:

66

answers:

3

I use a web service to convert files. The service returns the converted file as an HTTP POST, along with identifier data. My app receives the response, updates its database and saves the file to the appropriate location.

At least that's the idea, but how do I develop and test this on a local machine? Since it isn't publicly facing, I can't provide a directive URL. What's the best way to handle this? I want to keep the process as clean as possible, and the only ideas I can come up with have seemed excessively kludgey.

Given how common REST API development is, I assume there are well-established best practices for this. Any help appreciated.

A: 

The solution will change a bit depending on which server your using.

But the generally accepted method is using the loopback address: 127.0.0.1 in place of a fully qualified domain name. Your server may need to be reconfigured to listen on this IP address, but that's usually a trivial fix.

example: http://127.0.0.1/path/to/resource.html

You can use curl or even your browser if your application has a proper frontend. There are many other similar tools to test this from a command line, and each language has a set of libraries for establishing http connections and transferring data along them.

EmFi
My problem is that the service returning the response is isn't on my server, it's another company. The example above wouldn't resolve to my local machine.
jeffpatt
But your application is. Either you forge the HTTP POST to your application to test that your application is working correctly or configure port forwarding on your router to expose your application to the outside.
EmFi
I understand now, thanks. Forging the HTTP POST isn't something I would've thought of on my own, but that's a great solution.
jeffpatt
A: 

If your machine isn't accessible to the service you are using, then your only option would really be to build a local implementation of the service that will exercise your API. A rake task that sends the POST with the file and the info would be a nice thing so you could start your rails app locally, and then kick off the task with some params to run your application through its paces.

This is the case any time you are trying to develop a system that can't connect to a required resource during development. You need to build a development harness of sorts so that you can exercise all the different types of actions the external service will call on your application.

This certainly won't be easy or straight forward, especially if your interface to this external service is complicated. Be sure to have your test cases send bad POSTs to your application so that you are sure you handle both what you expect, and what you don't.

Also make sure that you do some integration testing with the actual service before you "go-live" with the application. Hopefully you can deploy to an external server that the web service will be able to access in order to test. Amazon's EC2 hosting environment would let you set up a server very quickly, run your tests, and then shut down without much cost at all.

danivovich
A: 

You have 2 options:

  1. Set up dynamic dns and expose your app to the outside world. This only works if you have full control over your network.

  2. Use something like webrat to fake the posts to your app. Since it's only 1 request, this seems pretty trivial.

Considering that you should be writing automated tests for this, I'd go with #2. I used to do #1 when developing facebook apps since there was far to many requests to mock them all out with webrat.

BJ Clark
Thanks for this. The idea of faking the post never occurred to me. Thanks also for the webrat recommendation.
jeffpatt