28
28
import kong .unirest .Unirest ;
29
29
import org .junit .jupiter .api .Test ;
30
30
31
+ import java .nio .charset .StandardCharsets ;
32
+
33
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
34
+
31
35
class AsBytesTest extends BddTest {
32
36
JacksonObjectMapper om = new JacksonObjectMapper ();
33
37
34
38
@ Test
35
39
void getGetResultAsBytes () {
36
40
byte [] content = Unirest .get (MockServer .GET )
37
- .asBytes ()
38
- .getBody ();
41
+ .asBytes ()
42
+ .getBody ();
39
43
40
44
RequestCapture cap = om .readValue (content , RequestCapture .class );
41
45
@@ -57,15 +61,35 @@ void getGetResultAsBytesAsync() throws Exception {
57
61
@ Test
58
62
void getGetResultAsBytesAsyncCallback () throws Exception {
59
63
Unirest .get (MockServer .GET )
60
- .queryString ("fruit" ,"apple" )
64
+ .queryString ("fruit" , "apple" )
61
65
.asBytesAsync (r -> {
62
66
RequestCapture cap = om .readValue (r .getBody (), RequestCapture .class );
63
- cap .assertParam ("fruit" ,"apple" );
67
+ cap .assertParam ("fruit" , "apple" );
64
68
asyncSuccess ();
65
69
})
66
70
.get ()
67
71
.getBody ();
68
72
69
73
assertAsync ();
70
74
}
75
+
76
+ @ Test // https://github.com/Kong/unirest-java/issues/424
77
+ void mappingErrorsFromAsBytes () {
78
+ MockServer .setStringResponse ("howdy" );
79
+ String r = Unirest .get (MockServer .ERROR_RESPONSE )
80
+ .asBytes ()
81
+ .mapError (String .class );
82
+
83
+ assertEquals ("howdy" , r );
84
+ }
85
+
86
+ @ Test // https://github.com/Kong/unirest-java/issues/424
87
+ void mappingErrorsFromAsBytesMapped () {
88
+ MockServer .setJsonAsResponse (new Foo ("howdy" ));
89
+ Foo r = Unirest .get (MockServer .ERROR_RESPONSE )
90
+ .asBytes ()
91
+ .mapError (Foo .class );
92
+
93
+ assertEquals ("howdy" , r .bar );
94
+ }
71
95
}
0 commit comments