mirror of
https://github.com/taigrr/log-socket
synced 2026-03-20 23:02:20 -07:00
test(ws,browser): add test suites for ws and browser packages (#22)
- Add ws/server_test.go: WebSocket connection, namespace filtering, non-upgrade rejection, SetUpgrader - Add ws/namespaces_test.go: NamespacesHandler response format and content - Add browser/browser_test.go: template rendering, ws URL construction, trailing slash handling - Fix goimports whitespace in ws/server.go
This commit is contained in:
62
ws/namespaces_test.go
Normal file
62
ws/namespaces_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package ws
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
logger "github.com/taigrr/log-socket/v2/log"
|
||||
)
|
||||
|
||||
func TestNamespacesHandler(t *testing.T) {
|
||||
// Log to a known namespace to ensure it appears
|
||||
nsLogger := logger.NewLogger("ns-handler-test")
|
||||
nsLogger.Info("register namespace")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/namespaces", nil)
|
||||
w := httptest.NewRecorder()
|
||||
NamespacesHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want 200", w.Code)
|
||||
}
|
||||
|
||||
ct := w.Header().Get("Content-Type")
|
||||
if ct != "application/json" {
|
||||
t.Errorf("Content-Type = %q, want application/json", ct)
|
||||
}
|
||||
|
||||
var result struct {
|
||||
Namespaces []string `json:"namespaces"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
found := false
|
||||
for _, ns := range result.Namespaces {
|
||||
if ns == "ns-handler-test" {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("namespace 'ns-handler-test' not found in %v", result.Namespaces)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNamespacesHandler_ResponseFormat(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/namespaces", nil)
|
||||
w := httptest.NewRecorder()
|
||||
NamespacesHandler(w, req)
|
||||
|
||||
var result map[string]interface{}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatalf("response is not valid JSON: %v", err)
|
||||
}
|
||||
|
||||
if _, ok := result["namespaces"]; !ok {
|
||||
t.Error("response missing 'namespaces' key")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user