-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
159 lines (118 loc) · 3.88 KB
/
handlers_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestLoginHandlerOK(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, "/login", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(login)
handler.ServeHTTP(rr, req)
// check the status code is 200.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}
func TestLogoutHandlerRedirectStatus(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/logout", nil)
if err != nil {
t.Fatal(err)
}
s := Session{
ID: "",
Name: "",
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(s.logout)
handler.ServeHTTP(rr, req)
// check the status code is 303.
if status := rr.Code; status != http.StatusSeeOther {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusSeeOther)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}
func TestAdminHandlerOK(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/user", nil)
if err != nil {
t.Fatal(err)
}
// w.Header().Set("Cache-Control", "no-cache, private, max-age=0")
rr := httptest.NewRecorder()
handler := http.HandlerFunc(admin)
handler.ServeHTTP(rr, req)
// check the status code is 200.
if status := rr.Code; status != http.StatusOK {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// check correct header setup
expected := "no-cache, private, max-age=0"
if header := rr.Result().Header.Get("Cache-Control"); header != expected {
t.Errorf("handler returned wrong header set: got %v want %v", header, expected)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}
func TestUnAuthHandlerOK(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/error", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(unAuth)
handler.ServeHTTP(rr, req)
// check the status code is 401.
if status := rr.Code; status != http.StatusUnauthorized {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}
func TestPageNotFoundHandlerOK(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/404", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(pageNotFound)
handler.ServeHTTP(rr, req)
// check the status code is 404.
if status := rr.Code; status != http.StatusNotFound {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusNotFound)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}
func TestRedirectToHTTPSHandlerRedirectStatus(t *testing.T) {
req, err := http.NewRequest(http.MethodGet, "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(redirectToHTTPS)
handler.ServeHTTP(rr, req)
// check the status code is 301.
if status := rr.Code; status != http.StatusMovedPermanently {
t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusMovedPermanently)
}
// check the response body is not empty.
if rr.Body.Len() == 0 {
t.Errorf("handler returned unexpected body length: got %v want %v", rr.Body.Len(), "> 0")
}
}