|
| 1 | +package serve |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | +) |
| 12 | + |
| 13 | +func TestGetFreePort(t *testing.T) { |
| 14 | + port, err := GetFreePort() |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("GetFreePort() error = %v", err) |
| 17 | + } |
| 18 | + if port <= 0 || port > 65535 { |
| 19 | + t.Errorf("GetFreePort() = %d, want valid port (1-65535)", port) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestGetFreePortUnique(t *testing.T) { |
| 24 | + port1, err := GetFreePort() |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("first GetFreePort() error = %v", err) |
| 27 | + } |
| 28 | + port2, err := GetFreePort() |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("second GetFreePort() error = %v", err) |
| 31 | + } |
| 32 | + if port1 <= 0 || port2 <= 0 { |
| 33 | + t.Errorf("expected valid ports, got %d and %d", port1, port2) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +func TestServeAssessment(t *testing.T) { |
| 38 | + dir := t.TempDir() |
| 39 | + |
| 40 | + content := "<html><body>hello</body></html>" |
| 41 | + if err := os.WriteFile(filepath.Join(dir, "index.html"), []byte(content), 0644); err != nil { |
| 42 | + t.Fatalf("failed to write test file: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + port, stop, err := ServeAssessment(dir) |
| 46 | + if err != nil { |
| 47 | + t.Fatalf("ServeAssessment() error = %v", err) |
| 48 | + } |
| 49 | + defer stop() |
| 50 | + |
| 51 | + if port <= 0 || port > 65535 { |
| 52 | + t.Errorf("ServeAssessment() port = %d, want valid port", port) |
| 53 | + } |
| 54 | + |
| 55 | + // Give server time to start |
| 56 | + time.Sleep(50 * time.Millisecond) |
| 57 | + |
| 58 | + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/index.html", port)) |
| 59 | + if err != nil { |
| 60 | + t.Fatalf("GET request failed: %v", err) |
| 61 | + } |
| 62 | + defer resp.Body.Close() |
| 63 | + |
| 64 | + if resp.StatusCode != http.StatusOK { |
| 65 | + t.Errorf("status = %d, want %d", resp.StatusCode, http.StatusOK) |
| 66 | + } |
| 67 | + |
| 68 | + body, err := io.ReadAll(resp.Body) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("failed to read response body: %v", err) |
| 71 | + } |
| 72 | + if string(body) != content { |
| 73 | + t.Errorf("body = %q, want %q", string(body), content) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestServeAssessmentStop(t *testing.T) { |
| 78 | + dir := t.TempDir() |
| 79 | + |
| 80 | + port, stop, err := ServeAssessment(dir) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("ServeAssessment() error = %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + time.Sleep(50 * time.Millisecond) |
| 86 | + stop() |
| 87 | + time.Sleep(50 * time.Millisecond) |
| 88 | + |
| 89 | + _, err = http.Get(fmt.Sprintf("http://localhost:%d/", port)) |
| 90 | + if err == nil { |
| 91 | + t.Error("expected request to fail after stop, but it succeeded") |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestServeAssessmentInvalidDir(t *testing.T) { |
| 96 | + // FileServer returns 404s for missing files rather than failing to start |
| 97 | + port, stop, err := ServeAssessment("/nonexistent/path/that/does/not/exist") |
| 98 | + if err != nil { |
| 99 | + return // acceptable to fail fast on bad dir |
| 100 | + } |
| 101 | + defer stop() |
| 102 | + |
| 103 | + time.Sleep(50 * time.Millisecond) |
| 104 | + |
| 105 | + resp, err := http.Get(fmt.Sprintf("http://localhost:%d/", port)) |
| 106 | + if err != nil { |
| 107 | + return |
| 108 | + } |
| 109 | + defer resp.Body.Close() |
| 110 | + |
| 111 | + if resp.StatusCode != http.StatusNotFound { |
| 112 | + t.Errorf("status = %d, want %d for missing dir", resp.StatusCode, http.StatusNotFound) |
| 113 | + } |
| 114 | +} |
0 commit comments