Skip to content

Commit 5a2dfbb

Browse files
authored
Release 1.0.5 version (#64)
Revert #40 with breaking functionality
1 parent d00a1f9 commit 5a2dfbb

File tree

3 files changed

+62
-78
lines changed

3 files changed

+62
-78
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<modelVersion>4.0.0</modelVersion>
77
<groupId>com.github.ericsson</groupId>
88
<artifactId>eiffel-commons</artifactId>
9-
<version>1.0.4</version>
9+
<version>1.0.5</version>
1010
<name>eiffel commons java</name>
1111
<description>A shared library for eiffel components</description>
1212

src/main/java/com/ericsson/eiffelcommons/http/HttpRequest.java

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,27 @@
1919
import java.io.File;
2020
import java.io.IOException;
2121
import java.io.UnsupportedEncodingException;
22-
import java.net.MalformedURLException;
2322
import java.net.URI;
2423
import java.net.URISyntaxException;
25-
import java.net.URL;
26-
import java.util.ArrayList;
24+
import java.util.HashMap;
2725
import java.util.Map;
2826

2927
import org.apache.commons.codec.binary.Base64;
3028
import org.apache.commons.io.FileUtils;
3129
import org.apache.commons.lang3.StringUtils;
32-
import org.apache.http.Consts;
3330
import org.apache.http.Header;
3431
import org.apache.http.HttpHeaders;
35-
import org.apache.http.NameValuePair;
3632
import org.apache.http.client.ClientProtocolException;
3733
import org.apache.http.client.methods.HttpDelete;
3834
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
3935
import org.apache.http.client.methods.HttpGet;
4036
import org.apache.http.client.methods.HttpPost;
4137
import org.apache.http.client.methods.HttpPut;
4238
import org.apache.http.client.methods.HttpRequestBase;
43-
import org.apache.http.client.utils.URLEncodedUtils;
39+
import org.apache.http.client.utils.URIBuilder;
4440
import org.apache.http.entity.ContentType;
4541
import org.apache.http.entity.StringEntity;
4642
import org.apache.http.message.BasicHeader;
47-
import org.apache.http.message.BasicNameValuePair;
4843

4944
import lombok.Getter;
5045
import lombok.Setter;
@@ -67,10 +62,10 @@ public enum HttpMethod {
6762
protected String endpoint;
6863

6964
@Getter
70-
protected ArrayList<NameValuePair> params;
65+
protected Map<String, String> params;
7166

7267
public HttpRequest() {
73-
params = new ArrayList<NameValuePair>();
68+
params = new HashMap<>();
7469
initExecutor(false);
7570
}
7671

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

8681
public HttpRequest(HttpMethod method, boolean persistentClient) {
87-
params = new ArrayList<NameValuePair>();
82+
params = new HashMap<>();
8883
setHttpMethod(method);
8984
initExecutor(persistentClient);
9085
}
@@ -136,7 +131,8 @@ public String getBaseUrl() {
136131
* @param baseUrl
137132
*/
138133
public HttpRequest setBaseUrl(String baseUrl) {
139-
this.baseUrl = trimBaseUrl(baseUrl);
134+
baseUrl = trimBaseUrl(baseUrl);
135+
this.baseUrl = baseUrl;
140136
return this;
141137
}
142138

@@ -209,7 +205,7 @@ public void addParameters(Map<String, String> parameters) {
209205
* @return HttpRequest
210206
*/
211207
public HttpRequest addParameter(String key, String value) {
212-
params.add(new BasicNameValuePair(key, value));
208+
params.put(key, value);
213209
return this;
214210
}
215211

@@ -295,9 +291,9 @@ public HttpRequest setBasicAuth(String username, String password)
295291
*/
296292
public ResponseEntity performRequest()
297293
throws URISyntaxException, ClientProtocolException, IOException {
298-
URI uri = createURI();
299-
uri = addParametersToURI(uri);
300-
request.setURI(uri);
294+
URIBuilder builder = createURIBuilder();
295+
builder = addParametersToURIBuilder(builder);
296+
request.setURI(builder.build());
301297
return executor.executeRequest(request);
302298
}
303299

@@ -306,57 +302,55 @@ public ResponseEntity performRequest()
306302
*
307303
* @return URI
308304
* @throws URISyntaxException
309-
* @throws MalformedURLException
310305
*/
311-
@Deprecated
312-
public URI getURI() throws MalformedURLException, URISyntaxException {
313-
return createURI();
306+
public URI getURI() throws URISyntaxException {
307+
URIBuilder builder = createURIBuilder();
308+
return builder.build();
314309
}
315310

316311
/**
317-
* Function that creates the URI from the baseUrl and endpoint
312+
* Function that adds parameters to the URIBuilder
318313
*
319-
* @param
320-
* @return URI
321-
* @throws MalformedURLException
322-
* @throws URISyntaxException
314+
* @param URIBuilder
315+
* @return URIBuilder
323316
*/
324-
public URI createURI() throws MalformedURLException, URISyntaxException {
325-
if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) {
326-
return new URL(baseUrl + endpoint).toURI();
327-
} else if (!StringUtils.isEmpty(endpoint)) {
328-
return new URL(baseUrl + "/" + endpoint).toURI();
329-
} else {
330-
return new URL(baseUrl).toURI();
317+
private URIBuilder addParametersToURIBuilder(URIBuilder builder) {
318+
if (!params.isEmpty()) {
319+
for (Map.Entry<String, String> entry : params.entrySet()) {
320+
builder.addParameter(entry.getKey(), entry.getValue());
321+
}
331322
}
332323

324+
return builder;
333325
}
334326

335327
/**
336-
* Function that adds parameters to the URI
328+
* Function that creates the URI from the baseUrl and endpoint
337329
*
338-
* @param oldUri
339-
* @return URI
330+
* @param
331+
* @return URIBuilder
340332
* @throws URISyntaxException
341333
*/
342-
private URI addParametersToURI(URI oldUri) throws URISyntaxException {
343-
String query = URLEncodedUtils.format(params, Consts.UTF_8);
344-
URI newUri = new URI(oldUri.getScheme(), oldUri.getAuthority(), oldUri.getPath(), query,
345-
oldUri.getFragment());
346-
return newUri;
334+
private URIBuilder createURIBuilder() throws URISyntaxException {
335+
if (!StringUtils.isEmpty(endpoint) && endpoint.startsWith("/")) {
336+
return new URIBuilder(baseUrl + endpoint);
337+
} else if (!StringUtils.isEmpty(endpoint)) {
338+
return new URIBuilder(baseUrl + "/" + endpoint);
339+
} else {
340+
return new URIBuilder(baseUrl);
341+
}
347342
}
348343

349344
/**
350345
* Function that trims the base url for trailing slashes
351346
*
352-
* @return trimmedUrl
347+
* @return HttpRequest
353348
*/
354349
private String trimBaseUrl(String baseUrl) {
355-
String trimmedUrl = baseUrl;
356350
if (baseUrl.endsWith("/")) {
357-
trimmedUrl = baseUrl.substring(0, baseUrl.length() - 1);
351+
baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
358352
}
359353

360-
return trimmedUrl;
354+
return baseUrl;
361355
}
362-
}
356+
}

src/test/java/com/ericsson/eiffelcommons/http/HttpRequestTest.java

Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.ericsson.eiffelcommons.http;
1+
package com.ericsson.eiffelcommons.httptest;
22

33
import static org.junit.Assert.assertEquals;
44
import static org.junit.Assert.assertNotNull;
@@ -9,7 +9,6 @@
99
import java.io.IOException;
1010
import java.io.InputStream;
1111
import java.io.UnsupportedEncodingException;
12-
import java.net.MalformedURLException;
1312
import java.net.URI;
1413
import java.net.URISyntaxException;
1514
import java.net.UnknownHostException;
@@ -22,6 +21,7 @@
2221
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
2322
import org.apache.http.client.methods.HttpGet;
2423
import org.apache.http.client.methods.HttpRequestBase;
24+
import org.apache.http.client.utils.URIBuilder;
2525
import org.apache.http.entity.ContentType;
2626
import org.junit.Test;
2727
import org.powermock.reflect.Whitebox;
@@ -33,7 +33,6 @@
3333
public class HttpRequestTest {
3434
private static final String URL_1 = "http://something.com";
3535
private static final String URL_2 = "http://something.com/";
36-
private static final String URL_3 = "http://someothing.com";
3736
private static final String URL_BAD_PROTOCOL = "httpl://something.com/";
3837
private static final String URL_BAD_SYNTAX = "http:<<something.com/";
3938
private static final String ENDPOINT_1 = "/testing/test/";
@@ -57,28 +56,29 @@ public class HttpRequestTest {
5756

5857
@Test
5958
public void testBuildingOfURI() throws Exception {
60-
6159
HttpRequest request = new HttpRequest(HttpMethod.POST);
62-
6360
request.setBaseUrl(URL_1);
6461
request.setEndpoint(ENDPOINT_1);
65-
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
66-
assertEquals(EXPECTED_URI, uri.toString());
62+
URIBuilder builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
63+
assertEquals(EXPECTED_URI, builder.toString());
6764

65+
request = new HttpRequest(HttpMethod.GET);
6866
request.setBaseUrl(URL_2);
6967
request.setEndpoint(ENDPOINT_1);
70-
uri = (URI) Whitebox.invokeMethod(request, "createURI");
71-
assertEquals(EXPECTED_URI, uri.toString());
68+
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
69+
assertEquals(EXPECTED_URI, builder.toString());
7270

71+
request = new HttpRequest(HttpMethod.DELETE);
7372
request.setBaseUrl(URL_2);
7473
request.setEndpoint(ENDPOINT_2);
75-
uri = (URI) Whitebox.invokeMethod(request, "createURI");
76-
assertEquals(EXPECTED_URI, uri.toString());
74+
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
75+
assertEquals(EXPECTED_URI, builder.toString());
7776

77+
request = new HttpRequest(HttpMethod.PUT);
7878
request.setBaseUrl(URL_1);
7979
request.setEndpoint(ENDPOINT_2);
80-
uri = (URI) Whitebox.invokeMethod(request, "createURI");
81-
assertEquals(EXPECTED_URI, uri.toString());
80+
builder = (URIBuilder) Whitebox.invokeMethod(request, "createURIBuilder");
81+
assertEquals(EXPECTED_URI, builder.toString());
8282
}
8383

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

163163
@Test(expected = IOException.class)
@@ -186,18 +186,6 @@ public void testParameterProperty() {
186186
}
187187

188188
@Test
189-
public void testAddParametersToURI() throws Exception {
190-
HttpRequest request = new HttpRequest(HttpMethod.GET);
191-
request.setBaseUrl(URL_1)
192-
.addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1)
193-
.addParameter(PARAMETER_KEY_2, PARAMETER_VALUE_2);
194-
URI uri = (URI) Whitebox.invokeMethod(request, "createURI");
195-
URI newUri = (URI) Whitebox.invokeMethod(request, "addParametersToURI", uri);
196-
String expectedURI = URL_1 + "?" + PARAMETER_KEY_1 + "=" + PARAMETER_VALUE_1 + "&"
197-
+ PARAMETER_KEY_2 + "=" + PARAMETER_VALUE_2;
198-
assertEquals(expectedURI, newUri.toString());
199-
}
200-
201189
public void testHeaderProperty() {
202190
HttpRequest request = new HttpRequest(HttpMethod.GET);
203191
request.addHeader(HEADER_KEY_1, HEADER_VALUE_1);
@@ -218,10 +206,10 @@ public void testHeaderProperty() {
218206
}
219207

220208
@Test
221-
public void testGetURI() throws MalformedURLException, URISyntaxException {
209+
public void testGetURI() throws URISyntaxException {
222210
HttpRequest request = new HttpRequest(HttpMethod.GET);
223211
request.setBaseUrl(URL_1).setEndpoint(ENDPOINT_1);
224-
URI uri = request.createURI();
212+
URI uri = request.getURI();
225213
String fullURI = uri.toString();
226214
assertEquals(URL_1 + ENDPOINT_1, fullURI);
227215
}
@@ -230,12 +218,13 @@ public void testGetURI() throws MalformedURLException, URISyntaxException {
230218
public void testPerformRequestUnknownHost()
231219
throws ClientProtocolException, URISyntaxException, IOException {
232220
HttpRequest request = new HttpRequest(HttpMethod.GET);
233-
request.setBaseUrl(URL_3).setEndpoint(ENDPOINT_1);
221+
request.setBaseUrl(URL_1).setEndpoint(ENDPOINT_1);
234222
request.performRequest();
235223
}
236224

237-
@Test(expected = MalformedURLException.class)
238-
public void testPerformRequestBadProtocol() throws ClientProtocolException, URISyntaxException, IOException {
225+
@Test(expected = ClientProtocolException.class)
226+
public void testPerformRequestBadProtocol()
227+
throws ClientProtocolException, URISyntaxException, IOException {
239228
HttpRequest request = new HttpRequest(HttpMethod.GET);
240229
request.setBaseUrl(URL_BAD_PROTOCOL);
241230
request.performRequest();
@@ -253,15 +242,16 @@ public void testPerformRequestBadSyntax()
253242
public void testPerformRequestNoEndpoint()
254243
throws ClientProtocolException, URISyntaxException, IOException {
255244
HttpRequest request = new HttpRequest(HttpMethod.GET);
256-
request.setBaseUrl(URL_3);
245+
request.setBaseUrl(URL_1);
257246
request.performRequest();
258247
}
259248

260249
@Test(expected = UnknownHostException.class)
261250
public void testPerformRequestWithParameters()
262251
throws ClientProtocolException, URISyntaxException, IOException {
263252
HttpRequest request = new HttpRequest(HttpMethod.GET);
264-
request.setBaseUrl(URL_3).addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1);
253+
request.setBaseUrl(URL_1).addParameter(PARAMETER_KEY_1, PARAMETER_VALUE_1);
265254
request.performRequest();
266255
}
267256
}
257+

0 commit comments

Comments
 (0)