Skip to content

Commit b4cbad3

Browse files
committed
add HEAD,PUT,DELETE,POST test for the rest sever using special charchets in the file name
1 parent 430976a commit b4cbad3

File tree

1 file changed

+116
-7
lines changed

1 file changed

+116
-7
lines changed

exist-core/src/test/java/org/exist/http/RESTServiceTest.java

Lines changed: 116 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,7 @@
2121
*/
2222
package org.exist.http;
2323

24-
import java.io.BufferedReader;
25-
import java.io.IOException;
26-
import java.io.InputStream;
27-
import java.io.InputStreamReader;
28-
import java.io.OutputStreamWriter;
29-
import java.io.Writer;
30-
import java.io.StringReader;
24+
import java.io.*;
3125
import java.net.HttpURLConnection;
3226
import java.net.URL;
3327
import java.net.URLEncoder;
@@ -1072,6 +1066,7 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
10721066
}
10731067

10741068
//test rest server ability to handle encoded characters
1069+
// all the tests with EncodedPath in function declaration aim to test rest server ability to handle special characters
10751070
@Test
10761071
public void doGetEncodedPath() throws IOException {
10771072
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
@@ -1098,6 +1093,120 @@ public void doGetEncodedPath() throws IOException {
10981093
}
10991094
}
11001095

1096+
@Test
1097+
public void doHeadEncodedPath() throws IOException {
1098+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1099+
final HttpURLConnection connect = getConnection(DOC_URI);
1100+
try {
1101+
connect.setRequestMethod("GET");
1102+
connect.connect();
1103+
1104+
final int r = connect.getResponseCode();
1105+
assertEquals("Server returned response code " + r, HttpStatus.OK_200, r);
1106+
} finally {
1107+
connect.disconnect();
1108+
}
1109+
}
1110+
1111+
@Test
1112+
public void doPutEncodedPath() throws IOException {
1113+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1114+
final HttpURLConnection connect = getConnection(DOC_URI);
1115+
final HttpURLConnection getConnect = getConnection(DOC_URI);
1116+
String data = "<foobar/>";
1117+
try {
1118+
connect.setRequestProperty("Authorization", "Basic " + credentials);
1119+
connect.setRequestMethod("PUT");
1120+
connect.setDoOutput(true);
1121+
connect.setRequestProperty("ContentType", "application/xml");
1122+
try (final Writer writer = new OutputStreamWriter(connect.getOutputStream(), UTF_8)) {
1123+
writer.write(data);
1124+
}
1125+
1126+
connect.connect();
1127+
final int r = connect.getResponseCode();
1128+
assertEquals("doPut: Server returned response code " + r, HttpStatus.CREATED_201, r);
1129+
1130+
// assert file content updated
1131+
getConnect.setRequestMethod("GET");
1132+
getConnect.connect();
1133+
1134+
final int res_code = getConnect.getResponseCode();
1135+
assertEquals("Server returned response code " + res_code, HttpStatus.OK_200, res_code);
1136+
1137+
String response = readResponse(getConnect.getInputStream());
1138+
1139+
//readResponse is appending \r\n to each line that's why its added the expected content
1140+
assertEquals("Server returned document content " + response,"<foobar/>\r\n",response);
1141+
1142+
} finally {
1143+
connect.disconnect();
1144+
getConnect.disconnect();
1145+
}
1146+
}
1147+
1148+
@Test
1149+
public void doPostEncodedPath() throws IOException {
1150+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1151+
final HttpURLConnection connect = getConnection(DOC_URI);
1152+
1153+
String data = "<query xmlns=\"http://exist.sourceforge.net/NS/exist\">\n" +
1154+
" <text>\n" +
1155+
" //foo\n" +
1156+
" </text>\n" +
1157+
"</query>";
1158+
try {
1159+
connect.setRequestProperty("Authorization", "Basic " + credentials);
1160+
connect.setRequestMethod("POST");
1161+
connect.setDoOutput(true);
1162+
connect.setRequestProperty("Content-Type", "application/xml");
1163+
try (final Writer writer = new OutputStreamWriter(connect.getOutputStream(), UTF_8)) {
1164+
writer.write(data);
1165+
}
1166+
1167+
connect.connect();
1168+
final int r = connect.getResponseCode();
1169+
assertEquals("doPut: Server returned response code " + r, HttpStatus.OK_200, r);
1170+
1171+
String response = readResponse(connect.getInputStream());
1172+
1173+
//readResponse is appending \r\n to each line that's why its added the expected content
1174+
assertTrue("Server returned " + response,response.contains("exist:hits=\"1\""));
1175+
1176+
} finally {
1177+
connect.disconnect();
1178+
}
1179+
}
1180+
1181+
@Test
1182+
public void doDeleteEncodedPath() throws IOException {
1183+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1184+
final HttpURLConnection connect = getConnection(DOC_URI);
1185+
final HttpURLConnection getConnect = getConnection(DOC_URI);
1186+
1187+
try {
1188+
connect.setRequestProperty("Authorization", "Basic " + credentials);
1189+
connect.setRequestMethod("DELETE");
1190+
connect.setDoOutput(true);
1191+
1192+
connect.connect();
1193+
final int r = connect.getResponseCode();
1194+
assertEquals("doPut: Server returned response code " + r, HttpStatus.OK_200, r);
1195+
1196+
// assert file content updated
1197+
getConnect.setRequestMethod("GET");
1198+
getConnect.connect();
1199+
1200+
1201+
final int res_code = getConnect.getResponseCode();
1202+
assertEquals("Server returned response code " + res_code, HttpStatus.NOT_FOUND_404, res_code);
1203+
1204+
}finally {
1205+
connect.disconnect();
1206+
getConnect.disconnect();
1207+
}
1208+
}
1209+
11011210
private void chmod(final String resourcePath, final String mode) throws IOException {
11021211
final String uri = getCollectionUri() +"?_query=" + URLEncoder.encode(
11031212
"sm:chmod(xs:anyURI('" + resourcePath + "'), '" + mode + "')",

0 commit comments

Comments
 (0)