Creating a unit testing tool for our URL shortening service
In the previous chapter, we created a URL shortening service. The structure of the URL shortener project we worked on previously looks like this:
├── main.go ├── models │ └── models.go └── utils └── encodeutils.go 2 directories, 3 files
In the main.go
file, we created two API handlers: one for GET
and one for POST
. We are going to write the unit tests for both of those handlers. Add a file called main_test.go
in the root directory of the project:
touch main_test.go
In order to test our API, we need to test our API handlers:
package main_test import ( "testing" "net/http" ) func TestGetOriginalURL(t *testing.T) { // make a dummy reques response, err := http.Get("http://localhost:8000/v1/short/1") if http.StatusOK != response.StatusCode { t.Errorf("Expected response code %d. Got %d\n", http.StatusOK, response.StatusCode) } if err != nil { t.Errorf("Encountered an error:", err) } }
There...