UploadUriProvider { public static URI getTargetLocation(final HttpServletRequest request) throws URISyntaxException, FileUploadException { if (request.getParameter(PARAM_PATH) != null) { String parameter = request.getParameter(PARAM_PATH); String decode = decodeFileNamePart(parameter); String str = encodeFileNamePart(decode); return new URI(str); } else if (request.getParameter(PARAM_FOLDER) != null) { return new URI(request.getParameter(PARAM_FOLDER).replaceAll("\\s", "%20") + "/" + encodeFileName(URIUtil.decode(request.getParameter(PARAM_FILENAME)))); } else { throw new FileUploadException("Path to file was invalid."); } }  static URI getTargetLocation(final HttpServletRequest request);  }
@Test(expected = FileUploadException.class) public void requestEmpty() throws URISyntaxException, FileUploadException { final HttpServletRequest request = mock(HttpServletRequest.class); UploadUriProvider.getTargetLocation(request); }
@Test public void paramIsNotEncoded() throws URISyntaxException, FileUploadException { final HttpServletRequest request = mock(HttpServletRequest.class); doReturn("project/hello & goodbye.txt").when(request).getParameter(PARAM_PATH); final URI uri = UploadUriProvider.getTargetLocation(request); assertEquals("project/hello+%26+goodbye.txt", uri.toString()); }
@Test public void paramIsEncoded() throws URISyntaxException, FileUploadException { final HttpServletRequest request = mock(HttpServletRequest.class); doReturn("project/hello+%26+goodbye.txt").when(request).getParameter(PARAM_PATH); final URI uri = UploadUriProvider.getTargetLocation(request); assertEquals("project/hello+%26+goodbye.txt", uri.toString()); }
@Test public void fileNameIsNotEncoded() throws URISyntaxException, FileUploadException { final HttpServletRequest request = mock(HttpServletRequest.class); doReturn("otherProject").when(request).getParameter(PARAM_FOLDER); doReturn("blaa & blaa.txt").when(request).getParameter(PARAM_FILENAME); final URI uri = UploadUriProvider.getTargetLocation(request); assertEquals("otherProject/blaa+%26+blaa.txt", uri.toString()); }
@Test public void fileNameIsEncoded() throws URISyntaxException, FileUploadException { final HttpServletRequest request = mock(HttpServletRequest.class); doReturn("otherProject/subfolder").when(request).getParameter(PARAM_FOLDER); doReturn("do+%26+it.txt").when(request).getParameter(PARAM_FILENAME); final URI uri = UploadUriProvider.getTargetLocation(request); assertEquals("otherProject/subfolder/do+%26+it.txt", uri.toString()); }