go - 在 Go 中测试从 Function 返回的 Cookie

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

Playground 链接:https://play.golang.org/p/T4dbZycMuT

我已经检查了有关 DeepEqual 函数的文档,据我所知,2 个结构/指针应该是相同的(特别是考虑到 Cookie 没有未导出的字段)。

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

最佳答案

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

如果您更改代码以使用 %#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() {

关于go - 在 Go 中测试从 Function 返回的 Cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46246728/

相关文章:

java - 用于复杂对象的 apache EqualsBuilder

javascript - 如何使用 should.js 进行 deepEqual 断言?

javascript - 函数检查嵌套对象的 "deep equality"

go - 如何完全禁用 HTTP/1.x 支持

mongodb - 带有 golang mongo-driver 的多个 maxPoolSize 配置

go - 是否自动绑定(bind)函数到结构?

java - deepEquals - 列表相等,集合不相等

go - golang 中延迟电子邮件发送到队列的最佳和最简单的方法

c++ - std::map 的比较参数对于严格排序有什么要求?

string - 在实现 insert() 算法时确定字符串值