Search in sources :

Example 1 with Response

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

the class JUnit4HttpApiTest method getProxy.

@Test
public void getProxy() throws Exception {
    final Response response = execute("GET", "http://foo.bar.not.existing.talend.com/component/test?api=true", null);
    assertEquals(HttpURLConnection.HTTP_OK, response.status());
    assertEquals(new String(response.payload()), "worked as expected");
    assertEquals("text/plain", response.headers().get("content-type"));
    assertEquals("true", response.headers().get("mocked"));
    assertEquals("true", response.headers().get("X-Talend-Proxy-JUnit"));
}
Also used : Response(org.talend.sdk.component.junit.http.api.Response) Test(org.junit.Test)

Example 2 with Response

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

the class JUnit5HttpApiTest method getProxy.

@Test
void getProxy() throws Exception {
    final Response response = execute("GET", "http://foo.bar.not.existing.talend.com/component/test?api=true", null);
    assertEquals(HttpURLConnection.HTTP_OK, response.status());
    assertEquals(new String(response.payload()), "worked as expected");
    assertEquals("text/plain", response.headers().get("content-type"));
    assertEquals("true", response.headers().get("mocked"));
    assertEquals("true", response.headers().get("X-Talend-Proxy-JUnit"));
}
Also used : Response(org.talend.sdk.component.junit.http.api.Response) Test(org.junit.jupiter.api.Test)

Example 3 with Response

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

the class JUnit5HttpsApiTest method getProxy.

// just ensure it is reentrant
@RepeatedTest(5)
void getProxy() throws Exception {
    final Response response = get();
    assertEquals(HttpURLConnection.HTTP_OK, response.status());
    assertEquals(new String(response.payload()), "worked as expected");
    assertEquals("text/plain", response.headers().get("content-type"));
    assertEquals("true", response.headers().get("mocked"));
    assertEquals("true", response.headers().get("X-Talend-Proxy-JUnit"));
}
Also used : Response(org.talend.sdk.component.junit.http.api.Response) RepeatedTest(org.junit.jupiter.api.RepeatedTest)

Example 4 with Response

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

the class ServingProxyHandler method channelRead0.

@Override
protected void channelRead0(final ChannelHandlerContext ctx, final FullHttpRequest request) {
    if (!request.decoderResult().isSuccess()) {
        sendError(ctx, HttpResponseStatus.BAD_REQUEST);
        return;
    }
    api.getExecutor().execute(() -> {
        final Map<String, String> headers = StreamSupport.stream(Spliterators.spliteratorUnknownSize(request.headers().iteratorAsString(), Spliterator.IMMUTABLE), false).collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
        final Attribute<String> baseAttr = ctx.channel().attr(Handlers.BASE);
        Optional<Response> matching = api.getResponseLocator().findMatching(new RequestImpl((baseAttr == null || baseAttr.get() == null ? "" : baseAttr.get()) + request.uri(), request.method().name(), headers), api.getHeaderFilter());
        if (!matching.isPresent()) {
            if (HttpMethod.CONNECT.name().equalsIgnoreCase(request.method().name())) {
                final Map<String, String> responseHeaders = new HashMap<>();
                responseHeaders.put(HttpHeaderNames.CONNECTION.toString(), HttpHeaderValues.KEEP_ALIVE.toString());
                responseHeaders.put(HttpHeaderNames.CONTENT_LENGTH.toString(), "0");
                matching = of(new ResponseImpl(responseHeaders, HttpResponseStatus.OK.code(), Unpooled.EMPTY_BUFFER.array()));
                if (api.getSslContext() != null) {
                    final SSLEngine sslEngine = api.getSslContext().createSSLEngine();
                    sslEngine.setUseClientMode(false);
                    ctx.channel().pipeline().addFirst("ssl", new SslHandler(sslEngine, true));
                    final String uri = request.uri();
                    final String[] parts = uri.split(":");
                    ctx.channel().attr(Handlers.BASE).set("https://" + parts[0] + (parts.length > 1 && !"443".equals(parts[1]) ? ":" + parts[1] : ""));
                }
            } else {
                sendError(ctx, new HttpResponseStatus(HttpURLConnection.HTTP_BAD_REQUEST, "You are in proxy mode. No response was found for the simulated request. Please ensure to capture it for next executions. " + request.method().name() + " " + request.uri()));
                return;
            }
        }
        final Response resp = matching.get();
        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);
        if (!api.isSkipProxyHeaders()) {
            response.headers().set("X-Talend-Proxy-JUnit", "true");
        }
        ofNullable(resp.headers()).ifPresent(h -> h.forEach((k, v) -> response.headers().set(k, v)));
        ctx.writeAndFlush(response);
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HttpVersion(io.netty.handler.codec.http.HttpVersion) Handlers.sendError(org.talend.sdk.component.junit.http.internal.impl.Handlers.sendError) Handlers.closeOnFlush(org.talend.sdk.component.junit.http.internal.impl.Handlers.closeOnFlush) Spliterators(java.util.Spliterators) Optional.of(java.util.Optional.of) Response(org.talend.sdk.component.junit.http.api.Response) HashMap(java.util.HashMap) 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) Map(java.util.Map) StreamSupport(java.util.stream.StreamSupport) Attribute(io.netty.util.Attribute) HttpHeaderValues(io.netty.handler.codec.http.HttpHeaderValues) Optional.ofNullable(java.util.Optional.ofNullable) HttpMethod(io.netty.handler.codec.http.HttpMethod) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) Slf4j(lombok.extern.slf4j.Slf4j) SslHandler(io.netty.handler.ssl.SslHandler) SimpleChannelInboundHandler(io.netty.channel.SimpleChannelInboundHandler) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) Optional(java.util.Optional) ChannelHandler(io.netty.channel.ChannelHandler) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) AllArgsConstructor(lombok.AllArgsConstructor) Spliterator(java.util.Spliterator) HttpUtil(io.netty.handler.codec.http.HttpUtil) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HashMap(java.util.HashMap) SSLEngine(javax.net.ssl.SSLEngine) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) ByteBuf(io.netty.buffer.ByteBuf) SslHandler(io.netty.handler.ssl.SslHandler) Response(org.talend.sdk.component.junit.http.api.Response) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) HashMap(java.util.HashMap) Collectors.toMap(java.util.stream.Collectors.toMap) Map(java.util.Map)

Example 5 with Response

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

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