forked from opensearch-project/security
-
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.
Signed-off-by: Craig Perkins <[email protected]>
- Loading branch information
Showing
2 changed files
with
67 additions
and
8 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
59 changes: 59 additions & 0 deletions
59
src/integrationTest/java/org/opensearch/test/framework/matcher/RestMatchers.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,59 @@ | ||
package org.opensearch.test.framework.matcher; | ||
|
||
import org.hamcrest.Description; | ||
import org.hamcrest.DiagnosingMatcher; | ||
|
||
import org.opensearch.test.framework.cluster.TestRestClient.HttpResponse; | ||
|
||
public class RestMatchers { | ||
|
||
private RestMatchers() {} | ||
|
||
public static DiagnosingMatcher<HttpResponse> isForbidden(String jsonPointer, String patternString) { | ||
return new DiagnosingMatcher<HttpResponse>() { | ||
|
||
@Override | ||
public void describeTo(Description description) { | ||
description.appendText("Response has status 403 Forbidden with a JSON response that has the value ") | ||
.appendValue(patternString) | ||
.appendText(" at ") | ||
.appendValue(jsonPointer); | ||
} | ||
|
||
@Override | ||
protected boolean matches(Object item, Description mismatchDescription) { | ||
if (!(item instanceof HttpResponse)) { | ||
mismatchDescription.appendValue(item).appendText(" is not a HttpResponse"); | ||
return false; | ||
} | ||
|
||
HttpResponse response = (HttpResponse) item; | ||
|
||
if (response.getStatusCode() != 403) { | ||
mismatchDescription.appendText("Status is not 403 Forbidden: ").appendText("\n").appendValue(item); | ||
return false; | ||
} | ||
|
||
try { | ||
String value = response.getTextFromJsonBody(jsonPointer); | ||
|
||
if (value == null) { | ||
mismatchDescription.appendText("Could not find value at " + jsonPointer).appendText("\n").appendValue(item); | ||
return false; | ||
} | ||
|
||
if (value.contains(patternString)) { | ||
return true; | ||
} else { | ||
mismatchDescription.appendText("Value at " + jsonPointer + " does not match pattern: " + patternString + "\n") | ||
.appendValue(item); | ||
return false; | ||
} | ||
} catch (Exception e) { | ||
mismatchDescription.appendText("Parsing request body failed with " + e).appendText("\n").appendValue(item); | ||
return false; | ||
} | ||
} | ||
}; | ||
} | ||
} |