Test <a> REST API
In this post we make use of the outer layer of REST, the mythical and often forgotten HATEOAS — Hypermedia As The Engine Of Application State. According to Roy Fielding an API needs to be hypertext driven to be Restful. What that means is that a response from an API is not only data but that the response also contains links to other resources and forms to make changes to the data. An example is a plain old HTML page that contains links to other pages making it possible for a client to browse the webb site and forms to be submitted to change the state of the web site.
When an API is connected with links its possible to verify that all links are pointing to a responding resource. To do that we´re gonna use one or two wget commands.
If the API requires authentication we can store the session cookie with help of the following command.
wget --keep-session-cookies --save-cookies cookies.txt --post-data “email=some_email&password=some_password” http://localhost:9292/api/auth/authenticate
The URL we POST to http://localhost:9292/api/auth/authenticate and the posted data email=some_email and password=some_password
When we have a authenticated session cookie we can crawl our API with the following command
wget --load-cookies cookies.txt --spider -r -nd -nv -o api_test.log http://localhost:9292/api
With this arguments we tell wget to follow every link it can find in the entry URL http://localhost:9292/api and sequentially crawl the hole web site. It will store its result in the file api_test.log
The output will look something like this:
Found 1 broken link.
FINISHED — 2016–12–02 15:27:01 —
Total wall clock time: 4m 5s
Downloaded: 6613 files, 45M in 0.09s (473 MB/s)
The result of our test is one broken link http://localhost:9292/api/broken.