@@ -213,6 +213,15 @@ public class RESTServiceTest {
213
213
private static String credentials ;
214
214
private static String badCredentials ;
215
215
216
+ private static final String TEST_ENCODED_XML_DOC_CONTENT = "<foo/>" ;
217
+ private static final String ENCODED_NAME = "AéB" ;
218
+ private static final XmldbURI GET_METHOD_ENCODED_COLLECTION_URI = XmldbURI .ROOT_COLLECTION_URI .append ("test-get-method-encoded" ).append (ENCODED_NAME );
219
+ private static final XmldbURI GET_METHOD_ENCODED_DOC_URI = GET_METHOD_ENCODED_COLLECTION_URI .append (ENCODED_NAME + ".xml" );
220
+ private static final XmldbURI PUT_METHOD_ENCODED_COLLECTION_URI = XmldbURI .ROOT_COLLECTION_URI .append ("test-put-method-encoded" ).append (ENCODED_NAME );
221
+ private static final XmldbURI PUT_METHOD_ENCODED_DOC_URI = PUT_METHOD_ENCODED_COLLECTION_URI .append (ENCODED_NAME + ".xml" );
222
+ private static final XmldbURI DELETE_METHOD_ENCODED_COLLECTION_URI = XmldbURI .ROOT_COLLECTION_URI .append ("test-delete-method-encoded" ).append (ENCODED_NAME );
223
+ private static final XmldbURI DELETE_METHOD_ENCODED_DOC_URI = DELETE_METHOD_ENCODED_COLLECTION_URI .append (ENCODED_NAME + ".xml" );
224
+
216
225
private static String getServerUri () {
217
226
return "http://localhost:" + existWebServer .getPort () + "/rest" ;
218
227
}
@@ -306,15 +315,17 @@ public static void setup() throws PermissionDeniedException, IOException, Trigge
306
315
credentials = Base64 .encodeBase64String ("admin:" .getBytes (UTF_8 ));
307
316
badCredentials = Base64 .encodeBase64String ("johndoe:this pw should fail" .getBytes (UTF_8 ));
308
317
309
- final XmldbURI TEST_XML_DOC_URI = XmldbURI .create ("AéB.xml" );
310
- final XmldbURI TEST_COLLECTION_URI = XmldbURI .create ("/db/AéB" );
311
- final String TEST_XML_DOC = "<foo/>" ;
312
-
313
318
final BrokerPool pool = existEmbeddedServer .getBrokerPool ();
314
319
try (final DBBroker broker = pool .get (Optional .of (pool .getSecurityManager ().getSystemSubject ()));
315
320
final Txn transaction = pool .getTransactionManager ().beginTransaction ()) {
316
- try (final Collection col = broker .getOrCreateCollection (transaction , TEST_COLLECTION_URI )) {
317
- broker .storeDocument (transaction , TEST_XML_DOC_URI , new StringInputSource (TEST_XML_DOC ), MimeType .XML_TYPE , col );
321
+
322
+ try (final Collection col = broker .getOrCreateCollection (transaction , GET_METHOD_ENCODED_COLLECTION_URI )) {
323
+ broker .storeDocument (transaction , GET_METHOD_ENCODED_DOC_URI .lastSegment (), new StringInputSource (TEST_ENCODED_XML_DOC_CONTENT ), MimeType .XML_TYPE , col );
324
+ broker .saveCollection (transaction , col );
325
+ }
326
+
327
+ try (final Collection col = broker .getOrCreateCollection (transaction , DELETE_METHOD_ENCODED_COLLECTION_URI )) {
328
+ broker .storeDocument (transaction , DELETE_METHOD_ENCODED_DOC_URI .lastSegment (), new StringInputSource (TEST_ENCODED_XML_DOC_CONTENT ), MimeType .XML_TYPE , col );
318
329
broker .saveCollection (transaction , col );
319
330
}
320
331
@@ -1132,12 +1143,12 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
1132
1143
}
1133
1144
}
1134
1145
1135
- //test rest server ability to handle encoded characters
1146
+ // test rest server ability to handle encoded characters
1136
1147
// all the tests with EncodedPath in function declaration aim to test rest server ability to handle special characters
1137
1148
@ Test
1138
1149
public void doGetEncodedPath () throws IOException {
1139
- String DOC_URI = getServerUri () + XmldbURI . ROOT_COLLECTION + "/AéB/AéB.xml" ;
1140
- final HttpURLConnection connect = getConnection (DOC_URI );
1150
+ final String docUri = getServerUri () + GET_METHOD_ENCODED_DOC_URI . getCollectionPath () ;
1151
+ final HttpURLConnection connect = getConnection (docUri );
1141
1152
try {
1142
1153
connect .setRequestMethod ("GET" );
1143
1154
connect .connect ();
@@ -1151,19 +1162,19 @@ public void doGetEncodedPath() throws IOException {
1151
1162
}
1152
1163
assertEquals ("Server returned content type " + contentType , "application/xml" , contentType );
1153
1164
1154
- String response = readResponse (connect .getInputStream ());
1165
+ final String response = readResponse (connect .getInputStream ());
1155
1166
1156
1167
//readResponse is appending \r\n to each line that's why its added the expected content
1157
- assertEquals ("Server returned document content " + response ,"<foobar/> \r \n " ,response );
1168
+ assertEquals ("Server returned document content " + response ,TEST_ENCODED_XML_DOC_CONTENT + " \r \n " ,response );
1158
1169
} finally {
1159
1170
connect .disconnect ();
1160
1171
}
1161
1172
}
1162
1173
1163
1174
@ Test
1164
1175
public void doHeadEncodedPath () throws IOException {
1165
- String DOC_URI = getServerUri () + XmldbURI . ROOT_COLLECTION + "/AéB/AéB.xml" ;
1166
- final HttpURLConnection connect = getConnection (DOC_URI );
1176
+ final String docUri = getServerUri () + GET_METHOD_ENCODED_DOC_URI . getCollectionPath () ;
1177
+ final HttpURLConnection connect = getConnection (docUri );
1167
1178
try {
1168
1179
connect .setRequestMethod ("GET" );
1169
1180
connect .connect ();
@@ -1177,10 +1188,10 @@ public void doHeadEncodedPath() throws IOException {
1177
1188
1178
1189
@ Test
1179
1190
public void doPutEncodedPath () throws IOException {
1180
- String DOC_URI = getServerUri () + XmldbURI . ROOT_COLLECTION + "/AéB/AéB.xml" ;
1181
- final HttpURLConnection connect = getConnection (DOC_URI );
1182
- final HttpURLConnection getConnect = getConnection (DOC_URI );
1183
- String data = "<foobar/>" ;
1191
+ final String docUri = getServerUri () + PUT_METHOD_ENCODED_DOC_URI . getCollectionPath () ;
1192
+ final HttpURLConnection connect = getConnection (docUri );
1193
+ final HttpURLConnection getConnect = getConnection (docUri );
1194
+ final String data = "<foobar/>" ;
1184
1195
try {
1185
1196
connect .setRequestProperty ("Authorization" , "Basic " + credentials );
1186
1197
connect .setRequestMethod ("PUT" );
@@ -1201,10 +1212,10 @@ public void doPutEncodedPath() throws IOException {
1201
1212
final int res_code = getConnect .getResponseCode ();
1202
1213
assertEquals ("Server returned response code " + res_code , HttpStatus .OK_200 , res_code );
1203
1214
1204
- String response = readResponse (getConnect .getInputStream ());
1215
+ final String response = readResponse (getConnect .getInputStream ());
1205
1216
1206
1217
//readResponse is appending \r\n to each line that's why its added the expected content
1207
- assertEquals ("Server returned document content " + response ,"<foobar/> \r \n " ,response );
1218
+ assertEquals ("Server returned document content " + response ,data + " \r \n " ,response );
1208
1219
1209
1220
} finally {
1210
1221
connect .disconnect ();
@@ -1214,10 +1225,10 @@ public void doPutEncodedPath() throws IOException {
1214
1225
1215
1226
@ Test
1216
1227
public void doPostEncodedPath () throws IOException {
1217
- String DOC_URI = getServerUri () + XmldbURI . ROOT_COLLECTION + "/AéB/AéB.xml" ;
1218
- final HttpURLConnection connect = getConnection (DOC_URI );
1228
+ final String docUri = getServerUri () + GET_METHOD_ENCODED_COLLECTION_URI . getCollectionPath () ;
1229
+ final HttpURLConnection connect = getConnection (docUri );
1219
1230
1220
- String data = "<query xmlns=\" http://exist.sourceforge.net/NS/exist\" >\n " +
1231
+ final String data = "<query xmlns=\" http://exist.sourceforge.net/NS/exist\" >\n " +
1221
1232
" <text>\n " +
1222
1233
" //foo\n " +
1223
1234
" </text>\n " +
@@ -1235,7 +1246,7 @@ public void doPostEncodedPath() throws IOException {
1235
1246
final int r = connect .getResponseCode ();
1236
1247
assertEquals ("doPut: Server returned response code " + r , HttpStatus .OK_200 , r );
1237
1248
1238
- String response = readResponse (connect .getInputStream ());
1249
+ final String response = readResponse (connect .getInputStream ());
1239
1250
1240
1251
//readResponse is appending \r\n to each line that's why its added the expected content
1241
1252
assertTrue ("Server returned " + response ,response .contains ("exist:hits=\" 1\" " ));
@@ -1247,9 +1258,9 @@ public void doPostEncodedPath() throws IOException {
1247
1258
1248
1259
@ Test
1249
1260
public void doDeleteEncodedPath () throws IOException {
1250
- String DOC_URI = getServerUri () + XmldbURI . ROOT_COLLECTION + "/AéB/AéB.xml" ;
1251
- final HttpURLConnection connect = getConnection (DOC_URI );
1252
- final HttpURLConnection getConnect = getConnection (DOC_URI );
1261
+ final String docUri = getServerUri () + DELETE_METHOD_ENCODED_DOC_URI . getCollectionPath () ;
1262
+ final HttpURLConnection connect = getConnection (docUri );
1263
+ final HttpURLConnection getConnect = getConnection (docUri );
1253
1264
1254
1265
try {
1255
1266
connect .setRequestProperty ("Authorization" , "Basic " + credentials );
0 commit comments