Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test a JSON API?

I'm putting together a JSON API which may or may not be built with Rails. I'd like to be able to verify that the JSON API is behaving as expected by testing it from a test client that only communicates via HTTP.

For example, the test client will send a request to a given URL on the test server, and then verify that the response is a JSON string equal to what is expected by the specs. The response may also be an HTTP response code such as a 401.

I'm currently running tests with QUnit and jQuery.ajax. It works, but I'm wondering if there's a better option. Has anyone else done something like this?

Thanks!

like image 783
Eric the Red Avatar asked Oct 24 '11 14:10

Eric the Red


1 Answers

Any language with a decent HTTP library and a JSON library ought to do. Just pick your favorite language and testing framework and go for it. I'd probably use RSpec and Rack::Test, especially if the API will be implemented in Ruby, but tastes vary.

Edit: You mentioned you'd like to run the tests outside the server. If you're sure about that then use e.g. Net::HTTP instead of Rack::Test.

like image 77
mpartel Avatar answered Nov 12 '22 15:11

mpartel