Skip to content

Commit d21d35f

Browse files
committed
fix: Fix test.
1 parent 8b9bbf6 commit d21d35f

File tree

5 files changed

+262
-46
lines changed

5 files changed

+262
-46
lines changed

caddy_admin_ui.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ type CaddyAdminUI struct {
4141
// Append suffix to request filename if origin name is not exists.
4242
SuffixNames []string `json:"suffix_names,omitempty"`
4343

44-
EnableShell bool `json:"enable_shell,omitempty"`
44+
EnableShell bool `json:"enable_shell,omitempty"`
4545

46-
Shell string `json:"shell,omitempty"`
46+
Shell string `json:"shell,omitempty"`
4747

4848
logger *zap.Logger
4949
}
@@ -64,13 +64,13 @@ func (adminUI *CaddyAdminUI) Provision(ctx caddy.Context) error {
6464

6565
adminUI.SuffixNames = []string{"html", "htm", "txt"}
6666

67-
adminUI.EnableShell = false
67+
adminUI.EnableShell = false
6868

69-
sh := os.Getenv("SHELL")
70-
if sh == "" {
71-
sh = "/bin/sh"
72-
}
73-
adminUI.Shell = sh
69+
sh := os.Getenv("SHELL")
70+
if sh == "" {
71+
sh = "/bin/sh"
72+
}
73+
adminUI.Shell = sh
7474

7575
files, err := getAllFilenames(&buildFs, "build")
7676
adminUI.logger.Debug("list files of caddy_admin_ui",
@@ -93,11 +93,11 @@ func (adminUI *CaddyAdminUI) ServeHTTP(w http.ResponseWriter, r *http.Request, n
9393
return err
9494
}
9595

96-
if adminUI.EnableShell {
97-
if r.URL.Path == "/ws/pty" {
98-
return adminUI.handleWsPty(w, r, next)
99-
}
100-
}
96+
if adminUI.EnableShell {
97+
if r.URL.Path == "/ws/pty" {
98+
return adminUI.handleWsPty(w, r, next)
99+
}
100+
}
101101

102102
filename := "build" + r.URL.Path
103103

@@ -263,8 +263,7 @@ func (fsrv *CaddyAdminUI) notFound(w http.ResponseWriter, r *http.Request, next
263263
// parseCaddyfile parses the caddy_admin_ui directive. It enables the static file
264264
// server and configures it with this syntax:
265265
//
266-
// caddy_admin_ui
267-
//
266+
// caddy_admin_ui
268267
func parseCaddyfile(h httpcaddyfile.Helper) (caddyhttp.MiddlewareHandler, error) {
269268
var adminUI CaddyAdminUI
270269

caddy_admin_ui_test.go

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
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 }

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ go 1.24
44

55
require (
66
github.com/caddyserver/caddy/v2 v2.10.0
7+
github.com/creack/pty v1.1.7
8+
github.com/gorilla/websocket v1.5.3
79
go.uber.org/zap v1.27.0
810
)
911

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ github.com/cpuguy83/go-md2man v1.0.10/go.mod h1:SmD6nW6nTyfqj6ABTjUi3V3JVMnlJmwc
116116
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
117117
github.com/cpuguy83/go-md2man/v2 v2.0.6 h1:XJtiaUW6dEEqVuZiMTn1ldk455QWwEIsMIJlo5vtkx0=
118118
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
119+
github.com/creack/pty v1.1.7 h1:6pwm8kMQKCmgUg0ZHTm5+/YvRK0s3THD/28+T6/kk4A=
119120
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
120121
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
121122
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -219,6 +220,8 @@ github.com/googleapis/gax-go/v2 v2.0.3/go.mod h1:LLvjysVCY1JZeum8Z6l8qUty8fiNwE0
219220
github.com/googleapis/gax-go/v2 v2.12.4 h1:9gWcmF85Wvq4ryPFvGFaOgPIs1AQX0d0bcbGw4Z96qg=
220221
github.com/googleapis/gax-go/v2 v2.12.4/go.mod h1:KYEYLorsnIGDi/rPC8b5TdlB9kbKoFubselGIoBMCwI=
221222
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
223+
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
224+
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
222225
github.com/gregjones/httpcache v0.0.0-20180305231024-9cad4c3443a7/go.mod h1:FecbI9+v66THATjSRHfNgh1IVFe/9kFxbXtjV0ctIMA=
223226
github.com/grpc-ecosystem/grpc-gateway v1.5.0/go.mod h1:RSKVYQBd5MCa4OVpNdGskqpgL2+G+NZTnrVHpWWfpdw=
224227
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=

0 commit comments

Comments
 (0)