/** * Assert the name of the controller method used to process the request * using the given Hamcrest {@link Matcher}. */ public ResultMatcher methodName(final Matcher<? super String> matcher) { return result -> { HandlerMethod handlerMethod = getHandlerMethod(result); assertThat("Handler method", handlerMethod.getMethod().getName(), matcher); }; }
/** * Assert the name of the controller method used to process the request. */ public ResultMatcher methodName(final String name) { return result -> { HandlerMethod handlerMethod = getHandlerMethod(result); assertEquals("Handler method", name, handlerMethod.getMethod().getName()); }; }
@Override public String getName(HandlerMethod handlerMethod, String mapping) { return handlerMethod.getMethod().getName(); } }
/** * Assert the controller method used to process the request. */ public ResultMatcher method(final Method method) { return result -> { HandlerMethod handlerMethod = getHandlerMethod(result); assertEquals("Handler method", method, handlerMethod.getMethod()); }; }
@ExceptionHandler(ArrayIndexOutOfBoundsException.class) public String handleWithHandlerMethod(HandlerMethod handlerMethod) { return "HandlerMethod: " + handlerMethod.getMethod().getName(); }
@ExceptionHandler(ArrayIndexOutOfBoundsException.class) public String handleWithHandlerMethod(HandlerMethod handlerMethod) { return "HandlerMethod: " + handlerMethod.getMethod().getName(); }
@Test public void getHandlerEmptyPathMatch() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", ""); HandlerMethod handlerMethod = getHandler(request); assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod()); request = new MockHttpServletRequest("GET", "/"); handlerMethod = getHandler(request); assertEquals(this.emptyMethod.getMethod(), handlerMethod.getMethod()); }
@Test public void getHandlerBestMatch() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); request.setParameter("p", "anything"); HandlerMethod handlerMethod = getHandler(request); assertEquals(this.fooParamMethod.getMethod(), handlerMethod.getMethod()); }
@Test public void directMatch() throws Exception { String key = "foo"; this.mapping.registerMapping(key, this.handler, this.method1); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get(key)); Mono<Object> result = this.mapping.getHandler(exchange); assertEquals(this.method1, ((HandlerMethod) result.block()).getMethod()); }
@Test public void getHandlerEmptyPathMatch() { Method expected = on(TestController.class).annot(requestMapping("")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); exchange = MockServerWebExchange.from(get("/")); hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void patternMatch() throws Exception { this.mapping.registerMapping("/fo*", this.handler, this.method1); this.mapping.registerMapping("/f*", this.handler, this.method2); MockServerWebExchange exchange = MockServerWebExchange.from(MockServerHttpRequest.get("/foo")); Mono<Object> result = this.mapping.getHandler(exchange); assertEquals(this.method1, ((HandlerMethod) result.block()).getMethod()); }
@Test // SPR-10825 public void placeHoldersInRequestMapping() throws Exception { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PlaceholderController()); builder.addPlaceholderValue("sys.login.ajax", "/foo"); builder.build(); RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/foo"); HandlerExecutionChain chain = hm.getHandler(request); assertNotNull(chain); assertEquals("handleWithPlaceholders", ((HandlerMethod) chain.getHandler()).getMethod().getName()); }
@Test public void getHandlerDirectMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params()).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@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 getHandlerGlobMatch() { Method expected = on(TestController.class).annot(requestMapping("/ba*").method(GET, HEAD)).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/bar")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@Test public void getHandlerBestMatch() { Method expected = on(TestController.class).annot(getMapping("/foo").params("p")).resolveMethod(); ServerWebExchange exchange = MockServerWebExchange.from(get("/foo?p=anything")); HandlerMethod hm = (HandlerMethod) this.handlerMapping.getHandler(exchange).block(); assertEquals(expected, hm.getMethod()); }
@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 // SPR-13637 public void suffixPatternMatch() throws Exception { TestStandaloneMockMvcBuilder builder = new TestStandaloneMockMvcBuilder(new PersonController()); builder.setUseSuffixPatternMatch(false); builder.build(); RequestMappingHandlerMapping hm = builder.wac.getBean(RequestMappingHandlerMapping.class); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/persons"); HandlerExecutionChain chain = hm.getHandler(request); assertNotNull(chain); assertEquals("persons", ((HandlerMethod) chain.getHandler()).getMethod().getName()); request = new MockHttpServletRequest("GET", "/persons.xml"); chain = hm.getHandler(request); assertNull(chain); }