1
2
3
4
5 package auth
6
7 import (
8 "net/http"
9 "reflect"
10 "strings"
11 "testing"
12 )
13
14 func TestParseUserAuth(t *testing.T) {
15 data := `https://example.com
16
17 Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
18 Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb
19
20 https://hello.com
21
22 Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc
23 Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs
24 Data: Test567
25
26 `
27
28 header1 := http.Header{
29 "Authorization": []string{
30 "Basic YWxhZGRpbjpvcGVuc2VzYW1l",
31 "Basic jpvcGVuc2VzYW1lYWxhZGRpb",
32 },
33 }
34 header2 := http.Header{
35 "Authorization": []string{
36 "Basic GVuc2VzYW1lYWxhZGRpbjpvc",
37 "Basic 1lYWxhZGRplW1lYWxhZGRpbs",
38 },
39 "Data": []string{
40 "Test567",
41 },
42 }
43 credentials, err := parseUserAuth(strings.NewReader(data))
44 if err != nil {
45 t.Errorf("parseUserAuth(%s): %v", data, err)
46 }
47 gotHeader, ok := credentials["example.com"]
48 if !ok || !reflect.DeepEqual(gotHeader, header1) {
49 t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header1)
50 }
51 gotHeader, ok = credentials["hello.com"]
52 if !ok || !reflect.DeepEqual(gotHeader, header2) {
53 t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header2)
54 }
55 }
56
57 func TestParseUserAuthInvalid(t *testing.T) {
58 testCases := []string{
59
60 `https://example.com
61 Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc
62
63 `,
64
65 `Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc
66
67 `,
68
69 `https://example.com
70
71 Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
72 Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb
73
74 Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc
75 Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs
76 Data: Test567
77
78 `,
79
80 `Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc
81
82 https://example.com
83
84 `,
85
86 `https://example.com
87 `,
88
89 `https://example.com
90
91 `,
92
93 `https://example.com
94
95 Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
96 Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb
97 https://hello.com
98
99 Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc
100 Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs
101 Data: Test567
102
103 `,
104 }
105 for _, tc := range testCases {
106 if credentials, err := parseUserAuth(strings.NewReader(tc)); err == nil {
107 t.Errorf("parseUserAuth(%s) should have failed, but got: %v", tc, credentials)
108 }
109 }
110 }
111
112 func TestParseUserAuthDuplicated(t *testing.T) {
113 data := `https://example.com
114
115 Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l
116 Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb
117
118 https://example.com
119
120 Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc
121 Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs
122 Data: Test567
123
124 `
125
126 header := http.Header{
127 "Authorization": []string{
128 "Basic GVuc2VzYW1lYWxhZGRpbjpvc",
129 "Basic 1lYWxhZGRplW1lYWxhZGRpbs",
130 },
131 "Data": []string{
132 "Test567",
133 },
134 }
135 credentials, err := parseUserAuth(strings.NewReader(data))
136 if err != nil {
137 t.Errorf("parseUserAuth(%s): %v", data, err)
138 }
139 gotHeader, ok := credentials["example.com"]
140 if !ok || !reflect.DeepEqual(gotHeader, header) {
141 t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header)
142 }
143 }
144
145 func TestParseUserAuthEmptyHeader(t *testing.T) {
146 data := "https://example.com\n\n\n"
147
148 header := http.Header{}
149 credentials, err := parseUserAuth(strings.NewReader(data))
150 if err != nil {
151 t.Errorf("parseUserAuth(%s): %v", data, err)
152 }
153 gotHeader, ok := credentials["example.com"]
154 if !ok || !reflect.DeepEqual(gotHeader, header) {
155 t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header)
156 }
157 }
158
159 func TestParseUserAuthEmpty(t *testing.T) {
160 data := ``
161
162 credentials, err := parseUserAuth(strings.NewReader(data))
163 if err != nil {
164 t.Errorf("parseUserAuth(%s) should have succeeded", data)
165 }
166 if credentials == nil {
167 t.Errorf("parseUserAuth(%s) should have returned a non-nil credential map, but got %v", data, credentials)
168 }
169 }
170
View as plain text