-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathspa_server_test.go
169 lines (139 loc) · 4.14 KB
/
spa_server_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
161
162
163
164
165
166
167
168
169
package spa_server
import (
"bytes"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"testing"
)
func TestSanityCheckIndexExists(t *testing.T) {
b, err := ioutil.ReadFile("public/index.html")
if err != nil {
t.Fatal("Error reading file: public/index.html")
}
knownString := []byte("Test")
if bytes.Contains(b, knownString) != true {
t.Fatal("File: public/index.html did not contain an expected string!")
}
}
func TestSanityCheckJsFileExists(t *testing.T) {
b, err := ioutil.ReadFile("public/js/test.js")
if err != nil {
t.Fatal("Error reading file: public/js/test.js")
}
knownString := []byte("Test")
if bytes.Contains(b, knownString) != true {
t.Fatal("File: public/js/test.js did not contain an expected string!")
}
}
func TestSanityCheckLoginDirectory(t *testing.T) {
f, err := os.Stat("public/login")
if os.IsNotExist(err) {
return
}
if f.IsDir() {
t.Fatal("Path should not exist!")
}
}
func TestSpaHandlerRoot(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected, _ := ioutil.ReadFile("public/index.html")
rb := rr.Body.Bytes()
if bytes.Compare(rb, expected) != 0 {
t.Error("handler returned unexpected body")
}
}
func TestSpaHandlerIndexShouldRedirect(t *testing.T) {
req, err := http.NewRequest("GET", "/index.html", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
location := rr.Header().Get("Location")
if location != "./" {
t.Errorf("Handler returned wrong Location header: got %v wanted ./", location)
}
}
func TestSpaHandlerDirectoryFound(t *testing.T) {
req, err := http.NewRequest("GET", "/js", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected, _ := ioutil.ReadFile("public/index.html")
rb := rr.Body.Bytes()
if bytes.Compare(rb, expected) != 0 {
t.Error("handler returned unexpected body")
}
}
func TestSpaHandlerDirectoryNotFound(t *testing.T) {
req, err := http.NewRequest("GET", "/login", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected, _ := ioutil.ReadFile("public/index.html")
rb := rr.Body.Bytes()
if bytes.Compare(rb, expected) != 0 {
t.Error("handler returned unexpected body")
}
}
func TestSpaHandlerFileFound(t *testing.T) {
req, err := http.NewRequest("GET", "/js/test.js", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected, _ := ioutil.ReadFile("public/js/test.js")
rb := rr.Body.Bytes()
if bytes.Compare(rb, expected) != 0 {
t.Error("handler returned unexpected body")
}
}
func TestSpaHandlerFileNotFound(t *testing.T) {
req, err := http.NewRequest("GET", "/js/missing.js", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := SpaHandler("public", "index.html")
handler.ServeHTTP(rr, req)
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
expected, _ := ioutil.ReadFile("public/index.html")
rb := rr.Body.Bytes()
if bytes.Compare(rb, expected) != 0 {
t.Error("handler returned unexpected body")
}
}