2015年2月24日星期二

SitePoint PHP Blog: Testing APIs with RAML


The SitePoint PHP blog has a new tutorial posted today showing you how to test your API via RAML, using the structure it defines to verify the requests and responses made to the API. This is the second part of the series and you can find part one (the introduction to RAML) here.



In a recent article I looked at RESTful API Modeling Language (RAML). I provided an overview of what RAML is all about, how to write it and some of its uses. This time, I'm going to look at some of the ways in which you can use RAML for testing. We'll start by using RAML to validate responses from an API. Then we'll look at an approach you could take to mock an API server, using a RAML file to create mock HTTP responses.


He starts off by defining a basic RAML document that defines an "Albums" structure with endpoints for "account" and "albums" with various data beneath each one (and created an application that follows it). He then shows how to combine Guzzle, PHPUnit and a RAML parser to grab the API definition and set up a sample test. A simple example test is provided showing you how to check the validity of a response structure. Then he gets into mocking the API using the RAML structure using the FastRoute router. He creates a mock object and a "dispatch" method to handle the request routing based on the contents of the RAML document. He also includes a method to check the parameter values on a request, ensuring they're the correct types.


Link: http://www.sitepoint.com/testing-apis-raml/

没有评论:

发表评论