Skip to content

Commit

Permalink
Merge pull request #211 from xmidt-org/denopink/feat/locator-remove-l…
Browse files Browse the repository at this point in the history
…eading_tailing-spaces

patch: ParseLocator, sanitize locator components
  • Loading branch information
schmidtw authored Oct 18, 2024
2 parents ab8273f + c6482be commit 65bfda2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
8 changes: 4 additions & 4 deletions id.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ func ParseLocator(locator string) (Locator, error) {

var l Locator

l.Scheme = strings.ToLower(match[1])
l.Authority = match[2]
l.Scheme = strings.TrimSpace(strings.ToLower(match[1]))
l.Authority = strings.TrimSpace(match[2])
if len(match) > 3 {
l.Service = strings.TrimPrefix(match[3], "/")
l.Service = strings.TrimSpace(strings.TrimPrefix(match[3], "/"))
}
if len(match) > 4 {
l.Ignored = match[4]
l.Ignored = strings.TrimSpace(match[4])
}

// If the locator is a device identifier, then we need to parse it.
Expand Down
16 changes: 16 additions & 0 deletions id_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ func TestParseLocator(t *testing.T) {
Scheme: SchemeDNS,
Authority: "foo.bar.com",
},
}, {
description: "event scheme (with spaces)",
locator: "event: targetedEvent ",
str: "event:targetedEvent",
want: Locator{
Scheme: SchemeEvent,
Authority: "targetedEvent",
},
}, {
description: "event scheme",
locator: "event:targetedEvent",
Expand Down Expand Up @@ -251,6 +259,14 @@ func TestParseLocator(t *testing.T) {
description: "invalid self scheme",
locator: "self:anything",
expectedErr: ErrorInvalidDeviceName,
}, {
description: "invalid event scheme (no authority)",
locator: "event:/anything",
expectedErr: ErrorInvalidLocator,
}, {
description: "invalid event scheme (no authority and with spaces)",
locator: "event: /anything",
expectedErr: ErrorInvalidLocator,
},
}
for _, tc := range tests {
Expand Down

0 comments on commit 65bfda2

Please sign in to comment.