Skip to content

Commit cca00a4

Browse files
committed
rename setup fuc and add doEncodedeGetTest
1 parent 50177b4 commit cca00a4

File tree

1 file changed

+63
-1
lines changed

1 file changed

+63
-1
lines changed

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,30 @@
3131
import java.net.HttpURLConnection;
3232
import java.net.URL;
3333
import java.net.URLEncoder;
34+
import java.util.Optional;
3435
import javax.xml.parsers.ParserConfigurationException;
3536

3637
import com.googlecode.junittoolbox.ParallelRunner;
3738
import org.apache.commons.codec.binary.Base64;
3839
import org.eclipse.jetty.http.HttpStatus;
40+
import org.exist.EXistException;
3941
import org.exist.Namespaces;
42+
import org.exist.collections.Collection;
43+
import org.exist.collections.triggers.TriggerException;
4044
import org.exist.dom.memtree.SAXAdapter;
45+
import org.exist.dom.persistent.LockedDocument;
46+
import org.exist.security.PermissionDeniedException;
47+
import org.exist.storage.BrokerPool;
48+
import org.exist.storage.DBBroker;
49+
import org.exist.storage.lock.Lock;
50+
import org.exist.storage.txn.Txn;
51+
import org.exist.test.ExistEmbeddedServer;
4152
import org.exist.test.ExistWebServer;
53+
import org.exist.test.TestConstants;
4254
import org.exist.util.ExistSAXParserFactory;
55+
import org.exist.util.LockException;
56+
import org.exist.util.MimeType;
57+
import org.exist.util.StringInputSource;
4358
import org.exist.xmldb.XmldbURI;
4459
import org.junit.runner.RunWith;
4560
import org.xml.sax.InputSource;
@@ -237,11 +252,31 @@ private static String getResourceUriPlus() {
237252
return getServerUri() + XmldbURI.ROOT_COLLECTION + "/test//../test/A-Za-z0-9_~!$&'()*+,;=@%20%23%25%27%2F%3F%5B%5Däöü.xml";
238253
}
239254

255+
@ClassRule
256+
public static final ExistEmbeddedServer existEmbeddedServer = new ExistEmbeddedServer(true, true);
240257

241258
@BeforeClass
242-
public static void createCredentials() {
259+
public static void setup() throws PermissionDeniedException, IOException, TriggerException {
243260
credentials = Base64.encodeBase64String("admin:".getBytes(UTF_8));
244261
badCredentials = Base64.encodeBase64String("johndoe:this pw should fail".getBytes(UTF_8));
262+
263+
//TODO create collection /db/AéB and store doc AéB.xml
264+
final XmldbURI TEST_XML_DOC_URI = XmldbURI.create("AéB.xml");
265+
final XmldbURI TEST_COLLECTION_URI = XmldbURI.create("/db/AéB");
266+
final String TEST_XML_DOC = "<foo/>";
267+
268+
final BrokerPool pool = existEmbeddedServer.getBrokerPool();
269+
try (final DBBroker broker = pool.get(Optional.of(pool.getSecurityManager().getSystemSubject()));
270+
final Txn transaction = pool.getTransactionManager().beginTransaction()) {
271+
try (final Collection col = broker.getOrCreateCollection(transaction, TEST_COLLECTION_URI)) {
272+
broker.storeDocument(transaction, TEST_XML_DOC_URI, new StringInputSource(TEST_XML_DOC), MimeType.XML_TYPE, col);
273+
broker.saveCollection(transaction, col);
274+
}
275+
276+
transaction.commit();
277+
} catch (EXistException | SAXException | LockException e) {
278+
throw new RuntimeException(e);
279+
}
245280
}
246281

247282
@Test
@@ -1036,6 +1071,33 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
10361071
}
10371072
}
10381073

1074+
//test rest server ability to handle encoded characters
1075+
@Test
1076+
public void doGetEncodedPath() throws IOException {
1077+
String DOC_URI = getServerUri() + XmldbURI.ROOT_COLLECTION + "/AéB/AéB.xml";
1078+
final HttpURLConnection connect = getConnection(DOC_URI);
1079+
try {
1080+
connect.setRequestMethod("GET");
1081+
connect.connect();
1082+
1083+
final int r = connect.getResponseCode();
1084+
assertEquals("Server returned response code " + r, HttpStatus.OK_200, r);
1085+
String contentType = connect.getContentType();
1086+
final int semicolon = contentType.indexOf(';');
1087+
if (semicolon > 0) {
1088+
contentType = contentType.substring(0, semicolon).trim();
1089+
}
1090+
assertEquals("Server returned content type " + contentType, "application/xml", contentType);
1091+
1092+
String response = readResponse(connect.getInputStream());
1093+
1094+
//readResponse is appending \r\n to each line that's why its added the expected content
1095+
assertEquals("Server returned document content " + response,"<foo/>\r\n",response);
1096+
} finally {
1097+
connect.disconnect();
1098+
}
1099+
}
1100+
10391101
private void chmod(final String resourcePath, final String mode) throws IOException {
10401102
final String uri = getCollectionUri() +"?_query=" + URLEncoder.encode(
10411103
"sm:chmod(xs:anyURI('" + resourcePath + "'), '" + mode + "')",

0 commit comments

Comments
 (0)