@Test // SPR-12854 public void getHandlerUnsatisfiedServletRequestParameterException() throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/params"); this.handlerMapping.getHandler(request); fail("UnsatisfiedServletRequestParameterException expected"); } catch (UnsatisfiedServletRequestParameterException ex) { List<String[]> groups = ex.getParamConditionGroups(); assertEquals(2, groups.size()); assertThat(Arrays.asList("foo=bar", "bar=baz"), containsInAnyOrder(groups.get(0)[0], groups.get(1)[0])); } }
private void testHttpMediaTypeNotAcceptableException(String url) throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("GET", url); request.addHeader("Accept", "application/json"); this.handlerMapping.getHandler(request); fail("HttpMediaTypeNotAcceptableException expected"); } catch (HttpMediaTypeNotAcceptableException ex) { assertEquals("Invalid supported producible media types", Collections.singletonList(new MediaType("application", "xml")), ex.getSupportedMediaTypes()); } }
private void testHttpMediaTypeNotSupportedException(String url) throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("PUT", url); request.setContentType("application/json"); this.handlerMapping.getHandler(request); fail("HttpMediaTypeNotSupportedException expected"); } catch (HttpMediaTypeNotSupportedException ex) { assertEquals("Invalid supported consumable media types", Collections.singletonList(new MediaType("application", "xml")), ex.getSupportedMediaTypes()); } }
@Test public void getHandlerMappedInterceptors() throws Exception { String path = "/foo"; HandlerInterceptor interceptor = new HandlerInterceptorAdapter() {}; MappedInterceptor mappedInterceptor = new MappedInterceptor(new String[] {path}, interceptor); TestRequestMappingInfoHandlerMapping mapping = new TestRequestMappingInfoHandlerMapping(); mapping.registerHandler(new TestController()); mapping.setInterceptors(new Object[] { mappedInterceptor }); mapping.setApplicationContext(new StaticWebApplicationContext()); HandlerExecutionChain chain = mapping.getHandler(new MockHttpServletRequest("GET", path)); assertNotNull(chain); assertNotNull(chain.getInterceptors()); assertSame(interceptor, chain.getInterceptors()[0]); chain = mapping.getHandler(new MockHttpServletRequest("GET", "/invalid")); assertNull(chain); }
@Test public void getHandlerTestInvalidContentType() throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/person/1"); request.setContentType("bogus"); this.handlerMapping.getHandler(request); fail("HttpMediaTypeNotSupportedException expected"); } catch (HttpMediaTypeNotSupportedException ex) { assertEquals("Invalid mime type \"bogus\": does not contain '/'", ex.getMessage()); } }
@Test(expected = HttpMediaTypeNotAcceptableException.class) // SPR-9603 public void getHandlerRequestMethodMatchFalsePositive() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/users"); request.addHeader("Accept", "application/xml"); this.handlerMapping.registerHandler(new UserController()); this.handlerMapping.getHandler(request); }
@Test public void getHandlerRequestMethodNotAllowed() throws Exception { try { MockHttpServletRequest request = new MockHttpServletRequest("POST", "/bar"); this.handlerMapping.getHandler(request); fail("HttpRequestMethodNotSupportedException expected"); } catch (HttpRequestMethodNotSupportedException ex) { assertArrayEquals("Invalid supported methods", new String[]{"GET", "HEAD"}, ex.getSupportedMethods()); } }
@Test public void getHandlerProducibleMediaTypesAttribute() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest("GET", "/content"); request.addHeader("Accept", "application/xml"); this.handlerMapping.getHandler(request); String name = HandlerMapping.PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE; assertEquals(Collections.singleton(MediaType.APPLICATION_XML), request.getAttribute(name)); request = new MockHttpServletRequest("GET", "/content"); request.addHeader("Accept", "application/json"); this.handlerMapping.getHandler(request); assertNull("Negated expression shouldn't be listed as producible type", request.getAttribute(name)); }
private HandlerMethod getHandler(MockHttpServletRequest request) throws Exception { HandlerExecutionChain chain = this.handlerMapping.getHandler(request); assertNotNull(chain); return (HandlerMethod) chain.getHandler(); }