Skip to content

Commit

Permalink
Refine error message when validating extension name (#845)
Browse files Browse the repository at this point in the history
Signed-off-by: jack.wong <jack.wong@bytedance.com>
Co-authored-by: jack.wong <jack.wong@bytedance.com>
  • Loading branch information
jackedelic and jack.wong authored Feb 21, 2023
1 parent 4cbabea commit 79ffd85
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion v2/event/extensions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func validateExtensionName(key string) error {

for _, c := range key {
if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) {
return errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z') or digits ('0' to '9') from the ASCII character set")
return errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z'), upper-case letters ('A' to 'Z') or digits ('0' to '9') from the ASCII character set")
}
}
return nil
Expand Down
40 changes: 40 additions & 0 deletions v2/event/extensions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/

package event

import (
"errors"
"testing"
)

func TestEvent_validateExtensionName(t *testing.T) {
testCases := map[string]struct {
key string
want error
}{
"empty key": {
key: "",
want: errors.New("bad key, CloudEvents attribute names MUST NOT be empty"),
},
"invalid character": {
key: "invalid_key",
want: errors.New("bad key, CloudEvents attribute names MUST consist of lower-case letters ('a' to 'z'), upper-case letters ('A' to 'Z') or digits ('0' to '9') from the ASCII character set"),
},
"valid key": {
key: "validkey123",
want: nil,
},
}

for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
err := validateExtensionName(tc.key)
if err != nil && err.Error() != tc.want.Error() || err == nil && tc.want != nil {
t.Errorf("unexpected error, expected: %v, actual: %v", tc.want, err)
}
})
}
}

0 comments on commit 79ffd85

Please sign in to comment.