测试从Go中的Function返回的Cookie

时间:2017-09-15 19:59:40

标签: go comparison deepequals

我正在尝试测试一个从Go中的请求中检索Cookie的函数,但是即使它们具有相同的值,比较也会失败。

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)
    }
}

游乐场链接:https://play.golang.org/p/T4dbZycMuT

我已经检查了DeepEqual函数的文档,从我可以看到的2个结构/指针应该是相同的(特别是Cookie没有未导出的字段)。

我可以更改函数来比较Cookie字符串但是我想知道是否有一个简单的解释为什么这不起作用或者是由于文档指定的“不一致”。 还有什么方法可以测试结构而不是这个场景中的字符串表示(或者我是否犯了错误)?

2 个答案:

答案 0 :(得分:3)

使用fmt.Printf("%#v")显示问题:

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)}
validCookie=&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)}

已解析的Cookie的Raw值已填充,而构建的Cookie没有Raw值,这是可以理解的。

游乐场:https://play.golang.org/p/ghzkjUoEGW

答案 1 :(得分:3)

将Cookie与reflect.DeepEquals进行比较是一个非常糟糕的主意。 http.Cookie type包含的组件不会转换为Cookie的文字标题表示,具体取决于它如何解析和/或操作。

如果您更改代码以使用%#v

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

......你会看到差异:

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)}'

相反,只需直接比较URL字符串:

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