1
- package com .ericsson .eiffelcommons .http ;
1
+ package com .ericsson .eiffelcommons .httptest ;
2
2
3
3
import static org .junit .Assert .assertEquals ;
4
4
import static org .junit .Assert .assertNotNull ;
9
9
import java .io .IOException ;
10
10
import java .io .InputStream ;
11
11
import java .io .UnsupportedEncodingException ;
12
- import java .net .MalformedURLException ;
13
12
import java .net .URI ;
14
13
import java .net .URISyntaxException ;
15
14
import java .net .UnknownHostException ;
22
21
import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
23
22
import org .apache .http .client .methods .HttpGet ;
24
23
import org .apache .http .client .methods .HttpRequestBase ;
24
+ import org .apache .http .client .utils .URIBuilder ;
25
25
import org .apache .http .entity .ContentType ;
26
26
import org .junit .Test ;
27
27
import org .powermock .reflect .Whitebox ;
33
33
public class HttpRequestTest {
34
34
private static final String URL_1 = "http://something.com" ;
35
35
private static final String URL_2 = "http://something.com/" ;
36
- private static final String URL_3 = "http://someothing.com" ;
37
36
private static final String URL_BAD_PROTOCOL = "httpl://something.com/" ;
38
37
private static final String URL_BAD_SYNTAX = "http:<<something.com/" ;
39
38
private static final String ENDPOINT_1 = "/testing/test/" ;
@@ -57,28 +56,29 @@ public class HttpRequestTest {
57
56
58
57
@ Test
59
58
public void testBuildingOfURI () throws Exception {
60
-
61
59
HttpRequest request = new HttpRequest (HttpMethod .POST );
62
-
63
60
request .setBaseUrl (URL_1 );
64
61
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 ());
67
64
65
+ request = new HttpRequest (HttpMethod .GET );
68
66
request .setBaseUrl (URL_2 );
69
67
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 ());
72
70
71
+ request = new HttpRequest (HttpMethod .DELETE );
73
72
request .setBaseUrl (URL_2 );
74
73
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 ());
77
76
77
+ request = new HttpRequest (HttpMethod .PUT );
78
78
request .setBaseUrl (URL_1 );
79
79
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 ());
82
82
}
83
83
84
84
@ Test
@@ -157,7 +157,7 @@ public void testBodyProperty() throws UnsupportedOperationException, IOException
157
157
actualBody = IOUtils .toString (entity .getContent (), "UTF-8" );
158
158
stream .close ();
159
159
assertTrue (actualHeader .contains (BODY_HEADER_DEFAULT ));
160
- assertEquals (BODY_CONTENT , actualBody . replace ( System . getProperty ( "line.separator" ), "" ) );
160
+ assertEquals (BODY_CONTENT + " \n " , actualBody );
161
161
}
162
162
163
163
@ Test (expected = IOException .class )
@@ -186,18 +186,6 @@ public void testParameterProperty() {
186
186
}
187
187
188
188
@ 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
-
201
189
public void testHeaderProperty () {
202
190
HttpRequest request = new HttpRequest (HttpMethod .GET );
203
191
request .addHeader (HEADER_KEY_1 , HEADER_VALUE_1 );
@@ -218,10 +206,10 @@ public void testHeaderProperty() {
218
206
}
219
207
220
208
@ Test
221
- public void testGetURI () throws MalformedURLException , URISyntaxException {
209
+ public void testGetURI () throws URISyntaxException {
222
210
HttpRequest request = new HttpRequest (HttpMethod .GET );
223
211
request .setBaseUrl (URL_1 ).setEndpoint (ENDPOINT_1 );
224
- URI uri = request .createURI ();
212
+ URI uri = request .getURI ();
225
213
String fullURI = uri .toString ();
226
214
assertEquals (URL_1 + ENDPOINT_1 , fullURI );
227
215
}
@@ -230,12 +218,13 @@ public void testGetURI() throws MalformedURLException, URISyntaxException {
230
218
public void testPerformRequestUnknownHost ()
231
219
throws ClientProtocolException , URISyntaxException , IOException {
232
220
HttpRequest request = new HttpRequest (HttpMethod .GET );
233
- request .setBaseUrl (URL_3 ).setEndpoint (ENDPOINT_1 );
221
+ request .setBaseUrl (URL_1 ).setEndpoint (ENDPOINT_1 );
234
222
request .performRequest ();
235
223
}
236
224
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 {
239
228
HttpRequest request = new HttpRequest (HttpMethod .GET );
240
229
request .setBaseUrl (URL_BAD_PROTOCOL );
241
230
request .performRequest ();
@@ -253,15 +242,16 @@ public void testPerformRequestBadSyntax()
253
242
public void testPerformRequestNoEndpoint ()
254
243
throws ClientProtocolException , URISyntaxException , IOException {
255
244
HttpRequest request = new HttpRequest (HttpMethod .GET );
256
- request .setBaseUrl (URL_3 );
245
+ request .setBaseUrl (URL_1 );
257
246
request .performRequest ();
258
247
}
259
248
260
249
@ Test (expected = UnknownHostException .class )
261
250
public void testPerformRequestWithParameters ()
262
251
throws ClientProtocolException , URISyntaxException , IOException {
263
252
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 );
265
254
request .performRequest ();
266
255
}
267
256
}
257
+
0 commit comments