|
| 1 | +package caddy_admin_ui |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/caddyserver/caddy/v2/caddyconfig/caddyfile" |
| 12 | + "github.com/caddyserver/caddy/v2/caddyconfig/httpcaddyfile" |
| 13 | + "github.com/caddyserver/caddy/v2/modules/caddyhttp" |
| 14 | + "go.uber.org/zap" |
| 15 | +) |
| 16 | + |
| 17 | +func TestCaddyModule(t *testing.T) { |
| 18 | + adminUI := CaddyAdminUI{} |
| 19 | + moduleInfo := adminUI.CaddyModule() |
| 20 | + |
| 21 | + if moduleInfo.ID != "http.handlers.caddy_admin_ui" { |
| 22 | + t.Errorf("Expected module ID 'http.handlers.caddy_admin_ui', got %s", moduleInfo.ID) |
| 23 | + } |
| 24 | + |
| 25 | + if moduleInfo.New == nil { |
| 26 | + t.Error("Expected New function to be non-nil") |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +func TestProvision(t *testing.T) { |
| 31 | + adminUI := CaddyAdminUI{} |
| 32 | + |
| 33 | + // Test that the struct can be created and has reasonable defaults |
| 34 | + if adminUI.IndexNames != nil { |
| 35 | + t.Log("IndexNames already initialized") |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func TestCalculateEtag(t *testing.T) { |
| 40 | + // Create a mock file info |
| 41 | + mockFile := &mockFileInfo{ |
| 42 | + name: "test.txt", |
| 43 | + size: 1024, |
| 44 | + modTime: time.Now(), |
| 45 | + isDir: false, |
| 46 | + } |
| 47 | + |
| 48 | + etag := calculateEtag(mockFile) |
| 49 | + if etag == "" { |
| 50 | + t.Error("Expected non-empty etag") |
| 51 | + } |
| 52 | + |
| 53 | + // Check etag format |
| 54 | + if !strings.HasPrefix(etag, "\"") || !strings.HasSuffix(etag, "\"") { |
| 55 | + t.Errorf("Expected etag to be quoted, got %s", etag) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestOpenFile(t *testing.T) { |
| 60 | + adminUI := CaddyAdminUI{ |
| 61 | + logger: zap.NewNop(), // Use a no-op logger to avoid nil pointer |
| 62 | + } |
| 63 | + |
| 64 | + // Test opening non-existent file |
| 65 | + w := httptest.NewRecorder() |
| 66 | + _, err := adminUI.openFile("nonexistent.txt", w) |
| 67 | + if err == nil { |
| 68 | + t.Error("Expected error for non-existent file") |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestNotFound(t *testing.T) { |
| 73 | + adminUI := CaddyAdminUI{ |
| 74 | + logger: zap.NewNop(), |
| 75 | + } |
| 76 | + w := httptest.NewRecorder() |
| 77 | + r := httptest.NewRequest("GET", "/nonexistent", nil) |
| 78 | + |
| 79 | + // Create a mock next handler |
| 80 | + called := false |
| 81 | + next := caddyhttp.HandlerFunc(func(w http.ResponseWriter, r *http.Request) error { |
| 82 | + called = true |
| 83 | + return nil |
| 84 | + }) |
| 85 | + |
| 86 | + err := adminUI.notFound(w, r, next) |
| 87 | + if err != nil { |
| 88 | + t.Errorf("notFound returned error: %v", err) |
| 89 | + } |
| 90 | + |
| 91 | + if !called { |
| 92 | + t.Error("Expected next handler to be called") |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +func TestParseCaddyfile(t *testing.T) { |
| 97 | + tests := []struct { |
| 98 | + name string |
| 99 | + input string |
| 100 | + enableShell bool |
| 101 | + shell string |
| 102 | + wantErr bool |
| 103 | + }{ |
| 104 | + { |
| 105 | + name: "basic directive", |
| 106 | + input: "caddy_admin_ui", |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "enable shell true", |
| 110 | + input: "caddy_admin_ui { enable_shell true }", |
| 111 | + enableShell: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + name: "enable shell false", |
| 115 | + input: "caddy_admin_ui { enable_shell false }", |
| 116 | + enableShell: false, |
| 117 | + }, |
| 118 | + { |
| 119 | + name: "custom shell", |
| 120 | + input: "caddy_admin_ui { shell /bin/bash }", |
| 121 | + shell: "/bin/bash", |
| 122 | + }, |
| 123 | + { |
| 124 | + name: "both options", |
| 125 | + input: "caddy_admin_ui { enable_shell true shell /bin/zsh }", |
| 126 | + enableShell: true, |
| 127 | + shell: "/bin/zsh", |
| 128 | + }, |
| 129 | + { |
| 130 | + name: "unknown directive", |
| 131 | + input: "caddy_admin_ui { unknown_directive }", |
| 132 | + wantErr: true, |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.name, func(t *testing.T) { |
| 138 | + h := httpcaddyfile.Helper{ |
| 139 | + Dispenser: caddyfile.NewTestDispenser(tt.input), |
| 140 | + } |
| 141 | + |
| 142 | + handler, err := parseCaddyfile(h) |
| 143 | + if (err != nil) != tt.wantErr { |
| 144 | + t.Errorf("parseCaddyfile() error = %v, wantErr %v", err, tt.wantErr) |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + if !tt.wantErr { |
| 149 | + adminUI, ok := handler.(*CaddyAdminUI) |
| 150 | + if !ok { |
| 151 | + t.Error("Expected handler to be *CaddyAdminUI") |
| 152 | + return |
| 153 | + } |
| 154 | + |
| 155 | + if tt.enableShell != adminUI.EnableShell { |
| 156 | + t.Errorf("EnableShell = %v, want %v", adminUI.EnableShell, tt.enableShell) |
| 157 | + } |
| 158 | + |
| 159 | + if tt.shell != "" && tt.shell != adminUI.Shell { |
| 160 | + t.Errorf("Shell = %v, want %v", adminUI.Shell, tt.shell) |
| 161 | + } |
| 162 | + } |
| 163 | + }) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestMapDirOpenError(t *testing.T) { |
| 168 | + tests := []struct { |
| 169 | + name string |
| 170 | + err error |
| 171 | + path string |
| 172 | + want error |
| 173 | + }{ |
| 174 | + { |
| 175 | + name: "not exist error", |
| 176 | + err: os.ErrNotExist, |
| 177 | + path: "/nonexistent/path", |
| 178 | + want: os.ErrNotExist, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tt := range tests { |
| 183 | + t.Run(tt.name, func(t *testing.T) { |
| 184 | + got := mapDirOpenError(tt.err, tt.path) |
| 185 | + if got != tt.want { |
| 186 | + t.Errorf("mapDirOpenError() = %v, want %v", got, tt.want) |
| 187 | + } |
| 188 | + }) |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +func TestInit(t *testing.T) { |
| 193 | + // Test that the module can be created |
| 194 | + adminUI := CaddyAdminUI{} |
| 195 | + moduleInfo := adminUI.CaddyModule() |
| 196 | + if moduleInfo.ID != "http.handlers.caddy_admin_ui" { |
| 197 | + t.Error("Module ID not set correctly") |
| 198 | + } |
| 199 | +} |
| 200 | + |
| 201 | +// Test helpers |
| 202 | + |
| 203 | +type mockFileInfo struct { |
| 204 | + name string |
| 205 | + size int64 |
| 206 | + modTime time.Time |
| 207 | + isDir bool |
| 208 | +} |
| 209 | + |
| 210 | +func (m *mockFileInfo) Name() string { return m.name } |
| 211 | +func (m *mockFileInfo) Size() int64 { return m.size } |
| 212 | +func (m *mockFileInfo) Mode() os.FileMode { return 0644 } |
| 213 | +func (m *mockFileInfo) ModTime() time.Time { return m.modTime } |
| 214 | +func (m *mockFileInfo) IsDir() bool { return m.isDir } |
| 215 | +func (m *mockFileInfo) Sys() interface{} { return nil } |
0 commit comments