MCPcopy
hub / github.com/perkeep/perkeep / TestBasicAuth

Function TestBasicAuth

internal/httputil/auth_test.go:130–182  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

128}
129
130func TestBasicAuth(t *testing.T) {
131 for _, d := range []struct {
132 header string
133 u, pw string
134 valid bool
135 }{
136 // Empty is invalid.
137 {},
138 {
139 // Missing password.
140 header: "Basic QWxhZGRpbg==",
141 },
142 {
143 // Malformed base64 encoding.
144 header: "Basic foo",
145 },
146 {
147 // Malformed header, no 'Basic ' prefix.
148 header: "QWxhZGRpbg==",
149 },
150 {
151 header: "Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
152 u: "Aladdin",
153 pw: "open sesame",
154 valid: true,
155 },
156 } {
157 req, err := http.NewRequest("GET", "/", nil)
158 if err != nil {
159 t.Fatal(err)
160 }
161 if d.header != "" {
162 req.Header.Set("Authorization", d.header)
163 }
164
165 u, pw, err := BasicAuth(req)
166 t.Log(d.header, err)
167 if d.valid && err != nil {
168 t.Error("Want success parse of auth header, got", err)
169 }
170 if !d.valid && err == nil {
171 t.Error("Want error parsing", d.header)
172 }
173
174 if d.u != u {
175 t.Errorf("Want user %q, got %q", d.u, u)
176 }
177
178 if d.pw != pw {
179 t.Errorf("Want password %q, got %q", d.pw, pw)
180 }
181 }
182}

Callers

nothing calls this directly

Calls 6

BasicAuthFunction · 0.85
NewRequestMethod · 0.80
FatalMethod · 0.80
LogMethod · 0.80
SetMethod · 0.65
ErrorMethod · 0.45

Tested by

no test coverage detected