Search in sources :

Example 1 with Query

use of org.talend.sdk.component.api.service.http.Query in project component-runtime by Talend.

the class HttpClientFactoryImplTest method ignoreNullQueryParam.

@Test
void ignoreNullQueryParam() throws IOException {
    final HttpServer server = HttpServer.create(new InetSocketAddress(0), 0);
    server.createContext("/").setHandler(httpExchange -> {
        final String query = httpExchange.getRequestURI().getQuery();
        httpExchange.sendResponseHeaders(HttpURLConnection.HTTP_OK, query.getBytes("utf-8").length);
        httpExchange.getResponseBody().write(query.getBytes("utf-8"));
        httpExchange.close();
    });
    try {
        server.start();
        final IgnoreNullQueryParam ok = newDefaultFactory().create(IgnoreNullQueryParam.class, null);
        ok.base("http://localhost:" + server.getAddress().getPort() + "/api");
        String query = ok.get("value", "", null);
        final Map<String, String> params = Stream.of(query.split("&")).map(s -> {
            final int equal = s.indexOf('=');
            if (equal > 0) {
                return new String[] { s.substring(0, equal), s.substring(equal + 1, s.length()) };
            }
            return new String[] { s, "true" };
        }).collect(toMap(s -> s[0], s -> s[1]));
        assertTrue(params.containsKey("param"));
        assertTrue(params.containsKey("emptyParam"));
        assertTrue(!params.containsKey("nullParam"));
    } finally {
        server.stop(0);
    }
}
Also used : Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) HttpURLConnection(java.net.HttpURLConnection) HttpServer(com.sun.net.httpserver.HttpServer) HttpClient(org.talend.sdk.component.api.service.http.HttpClient) Response(org.talend.sdk.component.api.service.http.Response) Decoder(org.talend.sdk.component.api.service.http.Decoder) HashMap(java.util.HashMap) HttpException(org.talend.sdk.component.api.service.http.HttpException) Path(org.talend.sdk.component.api.service.http.Path) Collections.singletonList(java.util.Collections.singletonList) Encoder(org.talend.sdk.component.api.service.http.Encoder) Internationalized(org.talend.sdk.component.api.internationalization.Internationalized) Collectors.toMap(java.util.stream.Collectors.toMap) Query(org.talend.sdk.component.api.service.http.Query) Arrays.asList(java.util.Arrays.asList) ParameterModelService(org.talend.sdk.component.runtime.manager.reflect.ParameterModelService) Map(java.util.Map) Headers(com.sun.net.httpserver.Headers) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) Request(org.talend.sdk.component.api.service.http.Request) Collections.emptyMap(java.util.Collections.emptyMap) JsonbBuilder(javax.json.bind.JsonbBuilder) Collection(java.util.Collection) IOException(java.io.IOException) Codec(org.talend.sdk.component.api.service.http.Codec) XmlRootElement(javax.xml.bind.annotation.XmlRootElement) InputStreamReader(java.io.InputStreamReader) InetSocketAddress(java.net.InetSocketAddress) Collectors.joining(java.util.stream.Collectors.joining) StandardCharsets(java.nio.charset.StandardCharsets) Test(org.junit.jupiter.api.Test) Header(org.talend.sdk.component.api.service.http.Header) Stream(java.util.stream.Stream) Type(java.lang.reflect.Type) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Data(lombok.Data) Service(org.talend.sdk.component.api.service.Service) BufferedReader(java.io.BufferedReader) ReflectionService(org.talend.sdk.component.runtime.manager.reflect.ReflectionService) AllArgsConstructor(lombok.AllArgsConstructor) NoArgsConstructor(lombok.NoArgsConstructor) InetSocketAddress(java.net.InetSocketAddress) HttpServer(com.sun.net.httpserver.HttpServer) Test(org.junit.jupiter.api.Test)

Aggregations

Headers (com.sun.net.httpserver.Headers)1 HttpServer (com.sun.net.httpserver.HttpServer)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 Type (java.lang.reflect.Type)1 HttpURLConnection (java.net.HttpURLConnection)1 InetSocketAddress (java.net.InetSocketAddress)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonList (java.util.Collections.singletonList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toMap (java.util.stream.Collectors.toMap)1 Stream (java.util.stream.Stream)1 JsonbBuilder (javax.json.bind.JsonbBuilder)1 XmlRootElement (javax.xml.bind.annotation.XmlRootElement)1