Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional webdav tests for filenames that should be url encoded. #24

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ public class AbstractWebDAVTest extends TestCase
* Root webdav view.
*/
public static final String ROOT = "http://localhost:8080/xwiki/webdav";

/** Build a correct escaped URI from the URI constructor */

/** URI scheme */
public static final String URI_SCHEME = "http";

/** URI host */
public static final String URI_HOST = "localhost";

/** URI port */
public static final int URI_PORT = 8080;

/** URI webdav root */
public static final String URI_WEBDAV_ROOT = "/xwiki/webdav";

/** location of the spaces view (uri construction) */
public static final String PATH_SPACES_VIEW = "/spaces";

/**
* location of the home view.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,62 @@ public void testUpdatePageWikiContent()
*/
public void testMakingAttachment()
{
String spaceUrl = SPACES + "/TestSpace";
String pageUrl = spaceUrl + "/TestPage";
String attachmentUrl = pageUrl + "/attachment.txt";
testMakingAttachmentFile("attachment.txt","normal text file");
}

/**
* Test making attachments with unsafe characters in filename.
* These characters should also always be encoded.
*/
public void testMakingAttachmentsUnsafeChars()
{
testMakingAttachmentFile("my attach.txt","space");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you replace the catch (URIException e) { fail(...) } with a throw RuntimeException("", e); you'll notice Commons HttpClient is throwing a URI exception apparently before making the call because it seems to want the request to be URI encoded before hand.

java.lang.RuntimeException: org.apache.commons.httpclient.URIException: escaped absolute path not valid
at org.xwiki.test.webdav.DefaultWebDAVTest.testMakingAttachmentFile(DefaultWebDAVTest.java:275)
at org.xwiki.test.webdav.DefaultWebDAVTest.testMakingAttachmentsUnsafeChars(DefaultWebDAVTest.java:227)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)

testMakingAttachmentFile("[bracket].txt","brackets");
testMakingAttachmentFile("{brace}.txt","braces");
testMakingAttachmentFile("^caret.txt","caret");
testMakingAttachmentFile("#pound.txt","pound");
testMakingAttachmentFile("%percent.txt","percent");
testMakingAttachmentFile("plus+plus.txt","plus");
}

/**
* Properly escape the given url path, using RFC 2396.
* This will properly convert encode, and quote illegal characters correctly.
*/
protected String getEscapedUrl(String pagePath){
String escapedUrl = "";
try{
java.net.URI uri = new java.net.URI(
URI_SCHEME, //scheme
null, //no user info
URI_HOST, //host
URI_PORT, //port
pagePath, //path
null, //no query
null); //no #fragment

escapedUrl = uri.toASCIIString();
}catch(java.net.URISyntaxException ue){
fail("Could not escape url: '"+pagePath+"' "+ue.getMessage());
}

return escapedUrl;
}

/**
* Test making attachment with filename given under /TestSpace/TestPage
*/
protected void testMakingAttachmentFile(String filename, String testing)
{
String spaceUrl = getEscapedUrl(
URI_WEBDAV_ROOT + PATH_SPACES_VIEW + "/TestSpace");

String pageUrl = getEscapedUrl(
URI_WEBDAV_ROOT + PATH_SPACES_VIEW + "/TestSpace/TestPage");

String attachmentUrl = getEscapedUrl(
URI_WEBDAV_ROOT + PATH_SPACES_VIEW + "/TestSpace/TestPage/" + filename);

String attachmentContent = "Attachment Content";
DeleteMethod deleteMethod = new DeleteMethod();
deleteMethod.setDoAuthentication(true);
Expand Down Expand Up @@ -250,9 +303,9 @@ public void testMakingAttachment()
deleteMethod.setPath(spaceUrl);
assertEquals(DavServletResponse.SC_NO_CONTENT, getHttpClient().executeMethod(deleteMethod));
} catch (HttpException ex) {
fail(ex.getMessage());
fail("Failed '"+testing+"' attachment: "+ex.getMessage());
} catch (IOException ex) {
fail(ex.getMessage());
fail("Failed '"+testing+"' attachment: "+ex.getMessage());
}
}
}
}