Search code examples
gocomparisondeepequals

Testing Cookie returned from Function in Go


I am attempting to test a function that retrieves a Cookie from a request in Go however even though they have the same value, the comparison fails.

package main

import (
    "fmt"
    "log"
    "net/http"
    "net/http/httptest"
    "reflect"
)

func GetCookie(url string) *http.Cookie {
    req, err := http.NewRequest("GET", url, nil)
    req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

    client := http.DefaultClient

    res, err := client.Do(req)
    if err != nil {
        panic(err)
    }
    defer res.Body.Close()

    cookies := res.Cookies()
    var mycookie *http.Cookie
    for _, c := range cookies {
        if c.Name == "mycookie" {
            mycookie = c
        }
    }

    return mycookie
}

func main() {
    validCookie := &http.Cookie{
        Name:     "mycookie",
        Value:    "SomeValue",
        Path:     "/mysite",
        HttpOnly: true,
        Secure:   true,
    }

    ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        http.SetCookie(w, validCookie)
        w.Header().Set("Content-Type", "text/plain")
        w.WriteHeader(200)
    }))
    defer ts.Close()

    fmt.Printf("EqualL Cookies: %t\n", reflect.DeepEqual(validCookie, validCookie))

    if got := GetCookie(ts.URL); !reflect.DeepEqual(got, validCookie) {
        log.Fatalf("NOT THE SAME\n got = '%v'\nwant = '%v'", got, validCookie)
    }
}

Playground link: https://play.golang.org/p/T4dbZycMuT

I have checked the documentation on the DeepEqual function and from what I can see the 2 structs/pointer should be the same (Specially given that Cookie has no unexported fields).

I can change the function to compare the Cookie Strings however I would like to know if there is a simple explanation why this does not work or is it due to "inconsistency" as the documentation specifies. Also is there any way to test for the struct rather than the string representation in this scenarion (Or did I make a mistake maybe)?


Solution

  • Comparing Cookies with reflect.DeepEquals is a very bad idea. The http.Cookie type contains components that do not translate into the literal header representation of the cookie, depending on how it's parsed and/or manipulated.

    If you change your code to use %#v:

    if got := GetCookie(ts.URL); !reflect.DeepEqual(got, validCookie) {
        log.Fatalf("NOT THE SAME\n got = '%#v'\nwant = '%#v'", got, validCookie)
    }
    

    ... you'll see the difference:

    EqualL Cookies: true
    2009/11/10 23:00:00 NOT THE SAME
     got = '&http.Cookie{Name:"mycookie", Value:"SomeValue", Path:"/mysite", Domain:"", Expires:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}, RawExpires:"", MaxAge:0, Secure:true, HttpOnly:true, Raw:"mycookie=SomeValue; Path=/mysite; HttpOnly; Secure", Unparsed:[]string(nil)}'
    want = '&http.Cookie{Name:"mycookie", Value:"SomeValue", Path:"/mysite", Domain:"", Expires:time.Time{wall:0x0, ext:0, loc:(*time.Location)(nil)}, RawExpires:"", MaxAge:0, Secure:true, HttpOnly:true, Raw:"", Unparsed:[]string(nil)}'
    

    Instead, just compare the URL strings directly:

    if got := GetCookie(ts.URL); got.String() == validCookie.String() {