Skip to content

Commit

Permalink
Add triming of the URI in the HTTPRequest class (#23)
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
erik-edling authored Aug 6, 2019
1 parent e675355 commit b60ea57
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 3 deletions.
47 changes: 44 additions & 3 deletions src/main/java/com/ericsson/eiffelcommons/utils/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public String getBaseUrl() {
* @param baseUrl
*/
public HttpRequest setBaseUrl(String baseUrl) {
baseUrl = trimBaseUrl(baseUrl);
this.baseUrl = baseUrl;
return this;
}
Expand Down Expand Up @@ -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<String, String> 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;
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit b60ea57

Please sign in to comment.