-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchallenge_test.go
73 lines (68 loc) · 1.83 KB
/
challenge_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
package digest
import (
"errors"
"net/http"
"strconv"
"testing"
"gotest.tools/v3/assert"
)
func TestChallenge(t *testing.T) {
tests := []struct {
input string
challenge *Challenge
}{
{
input: `Digest realm="AXIS_ACCC8EB3494E", nonce="PNHWZB6nBQA=316099a140230c2db387fc75ee1c8ae838a750d8", stale=true, algorithm=MD5, qop="auth"`,
challenge: &Challenge{
Realm: "AXIS_ACCC8EB3494E",
Nonce: "PNHWZB6nBQA=316099a140230c2db387fc75ee1c8ae838a750d8",
Stale: true,
Algorithm: "MD5",
QOP: []string{"auth"},
},
},
{
input: `Digest realm="AXIS_ACCC8EB3494E", nonce="PNHWZB6nBQA=316099a140230c2db387fc75ee1c8ae838a750d8", algorithm=MD5-sess, qop="auth"`,
challenge: &Challenge{
Realm: "AXIS_ACCC8EB3494E",
Nonce: "PNHWZB6nBQA=316099a140230c2db387fc75ee1c8ae838a750d8",
Algorithm: "MD5-sess",
QOP: []string{"auth"},
},
},
}
for i, tt := range tests {
t.Run(strconv.Itoa(i), func(t *testing.T) {
c, err := ParseChallenge(tt.input)
assert.NilError(t, err)
assert.DeepEqual(t, tt.challenge, c)
assert.DeepEqual(t, c.String(), tt.input)
})
}
}
func TestFindChallenge(t *testing.T) {
bad1 := &Challenge{
Realm: "test",
Nonce: "kvjkdfjs",
Algorithm: "MD5-sess",
QOP: []string{"auth"},
}
good := &Challenge{
Realm: "test",
Nonce: "jgdfsijdfisd",
Algorithm: "MD5",
QOP: []string{"auth"},
}
headers := http.Header{}
headers.Add("WWW-Authenticate", bad1.String())
headers.Add("WWW-Authenticate", good.String())
chal, err := FindChallenge(headers)
assert.NilError(t, err)
assert.DeepEqual(t, chal, good)
}
func TestFindChallenge_NotFound(t *testing.T) {
_, err := FindChallenge(http.Header{})
if !errors.Is(err, ErrNoChallenge) {
t.Fatalf("not an expected error: %s, expected ErrNoChallenge", err.Error())
}
}