use of com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser in project Sentinel by alibaba.
the class ProviderFilterTest method testCustomRequestOriginParser.
@Test
public void testCustomRequestOriginParser() {
String url = "/test/hello";
String resourceName = "GET:" + url;
String limitOrigin = "appB";
final String headerName = "X-APP";
configureRulesFor(resourceName, 0, limitOrigin);
SentinelJaxRsConfig.setRequestOriginParser(new RequestOriginParser() {
@Override
public String parseOrigin(ContainerRequestContext request) {
String origin = request.getHeaderString(headerName);
return origin != null ? origin : "";
}
});
Response response = given().header(headerName, "appA").get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));
Response blockedResp = given().header(headerName, "appB").get(url);
blockedResp.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
assertEquals(1, cn.blockQps(), 0.01);
}
use of com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser in project Sentinel by alibaba.
the class SentinelJaxRsQuarkusAdapterTest method testCustomRequestOriginParser.
@Test
public void testCustomRequestOriginParser() {
String url = "/test/hello";
String resourceName = "GET:" + url;
String limitOrigin = "appB";
final String headerName = "X-APP";
configureRulesFor(resourceName, 0, limitOrigin);
SentinelJaxRsConfig.setRequestOriginParser(new RequestOriginParser() {
@Override
public String parseOrigin(ContainerRequestContext request) {
String origin = request.getHeaderString(headerName);
return origin != null ? origin : "";
}
});
Response response = given().header(headerName, "appA").get(url);
response.then().statusCode(200).body(equalTo(HELLO_STR));
Response blockedResp = given().header(headerName, "appB").get(url);
blockedResp.then().statusCode(javax.ws.rs.core.Response.Status.TOO_MANY_REQUESTS.getStatusCode()).body(equalTo("Blocked by Sentinel (flow limiting)"));
ClusterNode cn = ClusterBuilderSlot.getClusterNode(resourceName);
assertNotNull(cn);
assertEquals(1, cn.passQps(), 0.01);
assertEquals(1, cn.blockQps(), 0.01);
}
Aggregations