1
1
package com .koushikdutta .async .test ;
2
2
3
+ import android .util .Log ;
4
+
3
5
import com .koushikdutta .async .AsyncServer ;
6
+ import com .koushikdutta .async .DataEmitter ;
7
+ import com .koushikdutta .async .DataSink ;
8
+ import com .koushikdutta .async .callback .CompletedCallback ;
9
+ import com .koushikdutta .async .http .AsyncHttpClient ;
10
+ import com .koushikdutta .async .http .AsyncHttpRequest ;
11
+ import com .koushikdutta .async .http .AsyncHttpRequestBody ;
12
+ import com .koushikdutta .async .http .StringBody ;
4
13
import com .koushikdutta .async .http .server .AsyncHttpServer ;
5
14
import com .koushikdutta .async .http .server .AsyncHttpServerRequest ;
6
15
import com .koushikdutta .async .http .server .AsyncHttpServerResponse ;
7
16
import com .koushikdutta .async .http .server .HttpServerRequestCallback ;
8
17
9
18
import junit .framework .TestCase ;
10
19
20
+ import java .net .URI ;
21
+ import java .util .concurrent .TimeoutException ;
22
+
11
23
/**
12
24
* Created by koush on 7/11/13.
13
25
*/
@@ -22,14 +34,15 @@ public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerRespo
22
34
public void run () {
23
35
response .send ("3" );
24
36
}
25
- }, 3000 );
37
+ }, 1000 );
26
38
}
27
39
});
28
40
29
- server .get ("/now" , new HttpServerRequestCallback () {
41
+ server .post ("/now" , new HttpServerRequestCallback () {
30
42
@ Override
31
43
public void onRequest (AsyncHttpServerRequest request , AsyncHttpServerResponse response ) {
32
- response .send ("now" );
44
+ StringBody body = (StringBody )request .getBody ();
45
+ response .send (body .get ());
33
46
}
34
47
});
35
48
}
@@ -44,9 +57,60 @@ protected void setUp() throws Exception {
44
57
@ Override
45
58
protected void tearDown () throws Exception {
46
59
super .tearDown ();
47
- AsyncServer .getDefault ().stop ();
60
+ // AsyncServer.getDefault().stop();
61
+ server .stop ();
62
+
48
63
}
49
64
50
65
public void testTimeout () throws Exception {
66
+ AsyncHttpRequest req = new AsyncHttpRequest (URI .create ("http://localhost:5000/3" ), "GET" );
67
+ req .setTimeout (1000 );
68
+ try {
69
+ AsyncHttpClient .getDefaultInstance ().executeString (req ).get ();
70
+ fail ();
71
+ }
72
+ catch (Exception e ) {
73
+ Log .d ("timeout" , "error" , e );
74
+ assertTrue (e .getCause () instanceof TimeoutException );
75
+ }
76
+
77
+ req = new AsyncHttpRequest (URI .create ("http://localhost:5000/3" ), "GET" );
78
+ assertEquals ("3" , AsyncHttpClient .getDefaultInstance ().executeString (req ).get ());
79
+ }
80
+
81
+ public void testSlowBody () throws Exception {
82
+ AsyncHttpRequest req = new AsyncHttpRequest (URI .create ("http://localhost:5000/now" ), "POST" );
83
+ req .setTimeout (1000 );
84
+ req .setLogging ("slowbody" , Log .VERBOSE );
85
+ req .setBody (new DelayedStringBody ("foo" ));
86
+ assertEquals ("foo" , AsyncHttpClient .getDefaultInstance ().executeString (req ).get ());
87
+
88
+ req = new AsyncHttpRequest (URI .create ("http://localhost:5000/3" ), "GET" );
89
+ req .setLogging ("slowbody" , Log .VERBOSE );
90
+ req .setTimeout (100 );
91
+ req .setBody (new DelayedStringBody ("foo" ));
92
+ try {
93
+ AsyncHttpClient .getDefaultInstance ().executeString (req ).get ();
94
+ fail ();
95
+ }
96
+ catch (Exception e ) {
97
+ Log .d ("timeout" , "error" , e );
98
+ assertTrue (e .getCause () instanceof TimeoutException );
99
+ }
100
+ }
101
+
102
+ class DelayedStringBody extends StringBody {
103
+ public DelayedStringBody (String value ) {
104
+ super (value );
105
+ }
106
+ @ Override
107
+ public void write (final AsyncHttpRequest request , final DataSink sink , final CompletedCallback completed ) {
108
+ AsyncServer .getDefault ().postDelayed (new Runnable () {
109
+ @ Override
110
+ public void run () {
111
+ DelayedStringBody .super .write (request , sink , completed );
112
+ }
113
+ }, 1000 );
114
+ }
51
115
}
52
116
}
0 commit comments