Skip to content

Commit

Permalink
test: get cookies from response
Browse files Browse the repository at this point in the history
  • Loading branch information
prongbang committed Sep 19, 2024
1 parent d4a1bb4 commit 5a50fa6
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions callx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -22,13 +40,17 @@ func Test_Get(t *testing.T) {
Interceptor: []callx.Interceptor{
callx.LoggerInterceptor(),
},
Cookies: true,
}
req := callx.New(c)

data := req.Get("/todos/1?q=callx")
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) {
Expand Down

0 comments on commit 5a50fa6

Please sign in to comment.