Test Cases for REST SERVICES In Golang

In this post, I will showcase how I wrote black box test cases for REST HTTP APIs in GO.

The EndPoints

Before jumping into the main code let us look at the end points which I am gonna test

POST /signup For signup functionality and creating new users.
GET /users For listing the users.

You can check full source code by clicking here, but I am going to walk you through the code snippet here as well. Feel free to jump to the next section if you are comfortable with the code.

Like many applications in Golang , the starting point for this web app is in the main function.It listens for 9090 port and handles these 2 end points as mentioned above. Along with the main function , in user.go , I also have “users” that will serve as a storage when a new user is created. However, You can later store and fetch users from Memcache,Databases or Datastores etc.

I used Julien Schmidt’s HTTP Router to route the HTTP request.If you never used this package before , do check out.It comes with the flexibility to build up awesome web applications.

For creating users the REST Endpoint is /signup. The handler function for the endpoint is signupHandler. In that Handler function initially, I am parsing the http.Request body of type ‘application-json’. Checking for each of the required fields and then calling up a checkUniquness function to ensure that username is unique across all users. If validation passes , I create a new user and respond back with ‘User created successfully’ message.In a case of any failure, I am calling the failureResponse function to send back the response with the appropriate message.

Another endpoint that I am testing is GET /users_list which lists all the users.The handler is userListHandler which parses down the users array and with the help of Json/Marshal Package the data is converted into JSON form.

The Tests

There are several test cases that I want to test :

  • Error in case of broken JSON
  • Error in case if the password length is less than 6
  • Error while any one of the parameters is missed
  • Successful user creation
  • Successful listing of user records in JSON

Note these are minimal no. of test cases you can write much more!!

Setting Up the Test code

Under the main package, I have created a file called user_test.go. The file consists of test functions for each of the rest endpoints.I created a global struct called TestStruct which consist of the request body , expected status code , response body and observed status code.We can test n number of the test cases with the help of TestStruct array objects. For a test case If the expected status code matches with the observed status code then the test case is passed.

Now let us look at the code for testing the signup end point :

There are totally 5 test cases in UserSignUp Test Handler.They are initialized with the test case request body and the expected status code.With the help of http.NewRequest and http.DefaultClient function I can make HTTP POST request to /signup. After receiving the response from web service I fill up the test case object with the response and observed status code fields which were initially left uninitialized.Then I called up a DisplayTestCaseResults function to display the results.

Setting Up Server and Testing Code

You can build up the project by specifying the go build command in the project directory.If the build was successful then the respective binary file will be generated in the same directory.You can run the binary file.

You can open up another terminal for test purpose and can call the go test user_test.go -v command to execute and see up the results.

That’s it. Everything is manual in go.Too much basic stuff!
You might find Go testing framework kind of lacking especially if you come from Java/Rails/Ruby background where stubbing/mocking is the norm. I find it interesting to code in go as there is no magic going on.

I was going to write down the other two tests but I guess I will leave it as an exercise. As always, you can go to the source code and see how I did it.

Thanks for reading!