server_test.go

  1package srv
  2
  3import (
  4	"net/http"
  5	"net/http/httptest"
  6	"os"
  7	"path/filepath"
  8	"strings"
  9	"testing"
 10)
 11
 12func TestServerSetupAndHandlers(t *testing.T) {
 13	tempDB := filepath.Join(t.TempDir(), "test_server.sqlite3")
 14	t.Cleanup(func() { os.Remove(tempDB) })
 15
 16	server, err := New(tempDB, "test-hostname")
 17	if err != nil {
 18		t.Fatalf("failed to create server: %v", err)
 19	}
 20
 21	// Test root endpoint without auth
 22	t.Run("root endpoint unauthenticated", func(t *testing.T) {
 23		req := httptest.NewRequest(http.MethodGet, "/", nil)
 24		w := httptest.NewRecorder()
 25
 26		server.HandleRoot(w, req)
 27
 28		if w.Code != http.StatusOK {
 29			t.Errorf("expected status 200, got %d", w.Code)
 30		}
 31
 32		body := w.Body.String()
 33		if !strings.Contains(body, "test-hostname") {
 34			t.Errorf("expected page to show hostname, got body: %s", body)
 35		}
 36		if !strings.Contains(body, "Go Template Project") {
 37			t.Errorf("expected page to contain headline, got body: %s", body)
 38		}
 39		if strings.Contains(body, "Signed in as") {
 40			t.Errorf("expected page to not be logged in, got body: %s", body)
 41		}
 42		if !strings.Contains(body, "Not signed in") {
 43			t.Errorf("expected page to show 'Not signed in', got body: %s", body)
 44		}
 45	})
 46
 47	// Test root endpoint with auth headers
 48	t.Run("root endpoint authenticated", func(t *testing.T) {
 49		req := httptest.NewRequest(http.MethodGet, "/", nil)
 50		req.Header.Set("X-ExeDev-UserID", "user123")
 51		req.Header.Set("X-ExeDev-Email", "test@example.com")
 52		w := httptest.NewRecorder()
 53
 54		server.HandleRoot(w, req)
 55
 56		if w.Code != http.StatusOK {
 57			t.Errorf("expected status 200, got %d", w.Code)
 58		}
 59
 60		body := w.Body.String()
 61		if !strings.Contains(body, "Signed in as") {
 62			t.Errorf("expected page to show logged in state, got body: %s", body)
 63		}
 64		if !strings.Contains(body, "test@example.com") {
 65			t.Error("expected page to show user email")
 66		}
 67	})
 68
 69	// Test view counter functionality
 70	t.Run("view counter increments", func(t *testing.T) {
 71		// Make first request
 72		req1 := httptest.NewRequest(http.MethodGet, "/", nil)
 73		req1.Header.Set("X-ExeDev-UserID", "counter-test")
 74		req1.RemoteAddr = "192.168.1.100:12345"
 75		w1 := httptest.NewRecorder()
 76		server.HandleRoot(w1, req1)
 77
 78		// Should show "1 times" or similar
 79		body1 := w1.Body.String()
 80		if !strings.Contains(body1, "1</strong> times") {
 81			t.Error("expected first visit to show 1 time")
 82		}
 83
 84		// Make second request with same user
 85		req2 := httptest.NewRequest(http.MethodGet, "/", nil)
 86		req2.Header.Set("X-ExeDev-UserID", "counter-test")
 87		req2.RemoteAddr = "192.168.1.100:12345"
 88		w2 := httptest.NewRecorder()
 89		server.HandleRoot(w2, req2)
 90
 91		// Should show "2 times" or similar
 92		body2 := w2.Body.String()
 93		if !strings.Contains(body2, "2</strong> times") {
 94			t.Error("expected second visit to show 2 times")
 95		}
 96	})
 97}
 98
 99func TestUtilityFunctions(t *testing.T) {
100	t.Run("mainDomainFromHost function", func(t *testing.T) {
101		tests := []struct {
102			input    string
103			expected string
104		}{
105			{"example.exe.cloud:8080", "exe.cloud:8080"},
106			{"example.exe.dev", "exe.dev"},
107			{"example.exe.cloud", "exe.cloud"},
108		}
109
110		for _, test := range tests {
111			result := mainDomainFromHost(test.input)
112			if result != test.expected {
113				t.Errorf("mainDomainFromHost(%q) = %q, expected %q", test.input, result, test.expected)
114			}
115		}
116	})
117}