forked from TrueCloudLab/certificates
Add GetBool(s string) bool to URI type.
This commit is contained in:
parent
500b540406
commit
f1ef3fb351
2 changed files with 44 additions and 0 deletions
|
@ -95,6 +95,16 @@ func (u *URI) Get(key string) string {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetBool returns true if a given key has the value "true". It will returns
|
||||||
|
// false otherwise.
|
||||||
|
func (u *URI) GetBool(key string) bool {
|
||||||
|
v := u.Values.Get(key)
|
||||||
|
if v == "" {
|
||||||
|
v = u.URL.Query().Get(key)
|
||||||
|
}
|
||||||
|
return strings.EqualFold(v, "true")
|
||||||
|
}
|
||||||
|
|
||||||
// GetEncoded returns the first value in the uri with the given key, it will
|
// GetEncoded returns the first value in the uri with the given key, it will
|
||||||
// return empty nil if that field is not present or is empty. If the return
|
// return empty nil if that field is not present or is empty. If the return
|
||||||
// value is hex encoded it will decode it and return it.
|
// value is hex encoded it will decode it and return it.
|
||||||
|
|
|
@ -212,6 +212,40 @@ func TestURI_Get(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestURI_GetBool(t *testing.T) {
|
||||||
|
mustParse := func(s string) *URI {
|
||||||
|
u, err := Parse(s)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
return u
|
||||||
|
}
|
||||||
|
type args struct {
|
||||||
|
key string
|
||||||
|
}
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
uri *URI
|
||||||
|
args args
|
||||||
|
want bool
|
||||||
|
}{
|
||||||
|
{"true", mustParse("azurekms:name=foo;vault=bar;hsm=true"), args{"hsm"}, true},
|
||||||
|
{"TRUE", mustParse("azurekms:name=foo;vault=bar;hsm=TRUE"), args{"hsm"}, true},
|
||||||
|
{"tRUe query", mustParse("azurekms:name=foo;vault=bar?hsm=tRUe"), args{"hsm"}, true},
|
||||||
|
{"false", mustParse("azurekms:name=foo;vault=bar;hsm=false"), args{"hsm"}, false},
|
||||||
|
{"false query", mustParse("azurekms:name=foo;vault=bar?hsm=false"), args{"hsm"}, false},
|
||||||
|
{"empty", mustParse("azurekms:name=foo;vault=bar;hsm=?bar=true"), args{"hsm"}, false},
|
||||||
|
{"missing", mustParse("azurekms:name=foo;vault=bar"), args{"hsm"}, false},
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
if got := tt.uri.GetBool(tt.args.key); got != tt.want {
|
||||||
|
t.Errorf("URI.GetBool() = %v, want %v", got, tt.want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestURI_GetEncoded(t *testing.T) {
|
func TestURI_GetEncoded(t *testing.T) {
|
||||||
mustParse := func(s string) *URI {
|
mustParse := func(s string) *URI {
|
||||||
u, err := Parse(s)
|
u, err := Parse(s)
|
||||||
|
|
Loading…
Reference in a new issue