Search in sources :

Example 1 with RequestOriginParser

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);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) RequestOriginParser(com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser) Test(org.junit.Test)

Example 2 with RequestOriginParser

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);
}
Also used : Response(io.restassured.response.Response) ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) RequestOriginParser(com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser) QuarkusUnitTest(io.quarkus.test.QuarkusUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

RequestOriginParser (com.alibaba.csp.sentinel.adapter.jaxrs.request.RequestOriginParser)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)2 Response (io.restassured.response.Response)2 ContainerRequestContext (javax.ws.rs.container.ContainerRequestContext)2 QuarkusUnitTest (io.quarkus.test.QuarkusUnitTest)1 Test (org.junit.Test)1 Test (org.junit.jupiter.api.Test)1