-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_handler_test.go
153 lines (128 loc) · 4.06 KB
/
basic_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
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
package middlewares
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHashExtractor(t *testing.T) {
req, err := http.NewRequest("GET", "/basic", nil)
if err != nil {
t.Fatal(err)
}
raw := "dG9tOnNoYXJkd2FyZQ==" //plain: tom:shardware
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", raw))
rval, err := basicAuthHash(req.Header)
assert.Nil(t, err, "should be nil")
assert.Equal(t, raw, rval, "should be equal")
t.Run("Empty authorization hash", func(t *testing.T) {
req.Header.Set("Authorization", "Basic ")
rval, err := basicAuthHash(req.Header)
assert.NotNil(t, err, "should not be nil")
assert.Empty(t, rval, "should be empty")
assert.Equal(t, "empty basic authorization hash", err.Error(), "should be equal")
})
t.Run("No authorization header", func(t *testing.T) {
req.Header.Del("Authorization")
rval, err := basicAuthHash(req.Header)
assert.NotNil(t, err, "should not be nil")
assert.Empty(t, rval, "should be empty")
assert.Equal(t, "no basic authorization header found", err.Error(), "should be equal")
})
t.Run("Decode b64", func(t *testing.T) {
rval, err := decodeBase64(raw)
assert.Nil(t, err, "should be nil")
assert.Equal(t, "tom:shardware", rval, "should be equal")
_, err = decodeBase64("notbase64")
assert.NotNil(t, err)
})
t.Run("BasicAuth struct", func(t *testing.T) {
plain := "tom:shardware"
a := newBasicAuth(plain)
assert.Equal(t, "tom", a.user, "should be equal")
assert.Equal(t, "shardware", a.pass, "should be equal")
plain = "tomshardware"
a = newBasicAuth(plain)
assert.Empty(t, a.user)
assert.Empty(t, a.pass)
plain = "tom:"
a = newBasicAuth(plain)
assert.Empty(t, a.user)
assert.Empty(t, a.pass)
plain = ":shardware"
a = newBasicAuth(plain)
assert.Empty(t, a.user)
assert.Empty(t, a.pass)
})
}
func TestBasicAuthHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/basic", nil)
if err != nil {
t.Fatal(err)
}
b64 := "dG9tOnNoYXJkd2FyZQ==" //plain: tom:shardware
t.Run("Extracts from context", func(t *testing.T) {
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", b64))
ctxHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, err := BasicCredentials(r.Context())
assert.Nil(t, err, "should be nil")
assert.Equal(t, "tom", u)
assert.Equal(t, "shardware", p)
})
rr := httptest.NewRecorder()
h := BasicAuthorizationHandler(ctxHandler)
h.ServeHTTP(rr, req)
})
t.Run("Missing basic auth data", func(t *testing.T) {
req.Header.Del("Authorization")
ctxHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, err := BasicCredentials(r.Context())
assert.NotNil(t, err, "should not be nil")
assert.Empty(t, u)
assert.Empty(t, p)
})
var b bytes.Buffer
SetOutput(&b)
rr := httptest.NewRecorder()
h := BasicAuthorizationHandler(ctxHandler)
h.ServeHTTP(rr, req)
bs := string(b.Bytes())
assert.Equal(t, "warn: no basic authorization header found\n", bs)
})
t.Run("Invalid basic auth data", func(t *testing.T) {
req.Header.Add("Authorization", "Basic thisisnotbase64")
ctxHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
u, p, err := BasicCredentials(r.Context())
assert.NotNil(t, err, "should not be nil")
assert.Empty(t, u)
assert.Empty(t, p)
})
var b bytes.Buffer
SetOutput(&b)
rr := httptest.NewRecorder()
h := BasicAuthorizationHandler(ctxHandler)
h.ServeHTTP(rr, req)
bs := string(b.Bytes())
assert.Contains(t, bs, "warn: ")
})
}
func ExampleBasicCredentials() {
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, pass, err := BasicCredentials(r.Context())
if err != nil {
// error handling
}
fmt.Printf("%s, %s", user, pass)
})
// ...
http.Handle("/", defaultHandler)
}
func ExampleBasicAuthorizationHandler() {
defaultHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// do something
})
http.Handle("/", BasicAuthorizationHandler(defaultHandler))
http.ListenAndServe(":3000", nil)
}