openvidu-test-browsers: CustomHttpClient fixes

pull/331/head
pabloFuente 2019-10-04 16:11:52 +02:00
parent 638176a6d4
commit 9cd389eb61
1 changed files with 27 additions and 8 deletions

View File

@ -17,6 +17,7 @@
package io.openvidu.test.browsers.utils; package io.openvidu.test.browsers.utils;
import java.io.IOException;
import java.security.KeyManagementException; import java.security.KeyManagementException;
import java.security.KeyStoreException; import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
@ -77,7 +78,6 @@ public class CustomHttpClient {
return Unirest.get(path).header("Authorization", credentials).asJson().getStatus(); return Unirest.get(path).header("Authorization", credentials).asJson().getStatus();
} }
public JSONObject rest(HttpMethod method, String path, int status) throws Exception { public JSONObject rest(HttpMethod method, String path, int status) throws Exception {
return this.commonRest(method, path, null, status); return this.commonRest(method, path, null, status);
} }
@ -179,8 +179,12 @@ public class CustomHttpClient {
return json; return json;
} }
public void shutdown() throws IOException {
Unirest.shutdown();
}
private org.json.JSONObject commonRest(HttpMethod method, String path, String body, int status) throws Exception { private org.json.JSONObject commonRest(HttpMethod method, String path, String body, int status) throws Exception {
HttpResponse<JsonNode> jsonResponse = null; HttpResponse<?> jsonResponse = null;
org.json.JSONObject json = null; org.json.JSONObject json = null;
path = openviduUrl + (path.startsWith("/") ? path : ("/" + path)); path = openviduUrl + (path.startsWith("/") ? path : ("/" + path));
@ -201,27 +205,42 @@ public class CustomHttpClient {
switch (method) { switch (method) {
case GET: case GET:
request = Unirest.get(path); request = Unirest.get(path);
request.header("Content-Type", "application/x-www-form-urlencoded");
break; break;
case POST: case POST:
request = Unirest.post(path); request = Unirest.post(path);
break; break;
case DELETE: case DELETE:
request = Unirest.delete(path); request = Unirest.delete(path);
request.header("Content-Type", "application/x-www-form-urlencoded");
break; break;
default: default:
break; break;
} }
request.header("Content-Type", "application/x-www-form-urlencoded");
} }
request = request.header("Authorization", this.headerAuth);
try { try {
jsonResponse = request.header("Authorization", this.headerAuth).asJson(); jsonResponse = request.asJson();
if (jsonResponse.getBody() != null) { if (jsonResponse.getBody() != null) {
json = jsonResponse.getBody().getObject(); json = ((JsonNode) jsonResponse.getBody()).getObject();
} }
} catch (UnirestException e) { } catch (UnirestException e) {
log.error(e.getMessage()); try {
throw new Exception("Error sending request to " + path + ": " + e.getMessage()); if (e.getCause().getCause().getCause() instanceof org.json.JSONException) {
try {
jsonResponse = request.asString();
} catch (UnirestException e1) {
throw new Exception("Error sending request to " + path + ": " + e.getMessage());
}
} else {
throw new Exception("Error sending request to " + path + ": " + e.getMessage());
}
} catch (NullPointerException e2) {
throw new Exception("Error sending request to " + path + ": " + e.getMessage());
}
} }
if (jsonResponse.getStatus() == 500) { if (jsonResponse.getStatus() == 500) {
log.error("Internal Server Error: {}", jsonResponse.getBody().toString()); log.error("Internal Server Error: {}", jsonResponse.getBody().toString());
} }