-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow excluding traffic by matching headers (#5)
- Loading branch information
Showing
8 changed files
with
132 additions
and
4 deletions.
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
5 changes: 5 additions & 0 deletions
5
...-api/src/main/java/com/getyourguide/openapi/validation/api/exclusions/ExcludedHeader.java
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,5 @@ | ||
package com.getyourguide.openapi.validation.api.exclusions; | ||
|
||
import java.util.regex.Pattern; | ||
|
||
public record ExcludedHeader(String headerName, Pattern headerValuePattern) { } |
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
56 changes: 56 additions & 0 deletions
56
...est/java/com/getyourguide/openapi/validation/api/selector/DefaultTrafficSelectorTest.java
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,56 @@ | ||
package com.getyourguide.openapi.validation.api.selector; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import com.getyourguide.openapi.validation.api.exclusions.ExcludedHeader; | ||
import com.getyourguide.openapi.validation.api.model.RequestMetaData; | ||
import java.net.URI; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.TreeMap; | ||
import java.util.regex.Pattern; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class DefaultTrafficSelectorTest { | ||
private final TrafficSelector trafficSelector = new DefaultTrafficSelector( | ||
1.0, | ||
null, | ||
List.of( | ||
new ExcludedHeader("User-Agent", Pattern.compile(".*(bingbot|googlebot).*", Pattern.CASE_INSENSITIVE)), | ||
new ExcludedHeader("x-is-bot", Pattern.compile("true", Pattern.CASE_INSENSITIVE)) | ||
) | ||
); | ||
|
||
@Test | ||
public void testIsExcludedByHeaderPattern() { | ||
assertHeaderIsExcluded(true, | ||
"user-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)"); | ||
assertHeaderIsExcluded(false, | ||
"User-Agent", | ||
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36"); | ||
|
||
assertHeaderIsExcluded(true, "x-is-bot", "true"); | ||
assertHeaderIsExcluded(false, "x-is-bot", "truebot"); | ||
|
||
} | ||
|
||
private void assertHeaderIsExcluded(boolean expectedExclusion, String headerName, String headerValue) { | ||
|
||
var request = new RequestMetaData( | ||
"GET", | ||
URI.create("https://api.example.com/v1/path"), | ||
toCaseInsensitiveMap(Map.of( | ||
"Content-Type", "application/json", | ||
"Content-Length", "10", | ||
headerName, headerValue | ||
)) | ||
); | ||
assertEquals(!expectedExclusion, trafficSelector.shouldRequestBeValidated(request)); | ||
} | ||
|
||
private Map<String, String> toCaseInsensitiveMap(Map<String, String> map) { | ||
var newMap = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); | ||
newMap.putAll(map); | ||
return newMap; | ||
} | ||
} |
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
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