@Test(expected = IllegalStateException.class) public void ambiguousMatch() throws Exception { this.mapping.registerMapping("/f?o", this.handler, this.method1); this.mapping.registerMapping("/fo?", this.handler, this.method2); this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", "/foo")); }
@Test public void getCorsConfigWithBeanNameHandler() throws Exception { String key = "foo"; String beanName = "handler1"; StaticWebApplicationContext context = new StaticWebApplicationContext(); context.registerSingleton(beanName, MyHandler.class); this.mapping.setApplicationContext(context); this.mapping.registerMapping(key, beanName, this.method1); HandlerMethod handlerMethod = this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key)); CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod); assertNotNull(config); assertEquals("http://" + beanName.hashCode() + this.method1.getName(), config.getAllowedOrigins().get(0)); }
@Test public void directMatch() throws Exception { String key = "foo"; this.mapping.registerMapping(key, this.handler, this.method1); MockHttpServletRequest request = new MockHttpServletRequest("GET", key); HandlerMethod result = this.mapping.getHandlerInternal(request); assertEquals(method1, result.getMethod()); assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)); }
@Test public void unregisterMapping() throws Exception { String key = "foo"; HandlerMethod handlerMethod = new HandlerMethod(this.handler, this.method1); this.mapping.registerMapping(key, this.handler, this.method1); assertNotNull(this.mapping.getHandlerInternal(new MockHttpServletRequest("GET", key))); this.mapping.unregisterMapping(key); assertNull(mapping.getHandlerInternal(new MockHttpServletRequest("GET", key))); assertNull(this.mapping.getMappingRegistry().getMappingsByUrl(key)); assertNull(this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(this.method1.getName())); assertNull(this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod)); }
@Test public void patternMatch() throws Exception { this.mapping.registerMapping("/fo*", this.handler, this.method1); this.mapping.registerMapping("/f*", this.handler, this.method2); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); HandlerMethod result = this.mapping.getHandlerInternal(request); assertEquals(method1, result.getMethod()); assertEquals(result, request.getAttribute(HandlerMapping.BEST_MATCHING_HANDLER_ATTRIBUTE)); }
@Override protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Exception { String lookupPath = getUrlPathHelper().getLookupPathForRequest(request); // 只接受根目录的请求 if (!path.equals(lookupPath)) { if (logger.isDebugEnabled()) { logger.debug("path not match with wxHandlerMapping, path is" + lookupPath); } return null; } if (isWxVerifyRequest(request)) { return wxVerifyMethodHandler; } if (isWxPostRequest(request)) { // WxRequest.Body wxRequestBody = wxRequestContext.get(request); WxRequest wxRequest = WxWebUtils.getWxRequestFromRequest(request); if (wxRequest == null) { wxRequest = new WxRequest(request, wxSessionManager); WxWebUtils.setWxRequestToRequest(request, wxRequest); wxRequest.setBody(wxXmlMessageConverter.read(request)); wxRequest.setButton(wxMenuManager.getMapping(wxRequest.getBody())); } final HandlerMethod handlerMethod = super.getHandlerInternal(request); return handlerMethod != null ? handlerMethod : defaultHandlerMethod; } return null; }