Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## [0.10.0] - Apr 2, 2026

### Changed

- **BREAKING**: `Cookie()` return type changed from `*fasthttp.Cookie` to `string`
- **BREAKING**: `Cookies()` return type changed from `[]*fasthttp.Cookie` to `map[string]string`

### Fixed

- Fixed dangling pointer bug in internal `cookie()` method

## [0.9.0] - Apr 1, 2026

### Added
Expand Down
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,13 @@ func (c *Context) DelHeader(key string) {
c.res.delHeader(key)
}

// Cookie returns the cookie with the given name.
func (c *Context) Cookie(name string) *fasthttp.Cookie {
// Cookie returns the cookie value with the given name.
func (c *Context) Cookie(name string) string {
return c.req.cookie(name)
}

// Cookies returns all cookies from the request.
func (c *Context) Cookies() []*fasthttp.Cookie {
func (c *Context) Cookies() map[string]string {
return c.req.cookies()
}

Expand Down
24 changes: 14 additions & 10 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,19 +613,17 @@ func TestCookie(t *testing.T) {
c.ctx.Request.Header.SetCookie("test", "value")

cookie := c.Cookie("test")
if cookie != nil {
if string(cookie.Key()) != "test" {
t.Errorf("Expected cookie key 'test', got '%s'", string(cookie.Key()))
}
if cookie != "value" {
t.Errorf("Expected cookie value 'value', got '%s'", cookie)
}
}

func TestCookieNotFound(t *testing.T) {
c, _ := createTestContext("GET", "/path", nil)

cookie := c.Cookie("nonexistent")
if cookie != nil {
t.Errorf("Expected nil for nonexistent cookie, got %v", cookie)
if cookie != "" {
t.Errorf("Expected empty string for nonexistent cookie, got '%s'", cookie)
}
}

Expand Down Expand Up @@ -996,8 +994,14 @@ func TestContext_Cookies(t *testing.T) {
c.req = newRequest(ctx)

cookies := c.Cookies()
if len(cookies) == 0 {
t.Error("Expected cookies to be non-empty")
if len(cookies) != 2 {
t.Errorf("Expected 2 cookies, got %d", len(cookies))
}
if cookies["session"] != "abc" {
t.Errorf("Expected session value 'abc', got '%s'", cookies["session"])
}
if cookies["theme"] != "dark" {
t.Errorf("Expected theme value 'dark', got '%s'", cookies["theme"])
}
}

Expand Down Expand Up @@ -1557,8 +1561,8 @@ func TestContext_CookieNotFound(t *testing.T) {
c, _ := createTestContext("GET", "/test", nil)

cookie := c.Cookie("nonexistent")
if cookie != nil {
t.Errorf("Expected nil for nonexistent cookie, got %v", cookie)
if cookie != "" {
t.Errorf("Expected empty string for nonexistent cookie, got '%s'", cookie)
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/cookie/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ func main() {

app.Get("/ping", func(ctx *lightning.Context) {
cookie := ctx.Cookie("sid")
if cookie != nil {
fmt.Println(string(cookie.Key()), string(cookie.Value()))
if cookie != "" {
fmt.Println("sid", cookie)
}

cookies := ctx.Cookies()
for _, c := range cookies {
fmt.Println(string(c.Key()), string(c.Value()))
for name, value := range cookies {
fmt.Println(name, value)
}

ctx.SetCookie("sid", "sid:xxxxxxxxxx")
Expand Down
21 changes: 6 additions & 15 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,23 +55,14 @@ func (r *request) headers() map[string]string {
return headers
}

func (r *request) cookie(name string) *fasthttp.Cookie {
var cookie fasthttp.Cookie
cookie.ParseBytes(r.ctx.Request.Header.Cookie(name))
if len(cookie.Key()) > 0 {
return &cookie
}
return nil
func (r *request) cookie(name string) string {
return string(r.ctx.Request.Header.Cookie(name))
}

func (r *request) cookies() []*fasthttp.Cookie {
var cookies []*fasthttp.Cookie
r.ctx.Request.Header.VisitAll(func(key, value []byte) {
if string(key) == "Cookie" {
var c fasthttp.Cookie
c.ParseBytes(value)
cookies = append(cookies, &c)
}
func (r *request) cookies() map[string]string {
cookies := make(map[string]string)
r.ctx.Request.Header.VisitAllCookie(func(key, value []byte) {
cookies[string(key)] = string(value)
})
return cookies
}
Expand Down
18 changes: 12 additions & 6 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func TestRequest_Cookie(t *testing.T) {
r := newRequest(ctx)

cookie := r.cookie("cookie1")
if cookie != nil && string(cookie.Key()) != "cookie1" {
t.Errorf("Expected cookie key 'cookie1', got '%s'", string(cookie.Key()))
if cookie != "value1" {
t.Errorf("Expected cookie value 'value1', got '%s'", cookie)
}

cookie = r.cookie("nonexistent")
if cookie != nil {
t.Errorf("Expected nil for nonexistent cookie, got %v", cookie)
if cookie != "" {
t.Errorf("Expected empty string for nonexistent cookie, got '%s'", cookie)
}
}

Expand All @@ -43,8 +43,14 @@ func TestRequest_Cookies(t *testing.T) {
r := newRequest(ctx)
cookies := r.cookies()

if len(cookies) == 0 {
t.Error("Expected cookies, got empty")
if len(cookies) != 2 {
t.Errorf("Expected 2 cookies, got %d", len(cookies))
}
if cookies["cookie1"] != "value1" {
t.Errorf("Expected cookie1 value 'value1', got '%s'", cookies["cookie1"])
}
if cookies["cookie2"] != "value2" {
t.Errorf("Expected cookie2 value 'value2', got '%s'", cookies["cookie2"])
}
}

Expand Down
Loading