douzhanglun4482
douzhanglun4482
2017-09-15 19:59

测试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 doudiyu1639 4年前

    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() {
    
    点赞 评论 复制链接分享
  • dpcj32769 dpcj32769 4年前

    Using fmt.Printf("%#v") shows the issue:

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

    The Raw value of the parsed cookie is filled, while the constructed cookie has no Raw value, understandably.

    Playground: https://play.golang.org/p/ghzkjUoEGW

    点赞 评论 复制链接分享

相关推荐