use of org.talend.sdk.component.junit.http.internal.impl.ResponseImpl in project component-runtime by Talend.
the class JUnit4HttpApiTest method execute.
private Response execute(final String method, final String uri, final String payload) throws Exception {
final URL url = new URL(uri);
final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
connection.setConnectTimeout(30000);
connection.setReadTimeout(20000);
connection.setRequestMethod(method);
if (payload != null) {
connection.setDoOutput(true);
connection.getOutputStream().write(payload.getBytes(StandardCharsets.UTF_8));
}
final int responseCode = connection.getResponseCode();
try {
final Map<String, String> headers = connection.getHeaderFields().entrySet().stream().filter(e -> e.getKey() != null).collect(toMap(Map.Entry::getKey, e -> e.getValue().stream().collect(Collectors.joining(","))));
return new ResponseImpl(headers, responseCode, responseCode < 399 ? IO.readBytes(connection.getInputStream()) : null);
} finally {
connection.disconnect();
}
}
use of org.talend.sdk.component.junit.http.internal.impl.ResponseImpl in project component-runtime by Talend.
the class JUnit5HttpApiTest method execute.
private Response execute(final String method, final String uri, final String payload) throws Exception {
final URL url = new URL(uri);
final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection());
connection.setConnectTimeout(30000);
connection.setReadTimeout(20000);
connection.setRequestMethod(method);
if (payload != null) {
connection.setDoOutput(true);
connection.getOutputStream().write(payload.getBytes(StandardCharsets.UTF_8));
}
final int responseCode = connection.getResponseCode();
try {
final Map<String, String> headers = connection.getHeaderFields().entrySet().stream().filter(e -> e.getKey() != null).collect(toMap(Map.Entry::getKey, e -> e.getValue().stream().collect(Collectors.joining(","))));
return new ResponseImpl(headers, responseCode, responseCode < 399 ? IO.readBytes(connection.getInputStream()) : null);
} finally {
connection.disconnect();
}
}
use of org.talend.sdk.component.junit.http.internal.impl.ResponseImpl in project component-runtime by Talend.
the class JUnit5HttpsApiTest method get.
private Response get() throws Exception {
final URL url = new URL("https://foo.bar.not.existing.talend.com/component/test?api=true");
final HttpsURLConnection connection = HttpsURLConnection.class.cast(url.openConnection());
connection.setSSLSocketFactory(handler.getSslContext().getSocketFactory());
connection.setConnectTimeout(300000);
connection.setReadTimeout(20000);
connection.connect();
final int responseCode = connection.getResponseCode();
try {
final Map<String, String> headers = connection.getHeaderFields().entrySet().stream().filter(e -> e.getKey() != null).collect(toMap(Map.Entry::getKey, e -> e.getValue().stream().collect(joining(","))));
return new ResponseImpl(headers, responseCode, IO.readBytes(connection.getInputStream()));
} finally {
connection.disconnect();
}
}
Aggregations