feat(check,serve): Startvalidierung, HTTPS-Server, Signal-Handling und Versionscheck
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NBHF4R9EAejDJUMdwr6C68
This commit is contained in:
parent
7e23df9fec
commit
2d223c5823
9 changed files with 1158 additions and 6 deletions
78
cmd/vpnportal/updatecheck_test.go
Normal file
78
cmd/vpnportal/updatecheck_test.go
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestUpdateCheckDisabledMakesNoRequest(t *testing.T) {
|
||||
called := false
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
called = true
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
if msg := checkForUpdate(context.Background(), false, srv.URL, "1.0.0", srv.Client()); msg != "" {
|
||||
t.Errorf("bei update_check=false darf nichts gemeldet werden, got %q", msg)
|
||||
}
|
||||
if called {
|
||||
t.Fatal("bei update_check=false darf kein Netzwerkzugriff stattfinden")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCheckReportsNewerVersion(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, `{"tag_name":"v1.4.0"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
msg := checkForUpdate(context.Background(), true, srv.URL, "1.2.0", srv.Client())
|
||||
if msg == "" {
|
||||
t.Fatal("abweichende Version muss gemeldet werden")
|
||||
}
|
||||
if !strings.Contains(msg, "1.4.0") {
|
||||
t.Errorf("Meldung nennt die neue Version nicht: %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCheckSilentWhenCurrent(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, `{"tag_name":"v1.2.0"}`)
|
||||
}))
|
||||
defer srv.Close()
|
||||
// Auch mit "v"-Präfix auf der einen und ohne auf der anderen Seite.
|
||||
if msg := checkForUpdate(context.Background(), true, srv.URL, "1.2.0", srv.Client()); msg != "" {
|
||||
t.Errorf("bei aktueller Version darf nichts gemeldet werden, got %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCheckSurvivesUnreachableEndpoint(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
|
||||
url := srv.URL
|
||||
client := srv.Client()
|
||||
srv.Close()
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
// Ein nicht erreichbarer Endpunkt darf den Start niemals stören.
|
||||
if msg := checkForUpdate(ctx, true, url, "1.2.0", client); msg != "" {
|
||||
t.Errorf("bei Fehler darf nichts gemeldet werden, got %q", msg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdateCheckIgnoresGarbage(t *testing.T) {
|
||||
for _, body := range []string{`nicht json`, `{}`, `{"tag_name":""}`, `{"tag_name":" "}`} {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
io.WriteString(w, body)
|
||||
}))
|
||||
if msg := checkForUpdate(context.Background(), true, srv.URL, "1.2.0", srv.Client()); msg != "" {
|
||||
t.Errorf("Antwort %q darf nichts melden, got %q", body, msg)
|
||||
}
|
||||
srv.Close()
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue