Testing Routes

If you’re like me, your next thought might relate to how you’re supposed to test PUT and DELETE methods. As you might know, most browsers only natively provide support for GET and POST. Making use of the other two request methods requires a bit of trickery, which we’ll review shortly.

Until then, the most user-friendly way to make custom requests is through a Google Chrome extension, called Advanced Rest Client. Once installed, you can easily specify both a URI, as well as the desired request method.

“The Advanced REST Client extensions provides an easy mechanism to test URIs.

cURL

If you’re somewhat comfortable in the command line, it’s recommended that you instead leverage the power of cURL to test these routes.

the X flag allows you to specify the request method that should be used. Please note that, in the previous snippet, we’re being explicit; the X flag is not necessary for GET requests, as they are the default.

Here’s an example for testing a few routes. Keep in mind that we’re using simple echo statements to describe the action that should take place.

Modifying data on the server will surely require the new data (likely obtained from a form). When using cURL, key-value pairs may be specified, using the -d flag, like so:

This command can be divided into three pieces:

1. What’s the URI?
2. What request method should be used?
3. What data should be passed to the server?

Using your server-side language of choice (in our case, PHP), you can then fetch this POST data in the same way that you normally would:

Browser Support

Okay, we understand how to specify a request method from the command line, but how might we do the same thing from an HTML form? Unfortunately, we can’t rely on:

Our current crop of browsers do not provide native support for these types of requests. The most common solution is to apply a bit of trickery, via hidden inputs. Using the Slim framework, here’s how we might update a friend.

Notice that, technically, the request method is still set to POST. However, behind the scenes, Slim will read the hidden input’s value, and proceed accordingly, dependent upon which verb is specified.

You’ll certainly find that most frameworks follow a similar pattern for specifying request types.

In this section, we reviewed but one framework’s implementation of resourceful routing. As you’ll find, though, the same is possible in most frameworks these days. For example, if you’re a Laravel artisan, then you might use the following syntax (as of version 4):

This convenient resource method specifies that we wish to generate the necessary routes for a friends resource, and make FriendsController responsible for handling the logic for each of these routes.

Manual Implementation

Should you wish to opt out of a framework, you can still implement this functionality on your own. Unfortunately, writing the necessary routing system from scratch is beyond the scope of this tutorial, however, here’s a few tips to get you started.

First, the most important question: how do we determine what the associated verb for a particular request is? If working along, create a new PHP file, and add:

If you run this page in the browser, the output should be GET. Excellent! Now, we have the necessary means to detect the request method, and proceed, as needed.

The following snippet is decidely elementary, and will require better structure for your projects, but it can be used as a starting point.

Testing

There are a variety of packages, which make the process of writing integration tests as simple as possible. However, let’s keep it simple and use PHP’s built-in cURL library (libcurl) to test some of these routes.

The first step would be to initialize cURL, and specify the desired URI. In our case, we’ll continue testing the friends resource.

Next, unless we’re testing the default GET request method, we’ll need to specify the request type.

By default, this output will immediately be echoed. To instead return the output to a variable, rather than displaying it directly, we can make use of the CURLOPT_RETURNTRANSFER setting.

That should do it! We only need to execute the request, and fetch the results.

PHPUnit

Let’s add this to a PHPUnit test - again, keeping it relatively simple, for readability’s sake. We’ll simply ensure that each route returns the proper 200 status code, signifying that the request has completed successfully. To dry things up, we’ll abstract the cURL functionality away to its own method, request().

Assuming that you have PHPUnit installed, run phpunit friendsTest.php. If successful, you should see green! Feel free to engage in a private victory dance.

To take things a step further, it makes sense to abstract that request method out to a base class that your tests can then extend. Or, even better, let a highly tested (and functional) third party tool handle the legwork. You might consider Goutte, which can be installed through Composer.

Once installed (composer install), we can ignore the manual cURL implementation from earlier, and instead make use of Goutte’s cleaner API, as demonstrated below:

You wouldn’t be the first person to consider such tests to be superfluous; however, I assure you that, as soon as one of these tests failing saves you from a silly blunder, you’ll instantly recognize their usefulness.

Further Learning

To continue your education, the best guide for learning REST comes courtesy of the folks at Apigee. Their presentation, “Teach a Dog to Rest,” compiles a mass of knowledge into a mere twenty minutes. This is required viewing for all beginners.

“Apigee offers one of the most user-friendly introductions to REST on the web.