O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

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

How to do it...

  1. Open the console and create the folder chapter11/recipe05.
  2. Navigate to the directory.
  3. Create the file sample_test.go with the following content:
        package main        import (          "fmt"          "io/ioutil"          "net/http"          "net/http/httptest"          "testing"          "time"        )        const cookieName = "X-Cookie"        func HandlerUnderTest(w http.ResponseWriter, r *http.Request) {          http.SetCookie(w, &http.Cookie{            Domain: "localhost",            Expires: time.Now().Add(3 * time.Hour),            Name: cookieName,          })          r.ParseForm()          username := r.FormValue("username")          fmt.Fprintf(w, "Hello %s!", username)        }        func TestHttpRequest(t *testing.T) {          req := httptest.NewRequest("GET",                          "http://unknown.io?username=John", nil)          w := httptest.NewRecorder()          HandlerUnderTest(w, req)          var res *http.Cookie for _, c := ...

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