-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathquick_fileserver_test.go
More file actions
165 lines (141 loc) · 4.76 KB
/
quick_fileserver_test.go
File metadata and controls
165 lines (141 loc) · 4.76 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
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
//go:build !exclude_test
package quick
import (
"embed"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
)
// TestQuickStatic verifies that the static file server correctly serves content from the local file system.
//
// It sets up a GET route that attempts to serve "static/index.html" and checks if the file is served successfully.
// This test ensures that static file serving via the local file system works as expected.
//
// Run with:
//
// 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 performs table-driven tests to validate static file serving functionality.
//
// It tests both local file system and embedded files (embed.FS) for routes like "/" and "/static/index.html".
// Each test case checks the expected status code and whether the expected content is present in the response body.
//
// Run with:
//
// 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)
}
})
}
}
func TestFileWithEmbedFS(t *testing.T) {
q := New()
q.Static("/static", staticFiles)
q.Get("/", func(c *Ctx) error {
return c.File("./static/index.html")
})
resp, err := q.Qtest(QuickTestOptions{
Method: MethodGet,
URI: "/",
})
if err != nil {
t.Fatalf("Qtest failed: %v", err)
}
if err := resp.AssertStatus(200); err != nil {
t.Error(err)
}
if err := resp.AssertBodyContains("File Server Go example html"); err != nil {
t.Error(err)
}
if err := resp.AssertHeaderContains("Content-Type", "text/html"); err != nil {
t.Error(err)
}
}