Testing an HTTP server with a database backend

In this section, you are going to see how to test a database server, which in this case is going to be a PostgreSQL server that works with an HTTP server written in Go. This is a special situation because you will have to get your database data in order to be able to test the correctness of the HTTP server.

For the purposes of this section, I am going to show two Go files named webServer.go and webServer_test.go. Before executing the code, you will need to download the Go package that will help you to work with PostgreSQL from Go, which requires the execution of the following command:

$ go get github.com/lib/pq
  

The Go code of webServer.go is as follows:

package main import ( "database/sql" ...

Get Mastering Go - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.