1- package com .ericsson .eiffelcommons .http ;
1+ package com .ericsson .eiffelcommons .httptest ;
22
33import static org .junit .Assert .assertEquals ;
44import static org .junit .Assert .assertNotNull ;
99import java .io .IOException ;
1010import java .io .InputStream ;
1111import java .io .UnsupportedEncodingException ;
12- import java .net .MalformedURLException ;
1312import java .net .URI ;
1413import java .net .URISyntaxException ;
1514import java .net .UnknownHostException ;
2221import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
2322import org .apache .http .client .methods .HttpGet ;
2423import org .apache .http .client .methods .HttpRequestBase ;
24+ import org .apache .http .client .utils .URIBuilder ;
2525import org .apache .http .entity .ContentType ;
2626import org .junit .Test ;
2727import org .powermock .reflect .Whitebox ;
3333public 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