forked from fjl/go-couchdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_test.go
51 lines (42 loc) · 1.38 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
50
51
package couchdb_test
import (
"net/http"
"testing"
"github.com/cabify/go-couchdb"
)
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 := couchdb.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 := couchdb.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 := couchdb.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)
}