DefaultResourceTranslator implements UrlToResourceTranslator { public ResourceMethod translate(RequestInfo info) { MutableRequest request = info.getRequest(); String resourceName = info.getRequestedUri(); logger.debug("trying to access {}", resourceName); HttpMethod method; try { method = HttpMethod.of(request); } catch (IllegalArgumentException e) { throw new MethodNotAllowedException(router.allowedMethodsFor(resourceName), request.getMethod()); } ResourceMethod resource = router.parse(resourceName, method, request); logger.debug("found resource {}", resource); return resource; } DefaultResourceTranslator(Router router); ResourceMethod translate(RequestInfo info);  }
@Test public void handlesInclude() { when(request.getAttribute(RequestInfo.INCLUDE_REQUEST_URI)).thenReturn("/url"); when(request.getMethod()).thenReturn("POST"); when(router.parse("/url", HttpMethod.POST, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(sameInstance(method))); }
@Test public void canHandleTheCorrectMethod() { when(request.getRequestURI()).thenReturn("/url"); when(request.getMethod()).thenReturn("POST"); when(router.parse("/url", HttpMethod.POST,webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void shouldAcceptCaseInsensitiveRequestMethods() { when(request.getRequestURI()).thenReturn("/url"); when(request.getMethod()).thenReturn("pOsT"); when(router.parse("/url", HttpMethod.POST,webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void shouldAcceptCaseInsensitiveGetRequestUsingThe_methodParameter() { when(request.getRequestURI()).thenReturn("/url"); when(request.getParameter("_method")).thenReturn("gEt"); when(request.getMethod()).thenReturn("POST"); when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test(expected=MethodNotAllowedException.class) public void shouldThrowExceptionWhenRequestANotKnownMethod() { when(request.getRequestURI()).thenReturn("/url"); when(request.getMethod()).thenReturn("COOK"); when(router.parse(anyString(), any(HttpMethod.class), any(MutableRequest.class))).thenReturn(method); translator.translate(info); }
@Test public void shouldOverrideTheHttpMethodByUsingThe_methodParameter() { when(request.getRequestURI()).thenReturn("/url"); when(request.getParameter("_method")).thenReturn("DELETE"); when(request.getMethod()).thenReturn("POST"); when(router.parse("/url", HttpMethod.DELETE, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleUrlIfRootContext() { when(request.getRequestURI()).thenReturn("/url"); when(request.getContextPath()).thenReturn(""); when(request.getMethod()).thenReturn("GET"); when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleUrlIfNonRootContext() { when(request.getRequestURI()).thenReturn("/custom_context/url"); when(request.getContextPath()).thenReturn("/custom_context"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/url", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleUrlIfPlainRootContext() { when(request.getRequestURI()).thenReturn("/"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleComposedUrlIfPlainRootContext() { when(request.getRequestURI()).thenReturn("/products/1"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleComposedUrlIfNonRootContext() { when(request.getRequestURI()).thenReturn("/custom_context/products/1"); when(request.getContextPath()).thenReturn("/custom_context"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }
@Test public void canHandleUrlWithAppendedJSessionID() { when(request.getRequestURI()).thenReturn( "/custom_context/products/1;jsessionid=aslfasfaslkj22234lkjsdfaklsf", "/custom_context/products/1;JSESSIONID=aslfasfaslkj22234lkjsdfaklsf", "/custom_context/products/1;jsessionID=aslfasfaslkj22234lkjsdfaklsf"); when(request.getContextPath()).thenReturn("/custom_context"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/products/1", HttpMethod.GET, webRequest)).thenReturn(method); assertThat(translator.translate(info), is(equalTo(method))); assertThat(translator.translate(info), is(equalTo(method))); assertThat(translator.translate(info), is(equalTo(method))); }
@Test public void canHandleUrlIfNonRootContextButPlainRequest() { when(request.getRequestURI()).thenReturn("/custom_context/"); when(request.getContextPath()).thenReturn("/custom_context"); when(request.getMethod()).thenReturn("GET"); when(router.parse("/", HttpMethod.GET, webRequest)).thenReturn(method); ResourceMethod resource = translator.translate(info); assertThat(resource, is(equalTo(method))); }