O'Reilly logo

Distributed Computing with Go by V.N. Nikhil Anurag

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

secure/secure_test.go

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required