CookieInterceptor extends HandlerInterceptorAdapter { public static boolean isSpider(HttpServletRequest request) { String ua = NetUtil.getUserAgent(); return ua == null || ua.matches("baiduspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler"); }  @Override boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler); static boolean isSpider(HttpServletRequest request);  }
@Test public void isSpider() { HttpServletRequest request = mock(HttpServletRequest.class); String[] ua = "baiduspider|googlebot|soso|bing|sogou|yahoo|sohu-search|yodao|YoudaoBot|robozilla|msnbot|MJ12bot|NHN|Twiceler".split("\\|"); for (int i = 0; i < ua.length; i++) { when(request.getHeader(NetUtil.USER_AGENT)).thenReturn(ua[i]); assertThat(CookieInterceptor.isSpider(request)).isTrue(); } }