From b60ea577924317fb91da7cae456c773184254157 Mon Sep 17 00:00:00 2001 From: Erik Edling <38752030+e-edling-ericsson@users.noreply.github.com> Date: Tue, 6 Aug 2019 11:28:18 +0200 Subject: [PATCH] Add triming of the URI in the HTTPRequest class (#23) - Before this change, seting a baseUrl ending with a "/" and setting a endpoint beginning with a "/" would result in "//" in the url. This change will trim the url so it doesn't matter if you add trailing slash on the baseurl or not as well as the leading slash on the endpoint. - Adding tests for this new functionality. --- .../eiffelcommons/utils/HttpRequest.java | 47 +++++++++++++++++-- .../Utilstest/HttpRequestTest.java | 42 +++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 src/test/java/com/ericsson/eiffelcommons/Utilstest/HttpRequestTest.java diff --git a/src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java b/src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java index 8d20bb87..112b39d5 100644 --- a/src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java +++ b/src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java @@ -102,6 +102,7 @@ public String getBaseUrl() { * @param baseUrl */ public HttpRequest setBaseUrl(String baseUrl) { + baseUrl = trimBaseUrl(baseUrl); this.baseUrl = baseUrl; return this; } @@ -212,14 +213,54 @@ public HttpRequest setBody(File file) throws IOException { * @throws ClientProtocolException */ public ResponseEntity performRequest() throws URISyntaxException, ClientProtocolException, IOException { - URIBuilder builder = new URIBuilder(baseUrl + endpoint); + URIBuilder builder = createURIBuilder(); + builder = addParametersToURIBuilder(builder); + request.setURI(builder.build()); + return executor.executeRequest(request); + } + + /** + * Function that adds parameters to the URIBuilder + * + * @param URIBuilder + * @return URIBuilder + */ + private URIBuilder addParametersToURIBuilder(URIBuilder builder) { if (!params.isEmpty()) { for (Map.Entry entry : params.entrySet()) { builder.addParameter(entry.getKey(), entry.getValue()); } } - request.setURI(builder.build()); - return executor.executeRequest(request); + + return builder; + } + + /** + * Function that creates the URI from the baseUrl and endpoint + * + * @param + * @return URIBuilder + * @throws URISyntaxException + */ + private URIBuilder createURIBuilder() throws URISyntaxException { + if(endpoint.startsWith("/")) { + return new URIBuilder(baseUrl + endpoint); + } else { + return new URIBuilder(baseUrl + "/" + endpoint); + } + } + + /** + * Function that trims the base url for trailing slashes + * + * @return HttpRequest + */ + private String trimBaseUrl(String baseUrl) { + if(baseUrl.endsWith("/")) { + baseUrl = baseUrl.substring(0, baseUrl.length() - 1); + } + + return baseUrl; } } \ No newline at end of file diff --git a/src/test/java/com/ericsson/eiffelcommons/Utilstest/HttpRequestTest.java b/src/test/java/com/ericsson/eiffelcommons/Utilstest/HttpRequestTest.java new file mode 100644 index 00000000..e872fa55 --- /dev/null +++ b/src/test/java/com/ericsson/eiffelcommons/Utilstest/HttpRequestTest.java @@ -0,0 +1,42 @@ +package com.ericsson.eiffelcommons.Utilstest; + +import static org.junit.Assert.assertEquals; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +import org.apache.http.client.utils.URIBuilder; +import org.junit.Test; + +import com.ericsson.eiffelcommons.utils.HttpRequest; +import com.ericsson.eiffelcommons.utils.HttpRequest.HttpMethod; + +public class HttpRequestTest { + @Test + public void testBuildingOfURI() throws NoSuchFieldException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException { + String expectedURI = "http://something.com/testing/test/"; + HttpRequest request= new HttpRequest(HttpMethod.POST); + Method method = request.getClass().getDeclaredMethod("createURIBuilder"); + method.setAccessible(true); + + request.setBaseUrl("http://something.com"); + request.setEndpoint("/testing/test/"); + URIBuilder builder = (URIBuilder) method.invoke(request); + assertEquals(expectedURI, builder.toString()); + + request.setBaseUrl("http://something.com/"); + request.setEndpoint("/testing/test/"); + builder = (URIBuilder) method.invoke(request); + assertEquals(expectedURI, builder.toString()); + + request.setBaseUrl("http://something.com/"); + request.setEndpoint("testing/test/"); + builder = (URIBuilder) method.invoke(request); + assertEquals(expectedURI, builder.toString()); + + request.setBaseUrl("http://something.com"); + request.setEndpoint("testing/test/"); + builder = (URIBuilder) method.invoke(request); + assertEquals(expectedURI, builder.toString()); + } +}