@@ -25,30 +25,12 @@ a copy of this software and associated documentation files (the
2525
2626package com .mashape .unirest .test .http ;
2727
28- import static org .junit .Assert .assertEquals ;
29- import static org .junit .Assert .assertFalse ;
30- import static org .junit .Assert .assertNotNull ;
31- import static org .junit .Assert .assertTrue ;
32- import static org .junit .Assert .fail ;
33-
34- import java .io .File ;
35- import java .io .FileInputStream ;
36- import java .io .FileNotFoundException ;
37- import java .io .IOException ;
38- import java .io .InputStream ;
39- import java .net .InetAddress ;
40- import java .net .URISyntaxException ;
41- import java .net .UnknownHostException ;
42- import java .util .Arrays ;
43- import java .util .Locale ;
44- import java .util .concurrent .CountDownLatch ;
45- import java .util .concurrent .ExecutionException ;
46- import java .util .concurrent .ExecutorService ;
47- import java .util .concurrent .Executors ;
48- import java .util .concurrent .Future ;
49- import java .util .concurrent .TimeUnit ;
50- import java .util .concurrent .atomic .AtomicInteger ;
51-
28+ import com .mashape .unirest .http .*;
29+ import com .mashape .unirest .http .async .Callback ;
30+ import com .mashape .unirest .http .exceptions .UnirestException ;
31+ import com .mashape .unirest .http .options .Options ;
32+ import com .mashape .unirest .request .GetRequest ;
33+ import com .mashape .unirest .request .HttpRequest ;
5234import org .apache .commons .io .IOUtils ;
5335import org .apache .http .entity .ContentType ;
5436import org .apache .http .impl .client .HttpClientBuilder ;
@@ -59,15 +41,15 @@ a copy of this software and associated documentation files (the
5941import org .junit .Before ;
6042import org .junit .Test ;
6143
62- import com . mashape . unirest . http . HttpResponse ;
63- import com . mashape . unirest . http . JsonNode ;
64- import com . mashape . unirest . http . ObjectMapper ;
65- import com . mashape . unirest . http . Unirest ;
66- import com . mashape . unirest . http . async . Callback ;
67- import com . mashape . unirest . http . exceptions . UnirestException ;
68- import com . mashape . unirest . http . options . Options ;
69- import com . mashape . unirest . request . GetRequest ;
70- import com . mashape . unirest . request . HttpRequest ;
44+ import java . io .* ;
45+ import java . net . InetAddress ;
46+ import java . net . URISyntaxException ;
47+ import java . net . UnknownHostException ;
48+ import java . util . Arrays ;
49+ import java . util . concurrent .* ;
50+ import java . util . concurrent . atomic . AtomicInteger ;
51+
52+ import static org . junit . Assert .* ;
7153
7254public class UnirestTest {
7355
@@ -248,34 +230,34 @@ public void testAsyncCallback() throws JSONException, InterruptedException, Exec
248230 .field ("param2" ,"bye" )
249231 .asJsonAsync (new Callback <JsonNode >() {
250232
251- public void failed (UnirestException e ) {
252- fail ();
253- }
233+ public void failed (UnirestException e ) {
234+ fail ();
235+ }
254236
255- public void completed (HttpResponse <JsonNode > jsonResponse ) {
256- assertTrue (jsonResponse .getHeaders ().size () > 0 );
257- assertTrue (jsonResponse .getBody ().toString ().length () > 0 );
258- assertFalse (jsonResponse .getRawBody () == null );
259- assertEquals (200 , jsonResponse .getStatus ());
237+ public void completed (HttpResponse <JsonNode > jsonResponse ) {
238+ assertTrue (jsonResponse .getHeaders ().size () > 0 );
239+ assertTrue (jsonResponse .getBody ().toString ().length () > 0 );
240+ assertFalse (jsonResponse .getRawBody () == null );
241+ assertEquals (200 , jsonResponse .getStatus ());
260242
261- JsonNode json = jsonResponse .getBody ();
262- assertFalse (json .isArray ());
263- assertNotNull (json .getObject ());
264- assertNotNull (json .getArray ());
265- assertEquals (1 , json .getArray ().length ());
266- assertNotNull (json .getArray ().get (0 ));
243+ JsonNode json = jsonResponse .getBody ();
244+ assertFalse (json .isArray ());
245+ assertNotNull (json .getObject ());
246+ assertNotNull (json .getArray ());
247+ assertEquals (1 , json .getArray ().length ());
248+ assertNotNull (json .getArray ().get (0 ));
267249
268- assertEquals ("value1" , json .getObject ().getJSONObject ("form" ).getString ("param1" ));
269- assertEquals ("bye" , json .getObject ().getJSONObject ("form" ).getString ("param2" ));
250+ assertEquals ("value1" , json .getObject ().getJSONObject ("form" ).getString ("param1" ));
251+ assertEquals ("bye" , json .getObject ().getJSONObject ("form" ).getString ("param2" ));
270252
271- status = true ;
272- lock .countDown ();
273- }
253+ status = true ;
254+ lock .countDown ();
255+ }
274256
275- public void cancelled () {
276- fail ();
277- }
278- });
257+ public void cancelled () {
258+ fail ();
259+ }
260+ });
279261
280262 lock .await (10 , TimeUnit .SECONDS );
281263 assertTrue (status );
@@ -831,33 +813,36 @@ public void setTimeoutsAndCustomClient() {
831813 }
832814
833815 @ Test
834- public void testObjectMapper () throws UnirestException , IOException {
835- final String responseJson = "{ \" locale \" : \" english \" }" ;
816+ public void testObjectMapperRead () throws UnirestException , IOException {
817+ Unirest . setObjectMapper ( new JacksonObjectMapper ()) ;
836818
837- Unirest .setObjectMapper (new ObjectMapper () {
838- public Object readValue (String ignored ) {
839- return Locale .ENGLISH ;
840- }
819+ GetResponse getResponseMock = new GetResponse ();
820+ getResponseMock .setUrl ("http://httpbin.org/get" );
841821
842- public String writeValue (Object ignored ) {
843- return responseJson ;
844- }
845- });
822+ HttpResponse <GetResponse > getResponse = Unirest .get (getResponseMock .getUrl ())
823+ .asObject (GetResponse .class );
846824
847- HttpResponse <Locale > getResponse = Unirest .get ("http://httpbin.org/get" ).asObject (Locale .class );
848825 assertEquals (200 , getResponse .getStatus ());
849- assertEquals (getResponse .getBody (), Locale .ENGLISH );
826+ assertEquals (getResponse .getBody ().getUrl (), getResponseMock .getUrl ());
827+ }
850828
851- HttpResponse <JsonNode > postResponse = Unirest .post ("http://httpbin.org/post" )
852- .header ("accept" , "application/json" )
853- .header ("Content-Type" , "application/json" )
854- .body (Locale .ENGLISH )
855- .asJson ();
829+ @ Test
830+ public void testObjectMapperWrite () throws UnirestException , IOException {
831+ Unirest .setObjectMapper (new JacksonObjectMapper ());
832+
833+ GetResponse postResponseMock = new GetResponse ();
834+ postResponseMock .setUrl ("http://httpbin.org/post" );
835+
836+ HttpResponse <JsonNode > postResponse = Unirest .post (postResponseMock .getUrl ())
837+ .header ("accept" , "application/json" )
838+ .header ("Content-Type" , "application/json" )
839+ .body (postResponseMock )
840+ .asJson ();
841+
842+ assertEquals (200 , postResponse .getStatus ());
843+ assertEquals (postResponse .getBody ().getObject ().getString ("data" ), "{\" url\" :\" http://httpbin.org/post\" }" );
844+ }
856845
857- assertEquals (200 , postResponse .getStatus ());
858- assertEquals (postResponse .getBody ().getObject ().getString ("data" ), responseJson );
859- }
860-
861846 @ Test
862847 public void testPostProvidesSortedParams () throws IOException {
863848 // Verify that fields are encoded into the body in sorted order.
0 commit comments