|
24 | 24 | import com.netflix.netty.common.HttpServerLifecycleChannelHandler.HttpServerLifecycleInboundChannelHandler; |
25 | 25 | import com.netflix.netty.common.HttpServerLifecycleChannelHandler.HttpServerLifecycleOutboundChannelHandler; |
26 | 26 | import io.netty.buffer.ByteBuf; |
| 27 | +import io.netty.buffer.Unpooled; |
27 | 28 | import io.netty.buffer.UnpooledByteBufAllocator; |
28 | 29 | import io.netty.channel.ChannelHandlerContext; |
29 | 30 | import io.netty.channel.ChannelInboundHandlerAdapter; |
30 | 31 | import io.netty.channel.embedded.EmbeddedChannel; |
31 | 32 | import io.netty.handler.codec.http.DefaultFullHttpRequest; |
| 33 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 34 | +import io.netty.handler.codec.http.DefaultHttpResponse; |
| 35 | +import io.netty.handler.codec.http.DefaultLastHttpContent; |
32 | 36 | import io.netty.handler.codec.http.FullHttpRequest; |
33 | 37 | import io.netty.handler.codec.http.HttpMethod; |
| 38 | +import io.netty.handler.codec.http.HttpResponseStatus; |
34 | 39 | import io.netty.handler.codec.http.HttpVersion; |
| 40 | +import io.netty.handler.codec.http.LastHttpContent; |
35 | 41 | import io.netty.util.ReferenceCountUtil; |
| 42 | +import java.util.ArrayList; |
| 43 | +import java.util.List; |
36 | 44 | import org.junit.jupiter.api.Test; |
37 | 45 |
|
38 | 46 | class HttpServerLifecycleChannelHandlerTest { |
39 | 47 |
|
| 48 | + private static final class CompleteEventCollector extends ChannelInboundHandlerAdapter { |
| 49 | + final List<CompleteEvent> events = new ArrayList<>(); |
| 50 | + |
| 51 | + @Override |
| 52 | + public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { |
| 53 | + if (evt instanceof CompleteEvent ce) { |
| 54 | + events.add(ce); |
| 55 | + } |
| 56 | + super.userEventTriggered(ctx, evt); |
| 57 | + } |
| 58 | + } |
| 59 | + |
40 | 60 | final class AssertReasonHandler extends ChannelInboundHandlerAdapter { |
41 | 61 |
|
42 | 62 | CompleteEvent completeEvent; |
@@ -82,6 +102,68 @@ void completionEventReasonIsCloseByDefault() { |
82 | 102 | assertThat(reasonHandler.getCompleteEvent().getReason()).isEqualTo(CompleteReason.CLOSE); |
83 | 103 | } |
84 | 104 |
|
| 105 | + @Test |
| 106 | + void sessionCompleteNotFiredAfterForwarded1xxAsTwoMessages() { |
| 107 | + // A 1xx forwarded from origin arrives as two separate pipeline objects: |
| 108 | + // an HttpResponse(103) and a trailing empty LastHttpContent. |
| 109 | + CompleteEventCollector collector = new CompleteEventCollector(); |
| 110 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler(), collector); |
| 111 | + channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED); |
| 112 | + |
| 113 | + channel.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS)); |
| 114 | + channel.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT); |
| 115 | + |
| 116 | + assertThat(collector.events).isEmpty(); |
| 117 | + channel.finishAndReleaseAll(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + void sessionCompleteNotFiredAfterFullHttpResponse100() { |
| 122 | + // Zuul-generated 100 Continue arrives as a FullHttpResponse (both HttpResponse and LastHttpContent). |
| 123 | + CompleteEventCollector collector = new CompleteEventCollector(); |
| 124 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler(), collector); |
| 125 | + channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED); |
| 126 | + |
| 127 | + channel.writeOutbound( |
| 128 | + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE, Unpooled.EMPTY_BUFFER)); |
| 129 | + |
| 130 | + assertThat(collector.events).isEmpty(); |
| 131 | + channel.finishAndReleaseAll(); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + void sessionCompleteFiredAfterFinalResponseFollowing1xx() { |
| 136 | + CompleteEventCollector collector = new CompleteEventCollector(); |
| 137 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler(), collector); |
| 138 | + channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED); |
| 139 | + |
| 140 | + // Interim 1xx pair — must NOT fire SESSION_COMPLETE |
| 141 | + channel.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.EARLY_HINTS)); |
| 142 | + channel.writeOutbound(LastHttpContent.EMPTY_LAST_CONTENT); |
| 143 | + assertThat(collector.events).isEmpty(); |
| 144 | + |
| 145 | + // Final 200 — must fire SESSION_COMPLETE exactly once |
| 146 | + channel.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)); |
| 147 | + channel.writeOutbound(new DefaultLastHttpContent()); |
| 148 | + assertThat(collector.events).hasSize(1); |
| 149 | + assertThat(collector.events.get(0).getReason()).isEqualTo(CompleteReason.SESSION_COMPLETE); |
| 150 | + channel.finishAndReleaseAll(); |
| 151 | + } |
| 152 | + |
| 153 | + @Test |
| 154 | + void sessionCompleteFiredForNormalFinalResponse() { |
| 155 | + CompleteEventCollector collector = new CompleteEventCollector(); |
| 156 | + EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler(), collector); |
| 157 | + channel.attr(HttpLifecycleChannelHandler.ATTR_STATE).set(State.STARTED); |
| 158 | + |
| 159 | + channel.writeOutbound(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK)); |
| 160 | + channel.writeOutbound(new DefaultLastHttpContent()); |
| 161 | + |
| 162 | + assertThat(collector.events).hasSize(1); |
| 163 | + assertThat(collector.events.get(0).getReason()).isEqualTo(CompleteReason.SESSION_COMPLETE); |
| 164 | + channel.finishAndReleaseAll(); |
| 165 | + } |
| 166 | + |
85 | 167 | @Test |
86 | 168 | void pipelineRejectReleasesIfNeeded() { |
87 | 169 |
|
|
0 commit comments