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
|
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// Just to make sure we don't panic, return err and not
// username and pass and cover the function.
func TestParseBadHeaders(t *testing.T) {
headers := []string{
// just empty string
"",
// missing auth type
"justplainstring",
// unknown auth type
"Whut justplainstring",
// invalid base64
"Basic Shmasic",
// random encoded string
"Basic YW55IGNhcm5hbCBwbGVhcw==",
}
for _, h := range headers {
_, err := ParseAuthHeader(h)
require.Error(t, err)
}
}
// Just to make sure we don't panic, return err and not
// username and pass and cover the function.
func TestParseSuccess(t *testing.T) {
headers := []struct {
Header string
Expected BasicAuth
}{
{
"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==",
BasicAuth{Username: "Aladdin", Password: "open sesame"},
},
// Make sure that String() produces valid header
{
(&BasicAuth{Username: "Alice", Password: "Here's bob"}).String(),
BasicAuth{Username: "Alice", Password: "Here's bob"},
},
// empty pass
{
"Basic QWxhZGRpbjo=",
BasicAuth{Username: "Aladdin", Password: ""},
},
}
for _, h := range headers {
request, err := ParseAuthHeader(h.Header)
require.NoError(t, err)
assert.Equal(t, h.Expected.Username, request.Username)
assert.Equal(t, h.Expected.Password, request.Password)
}
}
|