-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain_test.go
More file actions
58 lines (48 loc) · 1.3 KB
/
main_test.go
File metadata and controls
58 lines (48 loc) · 1.3 KB
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
package main
import (
"context"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"
"gofr.dev/pkg/gofr/datasource/file"
"gofr.dev/pkg/gofr/logging"
)
func TestServer(t *testing.T) {
dir := setupTestDir(t)
fs := file.NewLocalFileSystem(logging.NewMockLogger(logging.ERROR))
handler := &staticFileHandler{
fs: fs,
staticFilePath: dir,
defaultExtension: ".html",
next: http.NotFoundHandler(),
}
server := httptest.NewServer(handler)
t.Cleanup(server.Close)
tests := []struct {
path string
statusCode int
}{
{"/", http.StatusOK},
{"/docs", http.StatusOK},
{"/index", http.StatusOK},
{"/index/", http.StatusOK},
{filepath.Join(dir, "index.html"), http.StatusNotFound},
{"/index.html", http.StatusOK},
{"/nonexistent", http.StatusNotFound},
}
for _, test := range tests {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, server.URL+test.path, http.NoBody)
if err != nil {
t.Fatalf("Failed to create request: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
t.Fatalf("Failed to perform request: %v", err)
}
if resp.StatusCode != test.statusCode {
t.Errorf("Expected status code %v, got %v for path %v", test.statusCode, resp.StatusCode, test.path)
}
_ = resp.Body.Close()
}
}