XML.com: XML From the Inside Out
oreilly.comSafari Bookshelf.Conferences.

advertisement

Putting REST on Rails
by Dan Kubb | Pages: 1, 2, 3, 4, 5, 6

Trust, but Verify

It's always nice to have test cases that pass, but what about the real world? When I first write my test cases I always like to double-check them to make sure everything is working perfectly. Let's issue a few requests directly to our application using trusty old curl.

Test Viewing a List of Books

Before we start we need to bring up a test server with the following command:

ruby script/server

Then open a second command-line prompt and run the following:

curl http://localhost:3000/books/

The expected response is:

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <title>Books</title>
</books>

As you can see, there are no books listed yet.

Test Adding to a List of Books

Next, create a new book:

curl \
  -i \
  -X POST \
  -H 'Content-Type: application/xml' \
  -d '<book><title>a title</title><description>a description</description></book>' \
  http://localhost:3000/books/

The response to this request will include several headers, but the ones we care about are:

HTTP/1.1 201 Created
Location: http://localhost:3000/books/1

Note the URI in the Location header, we'll be using that in a moment.

To make sure the new book is listed in the collection:

curl http://localhost:3000/books/

The expected response is:

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <title>Books</title>
  <book>
    <id>1</id>
    <title>a title</title>
    <link href="http://localhost:3000/books/1"/>
  </book>
</books>

Make sure the link's href is the same as the Location header we got from the previous step.

Test Viewing a Book

Follow the Location header from the previous step:

curl http://localhost:3000/books/1

The expected response is:

<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>a title</title>
  <id type="integer">1</id>
  <description>a description</description>
</book>

Test Changing a Book

Update the book using PUT:

curl \
  -i \
  -X PUT \
  -H 'Content-Type: application/xml' \
  -d '<book><title>a new title</title><description>a new description</description></book>' \
  http://localhost:3000/books/1

This too will return several headers, but at this moment we only care about the first line:

HTTP/1.1 204 No Content

Retrieve the book again to see the changes:

curl http://localhost:3000/books/1

The expected response is:

<?xml version="1.0" encoding="UTF-8"?>
<book>
  <title>a new title</title>
  <id type="integer">1</id>
  <description>a new description</description>
</book>

Test Deleting a Book

We'll delete the book like this:

curl -i -X DELETE http://localhost:3000/books/1

Again, there will be several headers, but the one that tells us the DELETE was successful is the first line:

HTTP/1.1 204 No Content

To verify the book is deleted, perform a GET on the collection:

curl http://localhost:3000/books/

The expected response is:

<?xml version="1.0" encoding="UTF-8"?>
<books>
  <title>Books</title>
</books>

The collection contains no books now, just as we would expect.

Only the Beginning

So now we have a simple RESTful Rails application with a full test suite and HTTP method dispatching. We used the HTTP OPTIONS method to identify the methods each resource supports, and we based our test cases around a relatively simple RESTful protocol.

As far as building RESTful Rails applications in Ruby on Rails we're only at the beginning of what is possible. There's an increased interest by the Rails core development team, and David Heinemeier Hansson in particular, in HTTP and the REST architectural style. The RESTful Rails project is off to a good start with per-method dispatching; conditional GET, HTTP Authentication, and more are planned in the near future.

Given the momentum of Ruby on Rails it's a safe bet that in the coming months you'll see more RESTful applications and a better understanding of the HTTP protocol within the community.



1 to 9 of 9

  1. 2010-07-10 05:49:48 stellamcnorty
  2. Yea, standard ruby, sorry
    2006-08-01 18:06:17 johnarmstrong
  3. How about joined tables?
    2006-08-01 18:00:53 johnarmstrong
  4. This is an important tutorial
    2006-08-01 17:11:34 johnarmstrong
  5. Output problem.
    2006-07-18 12:59:48 Derek Dees
  6. Very very very great
    2006-05-30 12:17:52 johnarmstrong
  7. fixtures :books missing?
    2006-04-27 18:03:33 jabowery
  8. Flash integration
    2006-04-22 15:40:07 slipabuck
  9. test functionals Failure
    2006-04-21 11:49:22 Pedromatic
1 to 9 of 9