package main import ( "io/ioutil" "log" "net/http" "net/http/httptest" "testing" ) func TestCrmPixelCookie(t *testing.T) { req := buildReqCrmPixel() rr, _ := getHTTPResponse(req, crmpixel) verifyPixelSetCookie(t, rr) } func verifyPixelSetCookie(t *testing.T, rr *httptest.ResponseRecorder) { //check Set-Cookie v, ok := rr.HeaderMap["Set-Cookie"] //biukop_cl=59610affc2ccce92d|323295325|1499532031|e0010c3e24a201665b783e2c48ab323b87ef52b7; Expires=Tue, 06 Jul 2027 16:40:31 GMT log.Println(v) AssertEqual(t, ok, true, "Set-Cookie shold be available") AssertEqual(t, checkRecorderCookieBiukopCL(v), true, "cookie value should be valid") //compare body content pixel, err := crmpixelImage() AssertEqual(t, err, nil, "pixel image should have no error") m, err := ioutil.ReadAll(rr.Body) AssertEqual(t, testEq(pixel, m), true, "pixel image should be the same") } func TestRedirectHandler(t *testing.T) { req := buildReqRedirect() rr, _ := getHTTPResponse(req, crmpixel) verifyPixelSetCookie(t, rr) } func checkRecorderCookieBiukopCL(v []string) bool { // Copy the Cookie over to a new Request request := &http.Request{Header: http.Header{"Cookie": v}} // Extract the dropped cookie from the request. cookie, err := request.Cookie("biukop_cl") valid := isValidCookieBiukopCL(cookie.Value) return err == nil && valid } func buildReqCrmPixel() (req *http.Request) { req, err := http.NewRequest("GET", "/crmpixel.png", nil) if err != nil { log.Fatal(err) } //buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete) buildReqCommonHeader(req) return req } func buildReqRedirect() (req *http.Request) { req, err := http.NewRequest("GET", "/redirect?url=url=http%3A%2F%2Fkidshealth.org%2Fen%2Fparents%2Ffas.html", nil) if err != nil { log.Fatal(err) } //buildReqCommonSignature(req, IntraAPIConfig.CRMSecrete) buildReqCommonHeader(req) return req } //compare two byte slice func testEq(a, b []byte) bool { if a == nil && b == nil { return true } if a == nil || b == nil { return false } if len(a) != len(b) { return false } for i := range a { if a[i] != b[i] { return false } } return true }