@Test(expected = IllegalStateException.class) public void registerDuplicates() { this.mapping.registerMapping("foo", this.handler, this.method1); this.mapping.registerMapping("foo", this.handler, this.method2); }
@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 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)); }
@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)); }
this.mapping.registerMapping(key1, this.handler, this.method1); this.mapping.registerMapping(key2, this.handler, this.method2);
@Test public void registerMappingWithSameMethodAndTwoHandlerInstances() throws Exception { String key1 = "foo"; String key2 = "bar"; MyHandler handler1 = new MyHandler(); MyHandler handler2 = new MyHandler(); HandlerMethod handlerMethod1 = new HandlerMethod(handler1, this.method1); HandlerMethod handlerMethod2 = new HandlerMethod(handler2, this.method1); this.mapping.registerMapping(key1, handler1, this.method1); this.mapping.registerMapping(key2, handler2, this.method1); // Direct URL lookup List<String> directUrlMatches = this.mapping.getMappingRegistry().getMappingsByUrl(key1); assertNotNull(directUrlMatches); assertEquals(1, directUrlMatches.size()); assertEquals(key1, directUrlMatches.get(0)); // Mapping name lookup String name = this.method1.getName(); List<HandlerMethod> handlerMethods = this.mapping.getMappingRegistry().getHandlerMethodsByMappingName(name); assertNotNull(handlerMethods); assertEquals(2, handlerMethods.size()); assertEquals(handlerMethod1, handlerMethods.get(0)); assertEquals(handlerMethod2, handlerMethods.get(1)); // CORS lookup CorsConfiguration config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod1); assertNotNull(config); assertEquals("http://" + handler1.hashCode() + name, config.getAllowedOrigins().get(0)); config = this.mapping.getMappingRegistry().getCorsConfiguration(handlerMethod2); assertNotNull(config); assertEquals("http://" + handler2.hashCode() + name, 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)); }