Skip to content

Commit be3a965

Browse files
committed
[bugfix] Fix conflict between put and setup document paths used in test
1 parent 46eff0b commit be3a965

File tree

1 file changed

+37
-26
lines changed

1 file changed

+37
-26
lines changed

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

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ public class RESTServiceTest {
213213
private static String credentials;
214214
private static String badCredentials;
215215

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+
216225
private static String getServerUri() {
217226
return "http://localhost:" + existWebServer.getPort() + "/rest";
218227
}
@@ -306,15 +315,17 @@ public static void setup() throws PermissionDeniedException, IOException, Trigge
306315
credentials = Base64.encodeBase64String("admin:".getBytes(UTF_8));
307316
badCredentials = Base64.encodeBase64String("johndoe:this pw should fail".getBytes(UTF_8));
308317

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-
313318
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
314319
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
315320
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);
318329
broker.saveCollection(transaction, col);
319330
}
320331

@@ -1132,12 +1143,12 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
11321143
}
11331144
}
11341145

1135-
//test rest server ability to handle encoded characters
1146+
// test rest server ability to handle encoded characters
11361147
// all the tests with EncodedPath in function declaration aim to test rest server ability to handle special characters
11371148
@Test
11381149
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);
11411152
try {
11421153
connect.setRequestMethod("GET");
11431154
connect.connect();
@@ -1151,19 +1162,19 @@ public void doGetEncodedPath() throws IOException {
11511162
}
11521163
assertEquals("Server returned content type " + contentType, "application/xml", contentType);
11531164

1154-
String response = readResponse(connect.getInputStream());
1165+
final String response = readResponse(connect.getInputStream());
11551166

11561167
//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);
11581169
} finally {
11591170
connect.disconnect();
11601171
}
11611172
}
11621173

11631174
@Test
11641175
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);
11671178
try {
11681179
connect.setRequestMethod("GET");
11691180
connect.connect();
@@ -1177,10 +1188,10 @@ public void doHeadEncodedPath() throws IOException {
11771188

11781189
@Test
11791190
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/>";
11841195
try {
11851196
connect.setRequestProperty("Authorization", "Basic " + credentials);
11861197
connect.setRequestMethod("PUT");
@@ -1201,10 +1212,10 @@ public void doPutEncodedPath() throws IOException {
12011212
final int res_code = getConnect.getResponseCode();
12021213
assertEquals("Server returned response code " + res_code, HttpStatus.OK_200, res_code);
12031214

1204-
String response = readResponse(getConnect.getInputStream());
1215+
final String response = readResponse(getConnect.getInputStream());
12051216

12061217
//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);
12081219

12091220
} finally {
12101221
connect.disconnect();
@@ -1214,10 +1225,10 @@ public void doPutEncodedPath() throws IOException {
12141225

12151226
@Test
12161227
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);
12191230

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" +
12211232
" <text>\n" +
12221233
" //foo\n" +
12231234
" </text>\n" +
@@ -1235,7 +1246,7 @@ public void doPostEncodedPath() throws IOException {
12351246
final int r = connect.getResponseCode();
12361247
assertEquals("doPut: Server returned response code " + r, HttpStatus.OK_200, r);
12371248

1238-
String response = readResponse(connect.getInputStream());
1249+
final String response = readResponse(connect.getInputStream());
12391250

12401251
//readResponse is appending \r\n to each line that's why its added the expected content
12411252
assertTrue("Server returned " + response,response.contains("exist:hits=\"1\""));
@@ -1247,9 +1258,9 @@ public void doPostEncodedPath() throws IOException {
12471258

12481259
@Test
12491260
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);
12531264

12541265
try {
12551266
connect.setRequestProperty("Authorization", "Basic " + credentials);

0 commit comments

Comments
 (0)