1
2
3
4
5 package auth
6
7 import (
8 "net/http"
9 "reflect"
10 "testing"
11 )
12
13 func TestCredentialCache(t *testing.T) {
14 testCases := []netrcLine{
15 {"api.github.com", "user", "pwd"},
16 {"test.host", "user2", "pwd2"},
17 {"oneline", "user3", "pwd3"},
18 {"hasmacro.too", "user4", "pwd4"},
19 {"hasmacro.too", "user5", "pwd5"},
20 }
21 for _, tc := range testCases {
22 want := http.Request{Header: make(http.Header)}
23 want.SetBasicAuth(tc.login, tc.password)
24 storeCredential(tc.machine, want.Header)
25 got := &http.Request{Header: make(http.Header)}
26 ok := loadCredential(got, tc.machine)
27 if !ok || !reflect.DeepEqual(got.Header, want.Header) {
28 t.Errorf("loadCredential(%q):\nhave %q\nwant %q", tc.machine, got.Header, want.Header)
29 }
30 }
31
32
33 extraCases := []netrcLine{
34 {"https://api.github.com/foo", "user", "pwd"},
35 {"https://api.github.com/foo/bar/baz", "user", "pwd"},
36 {"https://example.com/abc", "", ""},
37 {"https://example.com/?/../api.github.com/", "", ""},
38 {"https://example.com/?/../api.github.com", "", ""},
39 {"https://example.com/../api.github.com/", "", ""},
40 {"https://example.com/../api.github.com", "", ""},
41 }
42 for _, tc := range extraCases {
43 want := http.Request{Header: make(http.Header)}
44 if tc.login != "" {
45 want.SetBasicAuth(tc.login, tc.password)
46 }
47 got := &http.Request{Header: make(http.Header)}
48 loadCredential(got, tc.machine)
49 if !reflect.DeepEqual(got.Header, want.Header) {
50 t.Errorf("loadCredential(%q):\nhave %q\nwant %q", tc.machine, got.Header, want.Header)
51 }
52 }
53 }
54
55 func TestCredentialCacheDelete(t *testing.T) {
56
57 want := http.Request{Header: make(http.Header)}
58 want.SetBasicAuth("user", "pwd")
59 storeCredential("api.github.com", want.Header)
60 got := &http.Request{Header: make(http.Header)}
61 ok := loadCredential(got, "api.github.com")
62 if !ok || !reflect.DeepEqual(got.Header, want.Header) {
63 t.Errorf("parseNetrc:\nhave %q\nwant %q", got.Header, want.Header)
64 }
65
66 want = http.Request{Header: make(http.Header)}
67 storeCredential("api.github.com", want.Header)
68 got = &http.Request{Header: make(http.Header)}
69 ok = loadCredential(got, "api.github.com")
70 if ok {
71 t.Errorf("loadCredential:\nhave %q\nwant %q", got.Header, want.Header)
72 }
73 }
74
View as plain text