-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_handler_test.go
61 lines (44 loc) · 1.01 KB
/
auth_handler_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
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
var nextCalled = false
func setup() (
rw *httptest.ResponseRecorder,
r *http.Request,
authz *AuthHandler) {
nextCalled = false
rw = httptest.NewRecorder()
r = httptest.NewRequest("", "/", nil)
authz = &AuthHandler{}
authz.Next = http.HandlerFunc(
func(rw http.ResponseWriter, r *http.Request) {
nextCalled = true
},
)
return
}
func TestCallNextWhenAuthorized(t *testing.T) {
rw, r, authz := setup()
r.Header.Set("Authorization", "Nic")
authz.ServeHTTP(rw, r)
if nextCalled == false {
t.Fatal("next should have been called")
}
}
func TestDoesNotCallNextWhenNotAuthorized(t *testing.T) {
rw, r, authz := setup()
authz.ServeHTTP(rw, r)
if nextCalled == true {
t.Fatal("next should not have been called")
}
}
func TestReturnsStatusUnauthorizedWhenNotAuthorized(t *testing.T) {
rw, r, authz := setup()
authz.ServeHTTP(rw, r)
if rw.Code != http.StatusUnauthorized {
t.Fatal("should have returned status unauthorized")
}
}