Skip to content

Commit

Permalink
feat: Add filtering-strict capability
Browse files Browse the repository at this point in the history
  • Loading branch information
keelerm84 committed Aug 29, 2024
1 parent 61376e4 commit a6d1e79
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 3 deletions.
6 changes: 6 additions & 0 deletions docs/service_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ and will send a `?filter=name` query parameter along with streaming/polling requ
For tests that involve filtering, the test harness will set the `filter` property of the `streaming` or `polling` configuration
object. The property will either be omitted if no filter is requested, or a non-empty string if requested.

#### Capability `"filtering-strict"`

When initially adding support for the `filtering` capability, the test harness assumed there were no character restrictions on the payload filter key. Later investigation has determined this is not the case.

If this capability is set, the test harness will send a filter key that contains a character that is not allowed in a filter key. The test service should ignore the invalid key and connect as if no filter was specified.

#### Capability `"evaluation-hooks"`

This means that the SDK has support for hooks and has the ability to register evaluation hooks.
Expand Down
18 changes: 17 additions & 1 deletion sdktests/common_tests_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sdktests

import (
"fmt"
"regexp"

"github.com/launchdarkly/go-sdk-common/v3/ldcontext"
m "github.com/launchdarkly/go-test-helpers/v2/matchers"
Expand Down Expand Up @@ -49,6 +50,18 @@ type environmentFilter struct {
o.Maybe[string]
}

// IsValid returns true if the filter key is a valid environment key.
//
// In this context, valid means either is missing, or passes the required regex.
func (p environmentFilter) IsValid() bool {
if !p.IsDefined() {
return true
}

regex := regexp.MustCompile(`^[a-zA-Z0-9][._\-a-zA-Z0-9]*$`)
return regex.MatchString(p.Value())
}

// String returns a human-readable representation of the filter key,
// suitable for test output.
func (p environmentFilter) String() string {
Expand All @@ -59,12 +72,15 @@ func (p environmentFilter) String() string {
// named "filter" with its value.
// If the filter is not present, it checks that the query parameter map *does not* contain
// a parameter named "filter".
func (p environmentFilter) Matcher() m.Matcher {
func (p environmentFilter) Matcher(strict bool) m.Matcher {
hasFilter := m.MapIncluding(
m.KV("filter", m.Equal(p.Value())),
)

if !p.IsDefined() {
hasFilter = m.Not(hasFilter)
} else if strict && !p.IsValid() {
hasFilter = m.Not(hasFilter)
}
return UniqueQueryParameters().Should(hasFilter)
}
Expand Down
2 changes: 1 addition & 1 deletion sdktests/common_tests_poll_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (c CommonPollingTests) RequestURLPath(t *ldtest.T, pathMatcher func(flagReq

request := dataSource.Endpoint().RequireConnection(t, time.Second)
m.In(t).For("request path").Assert(request.URL.Path, pathMatcher(method))
m.In(t).For("filter key").Assert(request.URL.RawQuery, filter.Matcher())
m.In(t).For("filter key").Assert(request.URL.RawQuery, filter.Matcher(t.Capabilities().Has(servicedef.CapabilityFilteringStrict)))
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion sdktests/common_tests_stream_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (c CommonStreamingTests) RequestURLPath(t *ldtest.T, pathMatcher func(flagR

request := dataSource.Endpoint().RequireConnection(t, time.Second)
m.In(t).For("request path").Assert(request.URL.Path, pathMatcher(method))
m.In(t).For("filter key").Assert(request.URL.RawQuery, filter.Matcher())
m.In(t).For("filter key").Assert(request.URL.RawQuery, filter.Matcher(t.Capabilities().Has(servicedef.CapabilityFilteringStrict)))
})
}
})
Expand Down
1 change: 1 addition & 0 deletions servicedef/service_params.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
CapabilityTags = "tags"
CapabilityUserType = "user-type"
CapabilityFiltering = "filtering"
CapabilityFilteringStrict = "filtering-strict"
CapabilityAutoEnvAttributes = "auto-env-attributes"
CapabilityMigrations = "migrations"
CapabilityEventSampling = "event-sampling"
Expand Down

0 comments on commit a6d1e79

Please sign in to comment.