Issue #7833 - Chunked Transfer-Encoding and HEAD response test (#8219) (#8226)

* Issue #7833 - Chunked Transfer-Encoding and HEAD response test

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2022-06-30 06:48:51 -05:00 committed by GitHub
parent cb918212f2
commit 6a7a749966
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 70 additions and 2 deletions

View File

@ -18,6 +18,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URI;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.FileStore;
@ -188,6 +189,7 @@ public class HugeResourceTest
context.setBaseResource(new PathResource(staticBase));
context.addServlet(PostServlet.class, "/post");
context.addServlet(ChunkedServlet.class, "/chunked/*");
String location = multipartTempDir.toString();
long maxFileSize = Long.MAX_VALUE;
@ -223,7 +225,7 @@ public class HugeResourceTest
@ParameterizedTest
@MethodSource("staticFiles")
public void testDownload(String filename, long expectedSize) throws Exception
public void testDownloadStatic(String filename, long expectedSize) throws Exception
{
URI destUri = server.getURI().resolve("/" + filename);
InputStreamResponseListener responseListener = new InputStreamResponseListener();
@ -250,7 +252,33 @@ public class HugeResourceTest
@ParameterizedTest
@MethodSource("staticFiles")
public void testHead(String filename, long expectedSize) throws Exception
public void testDownloadChunked(String filename, long expectedSize) throws Exception
{
URI destUri = server.getURI().resolve("/chunked/" + filename);
InputStreamResponseListener responseListener = new InputStreamResponseListener();
Request request = client.newRequest(destUri)
.method(HttpMethod.GET);
request.send(responseListener);
Response response = responseListener.get(5, TimeUnit.SECONDS);
assertThat("HTTP Response Code", response.getStatus(), is(200));
// dumpResponse(response);
String transferEncoding = response.getHeaders().get(HttpHeader.TRANSFER_ENCODING);
assertThat("Http Response Header: \"Transfer-Encoding\"", transferEncoding, is("chunked"));
try (ByteCountingOutputStream out = new ByteCountingOutputStream();
InputStream in = responseListener.getInputStream())
{
IO.copy(in, out);
assertThat("Downloaded Files Size: " + filename, out.getCount(), is(expectedSize));
}
}
@ParameterizedTest
@MethodSource("staticFiles")
public void testHeadStatic(String filename, long expectedSize) throws Exception
{
URI destUri = server.getURI().resolve("/" + filename);
InputStreamResponseListener responseListener = new InputStreamResponseListener();
@ -273,6 +301,30 @@ public class HugeResourceTest
assertThat("Http Response Header: \"Content-Length: " + contentLength + "\"", contentLengthLong, is(expectedSize));
}
@ParameterizedTest
@MethodSource("staticFiles")
public void testHeadChunked(String filename, long expectedSize) throws Exception
{
URI destUri = server.getURI().resolve("/chunked/" + filename);
InputStreamResponseListener responseListener = new InputStreamResponseListener();
Request request = client.newRequest(destUri)
.method(HttpMethod.HEAD);
request.send(responseListener);
Response response = responseListener.get(5, TimeUnit.SECONDS);
try (InputStream in = responseListener.getInputStream())
{
assertThat(in.read(), is(-1));
}
assertThat("HTTP Response Code", response.getStatus(), is(200));
// dumpResponse(response);
String transferEncoding = response.getHeaders().get(HttpHeader.TRANSFER_ENCODING);
assertThat("Http Response Header: \"Transfer-Encoding\"", transferEncoding, is("chunked"));
}
@ParameterizedTest
@MethodSource("staticFiles")
public void testUpload(String filename, long expectedSize) throws Exception
@ -359,6 +411,22 @@ public class HugeResourceTest
}
}
public static class ChunkedServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException
{
URL resource = req.getServletContext().getResource(req.getPathInfo());
OutputStream output = resp.getOutputStream();
try (InputStream input = resource.openStream())
{
resp.setContentType("application/octet-stream");
resp.flushBuffer();
IO.copy(input, output);
}
}
}
public static class MultipartServlet extends HttpServlet
{
@Override