Make your own API Testing Sandbox

5 min read

Required Software

What is API Testing?

Before developing a sandbox for API Testing what it is and its importance. API Testing, generally speaking, allows validation of endpoints and their results within an API. This validation can consist of checking status codes, the response body, the response headers, and many more attributes of the request. Before the end of this post, you will have stood up a fake API using Docker, as well as tested said API using JavaScript.

Why JavaScript for API Testing?

JavaScript has undoubtedly one of the largest development communities around. As a result, creating API Testing framework in the language ends up being a fairly simple task. If you're curious of what setting up an API Testing framework looks like in JavaScript check out my previous post here. Given the previously mentioned, JavaScript and the libraries that surround it can make a great API testing framework.

Why create an API Testing Sandbox?

Sandboxes are a great way to practice in any given programmatic subject. Whether you are a beginner or a seasoned vet a place to practice writing tests is significant. So let's jump into development!

Sandbox Setup

Enter httpbin

Httpbin is a simple HTTP service created by Kenneth Reitz that once running allows the user to send requests and get back specified responses. This means that using Httpbin we can return status codes, headers, or even response bodies as we see fit.

To get started with this service we will need to run it using docker. First ensure that you have docker installed, then using the following command to start the service.

docker run -p 8001:80 --name httpbin kennethreitz/httpbin
1

The command above will start the httpbin service on port 8001 of your machine, if you visit http://localhost:8001 you can see UI of the httpbin service. Feel free to familiarize yourself with the UI by clicking around and trying some requests to see what responses you get back. For example under HTTP Methods you can try the /get request. You should get the following response back if you click Try it out and then click Execute:

Curl: curl -X GET "http://localhost:8001/get" -H "accept: application/json"

Request URL: http://localhost:8001/get

Code 200

Response body

{
  "args": {},
  "headers": {
    "Accept": "application/json",
    "Accept-Encoding": "gzip, deflate, br",
    "Accept-Language": "en-US,en;q=0.9",
    "Connection": "keep-alive",
    "Cookie": "_ga=GA1.1.1242642017.1556946873; _gid=GA1.1.402070062.1556946873",
    "Host": "localhost:8001",
    "Referer": "http://localhost:8001/",
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) snap Chromium/73.0.3683.103 Chrome/73.0.3683.103 Safari/537.36"
  },
  "origin": "172.17.0.1",
  "url": "http://localhost:8001/get"
}

Response headers
 access-control-allow-credentials: true
 access-control-allow-origin: *
 connection: keep-alive
 content-length: 569
 content-type: application/json
 date: Sun, 05 May 2019 20:51:17 GMT
 server: gunicorn/19.9.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Great! Now that we have httpbin setup we can write some tests against it to practice our API Test writing skills.

Setting up our testing framework

Here we will be doing the minimum setup to write API Tests, to see a more complex API test setup check out my previous post.

First, we need to create a project, run the following commands from your terminal:

mkdir api-sandbox
cd api-sandbox
npm init -y
1
2
3

Next, we will need to install some dependencies to write our API tests. We will be using Jest as our testing framework and supertest as our API assertion library. So while in the api-sandbox directory run the following command to install the following dev dependencies:

npm install -D supertest jest
1

Then update your package.json file to allow running your test suite when npm test is run from the terminal.



 
 
 



{
  ...,
  "scripts": {
    "test": "jest"
  },
  ...
}
1
2
3
4
5
6
7

Great! You're now ready to start writing API tests against the httpbin server that we previously stood up. Let's start with a simple test against /get endpoint mentioned earlier. We can start by making a spec file and writing our test inside it, we'll name it httpbin.spec.js. By default when you run jest it will search for all files that end in .spec.js and execute the tests within them.

// ./httpbin.spec.js
const supertest = require("supertest");

describe("httpbin API tests", () => {
  it("should return 200 from the /get endpoint", async () => {
    await supertest("http://localhost:8001")
      .get("/get")
      .expect(200) // validate the status code
      .expect("Content-Type", "application/json") // validate a header
      .expect(response => {
        const { body } = response;

        expect(body.url).toBe("http://localhost:8001/get"); // validate the body contains a url property
      });
  });
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Once you are done writing the test run the following command in your terminal to run your tests.

npm test
1

That's it! You now can write tests against a fake server in JavaScript and validate your results. I would highly encourage you to read more documentation on the possible assertions that supertest and jest provide as well as explore the different requests that you can make to httpbin through their UI. All of the code, as well as instructions for managing the httpbin service, can be found here as well as in the resources down below.

Resources

Share