documentation | Matthew Daly's Bloghttps://matthewdaly.co.uk/blog/categories/documentation/
documentation | I'm a web developer in Norfolk. This is my blog...Sat, 10 Mar 2018 15:12:45 GMThttp://blogs.law.harvard.edu/tech/rssgrunt-blogbuilder https://github.com/matthewbdaly/grunt-blogbuilderMatthew Daly 2018https://matthewdaly.co.uk/blog/2016/08/08/testing-your-api-documentation-with-dredd/
https://matthewdaly.co.uk/blog/2016/08/08/testing-your-api-documentation-with-dredd/Mon, 08 Aug 2016 16:05:00 GMTDocumenting your API is something most developers agree is generally a Good Thing, but it’s a pain in the backside, and somewhat boring to do. What you really need is a tool that allows you to specify the details of your API before you start work, generate documentation from that specification, and test your implementation against that specification.

Fortunately, such a tool exists. The Blueprint specification allows you to document your API using a Markdown-like syntax. You can then create HTML documentation using a tool like Aglio or Apiary, and test it against your implementation using Dredd.

In this tutorial we’ll implement a very basic REST API using the Lumen framework. We’ll first specify our API, then we’ll implement routes to match the implementation. In the process, we’ll demonstrate the Blueprint specification in action.

Getting started

Assuming you already have PHP 5.6 or better and Composer installed, run the following command to create our Lumen app skeleton:

$ composer create-project --prefer-dist laravel/lumen demoapi

Once it has finished installing, we’ll also need to add the Dredd hooks:

$ cd demoapi

$ composer require ddelnano/dredd-hooks-php

We need to install Dredd. It’s a Node.js tool, so you’ll need to have that installed. We’ll also install Aglio to generate HTML versions of our documentation:

$ npm install -g aglio dredd

We also need to create a configuration file for Dredd, which you can do by running dredd init. Or you can just copy the one below:

dry-run:null

hookfiles:null

language:php

sandbox:false

server:'php -S localhost:3000 -t public/'

server-wait:3

init:false

custom:

apiaryApiKey:''

names:false

only:[]

reporter:apiary

output:[]

header:[]

sorted:false

user:null

inline-errors:false

details:false

method:[]

color:true

level:info

timestamp:false

silent:false

path:[]

hooks-worker-timeout:5000

hooks-worker-connect-timeout:1500

hooks-worker-connect-retry:500

hooks-worker-after-connect-wait:100

hooks-worker-term-timeout:5000

hooks-worker-term-retry:500

hooks-worker-handler-host:localhost

hooks-worker-handler-port:61321

config:./dredd.yml

blueprint:apiary.apib

endpoint:'http://localhost:3000'

If you choose to run dredd init, you’ll see prompts for a number of things, including:

The server command

The blueprint file name

The endpoint

Any Apiary API key

The language you want to use

There are Dredd hooks for many languages, so if you’re planning on building a REST API in a language other than PHP, don’t worry - you can still test it with Dredd, you’ll just get prompted to install different hooks.

Note the hookfiles section, which specifies a hookfile to run during the test in order to set up the API. We’ll touch on that in a moment. Also, note the server setting - this specifies the command we should call to run the server. In this case we’re using the PHP development server.

If you’re using Apiary with your API (which I highly recommend), you can also set the following parameter to ensure that every time you run Dredd, it submits the results to Apiary:

custom:

apiaryApiKey:<API KEY HERE>

apiaryApiName:<API NAME HERE>

Hookfiles

As mentioned, the hooks allow you to set up your API. In our case, we’ll need to set up some fixtures for our tests. Save this file at tests/dredd/hooks/hookfile.php:

<?php

useDredd\Hooks;

useIlluminate\Support\Facades\Artisan;

require__DIR__ . '/../../../vendor/autoload.php';

$app = require__DIR__ . '/../../../bootstrap/app.php';

$app->make(\Illuminate\Contracts\Console\Kernel::class)->bootstrap();

Hooks::beforeAll(function(&$transaction)use($app){

putenv('DB_CONNECTION=sqlite');

putenv('DB_DATABASE=:memory:');

Artisan::call('migrate:refresh');

Artisan::call('db:seed');

});

Hooks::beforeEach(function(&$transaction)use($app){

Artisan::call('migrate:refresh');

Artisan::call('db:seed');

});

Before the tests run, we set the environment up to use an in-memory SQLite database. We also migrate and seed the database, so we’re working with a clean database. As part of this tutorial, we’ll create seed files for the fixtures we need in the database.

This hookfile assumes that the user does not need to be authenticated to communicate with the API. If that’s not the case for your API, you may want to include something like this in your hookfile’s beforeEach callback:

$user = App\User::first();

$token = JWTAuth::fromUser($user);

$transaction->request->headers->Authorization = 'Bearer ' . $token;

Here we’re using the JWT Auth package for Laravel to authenticate users of our API, and we need to set the Authorization header to contain a valid JSON web token for the given user. If you’re using a different method, such as HTTP Basic authentication, you’ll need to amend this code to reflect that.

With that done, we need to create the Blueprint file for our API. Recall the following line in dredd.yml:

Our first route

Dredd is not a testing tool in the usual sense. Under no circumstances should you use it as a substitute for something like PHPUnit - that’s not what it’s for. It’s for ensuring that your documentation and your implementation remain in sync. However, it’s not entirely impractical to use it as a Behaviour-driven development tool in the same vein as Cucumber or Behat - you can use it to plan out the endpoints your API will have, the requests they accept, and the responses they return, and then verify your implementation against the documentation.

We will only have a single endpoint, in order to keep this tutorial as simple and concise as possible. Our endpoint will expose products for a shop, and will allow users to fetch, create, edit and delete products. Note that we won’t be implementing any kind of authentication, which in production is almost certainly not what you want - we’re just going for the simplest possible implementation.

First, we’ll implement getting a list of products:

FORMAT: 1A

# Demo API

# Products [/api/products]

Product object representation

## Get products [GET /api/products]

Get a list of products

+ Request (application/json)

+ Response 200 (application/json)

+ Body

{

"id": 1,

"name": "Purple widget",

"description": "A purple widget",

"price": 5.99,

"attributes": {

"colour": "Purple",

"size": "Small"

}

}

A little explanation is called for. First the FORMAT section denotes the version of the API. Then, the # Demo API section denotes the name of the API.

Next, we define the Products endpoint, followed by our first method. Then we define what should be contained in the request, and what the response should look like. Blueprint is a little more complex than that, but that’s sufficient to get us started.

Our route is returning HTML, not JSON, and is also raising a 404 error. So let’s fix that. First, let’s create our Product model at app/Product.php:

<?php

namespaceApp;

useIlluminate\Database\Eloquent\Model;

classProductextendsModel

{

//

}

Next, we need to create a migration for the database tables for the Product model:

$ php artisan make:migration create_product_table

Created Migration: 2016_08_08_105737_create_product_table

This will create a new file under database/migrations. Open this file and paste in the following:

<?php

useIlluminate\Database\Schema\Blueprint;

useIlluminate\Database\Migrations\Migration;

classCreateProductTableextendsMigration

{

/**

* Run the migrations.

*

* @return void

*/

publicfunctionup()

{

// Create products table

Schema::create('products', function(Blueprint $table){

$table->increments('id');

$table->string('name');

$table->text('description');

$table->float('price');

$table->json('attributes');

$table->timestamps();

});

}

/**

* Reverse the migrations.

*

* @return void

*/

publicfunctiondown()

{

// Drop products table

Schema::drop('products');

}

}

Note that we create fields that map to the attributes our API exposes. Also, note the use of the JSON field. In databases that support it, like PostgreSQL, it uses the native JSON support, otherwise it works like a text field. Next, we run the migration to create the table:

$ php artisan migrate

Migrated: 2016_08_08_105737_create_product_table

With our model done, we now need to ensure that when Dredd runs, there is some data in the database, so we’ll create a seeder file at database/seeds/ProductSeeder:

<?php

useIlluminate\Database\Seeder;

useCarbon\Carbon;

classProductSeederextendsSeeder

{

/**

* Run the database seeds.

*

* @return void

*/

publicfunctionrun()

{

// Add product

DB::table('products')->insert([

'name' => 'Purple widget',

'description' => 'A purple widget',

'price' => 5.99,

'attributes' => json_encode([

'colour' => 'purple',

'size' => 'Small'

]),

'created_at' => Carbon::now(),

'updated_at' => Carbon::now(),

]);

}

}

You also need to amend database/seeds/DatabaseSeeder to call it:

<?php

useIlluminate\Database\Seeder;

classDatabaseSeederextendsSeeder

{

/**

* Run the database seeds.

*

* @return void

*/

publicfunctionrun()

{

$this->call('ProductSeeder');

}

}

I found I also had to run the following command to find the new seeder:

$ composer dump-autoload

Then, call the seeder:

$ php artisan db:seed

Seeded: ProductSeeder

We also need to enable Eloquent, as Lumen disables it by default. Uncomment the following line in bootstrap/app.php:

$app->withEloquent();

With that done, we can move onto the controller.

Creating the controller

Create the following file at app/Http/Controllers/ProductController:

<?php

namespaceApp\Http\Controllers;

useIlluminate\Http\Request;

useApp\Product;

classProductControllerextendsController

{

private $product;

publicfunction__construct(Product $product){

$this->product = $product;

}

publicfunctionindex()

{

// Get all products

$products = $this->product->all();

// Send response

return response()->json($products, 200);

}

}

This implements the index route. Note that we inject the Product instance into the controller. Next, we need to hook it up in app/Http/routes.php:

Whoops, looks like we made a mistake here. The index route returns an array of objects, but we’re looking for a single object in the blueprint. We also need to wrap our attributes in quotes, and add the created_at and updated_at attributes. Let’s fix the blueprint:

That’s our read support done. We just need to add support for POST, PATCH and DELETE methods.

Our remaining methods

Let’s set up the test for our POST method first:

## Create products [POST /api/products]

Create a new product

+ name (string) - The product name

+ description (string) - The product description

+ price (float) - The product price

+ attributes (string) - The product attributes

+ Request (application/json)

+ Body

{

"name": "Blue widget",

"description": "A blue widget",

"price": 5.99,

"attributes": "{\"colour\": \"blue\",\"size\": \"Small\"}"

}

+ Response 201 (application/json)

+ Body

{

"id": 2,

"name": "Blue widget",

"description": "A blue widget",

"price": 5.99,

"attributes": "{\"colour\": \"blue\",\"size\": \"Small\"}",

"created_at": "*",

"updated_at": "*"

}

Note we specify the format of the parameters that should be passed through, and that our status code should be 201, not 200 - this is arguably a more correct choice for creating a resource. Be careful of the whitespace - I had some odd issues with it. Next, we add our route:

$app->post('/api/products', 'ProductController@store');

And the store() method in the controller:

publicfunctionstore(Request $request)

{

// Validate request

$valid = $this->validate($request, [

'name' => 'required|string',

'description' => 'required|string',

'price' => 'required|numeric',

'attributes' => 'string',

]);

// Create product

$product = new$this->product;

$product->name = $request->input('name');

$product->description = $request->input('description');

$product->price = $request->input('price');

$product->attributes = $request->input('attributes');

// Save product

$product->save();

// Send response

return response()->json($product, 201);

}

Note that we validate the attributes, to ensure they are correct and that the required ones exist. Running Dredd again should show the route is now in place:

Generating HTML version of your documentation

Now we have finished documenting and implementing our API, we need to generate an HTML version of it. One way is to use aglio:

$ aglio -i apiary.apib -o output.html

This will write the documentation to output.html. There’s also scope for choosing different themes if you wish.

You can also use Apiary, which has the advantage that they’ll create a stub of your API so that if you need to work with the API before it’s finished being implemented, you can use that as a placeholder.

Summary

The Blueprint language is a useful way of documenting your API, and makes it simple enough that it’s hard to weasel out of doing so. It’s worth taking a closer look at the specification as it goes into quite a lot of detail. It’s hard to ensure that the documentation and implementation remain in sync, so it’s a good idea to use Dredd to ensure that any changes you make don’t invalidate the documentation. With Aglio or Apiary, you can easily convert the documentation into a more attractive format.

You’ll find the source code for this demo API on Github, so if you get stuck, take a look at that. I did have a fair few issues with whitespace, so bear that in mind if it behaves oddly. I’ve also noticed a few quirks, such as Dredd not working properly if a route returns a 204 response code, which is why I couldn’t use that for deleting - this appears to be a bug, but hopefully this will be resolved soon.

I’ll say it again, Dredd is not a substitute for proper unit tests, and under no circumstances should you use it as one. However, it can be very useful as a way to plan how your API will work and ensure that it complies with that plan, and to ensure that the implementation and documentation don’t diverge. Used as part of your normal continuous integration setup, Dredd can make sure that any divergence between the docs and the application is picked up on and fixed as quickly as possible, while also making writing documentation less onerous.