Testing the RESTful Server
Explore how to thoroughly test a RESTful server built in Go using the curl command-line utility. This lesson guides you through testing endpoints for different HTTP methods including GET, POST, PUT, and DELETE, emphasizing the importance of server logs for debugging and proper user permissions in requests. Gain practical experience in identifying correct server behavior and handling errors in a PostgreSQL-backed API environment.
We'll cover the following...
This lesson shows how to test the RESTful server using the curl(1) utility. We should test the RESTful server as much and as extensively as possible to find bugs or unwanted behavior. As we use two files for the server implementation, we need to run it as go run main.go handlers.go. Additionally, do not forget to have PostgreSQL up and running. We begin by testing the /time handler, which works with all HTTP methods:
# curl localhost:1234/timeThe current time is: Mon, 17 May 2021 09:14:00 EEST
Next, we test the default handler:
# curl localhost:1234// is not supported. Thanks for visiting!# curl localhost:1234/doesNotExist/doesNotExist is not supported. Thanks for visiting!
Lastly, we see what happens if we use an unsupported HTTP method with a supported endpoint—in this case, the /getall endpoint that only works with GET:
# curl -s -X PUT -H 'Content-Type: application/json' localhost:1234/getallMethod not allowed!
Although the /getall endpoint requires a valid user to operate, the fact that we are using an HTTP method that is not supported by that endpoint takes precedence, and the call fails for the right reasons.
Note: It is important to look at the output of the RESTful server and the log entries that it generates during testing. Not all information can be sent back to a client, but the server process is allowed to print ...