forked from fjl/go-couchdb
-
Notifications
You must be signed in to change notification settings - Fork 8
/
auth_test.go
49 lines (41 loc) · 1.32 KB
/
auth_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
package couchdb
import (
"net/http"
"testing"
)
func TestBasicAuth(t *testing.T) {
tests := []struct{ username, password, header string }{
{"", "", "Basic Og=="},
{"user", "", "Basic dXNlcjo="},
{"", "password", "Basic OnBhc3N3b3Jk"},
{"user", "password", "Basic dXNlcjpwYXNzd29yZA=="},
}
for _, test := range tests {
req, _ := http.NewRequest("GET", "http://localhost/", nil)
auth := BasicAuth(test.username, test.password)
auth.AddAuth(req)
expected := http.Header{"Authorization": {test.header}}
check(t, "req headers", expected, req.Header)
}
}
func TestProxyAuthWithoutToken(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost/", nil)
auth := ProxyAuth("user", []string{"role1", "role2"}, "")
auth.AddAuth(req)
expected := http.Header{
"X-Auth-Couchdb-Username": {"user"},
"X-Auth-Couchdb-Roles": {"role1,role2"},
}
check(t, "req headers", expected, req.Header)
}
func TestProxyAuthWithToken(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost/", nil)
auth := ProxyAuth("user", []string{"role1", "role2"}, "secret")
auth.AddAuth(req)
expected := http.Header{
"X-Auth-Couchdb-Username": {"user"},
"X-Auth-Couchdb-Roles": {"role1,role2"},
"X-Auth-Couchdb-Token": {"027da48c8c642ca4c58eb982eec81915179e77a3"},
}
check(t, "req headers", expected, req.Header)
}