-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
44 lines (37 loc) · 860 Bytes
/
main_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
package main
import (
"io"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestFileServerHandler(t *testing.T) {
// setup test server
ts := httptest.NewServer(FileServerHandler())
defer ts.Close()
// send a GET request to the base url
res, err := http.Get(ts.URL)
if err != nil {
t.Fatal(err)
}
defer res.Body.Close()
// check status code
if res.StatusCode != http.StatusOK {
t.Fatalf("Expected 200 HTTP Status Code, received %d", res.StatusCode)
}
// check content
content, err := io.ReadAll(res.Body)
if err != nil {
t.Fatal(err)
}
// read the index.html file
expectedContent, err := os.ReadFile("./static/index.html")
if err != nil {
t.Fatal(err)
}
// compare with the server response
if string(content) != string(expectedContent) {
t.Fatalf("Expected content %q, received %q", expectedContent, content)
}
}