亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

為什么 gin SetCookie 添加新的 cookie 但不更改舊的?

為什么 gin SetCookie 添加新的 cookie 但不更改舊的?

Go
ibeautiful 2023-02-14 17:47:55
我正在嘗試在有ctx.SetCookie方法的地方測試注銷處理程序:func (a *authController) Logout(ctx *gin.Context) {    refreshToken, err := ctx.Cookie("refresh_token")    ...    ctx.SetCookie("access_token", "", -1, "/", "localhost", false, true)    ctx.SetCookie("refresh_token", "", -1, "/", "localhost", false, true)    ctx.SetCookie("logged_in", "", -1, "/", "localhost", false, true)    ctx.JSON(http.StatusOK, gin.H{"status": "success"})}測試函數中的代碼:recorder := httptest.NewRecorder()ctx, _ := gin.CreateTestContext(recorder)ctx.SetCookie("logged_in", "truee", 60*60, "/", "localhost", false, false)req, _ := http.NewRequest("GET", "/logout", nil)http.SetCookie(recorder, &http.Cookie{Name: "refresh_token", Value: "encodedRefreshToken", MaxAge: 60 * 60, Path: "/", Domain: "localhost", Secure: false, HttpOnly: true}})http.SetCookie(recorder, &http.Cookie{Name: "access_token", Value: "encodedAccessToken", MaxAge: 60 * 60, Path: "/", Domain: "localhost", Secure: false, HttpOnly: true})req.Header = http.Header{"Cookie": recorder.Result().Header["Set-Cookie"]}ctx.Request = reqtest.mock()authController.Logout(ctx)通話結束后,我正在嘗試檢查 cookie 是否已被刪除:coockies := recorder.Result().Cookies()for _, c := range coockies {    if c.Name == "access_token" {        assert.Equal(t, "", c.Value)    }    ...}而且我遇到這樣一個問題,setCookie 不更改 cookie,而是添加新的。也就是調用這個方法后,我有兩對cookies和access Token等。結果,測試沒有通過。我不明白我做錯了什么,能以某種方式解決嗎?還是應該這樣?
查看完整描述

1 回答

?
海綿寶寶撒

TA貢獻1809條經驗 獲得超8個贊

用于AddCookie為 設置 cookie recorder。


我認為你應該Logout像這樣測試處理程序:


package ${your_package}


import (

    "encoding/json"

    "net/http"

    "net/http/httptest"

    "testing"


    "github.com/gin-gonic/gin"

    "github.com/stretchr/testify/assert"

)


func Logout(ctx *gin.Context) {

    // refreshToken, err := ctx.Cookie("refresh_token")

    ctx.SetCookie("access_token", "", -1, "/", "localhost", false, true)

    ctx.SetCookie("refresh_token", "", -1, "/", "localhost", false, true)

    ctx.SetCookie("logged_in", "", -1, "/", "localhost", false, true)


    ctx.JSON(http.StatusOK, gin.H{"status": "success"})

}


func setupRouter() *gin.Engine {

    r := gin.Default()

    r.GET("/logout", Logout)

    return r

}


func TestLogout(t *testing.T) {

    w := httptest.NewRecorder()

    r := setupRouter()

    req, err := http.NewRequest("GET", "/logout", nil)

    assert.Nil(t, err)

    req.AddCookie(&http.Cookie{Name: "refresh_token", Value: "encodedRefreshToken", MaxAge: 60 * 60, Path: "/", Domain: "localhost", Secure: false, HttpOnly: true})

    req.AddCookie(&http.Cookie{Name: "access_token", Value: "encodedAccessToken", MaxAge: 60 * 60, Path: "/", Domain: "localhost", Secure: false, HttpOnly: true})

    r.ServeHTTP(w, req)


    for _, v := range w.Result().Cookies() {

        if v.Name == "access_token" {

            assert.Equal(t, "", v.Value)

        }

    }

    assert.Equal(t, http.StatusOK, w.Code)

    respBody := &gin.H{}

    err = json.Unmarshal(w.Body.Bytes(), respBody)

    assert.Nil(t, err)

    assert.Equal(t, gin.H{"status": "success"}, *respBody)

}

=== RUN   TestLogout

[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.


[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.

 - using env:   export GIN_MODE=release

 - using code:  gin.SetMode(gin.ReleaseMode)


[GIN-debug] GET    /logout                   --> app.Logout (3 handlers)

[GIN] 2022/09/19 - 10:46:00 | 200 |          81μs |                 | GET      "/logout"

--- PASS: TestLogout (0.00s)

PASS

參考: https: //gin-gonic.com/docs/testing/


查看完整回答
反對 回復 2023-02-14
  • 1 回答
  • 0 關注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號