diff --git a/build.gradle b/build.gradle index 503831c..84f740d 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,7 @@ import java.time.ZonedDateTime import java.time.format.DateTimeFormatter plugins { - id "com.github.spotbugs" version "1.6.9" + id "com.github.spotbugs" version "1.7.1" id "org.sonarqube" version "2.6.1" id "io.codearte.nexus-staging" version "0.11.0" id "org.xbib.gradle.plugin.asciidoctor" version "1.5.6.0.1" @@ -148,7 +148,7 @@ subprojects { scmDeveloperConnection = 'scm:git:git://github.com/' + user + '/' + name + '.git' } - task xbibUpload(type: Upload) { + /*task xbibUpload(type: Upload) { group = 'publish' configuration = configurations.archives uploadDescriptor = true @@ -162,7 +162,7 @@ subprojects { } } } - } + }*/ task sonaTypeUpload(type: Upload) { group = 'publish' diff --git a/gradle.properties b/gradle.properties index b58aa65..69a3ec0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,6 +1,6 @@ group = org.xbib name = netty-http -version = 4.1.35.1 +version = 4.1.35.2 # main packages netty.version = 4.1.35.Final diff --git a/netty-http-client/src/main/java/org/xbib/netty/http/client/RequestBuilder.java b/netty-http-client/src/main/java/org/xbib/netty/http/client/RequestBuilder.java index 665df93..f0e8a9f 100644 --- a/netty-http-client/src/main/java/org/xbib/netty/http/client/RequestBuilder.java +++ b/netty-http-client/src/main/java/org/xbib/netty/http/client/RequestBuilder.java @@ -285,6 +285,7 @@ public class RequestBuilder { if (url.getHost() == null) { throw new IllegalStateException("host in URL not defined: " + url); } + // add path from uri() if (uri != null) { try { url = URL.base(url).resolve(uri); @@ -292,9 +293,7 @@ public class RequestBuilder { throw new IllegalArgumentException(e); } } - // add explicit parameters to URL - queryParameters.forEach(param -> url.getQueryParams().add(param)); - // let Netty's query string decoder/encoder work over the URL to add paramters given implicitly in url() + // let Netty's query string decoder/encoder work over the URL to add parameters given implicitly in url() QueryStringDecoder queryStringDecoder = new QueryStringDecoder(URI.create(url.toString()), StandardCharsets.UTF_8); QueryStringEncoder queryStringEncoder = new QueryStringEncoder(queryStringDecoder.path()); for (Map.Entry> entry : queryStringDecoder.parameters().entrySet()) { @@ -302,15 +301,25 @@ public class RequestBuilder { queryStringEncoder.addParam(entry.getKey(), value); } } + // attach user query parameters + queryParameters.forEach(param -> queryStringEncoder.addParam(param.getFirst(), param.getSecond())); // build uri from QueryStringDecoder - StringBuilder sb = new StringBuilder(); String pathAndQuery = queryStringEncoder.toString(); + StringBuilder sb = new StringBuilder(); sb.append(pathAndQuery.isEmpty() ? "/" : pathAndQuery); String ref = url.getFragment(); if (ref != null && !ref.isEmpty()) { sb.append('#').append(ref); } String uri = sb.toString(); + // resolve again + if (!uri.equals("/")) { + try { + url = uri.startsWith("/") ? URL.base(url).resolve(uri) : URL.base(url).resolve("/" + uri) ; + } catch (URLSyntaxException | MalformedInputException | UnmappableCharacterException e) { + throw new IllegalArgumentException(e); + } + } DefaultHttpHeaders validatedHeaders = new DefaultHttpHeaders(true); validatedHeaders.set(headers); String scheme = url.getScheme(); diff --git a/netty-http-client/src/test/java/org/xbib/netty/http/client/test/RequestBuilderTest.java b/netty-http-client/src/test/java/org/xbib/netty/http/client/test/RequestBuilderTest.java index c6a99c5..caf234e 100644 --- a/netty-http-client/src/test/java/org/xbib/netty/http/client/test/RequestBuilderTest.java +++ b/netty-http-client/src/test/java/org/xbib/netty/http/client/test/RequestBuilderTest.java @@ -4,16 +4,54 @@ import io.netty.handler.codec.http.HttpMethod; import org.junit.jupiter.api.Test; import org.xbib.netty.http.client.Request; -import java.util.logging.Level; -import java.util.logging.Logger; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; class RequestBuilderTest { - private static final Logger logger = Logger.getLogger(RequestBuilderTest.class.getName()); - @Test void testSimpleRequest() { - Request request = Request.builder(HttpMethod.GET).content("Hello", "text/plain").build(); - logger.log(Level.INFO, request.toString()); + Request request = Request.builder(HttpMethod.GET) + .content("Hello", "text/plain") + .build(); + assertEquals("localhost", request.url().getHost()); + assertNull(request.url().getPort()); + assertEquals("Hello", request.content().toString(StandardCharsets.UTF_8)); + } + + @Test + void testGetRequest() { + Request request = Request.builder(HttpMethod.GET) + .url("http://xbib.org") + .addParameter("param1", "value1") + .addParameter("param2", "value2") + .build(); + assertEquals("?param1=value1¶m2=value2", request.relativeUri()); + assertEquals("http://xbib.org/?param1=value1¶m2=value2", request.url().toString()); + } + + @Test + void testPostRequest() { + Request request = Request.builder(HttpMethod.POST) + .url("http://xbib.org") + .addParameter("param1", "value1") + .addParameter("param2", "value2") + .content("Hello", "text/plain") + .build(); + assertEquals("?param1=value1¶m2=value2", request.relativeUri()); + assertEquals("http://xbib.org/?param1=value1¶m2=value2", request.url().toString()); + assertEquals("Hello", request.content().toString(StandardCharsets.UTF_8)); + } + + @Test + void testRequest() { + Request request = Request.get() + .url("https://google.com") + .setVersion("HTTP/1.1") + .build(); + assertEquals("google.com", request.url().getHost()); + } } diff --git a/netty-http-client/src/test/java/org/xbib/netty/http/client/test/URITest.java b/netty-http-client/src/test/java/org/xbib/netty/http/client/test/URITest.java index f4dc03a..d1fc5c2 100644 --- a/netty-http-client/src/test/java/org/xbib/netty/http/client/test/URITest.java +++ b/netty-http-client/src/test/java/org/xbib/netty/http/client/test/URITest.java @@ -21,7 +21,7 @@ class URITest { } @Test - void testRequestURIs() { + void testRelativeUri() { RequestBuilder httpRequestBuilder = Request.get(); httpRequestBuilder.url("https://localhost").uri("/path"); assertEquals("/path", httpRequestBuilder.build().relativeUri());