Skip to content

Commit

Permalink
adding some base tests
Browse files Browse the repository at this point in the history
  • Loading branch information
syntaqx committed Jun 30, 2024
1 parent a523292 commit ae13cfb
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
...

type MyCookies struct {
Debug bool `cookie:"DEBUG"`
Debug bool `cookie:"DEBUG"`
}
...

Expand Down
7 changes: 4 additions & 3 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type AccessTokenRequest struct {
}

func handler(w http.ResponseWriter, r *http.Request) {

// If none of the cookies are set, we'll set them and refresh the page
// so the rest of the demo functions.
_, err := cookie.Get(r, "Application-ID")
Expand Down Expand Up @@ -52,13 +51,15 @@ func setDemoCookies(w http.ResponseWriter) {

// Set cookies
cookie.Set(w, "Application-ID", uuid.Must(uuid.NewV7()).String(), options)
cookie.Set(w, "THEME", "default", options)
cookie.Set(w, "DEBUG", "true", options)

// Set signed cookies
cookie.SetSigned(w, "Access-Token", "some-access-token", options)
cookie.SetSigned(w, "User-ID", "123", options)
cookie.SetSigned(w, "Is-Admin", "true", options)
cookie.SetSigned(w, "Permissions", "read,write,execute", options)
cookie.SetSigned(w, "Expires-At", time.Now().Add(24*time.Hour).Format(time.RFC3339), options)
cookie.Set(w, "THEME", "default", options)
cookie.Set(w, "DEBUG", "true", options)
}

func main() {
Expand Down
11 changes: 8 additions & 3 deletions cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,10 @@ func Set(w http.ResponseWriter, name, value string, options *Options) {
}

if options.Signed {
h := hmac.New(sha256.New, SigningKey)
h.Write([]byte(value))
signature := base64.URLEncoding.EncodeToString(h.Sum(nil))
signature := generateHMAC(value)
cookie.Value = base64.URLEncoding.EncodeToString([]byte(value)) + "|" + signature
}

http.SetCookie(w, cookie)
}

Expand Down Expand Up @@ -231,3 +230,9 @@ func PopulateFromCookies(r *http.Request, dest interface{}) error {
}
return nil
}

func generateHMAC(value string) string {
h := hmac.New(sha256.New, SigningKey)
h.Write([]byte(value))
return base64.URLEncoding.EncodeToString(h.Sum(nil))
}
58 changes: 58 additions & 0 deletions cookie_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cookie

import (
"encoding/base64"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -89,6 +90,39 @@ func TestSet_WithoutOptions(t *testing.T) {
}
}

func TestSetSigned(t *testing.T) {
_, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatal(err)
}
w := httptest.NewRecorder()

name := "myCookie"
value := "myValue"

options := &Options{}

SetSigned(w, name, value, options)

// Get the response cookies
cookies := w.Result().Cookies()

// Check if the cookie was set correctly
if len(cookies) != 1 {
t.Errorf("Expected 1 cookie, got %d", len(cookies))
}
cookie := cookies[0]
if cookie.Name != name {
t.Errorf("Expected cookie name %s, got %s", name, cookie.Name)
}

// Check if the cookie value is signed
parts := strings.Split(cookie.Value, "|")
if len(parts) != 2 {
t.Errorf("Expected signed cookie value, got %s", cookie.Value)
}
}

func TestGet(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
cookieName := "myCookie"
Expand Down Expand Up @@ -119,6 +153,30 @@ func TestGetNonexistentCookie(t *testing.T) {
}
}

func TestGetSigned(t *testing.T) {
r := httptest.NewRequest(http.MethodGet, "/", nil)
cookieName := "myCookie"
cookieValue := "myValue"
signature := generateHMAC(cookieValue)
signedValue := base64.URLEncoding.EncodeToString([]byte(cookieValue)) + "|" + signature

cookie := &http.Cookie{
Name: cookieName,
Value: signedValue,
}

r.AddCookie(cookie)

value, err := GetSigned(r, cookieName)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}

if value != cookieValue {
t.Errorf("Expected cookie value %s, got %s", cookieValue, value)
}
}

func TestRemove(t *testing.T) {
w := httptest.NewRecorder()
name := "myCookie"
Expand Down

0 comments on commit ae13cfb

Please sign in to comment.