在 Go 中测试从 Function 返回的 Cookie
Testing Cookie returned from Function in Go
我正在尝试测试一个从 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)
}
}
游乐场link:https://play.golang.org/p/T4dbZycMuT
我已经检查了有关 DeepEqual 函数的文档,据我所知,2 structs/pointer 应该是相同的(特别是考虑到 Cookie 没有未导出的字段)。
我可以更改函数来比较 Cookie 字符串,但是我想知道是否有一个简单的解释为什么这不起作用,或者是由于文档指定的 "inconsistency"。
在这种情况下,还有什么方法可以测试结构而不是字符串表示(或者我可能犯了错误)?
使用 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
值,可以理解。
将 Cookie 与 reflect.DeepEquals
进行比较是一个非常糟糕的主意。 http.Cookie type 包含不会转换为 cookie 的文字 header 表示的组件,具体取决于它的解析方式 and/or 操作。
如果您更改代码以使用 %#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 中的请求中检索 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)
}
}
游乐场link:https://play.golang.org/p/T4dbZycMuT
我已经检查了有关 DeepEqual 函数的文档,据我所知,2 structs/pointer 应该是相同的(特别是考虑到 Cookie 没有未导出的字段)。
我可以更改函数来比较 Cookie 字符串,但是我想知道是否有一个简单的解释为什么这不起作用,或者是由于文档指定的 "inconsistency"。 在这种情况下,还有什么方法可以测试结构而不是字符串表示(或者我可能犯了错误)?
使用 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
值,可以理解。
将 Cookie 与 reflect.DeepEquals
进行比较是一个非常糟糕的主意。 http.Cookie type 包含不会转换为 cookie 的文字 header 表示的组件,具体取决于它的解析方式 and/or 操作。
如果您更改代码以使用 %#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() {