Search in sources :

Example 1 with ResponseImpl

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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) Response(org.talend.sdk.component.junit.http.api.Response) Test(org.junit.Test) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Rule(org.junit.Rule) Collectors.toMap(java.util.stream.Collectors.toMap) IO(org.apache.ziplock.IO) Map(java.util.Map) ClassRule(org.junit.ClassRule) Assert.assertEquals(org.junit.Assert.assertEquals) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl) HttpURLConnection(java.net.HttpURLConnection) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) URL(java.net.URL) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl)

Example 2 with ResponseImpl

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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.jupiter.api.Test) Collectors.toMap(java.util.stream.Collectors.toMap) IO(org.apache.ziplock.IO) URL(java.net.URL) Map(java.util.Map) Response(org.talend.sdk.component.junit.http.api.Response) Collectors(java.util.stream.Collectors) Assert.assertEquals(org.junit.Assert.assertEquals) StandardCharsets(java.nio.charset.StandardCharsets) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl) HttpURLConnection(java.net.HttpURLConnection) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) URL(java.net.URL) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl)

Example 3 with ResponseImpl

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();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Collectors.toMap(java.util.stream.Collectors.toMap) IO(org.apache.ziplock.IO) URL(java.net.URL) Map(java.util.Map) Response(org.talend.sdk.component.junit.http.api.Response) Collectors.joining(java.util.stream.Collectors.joining) Assert.assertEquals(org.junit.Assert.assertEquals) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) ResponseImpl(org.talend.sdk.component.junit.http.internal.impl.ResponseImpl)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3 URL (java.net.URL)3 Map (java.util.Map)3 Collectors.toMap (java.util.stream.Collectors.toMap)3 IO (org.apache.ziplock.IO)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Response (org.talend.sdk.component.junit.http.api.Response)3 ResponseImpl (org.talend.sdk.component.junit.http.internal.impl.ResponseImpl)3 StandardCharsets (java.nio.charset.StandardCharsets)2 Collectors (java.util.stream.Collectors)2 HttpApiHandler (org.talend.sdk.component.junit.http.api.HttpApiHandler)2 Collectors.joining (java.util.stream.Collectors.joining)1 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)1 ClassRule (org.junit.ClassRule)1 Rule (org.junit.Rule)1 Test (org.junit.Test)1 RepeatedTest (org.junit.jupiter.api.RepeatedTest)1 Test (org.junit.jupiter.api.Test)1