Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2c1803

Browse files
committed
Migrate ReactiveStreamsTest to Tomcat
1 parent94f8ef2 commitb2c1803

File tree

1 file changed

+165
-13
lines changed

1 file changed

+165
-13
lines changed

‎client/src/test/java/org/asynchttpclient/reactivestreams/ReactiveStreamsTest.java‎

Lines changed: 165 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,30 @@
1616
importstaticorg.asynchttpclient.Dsl.*;
1717
importstaticorg.asynchttpclient.test.TestUtils.LARGE_IMAGE_BYTES;
1818
importstaticorg.testng.Assert.assertEquals;
19+
importio.netty.buffer.ByteBuf;
20+
importio.netty.buffer.Unpooled;
1921
importio.netty.handler.codec.http.HttpHeaders;
2022

2123
importjava.io.ByteArrayOutputStream;
22-
importjava.nio.ByteBuffer;
24+
importjava.io.File;
25+
importjava.io.IOException;
2326
importjava.util.ArrayList;
2427
importjava.util.Collections;
28+
importjava.util.Enumeration;
2529
importjava.util.Iterator;
2630
importjava.util.List;
2731
importjava.util.concurrent.CountDownLatch;
2832
importjava.util.concurrent.ExecutionException;
2933

30-
importorg.asynchttpclient.AbstractBasicTest;
34+
importjavax.servlet.ServletException;
35+
importjavax.servlet.http.Cookie;
36+
importjavax.servlet.http.HttpServlet;
37+
importjavax.servlet.http.HttpServletRequest;
38+
importjavax.servlet.http.HttpServletResponse;
39+
40+
importorg.apache.catalina.Context;
41+
importorg.apache.catalina.startup.Tomcat;
42+
importorg.apache.commons.io.IOUtils;
3143
importorg.asynchttpclient.AsyncHttpClient;
3244
importorg.asynchttpclient.BoundRequestBuilder;
3345
importorg.asynchttpclient.HttpResponseBodyPart;
@@ -39,18 +51,158 @@
3951
importorg.reactivestreams.Publisher;
4052
importorg.reactivestreams.Subscriber;
4153
importorg.reactivestreams.Subscription;
54+
importorg.slf4j.Logger;
55+
importorg.slf4j.LoggerFactory;
56+
importorg.testng.annotations.AfterClass;
57+
importorg.testng.annotations.BeforeClass;
4258
importorg.testng.annotations.Test;
4359

4460
importrx.Observable;
4561
importrx.RxReactiveStreams;
4662

47-
publicclassReactiveStreamsTestextendsAbstractBasicTest {
63+
publicclassReactiveStreamsTest {
64+
65+
privatestaticfinalLoggerLOGGER =LoggerFactory.getLogger(ReactiveStreamsTest.class);
4866

49-
publicstaticPublisher<ByteBuffer>createPublisher(finalbyte[]bytes,finalintchunkSize) {
50-
Observable<ByteBuffer>observable =Observable.from(newByteBufferIterable(bytes,chunkSize));
67+
publicstaticPublisher<ByteBuf>createPublisher(finalbyte[]bytes,finalintchunkSize) {
68+
Observable<ByteBuf>observable =Observable.from(newByteBufIterable(bytes,chunkSize));
5169
returnRxReactiveStreams.toPublisher(observable);
5270
}
5371

72+
privateTomcattomcat;
73+
privateintport1;
74+
75+
@SuppressWarnings("serial")
76+
@BeforeClass(alwaysRun =true)
77+
publicvoidsetUpGlobal()throwsException {
78+
79+
Stringpath =newFile(".").getAbsolutePath() +"/target";
80+
81+
tomcat =newTomcat();
82+
tomcat.setHostname("localhost");
83+
tomcat.setPort(0);
84+
tomcat.setBaseDir(path);
85+
Contextctx =tomcat.addContext("",path);
86+
87+
Tomcat.addServlet(ctx,"webdav",newHttpServlet() {
88+
89+
@Override
90+
publicvoidservice(HttpServletRequesthttpRequest,HttpServletResponsehttpResponse)throwsServletException,IOException {
91+
LOGGER.debug("Echo received request {} on path {}",httpRequest,httpRequest.getServletContext().getContextPath());
92+
93+
if (httpRequest.getHeader("X-HEAD") !=null) {
94+
httpResponse.setContentLength(1);
95+
}
96+
97+
if (httpRequest.getHeader("X-ISO") !=null) {
98+
httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_ISO_8859_1_CHARSET);
99+
}else {
100+
httpResponse.setContentType(TestUtils.TEXT_HTML_CONTENT_TYPE_WITH_UTF_8_CHARSET);
101+
}
102+
103+
if (httpRequest.getMethod().equalsIgnoreCase("OPTIONS")) {
104+
httpResponse.addHeader("Allow","GET,HEAD,POST,OPTIONS,TRACE");
105+
}
106+
107+
Enumeration<String>e =httpRequest.getHeaderNames();
108+
StringheaderName;
109+
while (e.hasMoreElements()) {
110+
headerName =e.nextElement();
111+
if (headerName.startsWith("LockThread")) {
112+
finalintsleepTime =httpRequest.getIntHeader(headerName);
113+
try {
114+
Thread.sleep(sleepTime == -1 ?40 :sleepTime *1000);
115+
}catch (InterruptedExceptionex) {
116+
}
117+
}
118+
119+
if (headerName.startsWith("X-redirect")) {
120+
httpResponse.sendRedirect(httpRequest.getHeader("X-redirect"));
121+
return;
122+
}
123+
httpResponse.addHeader("X-" +headerName,httpRequest.getHeader(headerName));
124+
}
125+
126+
StringpathInfo =httpRequest.getPathInfo();
127+
if (pathInfo !=null)
128+
httpResponse.addHeader("X-pathInfo",pathInfo);
129+
130+
StringqueryString =httpRequest.getQueryString();
131+
if (queryString !=null)
132+
httpResponse.addHeader("X-queryString",queryString);
133+
134+
httpResponse.addHeader("X-KEEP-ALIVE",httpRequest.getRemoteAddr() +":" +httpRequest.getRemotePort());
135+
136+
Cookie[]cs =httpRequest.getCookies();
137+
if (cs !=null) {
138+
for (Cookiec :cs) {
139+
httpResponse.addCookie(c);
140+
}
141+
}
142+
143+
Enumeration<String>i =httpRequest.getParameterNames();
144+
if (i.hasMoreElements()) {
145+
StringBuilderrequestBody =newStringBuilder();
146+
while (i.hasMoreElements()) {
147+
headerName =i.nextElement();
148+
httpResponse.addHeader("X-" +headerName,httpRequest.getParameter(headerName));
149+
requestBody.append(headerName);
150+
requestBody.append("_");
151+
}
152+
153+
if (requestBody.length() >0) {
154+
Stringbody =requestBody.toString();
155+
httpResponse.getOutputStream().write(body.getBytes());
156+
}
157+
}
158+
159+
StringrequestBodyLength =httpRequest.getHeader("X-" +CONTENT_LENGTH);
160+
161+
if (requestBodyLength !=null) {
162+
byte[]requestBodyBytes =IOUtils.toByteArray(httpRequest.getInputStream());
163+
inttotal =requestBodyBytes.length;
164+
165+
httpResponse.addIntHeader("X-" +CONTENT_LENGTH,total);
166+
Stringmd5 =TestUtils.md5(requestBodyBytes,0,total);
167+
httpResponse.addHeader(CONTENT_MD5.toString(),md5);
168+
169+
httpResponse.getOutputStream().write(requestBodyBytes,0,total);
170+
}else {
171+
intsize =16384;
172+
if (httpRequest.getContentLength() >0) {
173+
size =httpRequest.getContentLength();
174+
}
175+
if (size >0) {
176+
intread =0;
177+
while (read > -1) {
178+
byte[]bytes =newbyte[size];
179+
read =httpRequest.getInputStream().read(bytes);
180+
if (read >0) {
181+
httpResponse.getOutputStream().write(bytes,0,read);
182+
}
183+
}
184+
}
185+
}
186+
187+
httpResponse.getOutputStream().flush();
188+
// FIXME don't always close, depends on the test, cf ReactiveStreamsTest
189+
// httpResponse.getOutputStream().close();
190+
}
191+
});
192+
ctx.addServletMappingDecoded("/*","webdav");
193+
tomcat.start();
194+
port1 =tomcat.getConnector().getLocalPort();
195+
}
196+
197+
@AfterClass(alwaysRun =true)
198+
publicvoidtearDownGlobal()throwsInterruptedException,Exception {
199+
tomcat.stop();
200+
}
201+
202+
privateStringgetTargetUrl() {
203+
returnString.format("http://localhost:%d/foo/test",port1);
204+
}
205+
54206
@Test(groups ="standalone")
55207
publicvoidtestStreamingPutImage()throwsException {
56208
try (AsyncHttpClientclient =asyncHttpClient(config().setRequestTimeout(100 *6000))) {
@@ -114,8 +266,8 @@ public static void main(String[] args) throws Exception {
114266
@Test(groups ="standalone",expectedExceptions =ExecutionException.class)
115267
publicvoidtestFailingStream()throwsException {
116268
try (AsyncHttpClientclient =asyncHttpClient(config().setRequestTimeout(100 *6000))) {
117-
Observable<ByteBuffer>failingObservable =Observable.error(newFailedStream());
118-
Publisher<ByteBuffer>failingPublisher =RxReactiveStreams.toPublisher(failingObservable);
269+
Observable<ByteBuf>failingObservable =Observable.error(newFailedStream());
270+
Publisher<ByteBuf>failingPublisher =RxReactiveStreams.toPublisher(failingObservable);
119271

120272
client.preparePut(getTargetUrl()).setBody(failingPublisher).execute().get();
121273
}
@@ -337,18 +489,18 @@ public void onComplete() {
337489
}
338490
}
339491

340-
staticclassByteBufferIterableimplementsIterable<ByteBuffer> {
492+
staticclassByteBufIterableimplementsIterable<ByteBuf> {
341493
privatefinalbyte[]payload;
342494
privatefinalintchunkSize;
343495

344-
publicByteBufferIterable(byte[]payload,intchunkSize) {
496+
publicByteBufIterable(byte[]payload,intchunkSize) {
345497
this.payload =payload;
346498
this.chunkSize =chunkSize;
347499
}
348500

349501
@Override
350-
publicIterator<ByteBuffer>iterator() {
351-
returnnewIterator<ByteBuffer>() {
502+
publicIterator<ByteBuf>iterator() {
503+
returnnewIterator<ByteBuf>() {
352504
privatevolatileintcurrentIndex =0;
353505

354506
@Override
@@ -357,11 +509,11 @@ public boolean hasNext() {
357509
}
358510

359511
@Override
360-
publicByteBuffernext() {
512+
publicByteBufnext() {
361513
intthisCurrentIndex =currentIndex;
362514
intlength =Math.min(chunkSize,payload.length -thisCurrentIndex);
363515
currentIndex +=length;
364-
returnByteBuffer.wrap(payload,thisCurrentIndex,length);
516+
returnUnpooled.wrappedBuffer(payload,thisCurrentIndex,length);
365517
}
366518

367519
@Override

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp