Skip to content

Commit

Permalink
tests for DefaultCookieName
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdata committed Feb 2, 2024
1 parent ccef66c commit 22ee06f
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
28 changes: 16 additions & 12 deletions defaultcookiename.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import (
var DefaultCookieName string

func init() {
DefaultCookieName = "jaws"
if s, err := os.Executable(); err == nil {
s = filepath.Base(s)
s = strings.TrimSuffix(s, filepath.Ext(s))
var b []byte
for _, ch := range s {
if ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') {
b = append(b, byte(ch))
}
}
if len(b) > 0 {
DefaultCookieName = s
exename, _ := os.Executable()
DefaultCookieName = makeCookieName(exename)
}

func makeCookieName(exename string) (cookie string) {
cookie = "jaws"
exename = filepath.Base(exename)
exename = strings.TrimSuffix(exename, filepath.Ext(exename))
var b []byte
for _, ch := range exename {
if ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ('0' <= ch && ch <= '9') {
b = append(b, byte(ch))
}
}
if len(b) > 0 {
cookie = string(b)
}
return
}
27 changes: 27 additions & 0 deletions defaultcookiename_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package jaws

import (
"path"
"testing"
)

func Test_makeCookieName(t *testing.T) {
tests := []struct {
name string
exename string
wantCookie string
}{
{"empty string", "", "jaws"},
{"Simple", "Simple", "Simple"},
{"suffix.ed", "suffix.ed", "suffix"},
{"path", path.Join("c:", "path", "file.ext"), "file"},
{"invalid chars", " !??_", "jaws"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotCookie := makeCookieName(tt.exename); gotCookie != tt.wantCookie {
t.Errorf("makeCookieName() = %v, want %v", gotCookie, tt.wantCookie)
}
})
}
}

0 comments on commit 22ee06f

Please sign in to comment.