February 2018
Intermediate to advanced
246 pages
6h 3m
English
Next, we will try to test the logic we have written in secure.go using unit tests. A good practice is to test each of the functions for all possible cases of success and failure. The test names explain the intent of the test, so let's look at the code:
// secure/secure_test.go package main import ( "net/http" "net/http/httptest" "testing" ) func TestIsAuthorizedSuccess(t *testing.T) { req, err := http.NewRequest("GET", "http://example.com", nil) if err != nil { t.Error("Unable to create request") } req.Header["Authorization"] = []string{"Bearer AUTH-TOKEN-1"} if isAuthorized(req) { t.Log("Request with correct Auth token was correctly processed.") } else { t.Error("Request with correct Auth token failed.") } } func TestIsAuthorizedFailTokenType(t ...Read now
Unlock full access