-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathquick_fileserver_test.go
160 lines (135 loc) · 4.4 KB
/
quick_fileserver_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
//go:build !exclude_test
package quick
import (
"embed"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// traditional test
///
// TestQuickStatic Tests if the static/* server functionality redirects correctly to index.html
// The will test TestQuickStatic(t *testing.T)
//
// $ go test -v -run ^TestQuickStatic
func TestQuickStatic(t *testing.T) {
q := New()
// Configure static file server from the "./static" directory
q.Static("/static", "./static")
// Define a route that serves static files
q.Get("/", func(c *Ctx) error {
c.File("static/*") // Testing if `static/index.html` is found
return nil
})
// Creating a test server
server := httptest.NewServer(q)
defer server.Close()
// Makes a GET request to "/"
resp, err := http.Get(server.URL + "/")
if err != nil {
t.Fatalf("Error making request: %v", err)
}
defer resp.Body.Close()
// Checks if the response is 200 OK
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status 200 OK, but received: %d", resp.StatusCode)
}
// Read the response content
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error reading response: %v", err)
}
// Check if the response contains any content expected from index.html
expectedContent := "<h1>File Server Go example html</h1>" // Example: if index.html has a <title> tag
if !strings.Contains(string(body), expectedContent) {
t.Errorf("Expected to find '%s' in the content, but did not find it", expectedContent)
}
}
// Table-driven test
// /
//
//go:embed static/*
var staticFiles embed.FS
// TestQuickStaticDriven Tests if the static/* server functionality redirects correctly to index.html
// The will test TestQuickStaticDriven(t *testing.T)
//
// $ go test -v -run ^TestQuickStaticDriven
func TestQuickStaticDriven(t *testing.T) {
tests := []struct {
name string // Test case description
useEmbed bool // Whether to use embedded files or local file system
path string // Path to test
statusCode int // Expected HTTP status code
expectBody string // Expected content in the response
}{
{"Serve index.html from file system", false, "/", http.StatusOK, "<h1>File Server Go example html</h1>"},
{"Serve static/index.html directly from file system", false, "/static/index.html", StatusNotFound, "404"},
{"Arquivo not found from file system", false, "/static/missing.html", http.StatusNotFound, "404"},
{"Serve index.html from embed FS", true, "/", http.StatusOK, "<h1>File Server Go example html</h1>"},
{"Serve static/index.html directly from embed FS", true, "/static/index.html", http.StatusNotFound, "404"},
{"Arquivo not found from embed FS", true, "/static/missing.html", http.StatusNotFound, "404"},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
q := New()
// Choose between embedded FS or local file system
if tc.useEmbed {
q.Static("/static", staticFiles)
} else {
q.Static("/static", "./static")
}
// Define a route for serving files
q.Get("/", func(c *Ctx) error {
c.File("static/*") // Must find `static/index.html`
return nil
})
// Creating a test server
server := httptest.NewServer(q)
defer server.Close()
// Making test request
resp, err := http.Get(server.URL + tc.path)
if err != nil {
t.Fatalf("Error making request to %s: %v", tc.path, err)
}
defer resp.Body.Close()
// Check the status code
if resp.StatusCode != tc.statusCode {
t.Errorf("Expected status %d, but received %d", tc.statusCode, resp.StatusCode)
}
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Error reading response: %v", err)
}
// Checks if the response contains the expected content
if tc.expectBody != "" && !strings.Contains(string(body), tc.expectBody) {
t.Errorf("Expected to find '%s' in the response body, but did not find it", tc.expectBody)
}
})
}
}
// ExampleServeStaticIndex demonstrates how to start the Quick server and serve static files correctly.
// The will return func ExampleServeStaticIndex()
//
// Run:
//
// $ go run main.go
// func ExampleQuick_Static() {
// //Quick Start
// q := New()
// /**
// //go:embed static/*
// var staticFiles embed.FS
// */
// // start FileServer
// // or
// // q.Static("/static", staticFiles)
// q.Static("/static", "./static")
// // send ServeFile
// q.Get("/", func(c *Ctx) error {
// c.File("./static/index.html")
// return nil
// })
// }