-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
106 lines (87 loc) · 2.64 KB
/
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
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
package main
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
)
func TestMain(m *testing.M) {
setupServer()
code := m.Run()
os.Exit(code)
}
func TestStaticFilesHandler(t *testing.T) {
testServer := httptest.NewServer(nil)
defer testServer.Close()
resp, err := http.Get(testServer.URL + "/index.html")
if err != nil {
t.Fatalf("Failed to make request to /index.html: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code 200, got %d", resp.StatusCode)
}
expectedContentType := "text/html"
if !strings.Contains(resp.Header.Get("Content-Type"), expectedContentType) {
t.Errorf("Expected Content-Type to include %s, got %s", expectedContentType, resp.Header.Get("Content-Type"))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
expectedContentSnippet := `<div id="root"></div>`
if !strings.Contains(string(body), expectedContentSnippet) {
t.Errorf("Expected response body to contain %q", expectedContentSnippet)
}
}
func TestAllowedSPAPaths(t *testing.T) {
testServer := httptest.NewServer(nil)
defer testServer.Close()
resp, err := http.Get(testServer.URL + "/time")
if err != nil {
t.Fatalf("Failed to make request to /time: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code 200, got %d", resp.StatusCode)
}
expectedContentType := "text/html"
if !strings.Contains(resp.Header.Get("Content-Type"), expectedContentType) {
t.Errorf("Expected Content-Type to include %s, got %s", expectedContentType, resp.Header.Get("Content-Type"))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
expectedContentSnippet := `<div id="root"></div>`
if !strings.Contains(string(body), expectedContentSnippet) {
t.Errorf("Expected response body to contain %q", expectedContentSnippet)
}
}
func TestTimeHandler(t *testing.T) {
testServer := httptest.NewServer(nil)
defer testServer.Close()
resp, err := http.Get(testServer.URL + "/api/time")
if err != nil {
t.Fatalf("Failed to make request to /api/time: %v", err)
}
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status code 200, got %d", resp.StatusCode)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatalf("Failed to read response body: %v", err)
}
var data struct {
Time string `json:"time"`
}
if err := json.Unmarshal(body, &data); err != nil {
t.Fatalf("Failed to unmarshal response body: %v", err)
}
if data.Time == "" {
t.Errorf("Expected 'time' key to be present and not empty")
}
}