Skip to content

Release 1.0.5 version #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.ericsson</groupId>
<artifactId>eiffel-commons</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<name>eiffel commons java</name>
<description>A shared library for eiffel components</description>

Expand Down
82 changes: 38 additions & 44 deletions src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,27 @@
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.Consts;
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;

import lombok.Getter;
import lombok.Setter;
Expand All @@ -67,10 +62,10 @@ public enum HttpMethod {
protected String endpoint;

@Getter
protected ArrayList<NameValuePair> params;
protected Map<String, String> params;

public HttpRequest() {
params = new ArrayList<NameValuePair>();
params = new HashMap<>();
initExecutor(false);
}

Expand All @@ -84,7 +79,7 @@ public HttpRequest(HttpMethod method, HttpExecutor executor) {
}

public HttpRequest(HttpMethod method, boolean persistentClient) {
params = new ArrayList<NameValuePair>();
params = new HashMap<>();
setHttpMethod(method);
initExecutor(persistentClient);
}
Expand Down Expand Up @@ -136,7 +131,8 @@ public String getBaseUrl() {
* @param baseUrl
*/
public HttpRequest setBaseUrl(String baseUrl) {
this.baseUrl = trimBaseUrl(baseUrl);
baseUrl = trimBaseUrl(baseUrl);
this.baseUrl = baseUrl;
return this;
}

Expand Down Expand Up @@ -209,7 +205,7 @@ public void addParameters(Map<String, String> parameters) {
* @return HttpRequest
*/
public HttpRequest addParameter(String key, String value) {
params.add(new BasicNameValuePair(key, value));
params.put(key, value);
return this;
}

Expand Down Expand Up @@ -295,9 +291,9 @@ public HttpRequest setBasicAuth(String username, String password)
*/
public ResponseEntity performRequest()
throws URISyntaxException, ClientProtocolException, IOException {
URI uri = createURI();
uri = addParametersToURI(uri);
request.setURI(uri);
URIBuilder builder = createURIBuilder();
builder = addParametersToURIBuilder(builder);
request.setURI(builder.build());
return executor.executeRequest(request);
}

Expand All @@ -306,57 +302,55 @@ public ResponseEntity performRequest()
*
* @return URI
* @throws URISyntaxException
* @throws MalformedURLException
*/
@Deprecated
public URI getURI() throws MalformedURLException, URISyntaxException {
return createURI();
public URI getURI() throws URISyntaxException {
URIBuilder builder = createURIBuilder();
return builder.build();
}

/**
* Function that creates the URI from the baseUrl and endpoint
* Function that adds parameters to the URIBuilder
*
* @param
* @return URI
* @throws MalformedURLException
* @throws URISyntaxException
* @param URIBuilder
* @return URIBuilder
*/
public URI createURI() throws MalformedURLException, URISyntaxException {
if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) {
return new URL(baseUrl + endpoint).toURI();
} else if (!StringUtils.isEmpty(endpoint)) {
return new URL(baseUrl + "/" + endpoint).toURI();
} else {
return new URL(baseUrl).toURI();
private URIBuilder addParametersToURIBuilder(URIBuilder builder) {
if (!params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
}

return builder;
}

/**
* Function that adds parameters to the URI
* Function that creates the URI from the baseUrl and endpoint
*
* @param oldUri
* @return URI
* @param
* @return URIBuilder
* @throws URISyntaxException
*/
private URI addParametersToURI(URI oldUri) throws URISyntaxException {
String query = URLEncodedUtils.format(params, Consts.UTF_8);
URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), query,
oldUri.getFragment());
return newUri;
private URIBuilder createURIBuilder() throws URISyntaxException {
if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) {
return new URIBuilder(baseUrl + endpoint);
} else if (!StringUtils.isEmpty(endpoint)) {
return new URIBuilder(baseUrl + "/" + endpoint);
} else {
return new URIBuilder(baseUrl);
}
}

/**
* Function that trims the base url for trailing slashes
*
* @return trimmedUrl
* @return HttpRequest
*/
private String trimBaseUrl(String baseUrl) {
String trimmedUrl = baseUrl;
if (baseUrl.endsWith("/")) {
trimmedUrl = baseUrl.substring(0, baseUrl.length() - 1);
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
}

return trimmedUrl;
return baseUrl;
}
}
}
56 changes: 23 additions & 33 deletions src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.ericsson.eiffelcommons.http;
package com.ericsson.eiffelcommons.httptest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
Expand All @@ -9,7 +9,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
Expand All @@ -22,6 +21,7 @@
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.junit.Test;
import org.powermock.reflect.Whitebox;
Expand All @@ -33,7 +33,6 @@
public class HttpRequestTest {
private static final String URL_1 = "http://something.com";
private static final String URL_2 = "http://something.com/";
private static final String URL_3 = "http://someothing.com";
private static final String URL_BAD_PROTOCOL = "httpl://something.com/";
private static final String URL_BAD_SYNTAX = "http:<<something.com/";
private static final String ENDPOINT_1 = "/testing/test/";
Expand All @@ -57,28 +56,29 @@ public class HttpRequestTest {

@Test
public void testBuildingOfURI() throws Exception {

HttpRequest request = new HttpRequest(HttpMethod.POST);

request.setBaseUrl(URL_1);
request.setEndpoint(ENDPOINT_1);
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());
URIBuilder builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_2);
request.setEndpoint(ENDPOINT_1);
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request = new HttpRequest(HttpMethod.DELETE);
request.setBaseUrl(URL_2);
request.setEndpoint(ENDPOINT_2);
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());

request = new HttpRequest(HttpMethod.PUT);
request.setBaseUrl(URL_1);
request.setEndpoint(ENDPOINT_2);
uri = (URI) Whitebox.invokeMethod(request, "createURI");
assertEquals(EXPECTED_URI, uri.toString());
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
assertEquals(EXPECTED_URI, builder.toString());
}

@Test
Expand Down Expand Up @@ -157,7 +157,7 @@ public void testBodyProperty() throws UnsupportedOperationException, IOException
actualBody = IOUtils.toString(entity.getContent(), "UTF-8");
stream.close();
assertTrue(actualHeader.contains(BODY_HEADER_DEFAULT));
assertEquals(BODY_CONTENT, actualBody.replace(System.getProperty("line.separator"), ""));
assertEquals(BODY_CONTENT + "\n", actualBody);
}

@Test(expected = IOException.class)
Expand Down Expand Up @@ -186,18 +186,6 @@ public void testParameterProperty() {
}

@Test
public void testAddParametersToURI() throws Exception {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_1)
.addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1)
.addParameter(PARAMETER_KEY_2, PARAMETER_VALUE_2);
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
URI newUri = (URI) Whitebox.invokeMethod(request, "addParametersToURI", uri);
String expectedURI = URL_1 + "?" + PARAMETER_KEY_1 + "=" + PARAMETER_VALUE_1 + "&"
+ PARAMETER_KEY_2 + "=" + PARAMETER_VALUE_2;
assertEquals(expectedURI, newUri.toString());
}

public void testHeaderProperty() {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.addHeader(HEADER_KEY_1, HEADER_VALUE_1);
Expand All @@ -218,10 +206,10 @@ public void testHeaderProperty() {
}

@Test
public void testGetURI() throws MalformedURLException, URISyntaxException {
public void testGetURI() throws URISyntaxException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_1).setEndpoint(ENDPOINT_1);
URI uri = request.createURI();
URI uri = request.getURI();
String fullURI = uri.toString();
assertEquals(URL_1 + ENDPOINT_1, fullURI);
}
Expand All @@ -230,12 +218,13 @@ public void testGetURI() throws MalformedURLException, URISyntaxException {
public void testPerformRequestUnknownHost()
throws ClientProtocolException, URISyntaxException, IOException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_3).setEndpoint(ENDPOINT_1);
request.setBaseUrl(URL_1).setEndpoint(ENDPOINT_1);
request.performRequest();
}

@Test(expected = MalformedURLException.class)
public void testPerformRequestBadProtocol() throws ClientProtocolException, URISyntaxException, IOException {
@Test(expected = ClientProtocolException.class)
public void testPerformRequestBadProtocol()
throws ClientProtocolException, URISyntaxException, IOException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_BAD_PROTOCOL);
request.performRequest();
Expand All @@ -253,15 +242,16 @@ public void testPerformRequestBadSyntax()
public void testPerformRequestNoEndpoint()
throws ClientProtocolException, URISyntaxException, IOException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_3);
request.setBaseUrl(URL_1);
request.performRequest();
}

@Test(expected = UnknownHostException.class)
public void testPerformRequestWithParameters()
throws ClientProtocolException, URISyntaxException, IOException {
HttpRequest request = new HttpRequest(HttpMethod.GET);
request.setBaseUrl(URL_3).addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1);
request.setBaseUrl(URL_1).addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1);
request.performRequest();
}
}

Loading