15
15
*/
16
16
package org .asynchttpclient .async ;
17
17
18
+ import java .io .File ;
19
+ import java .io .FileOutputStream ;
20
+ import java .io .IOException ;
21
+ import java .net .ServerSocket ;
22
+ import java .nio .charset .Charset ;
23
+ import java .util .Enumeration ;
24
+ import java .util .UUID ;
25
+
26
+ import javax .servlet .ServletException ;
27
+ import javax .servlet .http .Cookie ;
28
+ import javax .servlet .http .HttpServletRequest ;
29
+ import javax .servlet .http .HttpServletResponse ;
30
+
31
+ import org .apache .commons .io .FileUtils ;
18
32
import org .asynchttpclient .AsyncCompletionHandler ;
19
33
import org .asynchttpclient .AsyncHandler ;
20
34
import org .asynchttpclient .AsyncHttpClient ;
34
48
import org .testng .annotations .AfterClass ;
35
49
import org .testng .annotations .BeforeClass ;
36
50
37
- import javax .servlet .ServletException ;
38
- import javax .servlet .http .HttpServletRequest ;
39
- import javax .servlet .http .HttpServletResponse ;
40
- import java .io .IOException ;
41
- import java .net .ServerSocket ;
42
- import java .util .Enumeration ;
43
-
44
51
public abstract class AbstractBasicTest {
45
52
protected final Logger log = LoggerFactory .getLogger (AbstractBasicTest .class );
46
53
protected Server server ;
47
54
protected int port1 ;
48
55
protected int port2 ;
49
56
50
57
public final static int TIMEOUT = 30 ;
58
+ public static final File TMP = new File (System .getProperty ("java.io.tmpdir" ), "ahc-tests-" + UUID .randomUUID ().toString ().substring (0 , 8 ));
59
+ public static final byte [] PATTERN_BYTES = "FooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQixFooBarBazQix" .getBytes (Charset .forName ("UTF-16" ));
60
+ public static final File LARGE_IMAGE_FILE ;
61
+ public static byte [] LARGE_IMAGE_BYTES ;
62
+ public static final File SIMPLE_TEXT_FILE ;
63
+
64
+ static {
65
+ try {
66
+ TMP .mkdirs ();
67
+ TMP .deleteOnExit ();
68
+ LARGE_IMAGE_FILE = new File (AbstractBasicTest .class .getClassLoader ().getResource ("300k.png" ).toURI ());
69
+ LARGE_IMAGE_BYTES = FileUtils .readFileToByteArray (LARGE_IMAGE_FILE );
70
+ SIMPLE_TEXT_FILE = new File (AbstractBasicTest .class .getClassLoader ().getResource ("SimpleTextFile.txt" ).toURI ());
71
+ } catch (Exception e ) {
72
+ throw new ExceptionInInitializerError (e );
73
+ }
74
+ }
75
+
76
+ public static File createTempFile (byte [] pattern , int repeat ) throws IOException {
77
+ File tmpFile = File .createTempFile ("tmpfile-" , ".data" , TMP );
78
+ tmpFile .deleteOnExit ();
79
+ FileOutputStream out = null ;
80
+ try {
81
+ out = new FileOutputStream (tmpFile );
82
+ for (int i = 0 ; i < repeat ; i ++) {
83
+ out .write (pattern );
84
+ }
85
+
86
+ long expectedFileSize = PATTERN_BYTES .length * repeat ;
87
+ Assert .assertEquals (expectedFileSize , tmpFile .length (), "Invalid file length" );
88
+
89
+ return tmpFile ;
90
+ } finally {
91
+ if (out != null ) {
92
+ out .close ();
93
+ }
94
+ }
95
+ }
51
96
52
97
public static class EchoHandler extends AbstractHandler {
53
98
54
- /* @Override */
55
- public void handle (String pathInContext ,
56
- Request request ,
57
- HttpServletRequest httpRequest ,
58
- HttpServletResponse httpResponse ) throws IOException , ServletException {
99
+ @ Override
100
+ public void handle (String pathInContext , Request request , HttpServletRequest httpRequest , HttpServletResponse httpResponse ) throws IOException , ServletException {
59
101
60
102
if (httpRequest .getHeader ("X-HEAD" ) != null ) {
61
103
httpResponse .setContentLength (1 );
@@ -68,8 +110,9 @@ public void handle(String pathInContext,
68
110
}
69
111
70
112
if (request .getMethod ().equalsIgnoreCase ("OPTIONS" )) {
71
- httpResponse .addHeader ("Allow" ,"GET,HEAD,POST,OPTIONS,TRACE" );
72
- };
113
+ httpResponse .addHeader ("Allow" , "GET,HEAD,POST,OPTIONS,TRACE" );
114
+ }
115
+ ;
73
116
74
117
Enumeration <?> e = httpRequest .getHeaderNames ();
75
118
String param ;
@@ -110,9 +153,9 @@ public void handle(String pathInContext,
110
153
111
154
httpResponse .addHeader ("X-KEEP-ALIVE" , httpRequest .getRemoteAddr () + ":" + httpRequest .getRemotePort ());
112
155
113
- javax . servlet . http . Cookie [] cs = httpRequest .getCookies ();
156
+ Cookie [] cs = httpRequest .getCookies ();
114
157
if (cs != null ) {
115
- for (javax . servlet . http . Cookie c : cs ) {
158
+ for (Cookie c : cs ) {
116
159
httpResponse .addCookie (c );
117
160
}
118
161
}
@@ -142,9 +185,35 @@ public void handle(String pathInContext,
142
185
}
143
186
}
144
187
188
+ @ BeforeClass (alwaysRun = true )
189
+ public void setUpGlobal () throws Exception {
190
+ server = new Server ();
191
+
192
+ port1 = findFreePort ();
193
+ port2 = findFreePort ();
194
+
195
+ Connector listener = new SelectChannelConnector ();
196
+
197
+ listener .setHost ("127.0.0.1" );
198
+ listener .setPort (port1 );
199
+
200
+ server .addConnector (listener );
201
+
202
+ listener = new SelectChannelConnector ();
203
+ listener .setHost ("127.0.0.1" );
204
+ listener .setPort (port2 );
205
+
206
+ server .addConnector (listener );
207
+
208
+ server .setHandler (configureHandler ());
209
+ server .start ();
210
+ log .info ("Local HTTP server started successfully" );
211
+ }
212
+
145
213
@ AfterClass (alwaysRun = true )
146
214
public void tearDownGlobal () throws Exception {
147
- server .stop ();
215
+ if (server != null )
216
+ server .stop ();
148
217
}
149
218
150
219
protected int findFreePort () throws IOException {
@@ -154,8 +223,7 @@ protected int findFreePort() throws IOException {
154
223
socket = new ServerSocket (0 );
155
224
156
225
return socket .getLocalPort ();
157
- }
158
- finally {
226
+ } finally {
159
227
if (socket != null ) {
160
228
socket .close ();
161
229
}
@@ -174,31 +242,6 @@ public AbstractHandler configureHandler() throws Exception {
174
242
return new EchoHandler ();
175
243
}
176
244
177
- @ BeforeClass (alwaysRun = true )
178
- public void setUpGlobal () throws Exception {
179
- server = new Server ();
180
-
181
- port1 = findFreePort ();
182
- port2 = findFreePort ();
183
-
184
- Connector listener = new SelectChannelConnector ();
185
-
186
- listener .setHost ("127.0.0.1" );
187
- listener .setPort (port1 );
188
-
189
- server .addConnector (listener );
190
-
191
- listener = new SelectChannelConnector ();
192
- listener .setHost ("127.0.0.1" );
193
- listener .setPort (port2 );
194
-
195
- server .addConnector (listener );
196
-
197
- server .setHandler (configureHandler ());
198
- server .start ();
199
- log .info ("Local HTTP server started successfully" );
200
- }
201
-
202
245
public static class AsyncCompletionHandlerAdapter extends AsyncCompletionHandler <Response > {
203
246
public Runnable runnable ;
204
247
@@ -207,7 +250,7 @@ public Response onCompleted(Response response) throws Exception {
207
250
return response ;
208
251
}
209
252
210
- /* @Override */
253
+ @ Override
211
254
public void onThrowable (Throwable t ) {
212
255
t .printStackTrace ();
213
256
Assert .fail ("Unexpected exception: " + t .getMessage (), t );
@@ -217,35 +260,32 @@ public void onThrowable(Throwable t) {
217
260
218
261
public static class AsyncHandlerAdapter implements AsyncHandler <String > {
219
262
220
-
221
- /* @Override */
263
+ @ Override
222
264
public void onThrowable (Throwable t ) {
223
265
t .printStackTrace ();
224
266
Assert .fail ("Unexpected exception" , t );
225
267
}
226
268
227
- /* @Override */
269
+ @ Override
228
270
public STATE onBodyPartReceived (final HttpResponseBodyPart content ) throws Exception {
229
271
return STATE .CONTINUE ;
230
272
}
231
273
232
- /* @Override */
274
+ @ Override
233
275
public STATE onStatusReceived (final HttpResponseStatus responseStatus ) throws Exception {
234
276
return STATE .CONTINUE ;
235
277
}
236
278
237
- /* @Override */
279
+ @ Override
238
280
public STATE onHeadersReceived (final HttpResponseHeaders headers ) throws Exception {
239
281
return STATE .CONTINUE ;
240
282
}
241
283
242
- /* @Override */
284
+ @ Override
243
285
public String onCompleted () throws Exception {
244
286
return "" ;
245
287
}
246
-
247
288
}
248
289
249
290
public abstract AsyncHttpClient getAsyncHttpClient (AsyncHttpClientConfig config );
250
-
251
291
}
0 commit comments