API Testing with Frisk (and Joind.in!)

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.

[php]
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);
}
[/php]

The large part of the example test is made up of the “setup” 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 “paths” in the assertContains to look inside of a JSON object. So, you could do something like:

[php]
<?php

$this->post($settings)
->assertContains(‘look for me’,’message_list/message/message1′);

?>
[/php]

That will look at “message1” in this JSON to see if it contains that “look for me” string:

{“message_list”:{“message”:{“message1″:”I think you should look for me”,”message2″:”but not here”}}}

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

Leave a comment