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

Commit9a5f9ef

Browse files
author
Stephane Landelle
committed
Drop InputStreamBodyGenerator marking strategy,closeAsyncHttpClient#760
1 parent1abbe75 commit9a5f9ef

File tree

2 files changed

+48
-84
lines changed

2 files changed

+48
-84
lines changed

‎src/main/java/com/ning/http/client/generators/InputStreamBodyGenerator.java‎

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,6 @@ public class InputStreamBodyGenerator implements BodyGenerator {
3939

4040
publicInputStreamBodyGenerator(InputStreaminputStream) {
4141
this.inputStream =inputStream;
42-
43-
if (inputStream.markSupported()) {
44-
inputStream.mark(0);
45-
}else {
46-
logger.info("inputStream.markSupported() not supported. Some features will not work.");
47-
}
4842
}
4943

5044
/**
@@ -96,9 +90,6 @@ public long read(ByteBuffer buffer) throws IOException {
9690

9791
returnbuffer.position();
9892
}else {
99-
if (inputStream.markSupported()) {
100-
inputStream.reset();
101-
}
10293
eof =false;
10394
}
10495
return -1;
@@ -114,14 +105,8 @@ public long read(ByteBuffer buffer) throws IOException {
114105
buffer.put(chunk,0,read);
115106
// Was missing the final chunk \r\n.
116107
buffer.put(END_PADDING);
117-
}else {
118-
if (read >0) {
119-
buffer.put(chunk,0,read);
120-
}else {
121-
if (inputStream.markSupported()) {
122-
inputStream.reset();
123-
}
124-
}
108+
}elseif (read >0) {
109+
buffer.put(chunk,0,read);
125110
}
126111
returnread;
127112
}

‎src/test/java/com/ning/http/client/async/ChunkingTest.java‎

Lines changed: 46 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,20 @@
1818
importcom.ning.http.client.RequestBuilder;
1919
importcom.ning.http.client.Response;
2020
importcom.ning.http.client.generators.InputStreamBodyGenerator;
21+
2122
importorg.testng.annotations.Test;
2223

2324
importjava.io.BufferedInputStream;
2425
importjava.io.ByteArrayOutputStream;
2526
importjava.io.File;
2627
importjava.io.FileInputStream;
28+
importjava.io.IOException;
2729
importjava.io.InputStream;
30+
importjava.net.URISyntaxException;
2831
importjava.net.URL;
2932
importjava.util.Random;
3033

31-
importstaticorg.testng.Assert.assertNotNull;
32-
importstaticorg.testng.AssertJUnit.assertEquals;
33-
importstaticorg.testng.AssertJUnit.assertTrue;
34+
importstaticorg.testng.Assert.*;
3435
importstaticorg.testng.FileAssert.fail;
3536

3637
/**
@@ -69,64 +70,54 @@ abstract public class ChunkingTest extends AbstractBasicTest {
6970
* Tests that the custom chunked stream result in success and content returned that is unchunked
7071
*/
7172
@Test()
72-
publicvoidtestCustomChunking()throwsThrowable {
73-
doTest(true);
73+
publicvoidtestBufferLargerThanFile()throwsThrowable {
74+
doTest(newBufferedInputStream(newFileInputStream(getTestFile()),400000));
7475
}
7576

76-
privatevoiddoTest(booleancustomChunkedInputStream)throwsException {
77-
AsyncHttpClientc =null;
78-
try {
79-
AsyncHttpClientConfig.Builderbc =newAsyncHttpClientConfig.Builder();
77+
@Test()
78+
publicvoidtestBufferSmallThanFile()throwsThrowable {
79+
doTest(newBufferedInputStream(newFileInputStream(getTestFile())));
80+
}
8081

81-
bc.setAllowPoolingConnection(true);
82-
bc.setMaximumConnectionsPerHost(1);
83-
bc.setMaximumConnectionsTotal(1);
84-
bc.setConnectionTimeoutInMs(1000);
85-
bc.setRequestTimeoutInMs(1000);
86-
bc.setFollowRedirects(true);
82+
@Test()
83+
publicvoidtestDirectFile()throwsThrowable {
84+
doTest(newFileInputStream(getTestFile()));
85+
}
8786

88-
c =getAsyncHttpClient(bc.build());
87+
publicvoiddoTest(InputStreamis)throwsThrowable {
88+
AsyncHttpClientConfig.Builderbc =newAsyncHttpClientConfig.Builder()//
89+
.setAllowPoolingConnection(true)//
90+
.setMaximumConnectionsPerHost(1)//
91+
.setMaximumConnectionsTotal(1)//
92+
.setConnectionTimeoutInMs(1000)//
93+
.setRequestTimeoutInMs(1000)//
94+
.setFollowRedirects(true);
8995

96+
AsyncHttpClientclient =getAsyncHttpClient(bc.build());
97+
try {
9098
RequestBuilderbuilder =newRequestBuilder("POST");
9199
builder.setUrl(getTargetUrl());
92-
if (customChunkedInputStream) {
93-
// made buff in stream big enough to mark.
94-
builder.setBody(newInputStreamBodyGenerator(newBufferedInputStream(newFileInputStream(getTestFile()),400000)));
100+
// made buff in stream big enough to mark.
101+
builder.setBody(newInputStreamBodyGenerator(is));
102+
103+
ListenableFuture<Response>response =client.executeRequest(builder.build());
104+
Responseres =response.get();
105+
assertNotNull(res.getResponseBodyAsStream());
106+
if (500 ==res.getStatusCode()) {
107+
assertEquals(res.getStatusCode(),500,"Should have 500 status code");
108+
assertTrue(res.getHeader("X-Exception").contains("invalid.chunk.length"),"Should have failed due to chunking");
109+
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" +res.getHeader("X-Exception"));
95110
}else {
96-
// made buff in stream big enough to mark.
97-
builder.setBody(newInputStreamBodyGenerator(newBufferedInputStream(newFileInputStream(getTestFile()),400000)));
98-
}
99-
com.ning.http.client.Requestr =builder.build();
100-
Responseres =null;
101-
102-
try {
103-
ListenableFuture<Response>response =c.executeRequest(r);
104-
res =response.get();
105-
assertNotNull(res.getResponseBodyAsStream());
106-
if (500 ==res.getStatusCode()) {
107-
System.out.println("==============");
108-
System.out.println("500 response from call");
109-
System.out.println("Headers:" +res.getHeaders());
110-
System.out.println("==============");
111-
System.out.flush();
112-
assertEquals("Should have 500 status code",500,res.getStatusCode());
113-
assertTrue("Should have failed due to chunking",res.getHeader("X-Exception").contains("invalid.chunk.length"));
114-
fail("HARD Failing the test due to provided InputStreamBodyGenerator, chunking incorrectly:" +res.getHeader("X-Exception"));
115-
}else {
116-
assertEquals(LARGE_IMAGE_BYTES,readInputStreamToBytes(res.getResponseBodyAsStream()));
117-
}
118-
}catch (Exceptione) {
119-
120-
fail("Exception Thrown:" +e.getMessage());
111+
assertEquals(readInputStreamToBytes(res.getResponseBodyAsStream()),LARGE_IMAGE_BYTES);
121112
}
122113
}finally {
123-
if (c !=null)
124-
c.close();
114+
if (client !=null)
115+
client.close();
125116
}
126117
}
127-
128-
privatebyte[]readInputStreamToBytes(InputStreamstream) {
129-
byte[]data =newbyte[0];
118+
119+
120+
privatebyte[]readInputStreamToBytes(InputStreamstream)throwsIOException {
130121
ByteArrayOutputStreambuffer =newByteArrayOutputStream();
131122
try {
132123
intnRead;
@@ -135,33 +126,21 @@ private byte[] readInputStreamToBytes(InputStream stream) {
135126
while ((nRead =stream.read(tmp,0,tmp.length)) != -1) {
136127
buffer.write(tmp,0,nRead);
137128
}
129+
138130
buffer.flush();
139-
data =buffer.toByteArray();
140-
}catch (Exceptione) {
131+
returnbuffer.toByteArray();
141132

142133
}finally {
143134
try {
144135
stream.close();
145136
}catch (Exceptione2) {
146137
}
147-
returndata;
148138
}
149139
}
150140

151-
privatestaticFilegetTestFile() {
141+
privatestaticFilegetTestFile()throwsURISyntaxException{
152142
StringtestResource1 ="300k.png";
153-
154-
FiletestResource1File =null;
155-
try {
156-
ClassLoadercl =ChunkingTest.class.getClassLoader();
157-
URLurl =cl.getResource(testResource1);
158-
testResource1File =newFile(url.toURI());
159-
}catch (Throwablee) {
160-
// TODO Auto-generated catch block
161-
fail("unable to find " +testResource1);
162-
}
163-
164-
returntestResource1File;
143+
URLurl =ChunkingTest.class.getClassLoader().getResource(testResource1);
144+
returnnewFile(url.toURI());
165145
}
166-
167-
}
146+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp