-
Notifications
You must be signed in to change notification settings - Fork 93
/
passportAuth_test.go
66 lines (61 loc) · 1.68 KB
/
passportAuth_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
package gowebdav
import (
"bytes"
"net/http"
"net/url"
"regexp"
"testing"
)
// testing the creation is enough as it handles the authorization during init
func TestNewPassportAuth(t *testing.T) {
user := "user"
pass := "password"
p1 := "some,comma,separated,values"
token := "from-PP='token'"
authHandler := func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
reg, err := regexp.Compile("Passport1\\.4 sign-in=" + url.QueryEscape(user) + ",pwd=" + url.QueryEscape(pass) + ",OrgVerb=GET,OrgUrl=.*," + p1)
if err != nil {
t.Error(err)
}
if reg.MatchString(r.Header.Get("Authorization")) {
w.Header().Set("Authentication-Info", token)
w.WriteHeader(200)
return
}
}
}
authsrv, _, _ := newAuthSrv(t, authHandler)
defer authsrv.Close()
dataHandler := func(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
reg, err := regexp.Compile("Passport1\\.4 " + token)
if err != nil {
t.Error(err)
}
if reg.MatchString(r.Header.Get("Authorization")) {
w.Header().Set("Set-Cookie", "Pass=port")
h.ServeHTTP(w, r)
return
}
for _, c := range r.Cookies() {
if c.Name == "Pass" && c.Value == "port" {
h.ServeHTTP(w, r)
return
}
}
w.Header().Set("Www-Authenticate", "Passport1.4 "+p1)
http.Redirect(w, r, authsrv.URL+"/", 302)
}
}
srv, _, _ := newAuthSrv(t, dataHandler)
defer srv.Close()
cli := NewClient(srv.URL, user, pass)
data, err := cli.Read("/hello.txt")
if err != nil {
t.Errorf("got error=%v; want nil", err)
}
if !bytes.Equal(data, []byte("hello gowebdav\n")) {
t.Logf("got data=%v; want=hello gowebdav", data)
}
}