From 5a50fa6efe8803921f09f412d34ed600a28481af Mon Sep 17 00:00:00 2001 From: prongbang Date: Thu, 19 Sep 2024 23:35:03 +0700 Subject: [PATCH] test: get cookies from response --- callx_test.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/callx_test.go b/callx_test.go index f843dc2..0912fa3 100644 --- a/callx_test.go +++ b/callx_test.go @@ -12,6 +12,24 @@ import ( func Test_Get(t *testing.T) { ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // Set a cookie + cookie := &http.Cookie{ + Name: "session_id", + Value: "12345", + Path: "/", + HttpOnly: true, + MaxAge: 3600, // Cookie expires in 1 hour + } + http.SetCookie(w, cookie) + cookie2 := &http.Cookie{ + Name: "user_preferences", + Value: "dark_mode=true", + Path: "/", + HttpOnly: false, + MaxAge: 86400, // Cookie expires in 1 day + SameSite: http.SameSiteStrictMode, // Optionally set SameSite attribute + } + http.SetCookie(w, cookie2) _, _ = fmt.Fprintln(w, "Hello, Get") })) defer ts.Close() @@ -22,6 +40,7 @@ func Test_Get(t *testing.T) { Interceptor: []callx.Interceptor{ callx.LoggerInterceptor(), }, + Cookies: true, } req := callx.New(c) @@ -29,6 +48,9 @@ func Test_Get(t *testing.T) { if data.Code != 200 && string(data.Data) != "Hello, Get" { t.Error("CallX Get Error", data) } + if !strings.Contains(data.Cookies["session_id"], "session_id=12345;") { + t.Error("CallX Get Error", data) + } } func Test_PostBodyNil(t *testing.T) {