douzhanglun4482 2017-09-15 19:59
浏览 40
已采纳

测试Go中从Function返回的Cookie

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
", reflect.DeepEqual(validCookie, validCookie))

    if got := GetCookie(ts.URL); !reflect.DeepEqual(got, validCookie) {
        log.Fatalf("NOT THE SAME
 got = '%v'
want = '%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)?

  • 写回答

2条回答 默认 最新

  • doudiyu1639 2017-09-15 20:12
    关注

    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
     got = '%#v'
    want = '%#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() {
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)