-
Notifications
You must be signed in to change notification settings - Fork 57
/
auth_test.go
62 lines (45 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
50
51
52
53
54
55
56
57
58
59
60
61
62
package goconfluence
import (
"encoding/base64"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestAuth(t *testing.T) {
t.Run("basic-auth", func(t *testing.T) {
req := httptest.NewRequest("POST", "https://test.test", nil)
api, err := NewAPI("https://test.test", "username", "token")
assert.Nil(t, err)
assert.Empty(t, req.Header)
api.Auth(req)
h := req.Header.Get("Authorization")
assert.NotEmpty(t, h)
split := strings.Split(h, " ")
assert.Len(t, split, 2)
b, err := base64.StdEncoding.DecodeString(split[1])
assert.Nil(t, err)
auth := strings.Split(string(b), ":")
assert.Len(t, auth, 2)
assert.Equal(t, "username", auth[0])
assert.Equal(t, "token", auth[1])
})
t.Run("empty-auth", func(t *testing.T) {
req := httptest.NewRequest("POST", "https://test.test", nil)
api, err := NewAPI("https://test.test", "", "")
assert.Nil(t, err)
assert.Empty(t, req.Header)
api.Auth(req)
h := req.Header.Get("Authorization")
assert.Empty(t, h)
})
t.Run("token-auth", func(t *testing.T) {
req := httptest.NewRequest("POST", "https://test.test", nil)
api, err := NewAPI("https://test.test", "", "token")
assert.Nil(t, err)
assert.Empty(t, req.Header)
api.Auth(req)
h := req.Header.Get("Authorization")
assert.Equal(t, "Bearer token", h)
})
}