21
21
*/
22
22
package org .exist .http ;
23
23
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 .*;
31
25
import java .net .HttpURLConnection ;
32
26
import java .net .URL ;
33
27
import java .net .URLEncoder ;
@@ -1072,6 +1066,7 @@ public void execSetUidQueryWithBearerAuth() throws IOException {
1072
1066
}
1073
1067
1074
1068
//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
1075
1070
@ Test
1076
1071
public void doGetEncodedPath () throws IOException {
1077
1072
String DOC_URI = getServerUri () + XmldbURI .ROOT_COLLECTION + "/AéB/AéB.xml" ;
@@ -1098,6 +1093,120 @@ public void doGetEncodedPath() throws IOException {
1098
1093
}
1099
1094
}
1100
1095
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
+
1101
1210
private void chmod (final String resourcePath , final String mode ) throws IOException {
1102
1211
final String uri = getCollectionUri () +"?_query=" + URLEncoder .encode (
1103
1212
"sm:chmod(xs:anyURI('" + resourcePath + "'), '" + mode + "')" ,
0 commit comments