PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

API Testing with Frisk (and Joind.in!)

Note: This article was originally published at Planet PHP on 26 October 2010.
Planet PHP

Two great tastes taste great together, right? As I've been working more on Frisk in my spare time (yes, there's a little in there between PHPDeveloper.org and Joind.in) I've been trying to develop features for it that would be helpful not only for testing web site interaction, but also in working with other kinds of web-based interfaces. Naturally, my thoughts wandered over to the Joind.in API and how I could use Frisk to work with and test the responses it gives.

I've added a few more handy things lately - like matching values inside of JSON objects and different output options - but one of the latest things is an improvement to the POST and GET handlers to allow for more verbose options on requests. This comes in handy when we want to test something a bit more technical than just your average web site.

First, if you just want to check out the test, here's the code in a handy .phps file. It's the sample test I'll be describing.

public function testSiteStatus() { // This is our object to send $postData = json_encode(array('request'=array('action'=array('type'='status','data'=array('test_string'='my test'))))); // This is what the object that comes back should look like $response = json_encode(array('dt'=date('r'),'test_string'='my test')); // Build the settings for the connection $settings=array('location' = '/api/site', 'host' = 'ji-enygma.localhost', 'postData' = $postData, 'outputFormat' = 'json', 'headers' = array('Content-Type'='text/json')); // Make the request and check the response! $this-post($settings) -assertEquals($response); }

The large part of the example test is made up of the aosetupa to even make the test happen. In $postData I've created a JSON message based on what the Joind.in API is expecting for the Site/Status request and pushed it into a string. The second JSON object, $response, mimics what the API's response will look like. The $settings array is the more verbose way of specifying settings for the ActionPost request (including the additional headers). The Joind.in API requires that you give it a Content-Type of the message you're giving it - it's not a very good guesser.

The next two lines do all of the work - make the post request to the remote API, return the message and check to see if it's the same as $response. Simple, right?

If you wanted to get a little more fancy with checking, you can use the aopathsa in the assertContains to look inside of a JSON object. So, you could do something like:

post($settings) -assertContains('look for me','message_list/message/message1');?

That will look at aomessage1a in this JSON to see if it contains that aolook for mea string:

{amessage_lista:{amessagea:{amessage1a:aI think you should look for mea,amessage2a:abut not herea}}}

You can find out more about the Joind.in API and all it has to offer on the Joind.in site and for more information on Frisk, check out it's project on github.

  • Joind.in API - Interface directly with Community events & feedback
  • Frisk - automated functional unit testing