Skip to content

Commit e584260

Browse files
committed
(ISSUE-87) Honor status code in the sendContent method
1 parent 26de51d commit e584260

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

src/main/java/io/cdap/http/internal/BasicHttpResponder.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2017-2019 Cask Data, Inc.
2+
* Copyright © 2017-2020 Cask Data, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -26,7 +26,6 @@
2626
import io.netty.channel.Channel;
2727
import io.netty.channel.ChannelFuture;
2828
import io.netty.channel.ChannelFutureListener;
29-
import io.netty.channel.ChannelHandler;
3029
import io.netty.channel.ChannelHandlerContext;
3130
import io.netty.channel.ChannelPipeline;
3231
import io.netty.channel.DefaultFileRegion;
@@ -174,7 +173,7 @@ public void sendContent(HttpResponseStatus status, final BodyProducer bodyProduc
174173
return;
175174
}
176175

177-
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
176+
HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, status);
178177
addContentTypeIfMissing(response.headers().add(headers), OCTET_STREAM_TYPE);
179178

180179
if (contentLength < 0L) {

src/test/java/io/cdap/http/HttpServerTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2014-2019 Cask Data, Inc.
2+
* Copyright © 2014-2020 Cask Data, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -843,6 +843,14 @@ public void testBodyProducer() throws Exception {
843843
Assert.assertFalse(failureFile.isFile());
844844
}
845845

846+
@Test
847+
public void testBodyProducerStatus() throws Exception {
848+
for (int status : Arrays.asList(200, 400, 404, 500)) {
849+
HttpURLConnection urlConn = request("/test/v1/produceBodyWithStatus?status=" + status, HttpMethod.GET);
850+
Assert.assertEquals(status, urlConn.getResponseCode());
851+
}
852+
}
853+
846854
@Test
847855
public void testCompressResponse() throws Exception {
848856
HttpURLConnection urlConn = request("/test/v1/compressResponse?message=Testing+message", HttpMethod.GET);

src/test/java/io/cdap/http/TestHandler.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright © 2014-2019 Cask Data, Inc.
2+
* Copyright © 2014-2020 Cask Data, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
55
* use this file except in compliance with the License. You may obtain a copy of
@@ -462,6 +462,29 @@ public void handleError(@Nullable Throwable cause) {
462462
}, EmptyHttpHeaders.INSTANCE);
463463
}
464464

465+
@Path("/produceBodyWithStatus")
466+
@GET
467+
public void produceBodyWithStatus(HttpRequest request, HttpResponder responder,
468+
@QueryParam("status") @DefaultValue("200") final int status) {
469+
470+
responder.sendContent(HttpResponseStatus.valueOf(status), new BodyProducer() {
471+
@Override
472+
public ByteBuf nextChunk() {
473+
return Unpooled.EMPTY_BUFFER;
474+
}
475+
476+
@Override
477+
public void finished() {
478+
// no-op
479+
}
480+
481+
@Override
482+
public void handleError(@Nullable Throwable cause) {
483+
// no-op
484+
}
485+
}, EmptyHttpHeaders.INSTANCE);
486+
}
487+
465488
@Path("/uexception")
466489
@GET
467490
public void testException(HttpRequest request, HttpResponder responder) {

0 commit comments

Comments
 (0)