-
Notifications
You must be signed in to change notification settings - Fork 93
/
digestAuth_test.go
75 lines (60 loc) · 1.9 KB
/
digestAuth_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
package gowebdav
import (
"errors"
"net/http"
"strings"
"testing"
)
func TestNewDigestAuth(t *testing.T) {
a := &DigestAuth{user: "user", pw: "password", digestParts: make(map[string]string, 0)}
ex := "DigestAuth login: user"
if a.String() != ex {
t.Error("expected: " + ex + " got: " + a.String())
}
if a.Clone() == a {
t.Error("expected a different instance")
}
if a.Close() != nil {
t.Error("expected close without errors")
}
}
func TestDigestAuthAuthorize(t *testing.T) {
a := &DigestAuth{user: "user", pw: "password", digestParts: make(map[string]string, 0)}
rq, _ := http.NewRequest("GET", "http://localhost/", nil)
a.Authorize(nil, rq, "/")
// TODO this is a very lazy test it cuts of cnonce
ex := `Digest username="user", realm="", nonce="", uri="/", nc=1, cnonce="`
if strings.Index(rq.Header.Get("Authorization"), ex) != 0 {
t.Error("got wrong Authorization header: " + rq.Header.Get("Authorization"))
}
}
func TestDigestAuthVerify(t *testing.T) {
a := &DigestAuth{user: "user", pw: "password", digestParts: make(map[string]string, 0)}
// Nominal test: 200 OK response
rs := &http.Response{
Status: "200 OK",
StatusCode: http.StatusOK,
}
redo, err := a.Verify(nil, rs, "/")
if err != nil {
t.Errorf("got error: %v, want nil", err)
}
if redo {
t.Errorf("got redo: %t, want false", redo)
}
// Digest expiration test: 401 Unauthorized response with stale directive in WWW-Authenticate header
rs = &http.Response{
Status: "401 Unauthorized",
StatusCode: http.StatusUnauthorized,
Header: http.Header{
"Www-Authenticate": []string{"Digest realm=\"webdav\", nonce=\"YVvALpkdBgA=931bbf2b6fa9dda227361dba38a735f005fd9f97\", algorithm=MD5, qop=\"auth\", stale=true"},
},
}
redo, err = a.Verify(nil, rs, "/")
if !errors.Is(err, ErrAuthChanged) {
t.Errorf("got error: %v, want ErrAuthChanged", err)
}
if !redo {
t.Errorf("got redo: %t, want true", redo)
}
}