Files
code-utils/launch_icon_test.go

61 lines
1.3 KiB
Go
Raw Normal View History

2026-08-15 17:18:00 +08:00
package main
import (
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
)
func TestIconFromDir(t *testing.T) {
dir := t.TempDir()
png := []byte{0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 1, 2, 3}
if e := os.WriteFile(filepath.Join(dir, "favicon.png"), png, 0644); e != nil {
t.Fatal(e)
}
u := iconFromDir(dir)
if !strings.HasPrefix(u, "data:image/png;base64,") {
t.Fatalf("got %q", u)
}
}
func TestIconFromPortFavicon(t *testing.T) {
ico := []byte{0, 0, 1, 0, 1, 0, 16, 16, 0, 0, 1, 0, 32, 0}
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/favicon.ico" {
w.Header().Set("Content-Type", "image/x-icon")
_, _ = w.Write(ico)
return
}
http.NotFound(w, r)
}))
defer srv.Close()
// iconFromPort 用 127.0.0.1:port从 httptest URL 解析端口
u := srv.URL
port := 0
if i := strings.LastIndex(u, ":"); i >= 0 {
_, _ = fmtSscanf(u[i+1:], &port)
}
if port == 0 {
t.Fatal("no port")
}
got := iconFromPort(port)
if !strings.HasPrefix(got, "data:image/") {
t.Fatalf("got %q", got)
}
}
func fmtSscanf(s string, port *int) (int, error) {
n := 0
for _, c := range s {
if c < '0' || c > '9' {
break
}
n = n*10 + int(c-'0')
}
*port = n
return 1, nil
}