Search in sources :

Example 1 with DefaultUrlCleaner

use of com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner in project Sentinel by alibaba.

the class CommonFilterTest method testUrlCleaner.

private void testUrlCleaner() throws Exception {
    final String fooPrefix = "/foo/";
    String url1 = fooPrefix + 1;
    String url2 = fooPrefix + 2;
    WebCallbackManager.setUrlCleaner(new UrlCleaner() {

        @Override
        public String clean(String originUrl) {
            if (originUrl.startsWith(fooPrefix)) {
                return "/foo/*";
            }
            return originUrl;
        }
    });
    this.mvc.perform(get(url1).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 1"));
    this.mvc.perform(get(url2).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Hello 2"));
    ClusterNode cn = ClusterBuilderSlot.getClusterNode(fooPrefix + "*");
    assertEquals(2, cn.passQps(), 0.01);
    assertNull(ClusterBuilderSlot.getClusterNode(url1));
    assertNull(ClusterBuilderSlot.getClusterNode(url2));
    WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}
Also used : ClusterNode(com.alibaba.csp.sentinel.node.ClusterNode) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)

Example 2 with DefaultUrlCleaner

use of com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner in project Sentinel by alibaba.

the class CommonFilterTest method testUrlExclusion.

private void testUrlExclusion() throws Exception {
    final String excludePrefix = "/exclude/";
    String url = excludePrefix + 1;
    WebCallbackManager.setUrlCleaner(new UrlCleaner() {

        @Override
        public String clean(String originUrl) {
            if (originUrl.startsWith(excludePrefix)) {
                return "";
            }
            return originUrl;
        }
    });
    this.mvc.perform(get(url).accept(MediaType.TEXT_PLAIN)).andExpect(status().isOk()).andExpect(content().string("Exclude 1"));
    assertNull(ClusterBuilderSlot.getClusterNode(url));
    WebCallbackManager.setUrlCleaner(new DefaultUrlCleaner());
}
Also used : DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner) UrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner) DefaultUrlCleaner(com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)

Aggregations

DefaultUrlCleaner (com.alibaba.csp.sentinel.adapter.servlet.callback.DefaultUrlCleaner)2 UrlCleaner (com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1