Skip to content

Commit

Permalink
test: base64 encoded password
Browse files Browse the repository at this point in the history
refs #278
  • Loading branch information
caarlos0 committed Sep 7, 2023
1 parent b240e5a commit fd0f782
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions env_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package env

import (
"encoding/base64"
"errors"
"fmt"
"net/http"
Expand Down Expand Up @@ -1895,3 +1896,28 @@ func TestParseWithOptionsOverride(t *testing.T) {
},
}}))
}

type Password []byte

func (p *Password) UnmarshalText(text []byte) error {
out, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return err
}
*p = out
return nil
}

type UsernameAndPassword struct {
Username string `env:"USER"`
Password *Password `env:"PWD"`
}

func TestBase64Password(t *testing.T) {
t.Setenv("USER", "admin")
t.Setenv("PWD", base64.StdEncoding.EncodeToString([]byte("admin123")))
var c UsernameAndPassword
isNoErr(t, Parse(&c))
isEqual(t, "admin", c.Username)
isEqual(t, "admin123", string(*c.Password))
}

0 comments on commit fd0f782

Please sign in to comment.