Search in sources :

Example 6 with Response

use of org.talend.sdk.component.junit.http.api.Response 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 7 with Response

use of org.talend.sdk.component.junit.http.api.Response in project component-runtime by Talend.

the class PassthroughHandler method doHttpRequest.

private void doHttpRequest(final FullHttpRequest request, final ChannelHandlerContext ctx) {
    try {
        final Attribute<String> baseAttr = ctx.channel().attr(Handlers.BASE);
        final String requestUri = (baseAttr == null || baseAttr.get() == null ? "" : baseAttr.get()) + request.uri();
        // do the remote request with all the incoming data and save it
        // note: this request must be synchronous for now
        final Response resp;
        try {
            final URL url = new URL(requestUri);
            final HttpURLConnection connection = HttpURLConnection.class.cast(url.openConnection(Proxy.NO_PROXY));
            connection.setConnectTimeout(30000);
            connection.setReadTimeout(20000);
            if (HttpsURLConnection.class.isInstance(connection) && api.getSslContext() != null) {
                final HttpsURLConnection httpsURLConnection = HttpsURLConnection.class.cast(connection);
                httpsURLConnection.setHostnameVerifier((h, s) -> true);
                httpsURLConnection.setSSLSocketFactory(api.getSslContext().getSocketFactory());
            }
            request.headers().forEach(e -> connection.setRequestProperty(e.getKey(), e.getValue()));
            if (request.method() != null) {
                final String requestMethod = request.method().name();
                connection.setRequestMethod(requestMethod);
                if (!"HEAD".equalsIgnoreCase(requestMethod) && request.content().readableBytes() > 0) {
                    connection.setDoOutput(true);
                    request.content().readBytes(connection.getOutputStream(), request.content().readableBytes());
                }
            }
            final int responseCode = connection.getResponseCode();
            final int defaultLength = ofNullable(connection.getHeaderField("content-length")).map(Integer::parseInt).orElse(8192);
            resp = new ResponseImpl(connection.getHeaderFields().entrySet().stream().filter(e -> e.getKey() != null).filter(h -> !api.getHeaderFilter().test(h.getKey())).collect(toMap(Map.Entry::getKey, e -> e.getValue().stream().collect(Collectors.joining(",")))), responseCode, responseCode <= 399 ? slurp(connection.getInputStream(), defaultLength) : slurp(connection.getErrorStream(), defaultLength));
        } catch (final Exception e) {
            log.error(e.getMessage(), e);
            sendError(ctx, HttpResponseStatus.BAD_REQUEST);
            return;
        }
        beforeResponse(requestUri, request, resp);
        final ByteBuf bytes = ofNullable(resp.payload()).map(Unpooled::copiedBuffer).orElse(Unpooled.EMPTY_BUFFER);
        final HttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(resp.status()), bytes);
        HttpUtil.setContentLength(response, bytes.array().length);
        ofNullable(resp.headers()).ifPresent(h -> h.forEach((k, v) -> response.headers().set(k, v)));
        ctx.writeAndFlush(response);
    } finally {
        request.release();
    }
}
Also used : BASE(org.talend.sdk.component.junit.http.internal.impl.Handlers.BASE) HttpURLConnection(java.net.HttpURLConnection) HttpVersion(io.netty.handler.codec.http.HttpVersion) HttpUtil.setKeepAlive(io.netty.handler.codec.http.HttpUtil.setKeepAlive) Handlers.sendError(org.talend.sdk.component.junit.http.internal.impl.Handlers.sendError) Handlers.closeOnFlush(org.talend.sdk.component.junit.http.internal.impl.Handlers.closeOnFlush) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) Response(org.talend.sdk.component.junit.http.api.Response) SSLEngine(javax.net.ssl.SSLEngine) HttpApiHandler(org.talend.sdk.component.junit.http.api.HttpApiHandler) Unpooled(io.netty.buffer.Unpooled) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Collectors.toMap(java.util.stream.Collectors.toMap) ByteBuf(io.netty.buffer.ByteBuf) Proxy(java.net.Proxy) Map(java.util.Map) Attribute(io.netty.util.Attribute) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Optional.ofNullable(java.util.Optional.ofNullable) HttpUtil.setContentLength(io.netty.handler.codec.http.HttpUtil.setContentLength) HttpMethod(io.netty.handler.codec.http.HttpMethod) IOException(java.io.IOException) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Collectors(java.util.stream.Collectors) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Slf4j(lombok.extern.slf4j.Slf4j) SslHandler(io.netty.handler.ssl.SslHandler) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) AllArgsConstructor(lombok.AllArgsConstructor) InputStream(java.io.InputStream) HttpUtil(io.netty.handler.codec.http.HttpUtil) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) URL(java.net.URL) IOException(java.io.IOException) Response(org.talend.sdk.component.junit.http.api.Response) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpURLConnection(java.net.HttpURLConnection) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 8 with Response

use of org.talend.sdk.component.junit.http.api.Response 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

Response (org.talend.sdk.component.junit.http.api.Response)8 HttpURLConnection (java.net.HttpURLConnection)5 Map (java.util.Map)5 Collectors.toMap (java.util.stream.Collectors.toMap)5 URL (java.net.URL)4 HttpApiHandler (org.talend.sdk.component.junit.http.api.HttpApiHandler)4 Collectors (java.util.stream.Collectors)3 IO (org.apache.ziplock.IO)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 ResponseImpl (org.talend.sdk.component.junit.http.internal.impl.ResponseImpl)3 ByteBuf (io.netty.buffer.ByteBuf)2 Unpooled (io.netty.buffer.Unpooled)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 SimpleChannelInboundHandler (io.netty.channel.SimpleChannelInboundHandler)2 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)2 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)2 HttpMethod (io.netty.handler.codec.http.HttpMethod)2 HttpResponse (io.netty.handler.codec.http.HttpResponse)2 HttpResponseStatus (io.netty.handler.codec.http.HttpResponseStatus)2 HttpUtil (io.netty.handler.codec.http.HttpUtil)2