Skip to content

Commit

Permalink
fix: check that contextValue starts with (#240)
Browse files Browse the repository at this point in the history
* fix: check that contextValue starts with

There had been an inversion of variable usage for one of our cases in
the matcher. This PR makes sure to compare contextValue to see if it
starts with the requested value in the constraint, instead of the other
way around.

fixes #238
  • Loading branch information
chriswk committed May 6, 2024
1 parent b2ac9ae commit 0e23677
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,18 @@ private boolean startsWith(
List<String> values, Optional<String> contextValue, boolean caseInsensitive) {
return contextValue
.map(
c ->
actualContextValue ->
values.stream()
.anyMatch(
v -> {
value -> {
if (caseInsensitive) {
return v.toLowerCase(comparisonLocale)
return actualContextValue
.toLowerCase(comparisonLocale)
.startsWith(
c.toLowerCase(
value.toLowerCase(
comparisonLocale));
} else {
return c.startsWith(v);
return actualContextValue.startsWith(value);
}
}))
.orElse(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,56 @@ public void shouldSupportInvertingStringContains() {
.build();
assertThat(strategy.isEnabled(parameters, ctx, constraintList)).isFalse();
}

@Test
public void startsWithShouldMatchCorrectlyWhenCaseSensitive() {
Strategy strategy = new DefaultStrategy();
List<Constraint> constraintList =
Collections.singletonList(
new Constraint(
"email",
Operator.STR_STARTS_WITH,
Collections.singletonList("testuser"),
false,
false));
Map<String, String> parameters = new HashMap<>();
UnleashContext ctx =
UnleashContext.builder()
.environment("dev")
.addProperty("email", "TESTUSER@getunleash.io")
.build();
assertThat(strategy.isEnabled(parameters, ctx, constraintList)).isFalse();
UnleashContext ctx2 =
UnleashContext.builder()
.environment("dev")
.addProperty("email", "testuser@getunleash.io")
.build();
assertThat(strategy.isEnabled(parameters, ctx2, constraintList)).isTrue();
}

@Test
public void startsWithShouldMatchCorrectlyWhenCaseInsensitive() {
Strategy strategy = new DefaultStrategy();
List<Constraint> constraintList =
Collections.singletonList(
new Constraint(
"email",
Operator.STR_STARTS_WITH,
Collections.singletonList("testuser"),
false,
true));
Map<String, String> parameters = new HashMap<>();
UnleashContext ctx =
UnleashContext.builder()
.environment("dev")
.addProperty("email", "TESTUSER@getunleash.io")
.build();
assertThat(strategy.isEnabled(parameters, ctx, constraintList)).isTrue();
UnleashContext ctx2 =
UnleashContext.builder()
.environment("dev")
.addProperty("email", "testuser@getunleash.io")
.build();
assertThat(strategy.isEnabled(parameters, ctx2, constraintList)).isTrue();
}
}

0 comments on commit 0e23677

Please sign in to comment.