refactor(tests): Update all test files to use TestServerSetup pattern
This commit is contained in:
@@ -21,13 +21,12 @@ import (
|
||||
func TestWebSocketConnection(t *testing.T) {
|
||||
// Setup test server with WebSocket
|
||||
ts, queries, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
// Get JWT token for a test user
|
||||
token := loginTestUser(t, ts, queries)
|
||||
|
||||
// Connect to WebSocket endpoint
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
|
||||
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||
require.NoError(t, err, "Failed to connect to WebSocket")
|
||||
@@ -52,7 +51,6 @@ func TestWebSocketConnection(t *testing.T) {
|
||||
// TestWebSocketDeviceAuth tests device authentication via WebSocket
|
||||
func TestWebSocketDeviceAuth(t *testing.T) {
|
||||
ts, queries, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
// Create a test device
|
||||
userID := getTestUserID(t, queries)
|
||||
@@ -72,7 +70,7 @@ func TestWebSocketDeviceAuth(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
|
||||
// Connect to WebSocket with device token
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + "/ws/sync?token=device-auth-test"
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=device-auth-test"
|
||||
req, _ := http.NewRequest("GET", wsURL, nil)
|
||||
req.Header.Set("Authorization", "Bearer test-device-token-"+deviceID.String())
|
||||
|
||||
@@ -86,7 +84,6 @@ func TestWebSocketDeviceAuth(t *testing.T) {
|
||||
// TestWebSocketProgressBroadcast tests that progress updates are broadcast to connected clients
|
||||
func TestWebSocketProgressBroadcast(t *testing.T) {
|
||||
ts, queries, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
// Get JWT token
|
||||
token := loginTestUser(t, ts, queries)
|
||||
@@ -96,7 +93,7 @@ func TestWebSocketProgressBroadcast(t *testing.T) {
|
||||
mediaID := createTestMediaItem(t, queries, userID)
|
||||
|
||||
// Connect WebSocket client
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||
require.NoError(t, err)
|
||||
defer ws.Close()
|
||||
@@ -117,7 +114,7 @@ func TestWebSocketProgressBroadcast(t *testing.T) {
|
||||
}
|
||||
body, _ := json.Marshal(progressReq)
|
||||
|
||||
req, _ := http.NewRequest("POST", ts.URL+"/api/progress/"+mediaID, strings.NewReader(string(body)))
|
||||
req, _ := http.NewRequest("POST", setup.Server.URL+"/api/progress/"+mediaID, strings.NewReader(string(body)))
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
@@ -149,11 +146,10 @@ func TestWebSocketProgressBroadcast(t *testing.T) {
|
||||
// TestWebSocketPingPong tests that ping/pong messages work correctly
|
||||
func TestWebSocketPingPong(t *testing.T) {
|
||||
ts, queries, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
token := loginTestUser(t, ts, queries)
|
||||
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=" + token
|
||||
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||
require.NoError(t, err)
|
||||
defer ws.Close()
|
||||
@@ -182,14 +178,13 @@ func TestWebSocketPingPong(t *testing.T) {
|
||||
// TestWebSocketConnectionLimit tests that the server handles multiple connections
|
||||
func TestWebSocketConnectionLimit(t *testing.T) {
|
||||
ts, queries, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
|
||||
token := loginTestUser(t, ts, queries)
|
||||
|
||||
// Create multiple connections
|
||||
connections := make([]*websocket.Conn, 5)
|
||||
for i := 0; i < 5; i++ {
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + fmt.Sprintf("/ws/sync?token=%s&conn=%d", token, i)
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + fmt.Sprintf("/ws/sync?token=%s&conn=%d", token, i)
|
||||
ws, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||
require.NoError(t, err, "Failed to create connection %d", i)
|
||||
connections[i] = ws
|
||||
@@ -207,11 +202,10 @@ func TestWebSocketConnectionLimit(t *testing.T) {
|
||||
|
||||
// TestWebSocketInvalidToken tests that invalid tokens are rejected
|
||||
func TestWebSocketInvalidToken(t *testing.T) {
|
||||
ts, _, _ := setupTestServer(t)
|
||||
defer ts.Close()
|
||||
setup := setupTestServer(t)
|
||||
|
||||
// Try to connect with invalid token
|
||||
wsURL := strings.Replace(ts.URL, "http", "ws", 1) + "/ws/sync?token=invalid-token"
|
||||
wsURL := strings.Replace(setup.Server.URL, "http", "ws", 1) + "/ws/sync?token=invalid-token"
|
||||
|
||||
_, _, err := websocket.DefaultDialer.Dial(wsURL, nil)
|
||||
assert.Error(t, err, "Expected error when connecting with invalid token")
|
||||
|
||||
Reference in New Issue
Block a user