Testing a JSON API

107 views
Skip to first unread message

Eric Sorenson

unread,
Oct 25, 2011, 4:00:43 PM10/25/11
to Mad Railers, the Madison, WI Ruby on Rails Users Group
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!

Bradley Grzesiak

unread,
Oct 25, 2011, 4:09:13 PM10/25/11
to mad-r...@googlegroups.com
You can absolutely use Capybara to test any HTTP service. The trick is to make sure that Capybara doesn't wrap your JSON in an HTML skeleton. I think you need to ask for the source not the body...

See the "Calling remote servers" section of https://github.com/jnicklas/capybara/

:brad


--
You received this message because you are subscribed to the Google Groups "Mad Railers, the Madison, WI Ruby on Rails Users Group" group.
To visit the Madison Rails Home page, go to http://madisonrails.com/
To post to this group, send email to Mad-R...@googlegroups.com
To unsubscribe from this group, send email to Mad-Railers...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Mad-Railers?hl=en



--
Bradley Grzesiak
founder, bendyworks llc
http://bendyworks.com/

Nicholas Klick

unread,
Oct 25, 2011, 4:09:48 PM10/25/11
to mad-r...@googlegroups.com
If you decide to use Ruby for the server side, I would recommend this book, unless you have it already, which covers Ruby and JSON API's pretty thoroughly:

I'm pretty sure the entire book is TDD. I have a copy if you want to borrow it. 

Nicholas


On Tue, Oct 25, 2011 at 3:00 PM, Eric Sorenson <eric.s...@dubharmonic.com> wrote:

Matthew Margolis

unread,
Oct 25, 2011, 9:57:46 PM10/25/11
to mad-r...@googlegroups.com
I don't have any experience using it but
https://github.com/collectiveidea/json_spec looks interesting.

On Tue, Oct 25, 2011 at 3:00 PM, Eric Sorenson
<eric.s...@dubharmonic.com> wrote:

Reply all
Reply to author
Forward
0 new messages