Skip to content

Commit

Permalink
[bugfix] Fix conflict between put and setup document paths used in test
Browse files Browse the repository at this point in the history
  • Loading branch information
adamretter committed Sep 28, 2022
1 parent b14363c commit fbddd3e
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions exist-core/src/test/java/org/exist/http/RESTServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,15 @@ public class RESTServiceTest {
private static String credentials;
private static String badCredentials;

private static final String TEST_ENCODED_XML_DOC_CONTENT = "<foo/>";
private static final String ENCODED_NAME = "AéB";
private static final XmldbURI GET_METHOD_ENCODED_COLLECTION_URI = XmldbURI.ROOT_COLLECTION_URI.append("test-get-method-encoded").append(ENCODED_NAME);
private static final XmldbURI GET_METHOD_ENCODED_DOC_URI = GET_METHOD_ENCODED_COLLECTION_URI.append(ENCODED_NAME + ".xml");
private static final XmldbURI PUT_METHOD_ENCODED_COLLECTION_URI = XmldbURI.ROOT_COLLECTION_URI.append("test-put-method-encoded").append(ENCODED_NAME);
private static final XmldbURI PUT_METHOD_ENCODED_DOC_URI = PUT_METHOD_ENCODED_COLLECTION_URI.append(ENCODED_NAME + ".xml");
private static final XmldbURI DELETE_METHOD_ENCODED_COLLECTION_URI = XmldbURI.ROOT_COLLECTION_URI.append("test-delete-method-encoded").append(ENCODED_NAME);
private static final XmldbURI DELETE_METHOD_ENCODED_DOC_URI = DELETE_METHOD_ENCODED_COLLECTION_URI.append(ENCODED_NAME + ".xml");

private static String getServerUri() {
return "http://localhost:" + existWebServer.getPort() + "/rest";
}
Expand Down Expand Up @@ -276,15 +285,17 @@ public static void setup() throws PermissionDeniedException, IOException, Trigge
credentials = Base64.encodeBase64String("admin:".getBytes(UTF_8));
badCredentials = Base64.encodeBase64String("johndoe:this pw should fail".getBytes(UTF_8));

final XmldbURI TEST_XML_DOC_URI = XmldbURI.create("AéB.xml");
final XmldbURI TEST_COLLECTION_URI = XmldbURI.create("/db/AéB");
final String TEST_XML_DOC = "<foo/>";

final BrokerPool pool = existEmbeddedServer.getBrokerPool();
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
try (final Collection col = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI)) {
broker.storeDocument(transaction, TEST_XML_DOC_URI, new StringInputSource(TEST_XML_DOC), MimeType.XML_TYPE, col);

try (final Collection col = broker.getOrCreateCollection(transaction, GET_METHOD_ENCODED_COLLECTION_URI)) {
broker.storeDocument(transaction, GET_METHOD_ENCODED_DOC_URI.lastSegment(), new StringInputSource(TEST_ENCODED_XML_DOC_CONTENT), MimeType.XML_TYPE, col);
broker.saveCollection(transaction, col);
}

try (final Collection col = broker.getOrCreateCollection(transaction, DELETE_METHOD_ENCODED_COLLECTION_URI)) {
broker.storeDocument(transaction, DELETE_METHOD_ENCODED_DOC_URI.lastSegment(), new StringInputSource(TEST_ENCODED_XML_DOC_CONTENT), MimeType.XML_TYPE, col);
broker.saveCollection(transaction, col);
}

Expand Down Expand Up @@ -1096,12 +1107,12 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
}
}

//test rest server ability to handle encoded characters
// test rest server ability to handle encoded characters
// all the tests with EncodedPath in function declaration aim to test rest server ability to handle special characters
@Test
public void doGetEncodedPath() throws IOException {
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
final HttpURLConnection connect = getConnection(DOC_URI);
final String docUri = getServerUri() + GET_METHOD_ENCODED_DOC_URI.getCollectionPath();
final HttpURLConnection connect = getConnection(docUri);
try {
connect.setRequestMethod("GET");
connect.connect();
Expand All @@ -1115,19 +1126,19 @@ public void doGetEncodedPath() throws IOException {
}
assertEquals("Server returned content type " + contentType, "application/xml", contentType);

String response = readResponse(connect.getInputStream());
final String response = readResponse(connect.getInputStream());

//readResponse is appending \r\n to each line that's why its added the expected content
assertEquals("Server returned document content " + response,"<foobar/>\r\n",response);
assertEquals("Server returned document content " + response,TEST_ENCODED_XML_DOC_CONTENT + "\r\n",response);
} finally {
connect.disconnect();
}
}

@Test
public void doHeadEncodedPath() throws IOException {
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
final HttpURLConnection connect = getConnection(DOC_URI);
final String docUri = getServerUri() + GET_METHOD_ENCODED_DOC_URI.getCollectionPath();
final HttpURLConnection connect = getConnection(docUri);
try {
connect.setRequestMethod("GET");
connect.connect();
Expand All @@ -1141,10 +1152,10 @@ public void doHeadEncodedPath() throws IOException {

@Test
public void doPutEncodedPath() throws IOException {
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
final HttpURLConnection connect = getConnection(DOC_URI);
final HttpURLConnection getConnect = getConnection(DOC_URI);
String data = "<foobar/>";
final String docUri = getServerUri() + PUT_METHOD_ENCODED_DOC_URI.getCollectionPath();
final HttpURLConnection connect = getConnection(docUri);
final HttpURLConnection getConnect = getConnection(docUri);
final String data = "<foobar/>";
try {
connect.setRequestProperty("Authorization", "Basic " + credentials);
connect.setRequestMethod("PUT");
Expand All @@ -1165,10 +1176,10 @@ public void doPutEncodedPath() throws IOException {
final int res_code = getConnect.getResponseCode();
assertEquals("Server returned response code " + res_code, HttpStatus.OK_200, res_code);

String response = readResponse(getConnect.getInputStream());
final String response = readResponse(getConnect.getInputStream());

//readResponse is appending \r\n to each line that's why its added the expected content
assertEquals("Server returned document content " + response,"<foobar/>\r\n",response);
assertEquals("Server returned document content " + response,data + "\r\n",response);

} finally {
connect.disconnect();
Expand All @@ -1178,10 +1189,10 @@ public void doPutEncodedPath() throws IOException {

@Test
public void doPostEncodedPath() throws IOException {
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
final HttpURLConnection connect = getConnection(DOC_URI);
final String docUri = getServerUri() + GET_METHOD_ENCODED_COLLECTION_URI.getCollectionPath();
final HttpURLConnection connect = getConnection(docUri);

String data = "<query xmlns=\"http://exist.sourceforge.net/NS/exist\">\n" +
final String data = "<query xmlns=\"http://exist.sourceforge.net/NS/exist\">\n" +
" <text>\n" +
" //foo\n" +
" </text>\n" +
Expand All @@ -1199,7 +1210,7 @@ public void doPostEncodedPath() throws IOException {
final int r = connect.getResponseCode();
assertEquals("doPut: Server returned response code " + r, HttpStatus.OK_200, r);

String response = readResponse(connect.getInputStream());
final String response = readResponse(connect.getInputStream());

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

@Test
public void doDeleteEncodedPath() throws IOException {
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
final HttpURLConnection connect = getConnection(DOC_URI);
final HttpURLConnection getConnect = getConnection(DOC_URI);
final String docUri = getServerUri() + DELETE_METHOD_ENCODED_DOC_URI.getCollectionPath();
final HttpURLConnection connect = getConnection(docUri);
final HttpURLConnection getConnect = getConnection(docUri);

try {
connect.setRequestProperty("Authorization", "Basic " + credentials);
Expand Down

0 comments on commit fbddd3e

Please sign in to comment.