-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb_test.go
56 lines (41 loc) · 1.08 KB
/
web_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
package main
import (
"math"
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
var test_rw *httptest.ResponseRecorder
func init() {
addRoutes()
test_rw = httptest.NewRecorder()
}
func testRequest(t *testing.T, method string, path string, contentLength int) {
req, err := http.NewRequest(method, path, nil)
if err != nil {
t.Fatal(err)
}
http.DefaultServeMux.ServeHTTP(test_rw, req)
if test_rw.Code != http.StatusOK {
t.Fatalf("%v != %v", test_rw.Code, http.StatusOK)
}
header := test_rw.Header()
isContentLength, _ := strconv.Atoi(header.Get("Content-Length"))
if math.Abs(float64(isContentLength-contentLength)) > 10 {
t.Fatalf("%v != %v", isContentLength, contentLength)
}
}
func TestGetRoot(t *testing.T) {
testRequest(t, http.MethodGet, "/", 493)
testRequest(t, http.MethodGet, "/htmx.min.js", 49567)
}
func TestGetCreateForm(t *testing.T) {
testRequest(t, http.MethodGet, "/form/create", 304)
}
func TestGetData(t *testing.T) {
testRequest(t, http.MethodGet, "/data", 304)
}
func TestPOSTData(t *testing.T) {
testRequest(t, http.MethodPost, "/data", 304)
}