| 1 |
// Copyright 2018 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package auth |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/http" |
| 9 |
"reflect" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func TestParseUserAuth(t *testing.T) { |
| 14 |
data := `https://example.com |
| 15 |
|
| 16 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 17 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 18 |
|
| 19 |
https://hello.com |
| 20 |
|
| 21 |
Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc |
| 22 |
Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs |
| 23 |
Data: Test567 |
| 24 |
|
| 25 |
` |
| 26 |
// Build the expected header |
| 27 |
header1 := http.Header{ |
| 28 |
"Authorization": []string{ |
| 29 |
"Basic YWxhZGRpbjpvcGVuc2VzYW1l", |
| 30 |
"Basic jpvcGVuc2VzYW1lYWxhZGRpb", |
| 31 |
}, |
| 32 |
} |
| 33 |
header2 := http.Header{ |
| 34 |
"Authorization": []string{ |
| 35 |
"Basic GVuc2VzYW1lYWxhZGRpbjpvc", |
| 36 |
"Basic 1lYWxhZGRplW1lYWxhZGRpbs", |
| 37 |
}, |
| 38 |
"Data": []string{ |
| 39 |
"Test567", |
| 40 |
}, |
| 41 |
} |
| 42 |
credentials, err := parseUserAuth(data) |
| 43 |
if err != nil { |
| 44 |
t.Errorf("parseUserAuth(%s): %v", data, err) |
| 45 |
} |
| 46 |
gotHeader, ok := credentials["example.com"] |
| 47 |
if !ok || !reflect.DeepEqual(gotHeader, header1) { |
| 48 |
t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header1) |
| 49 |
} |
| 50 |
gotHeader, ok = credentials["hello.com"] |
| 51 |
if !ok || !reflect.DeepEqual(gotHeader, header2) { |
| 52 |
t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header2) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
func TestParseUserAuthInvalid(t *testing.T) { |
| 57 |
testCases := []string{ |
| 58 |
// Missing new line after url. |
| 59 |
`https://example.com |
| 60 |
Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc |
| 61 |
|
| 62 |
`, |
| 63 |
// Missing url. |
| 64 |
`Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc |
| 65 |
|
| 66 |
`, |
| 67 |
// Missing url. |
| 68 |
`https://example.com |
| 69 |
|
| 70 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 71 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 72 |
|
| 73 |
Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc |
| 74 |
Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs |
| 75 |
Data: Test567 |
| 76 |
|
| 77 |
`, |
| 78 |
// Wrong order. |
| 79 |
`Authorization: Basic AVuc2VzYW1lYWxhZGRpbjpvc |
| 80 |
|
| 81 |
https://example.com |
| 82 |
|
| 83 |
`, |
| 84 |
// Missing new lines after URL. |
| 85 |
`https://example.com |
| 86 |
`, |
| 87 |
// Missing new line after empty header. |
| 88 |
`https://example.com |
| 89 |
|
| 90 |
`, |
| 91 |
// Missing new line between blocks. |
| 92 |
`https://example.com |
| 93 |
|
| 94 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 95 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 96 |
https://hello.com |
| 97 |
|
| 98 |
Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc |
| 99 |
Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs |
| 100 |
Data: Test567 |
| 101 |
|
| 102 |
`, |
| 103 |
// Continuation in URL line |
| 104 |
`https://example.com/ |
| 105 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 106 |
`, |
| 107 |
|
| 108 |
// Continuation in header line |
| 109 |
`https://example.com |
| 110 |
|
| 111 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 112 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 113 |
`, |
| 114 |
|
| 115 |
// Continuation in multiple header lines |
| 116 |
`https://example.com |
| 117 |
|
| 118 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 119 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 120 |
Authorization: Basic dGhpc2lzYWxvbmdzdHJpbmc= |
| 121 |
`, |
| 122 |
|
| 123 |
// Continuation with mixed spacing |
| 124 |
`https://example.com |
| 125 |
|
| 126 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 127 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 128 |
`, |
| 129 |
|
| 130 |
// Continuation with tab character |
| 131 |
`https://example.com |
| 132 |
|
| 133 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 134 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 135 |
`, |
| 136 |
// Continuation at the start of a block |
| 137 |
` https://example.com |
| 138 |
|
| 139 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 140 |
`, |
| 141 |
|
| 142 |
// Continuation after a blank line |
| 143 |
`https://example.com |
| 144 |
|
| 145 |
|
| 146 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 147 |
`, |
| 148 |
} |
| 149 |
for _, tc := range testCases { |
| 150 |
if credentials, err := parseUserAuth(tc); err == nil { |
| 151 |
t.Errorf("parseUserAuth(%s) should have failed, but got: %v", tc, credentials) |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
func TestParseUserAuthDuplicated(t *testing.T) { |
| 157 |
data := `https://example.com |
| 158 |
|
| 159 |
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l |
| 160 |
Authorization: Basic jpvcGVuc2VzYW1lYWxhZGRpb |
| 161 |
|
| 162 |
https://example.com |
| 163 |
|
| 164 |
Authorization: Basic GVuc2VzYW1lYWxhZGRpbjpvc |
| 165 |
Authorization: Basic 1lYWxhZGRplW1lYWxhZGRpbs |
| 166 |
Data: Test567 |
| 167 |
|
| 168 |
` |
| 169 |
// Build the expected header |
| 170 |
header := http.Header{ |
| 171 |
"Authorization": []string{ |
| 172 |
"Basic GVuc2VzYW1lYWxhZGRpbjpvc", |
| 173 |
"Basic 1lYWxhZGRplW1lYWxhZGRpbs", |
| 174 |
}, |
| 175 |
"Data": []string{ |
| 176 |
"Test567", |
| 177 |
}, |
| 178 |
} |
| 179 |
credentials, err := parseUserAuth(data) |
| 180 |
if err != nil { |
| 181 |
t.Errorf("parseUserAuth(%s): %v", data, err) |
| 182 |
} |
| 183 |
gotHeader, ok := credentials["example.com"] |
| 184 |
if !ok || !reflect.DeepEqual(gotHeader, header) { |
| 185 |
t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header) |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
func TestParseUserAuthEmptyHeader(t *testing.T) { |
| 190 |
data := "https://example.com\n\n\n" |
| 191 |
// Build the expected header |
| 192 |
header := http.Header{} |
| 193 |
credentials, err := parseUserAuth(data) |
| 194 |
if err != nil { |
| 195 |
t.Errorf("parseUserAuth(%s): %v", data, err) |
| 196 |
} |
| 197 |
gotHeader, ok := credentials["example.com"] |
| 198 |
if !ok || !reflect.DeepEqual(gotHeader, header) { |
| 199 |
t.Errorf("parseUserAuth(%s):\nhave %q\nwant %q", data, gotHeader, header) |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
func TestParseUserAuthEmpty(t *testing.T) { |
| 204 |
data := `` |
| 205 |
// Build the expected header |
| 206 |
credentials, err := parseUserAuth(data) |
| 207 |
if err != nil { |
| 208 |
t.Errorf("parseUserAuth(%s) should have succeeded", data) |
| 209 |
} |
| 210 |
if credentials == nil { |
| 211 |
t.Errorf("parseUserAuth(%s) should have returned a non-nil credential map, but got %v", data, credentials) |
| 212 |
} |
| 213 |
} |