-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #594 from ably/integration/subscription-filters
Integration/subscription filters
- Loading branch information
Showing
5 changed files
with
219 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ablyutil | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"regexp" | ||
) | ||
|
||
// derivedChannelMatch provides the qualifyingParam and channelName from a channel regex match for derived channels | ||
type derivedChannelMatch struct { | ||
QualifierParam string | ||
ChannelName string | ||
} | ||
|
||
// This regex check is to retain existing channel params if any e.g [?rewind=1]foo to | ||
// [filter=xyz?rewind=1]foo. This is to keep channel compatibility around use of | ||
// channel params that work with derived channels. | ||
func MatchDerivedChannel(name string) (*derivedChannelMatch, error) { | ||
regex := `^(\[([^?]*)(?:(.*))\])?(.+)$` | ||
r, err := regexp.Compile(regex) | ||
if err != nil { | ||
err := errors.New("regex compilation failed") | ||
return nil, err | ||
} | ||
match := r.FindStringSubmatch(name) | ||
|
||
if len(match) == 0 || len(match) < 5 { | ||
err := errors.New("regex match failed") | ||
return nil, err | ||
} | ||
// Fail if there is already a channel qualifier, | ||
// eg [meta]foo should fail instead of just overriding with [filter=xyz]foo | ||
if len(match[2]) > 0 { | ||
err := fmt.Errorf("cannot use a derived option with a %s channel", match[2]) | ||
return nil, err | ||
} | ||
|
||
// Return match values to be added to derive channel quantifier. | ||
return &derivedChannelMatch{ | ||
QualifierParam: match[3], | ||
ChannelName: match[4], | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//go:build !integration | ||
// +build !integration | ||
|
||
package ablyutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestMatchDerivedChannel(t *testing.T) { | ||
var channels = []struct { | ||
name string | ||
input string | ||
want *derivedChannelMatch | ||
}{ | ||
{"valid with base channel name", "foo", &derivedChannelMatch{QualifierParam: "", ChannelName: "foo"}}, | ||
{"valid with base channel namespace", "foo:bar", &derivedChannelMatch{QualifierParam: "", ChannelName: "foo:bar"}}, | ||
{"valid with existing qualifying option", "[?rewind=1]foo", &derivedChannelMatch{QualifierParam: "?rewind=1", ChannelName: "foo"}}, | ||
{"valid with existing qualifying option with channel namespace", "[?rewind=1]foo:bar", &derivedChannelMatch{QualifierParam: "?rewind=1", ChannelName: "foo:bar"}}, | ||
{"fail with invalid param with channel namespace", "[param:invalid]foo:bar", nil}, | ||
{"fail with wrong channel option param", "[param=1]foo", nil}, | ||
{"fail with invalid qualifying option", "[meta]foo", nil}, | ||
{"fail with invalid regex match", "[failed-match]foo", nil}, | ||
} | ||
|
||
for _, tt := range channels { | ||
t.Run(tt.name, func(t *testing.T) { | ||
match, err := MatchDerivedChannel(tt.input) | ||
if err != nil { | ||
assert.Error(t, err, "An error is expected for the regex match") | ||
} | ||
assert.Equal(t, tt.want, match, "invalid output received") | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters