-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathamis_test.go
228 lines (185 loc) · 5.21 KB
/
amis_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package amisgo
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
const (
testFileContent = "test content"
testFileName = "test.txt"
testOKResponse = "ok"
)
// Helper functions for common test operations
func assertStatus(t *testing.T, got, want int) {
t.Helper()
if got != want {
t.Errorf("expected status %d, got %d", want, got)
}
}
func assertBody(t *testing.T, got, want string) {
t.Helper()
if got != want {
t.Errorf("expected body %q, got %q", want, got)
}
}
func assertContains(t *testing.T, body, substr string) {
t.Helper()
if !strings.Contains(body, substr) {
t.Errorf("expected body to contain %q", substr)
}
}
func makeRequest(handler http.Handler, method, path string) (*httptest.ResponseRecorder, *http.Request) {
w := httptest.NewRecorder()
r := httptest.NewRequest(method, path, nil)
handler.ServeHTTP(w, r)
return w, r
}
func createTestFile(t *testing.T, dir string) {
t.Helper()
err := os.WriteFile(dir+"/"+testFileName, []byte(testFileContent), 0o644)
if err != nil {
t.Fatal(err)
}
}
func TestNew(t *testing.T) {
e := New()
if e == nil {
t.Error("New() should return a non-nil Engine")
}
}
func TestMount(t *testing.T) {
e := New()
page := struct {
Title string `json:"title"`
Type string `json:"type"`
}{
Title: "Test Page",
Type: "page",
}
e.Mount("/page", page)
t.Run("base path", func(t *testing.T) {
w, _ := makeRequest(e, "GET", "/page")
assertStatus(t, w.Code, http.StatusOK)
body := w.Body.String()
assertContains(t, body, "<!DOCTYPE html>")
expectedJSON, _ := json.Marshal(page)
assertContains(t, body, string(expectedJSON))
})
t.Run("sub path", func(t *testing.T) {
w, _ := makeRequest(e, "GET", "/page/subpath")
assertStatus(t, w.Code, http.StatusNotFound)
})
}
func TestRedirect(t *testing.T) {
e := New()
e.Redirect("/old", "/new", http.StatusPermanentRedirect)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/old", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusPermanentRedirect {
t.Errorf("expected status 308, got %d", w.Code)
}
location := w.Header().Get("Location")
if location != "/new" {
t.Errorf("expected redirect to /new, got %s", location)
}
}
func TestHandle(t *testing.T) {
e := New()
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(testOKResponse))
})
e.Handle("/api/handle", handler)
w, _ := makeRequest(e, "GET", "/api/handle")
assertStatus(t, w.Code, http.StatusOK)
assertBody(t, w.Body.String(), testOKResponse)
}
func TestHandleFunc(t *testing.T) {
e := New()
e.HandleFunc("/api/handlefunc", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(testOKResponse))
})
w, _ := makeRequest(e, "GET", "/api/handlefunc")
assertStatus(t, w.Code, http.StatusOK)
assertBody(t, w.Body.String(), testOKResponse)
}
func TestStaticFiles(t *testing.T) {
tmpDir := t.TempDir()
createTestFile(t, tmpDir)
e := New()
e.StaticFiles("/static/", tmpDir)
t.Run("serve static file", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/static/"+testFileName, nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
content, _ := io.ReadAll(w.Body)
if string(content) != testFileContent {
t.Error("unexpected file content")
}
})
t.Run("404 for non-existent file", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/static/nonexistent.txt", nil)
e.ServeHTTP(w, r)
if w.Code != http.StatusNotFound {
t.Errorf("expected status 404, got %d", w.Code)
}
})
}
func TestAsHandler(t *testing.T) {
tmpDir := t.TempDir()
createTestFile(t, tmpDir)
e := New()
e.Mount("/admin/page", struct{}{})
e.StaticFS("/admin/static/", http.Dir(tmpDir))
// Create main mux and mount engine
mux := http.NewServeMux()
mux.Handle("/admin/", e)
t.Run("serve mounted component", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/admin/page", nil)
mux.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
})
t.Run("serve static file", func(t *testing.T) {
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/admin/static/"+testFileName, nil)
mux.ServeHTTP(w, r)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
}
content, _ := io.ReadAll(w.Body)
if string(content) != testFileContent {
t.Error("unexpected file content")
}
})
}
func TestUse(t *testing.T) {
e := New()
middleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("X-Custom-Header", "Middleware Applied")
next.ServeHTTP(w, r)
})
}
e.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Hello, World!"))
}, middleware)
w := httptest.NewRecorder()
r := httptest.NewRequest("GET", "/test", nil)
e.ServeHTTP(w, r)
assertStatus(t, w.Code, http.StatusOK)
assertBody(t, w.Body.String(), "Hello, World!")
if header := w.Header().Get("X-Custom-Header"); header != "Middleware Applied" {
t.Errorf("expected header X-Custom-Header to be 'Middleware Applied', got %s", header)
}
}