refactor(test): adopt Go 1.22 ServeMux in provider mock servers

This commit is contained in:
zarazaex69
2026-05-12 21:31:25 +03:00
parent 922c326533
commit d4d71c6810
3 changed files with 84 additions and 94 deletions

View File

@@ -6,7 +6,6 @@ import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
@@ -21,27 +20,19 @@ func withJazzAPIServer(t *testing.T, h http.Handler) {
apiBase = srv.URL
}
//nolint:cyclop // table-driven test naturally has many branches
func TestCreateMeetingAndPreconnect(t *testing.T) {
withJazzAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("POST /room/create-meeting", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get(headerAuthType) != authTypeAnonymous {
t.Fatalf("missing auth header: %v", r.Header)
}
switch r.URL.Path {
case "/room/create-meeting": //nolint:goconst // test literal, repetition is intentional
if r.Method != http.MethodPost {
t.Fatalf("create method = %s", r.Method)
}
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "room-1", Password: "pass"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
case "/room/room-1/preconnect":
if r.Method != http.MethodPost {
t.Fatalf("preconnect method = %s", r.Method)
}
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"}) //nolint:goconst,lll // test literal, repetition is intentional
default:
http.NotFound(w, r)
}
}))
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "room-1", Password: "pass"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
})
mux.HandleFunc("POST /room/room-1/preconnect", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"}) //nolint:goconst,lll // test literal, repetition is intentional
})
withJazzAPIServer(t, mux)
headers := map[string]string{
headerAuthType: authTypeAnonymous,
@@ -64,18 +55,16 @@ func TestCreateMeetingAndPreconnect(t *testing.T) {
}
}
//nolint:cyclop // table-driven test naturally has many branches
func TestCreateRoomAndJoinRoom(t *testing.T) {
withJazzAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/room/create-meeting":
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "new-room", Password: "new-pass"}) //nolint:goconst,gosec,lll // test literal; G117 is a false positive for test fixtures
case "/room/new-room/preconnect", "/room/existing/preconnect":
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"})
default:
http.NotFound(w, r)
}
}))
mux := http.NewServeMux()
mux.HandleFunc("POST /room/create-meeting", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "new-room", Password: "new-pass"}) //nolint:goconst,gosec,lll // test literal; G117 is a false positive for test fixtures
})
mux.HandleFunc("POST /room/{id}/preconnect", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"})
})
withJazzAPIServer(t, mux)
room, err := createRoom(context.Background())
if err != nil {
@@ -95,14 +84,15 @@ func TestCreateRoomAndJoinRoom(t *testing.T) {
}
func TestJazzAPIErrors(t *testing.T) {
withJazzAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch {
case strings.Contains(r.URL.Path, "create-meeting"):
http.Error(w, "bad", http.StatusTeapot)
default:
http.Error(w, "bad", http.StatusInternalServerError)
}
}))
mux := http.NewServeMux()
mux.HandleFunc("/room/create-meeting", func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "bad", http.StatusTeapot)
})
mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
http.Error(w, "bad", http.StatusInternalServerError)
})
withJazzAPIServer(t, mux)
if _, err := createMeeting(context.Background(), nil); !errors.Is(err, errCreateRoomFailed) {
t.Fatalf("createMeeting() error = %v, want %v", err, errCreateRoomFailed)
@@ -113,16 +103,15 @@ func TestJazzAPIErrors(t *testing.T) {
}
func TestNewPeerUsesRoomAPI(t *testing.T) {
withJazzAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/room/create-meeting":
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "new-room", Password: "new-pass"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
case "/room/new-room/preconnect", "/room/existing/preconnect":
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"})
default:
http.NotFound(w, r)
}
}))
mux := http.NewServeMux()
mux.HandleFunc("POST /room/create-meeting", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(createResponse{RoomID: "new-room", Password: "new-pass"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
})
mux.HandleFunc("POST /room/{id}/preconnect", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(map[string]string{"connectorUrl": "wss://connector"})
})
withJazzAPIServer(t, mux)
created, err := NewPeer(context.Background(), "any", "peer", nil)
if err != nil {

View File

@@ -22,12 +22,10 @@ func withTelemostAPIServer(t *testing.T, h http.Handler) {
}
func TestGetConnectionInfo(t *testing.T) {
withTelemostAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
t.Fatalf("method = %s", r.Method)
}
if !strings.Contains(r.URL.EscapedPath(), "/conferences/room%2Fid/connection") {
t.Fatalf("path = %q escaped=%q", r.URL.Path, r.URL.EscapedPath())
mux := http.NewServeMux()
mux.HandleFunc("GET /conferences/{id...}", func(w http.ResponseWriter, r *http.Request) {
if !strings.HasPrefix(r.URL.Path, "/conferences/room/id/connection") {
t.Fatalf("path = %q", r.URL.Path)
}
if r.URL.Query().Get("display_name") != "peer" {
t.Fatalf("display_name query = %q", r.URL.Query().Get("display_name"))
@@ -37,7 +35,9 @@ func TestGetConnectionInfo(t *testing.T) {
PeerID: "peer-id", //nolint:goconst // test literal, repetition is intentional
Credentials: "creds", //nolint:goconst // test literal, repetition is intentional
})
}))
})
withTelemostAPIServer(t, mux)
info, err := GetConnectionInfo(context.Background(), "room/id", "peer")
if err != nil {
@@ -65,13 +65,16 @@ func TestGetConnectionInfoErrors(t *testing.T) {
}
func TestTelemostNewPeerUsesConnectionInfo(t *testing.T) {
withTelemostAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
mux := http.NewServeMux()
mux.HandleFunc("GET /", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(ConnectionInfo{
RoomID: "room",
PeerID: "peer-id",
Credentials: "creds",
})
}))
})
withTelemostAPIServer(t, mux)
p, err := NewPeer(context.Background(), "room", "name", nil)
if err != nil {

View File

@@ -20,32 +20,29 @@ func withWBAPIServer(t *testing.T, h http.Handler) {
apiBase = srv.URL
}
//nolint:cyclop // table-driven test naturally has many branches
func TestWBStreamAPIHappyPath(t *testing.T) {
withWBAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/auth/api/v1/auth/user/guest-register":
if r.Method != http.MethodPost {
t.Fatalf("guest method = %s", r.Method)
}
_ = json.NewEncoder(w).Encode(guestRegisterResponse{AccessToken: "access"}) //nolint:goconst,gosec,lll // test literal; G117 is a false positive for test fixtures
case "/api-room/api/v2/room":
if r.Header.Get("Authorization") != "Bearer access" {
t.Fatalf("room auth = %q", r.Header.Get("Authorization"))
}
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(createRoomResponse{RoomID: "room"}) //nolint:goconst,lll // test literal, repetition is intentional
case "/api-room/api/v1/room/room/join":
w.WriteHeader(http.StatusOK)
case "/api-room-manager/v2/room/room/connection-details":
if r.URL.Query().Get("displayName") != "peer" {
t.Fatalf("displayName query = %q", r.URL.Query().Get("displayName"))
}
_ = json.NewEncoder(w).Encode(tokenResponse{RoomToken: "token"}) //nolint:goconst,lll // test literal, repetition is intentional
default:
http.NotFound(w, r)
mux := http.NewServeMux()
mux.HandleFunc("POST /auth/api/v1/auth/user/guest-register", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(guestRegisterResponse{AccessToken: "access"}) //nolint:goconst,gosec,lll // test literal; G117 is a false positive for test fixtures
})
mux.HandleFunc("POST /api-room/api/v2/room", func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Authorization") != "Bearer access" {
t.Fatalf("room auth = %q", r.Header.Get("Authorization"))
}
}))
w.WriteHeader(http.StatusCreated)
_ = json.NewEncoder(w).Encode(createRoomResponse{RoomID: "room"}) //nolint:goconst,lll // test literal, repetition is intentional
})
mux.HandleFunc("POST /api-room/api/v1/room/room/join", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("GET /api-room-manager/v2/room/room/connection-details", func(w http.ResponseWriter, r *http.Request) {
if r.URL.Query().Get("displayName") != "peer" {
t.Fatalf("displayName query = %q", r.URL.Query().Get("displayName"))
}
_ = json.NewEncoder(w).Encode(tokenResponse{RoomToken: "token"}) //nolint:goconst,lll // test literal, repetition is intentional
})
withWBAPIServer(t, mux)
access, err := registerGuest(context.Background(), "peer")
if err != nil {
@@ -95,20 +92,21 @@ func TestWBStreamAPIErrors(t *testing.T) {
}
func TestWBStreamGetRoomToken(t *testing.T) {
withWBAPIServer(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/auth/api/v1/auth/user/guest-register":
_ = json.NewEncoder(w).Encode(guestRegisterResponse{AccessToken: "access"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
case "/api-room/api/v2/room":
_ = json.NewEncoder(w).Encode(createRoomResponse{RoomID: "created"})
case "/api-room/api/v1/room/created/join":
w.WriteHeader(http.StatusOK)
case "/api-room-manager/v2/room/created/connection-details":
_ = json.NewEncoder(w).Encode(tokenResponse{RoomToken: "token"})
default:
http.NotFound(w, r)
}
}))
mux := http.NewServeMux()
mux.HandleFunc("POST /auth/api/v1/auth/user/guest-register", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(guestRegisterResponse{AccessToken: "access"}) //nolint:gosec,lll // G117: test-only struct mirroring upstream API shape
})
mux.HandleFunc("POST /api-room/api/v2/room", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(createRoomResponse{RoomID: "created"})
})
mux.HandleFunc("POST /api-room/api/v1/room/{id}/join", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
mux.HandleFunc("GET /api-room-manager/v2/room/{id}/connection-details", func(w http.ResponseWriter, _ *http.Request) {
_ = json.NewEncoder(w).Encode(tokenResponse{RoomToken: "token"})
})
withWBAPIServer(t, mux)
p, err := NewPeer(context.Background(), "any", "peer", nil)
if err != nil {