|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.http.nio.netty.internal.nrs; |
| 17 | + |
| 18 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | + |
| 21 | +import io.netty.buffer.ByteBuf; |
| 22 | +import io.netty.buffer.Unpooled; |
| 23 | +import io.netty.channel.ChannelHandlerContext; |
| 24 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 25 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 26 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 27 | +import io.netty.handler.codec.http.DefaultHttpContent; |
| 28 | +import io.netty.handler.codec.http.HttpContent; |
| 29 | +import io.netty.handler.codec.http.HttpMethod; |
| 30 | +import io.netty.handler.codec.http.HttpResponse; |
| 31 | +import io.netty.handler.codec.http.HttpResponseStatus; |
| 32 | +import io.netty.handler.codec.http.HttpUtil; |
| 33 | +import io.netty.handler.codec.http.HttpVersion; |
| 34 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 35 | +import java.util.concurrent.atomic.AtomicReference; |
| 36 | +import org.junit.jupiter.api.AfterEach; |
| 37 | +import org.junit.jupiter.api.BeforeEach; |
| 38 | +import org.junit.jupiter.api.Test; |
| 39 | +import org.reactivestreams.Publisher; |
| 40 | +import org.reactivestreams.Subscriber; |
| 41 | +import org.reactivestreams.Subscription; |
| 42 | + |
| 43 | +/** |
| 44 | + * Regression coverage for the NPE reported in |
| 45 | + * <a href="https://github.com/aws/aws-sdk-java-v2/issues/7271">#7271</a>: when a request carries |
| 46 | + * {@code Expect: 100-continue}, {@link HttpStreamsClientHandler} defers subscribing the {@link HandlerSubscriber} to the |
| 47 | + * body until the server answers {@code 100 Continue}. A {@code channelWritabilityChanged} event fired during that window |
| 48 | + * used to dereference the still-null subscription and throw. |
| 49 | + */ |
| 50 | +public class HandlerSubscriberExpectContinueTest { |
| 51 | + |
| 52 | + private EmbeddedChannel channel; |
| 53 | + private ExceptionCapturingHandler exceptionCapture; |
| 54 | + |
| 55 | + @BeforeEach |
| 56 | + public void setup() { |
| 57 | + channel = new EmbeddedChannel(new HttpStreamsClientHandler()); |
| 58 | + exceptionCapture = new ExceptionCapturingHandler(); |
| 59 | + channel.pipeline().addLast(exceptionCapture); |
| 60 | + } |
| 61 | + |
| 62 | + @AfterEach |
| 63 | + public void teardown() { |
| 64 | + channel.finishAndReleaseAll(); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void channelWritabilityChanged_whenSubscriptionPending_doesNotRouteExceptionToPipeline() { |
| 69 | + writeExpectContinueRequest(new SingleChunkPublisher("body")); |
| 70 | + |
| 71 | + channel.pipeline().fireChannelWritabilityChanged(); |
| 72 | + |
| 73 | + assertThat(exceptionCapture.captured()).isNull(); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void channelWritabilityChanged_whenSubscriptionPending_bodyStreamsAfter100Continue() { |
| 78 | + SingleChunkPublisher body = new SingleChunkPublisher("body"); |
| 79 | + writeExpectContinueRequest(body); |
| 80 | + |
| 81 | + channel.pipeline().fireChannelWritabilityChanged(); |
| 82 | + deliver100Continue(); |
| 83 | + |
| 84 | + assertThat(body.subscribed()).isTrue(); |
| 85 | + assertThat(streamedBody()).isEqualTo("body"); |
| 86 | + assertThat(exceptionCapture.captured()).isNull(); |
| 87 | + } |
| 88 | + |
| 89 | + private void writeExpectContinueRequest(Publisher<HttpContent> body) { |
| 90 | + DefaultStreamedHttpRequest request = |
| 91 | + new DefaultStreamedHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/", body); |
| 92 | + HttpUtil.set100ContinueExpected(request, true); |
| 93 | + channel.writeOutbound(request); |
| 94 | + } |
| 95 | + |
| 96 | + private void deliver100Continue() { |
| 97 | + HttpResponse continueResponse = |
| 98 | + new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.CONTINUE); |
| 99 | + channel.writeInbound(continueResponse); |
| 100 | + channel.runPendingTasks(); |
| 101 | + } |
| 102 | + |
| 103 | + private String streamedBody() { |
| 104 | + StringBuilder body = new StringBuilder(); |
| 105 | + Object msg; |
| 106 | + while ((msg = channel.readOutbound()) != null) { |
| 107 | + if (msg instanceof HttpContent) { |
| 108 | + body.append(((HttpContent) msg).content().toString(UTF_8)); |
| 109 | + } |
| 110 | + } |
| 111 | + return body.toString(); |
| 112 | + } |
| 113 | + |
| 114 | + private static final class ExceptionCapturingHandler extends ChannelInboundHandlerAdapter { |
| 115 | + private final AtomicReference<Throwable> captured = new AtomicReference<>(); |
| 116 | + |
| 117 | + @Override |
| 118 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 119 | + captured.compareAndSet(null, cause); |
| 120 | + } |
| 121 | + |
| 122 | + Throwable captured() { |
| 123 | + return captured.get(); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + private static final class SingleChunkPublisher implements Publisher<HttpContent> { |
| 128 | + private final byte[] payload; |
| 129 | + private final AtomicBoolean subscribed = new AtomicBoolean(false); |
| 130 | + |
| 131 | + private SingleChunkPublisher(String payload) { |
| 132 | + this.payload = payload.getBytes(UTF_8); |
| 133 | + } |
| 134 | + |
| 135 | + boolean subscribed() { |
| 136 | + return subscribed.get(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void subscribe(Subscriber<? super HttpContent> subscriber) { |
| 141 | + subscribed.set(true); |
| 142 | + subscriber.onSubscribe(new Subscription() { |
| 143 | + private boolean delivered; |
| 144 | + |
| 145 | + @Override |
| 146 | + public void request(long n) { |
| 147 | + if (n <= 0 || delivered) { |
| 148 | + return; |
| 149 | + } |
| 150 | + delivered = true; |
| 151 | + ByteBuf buf = Unpooled.wrappedBuffer(payload); |
| 152 | + subscriber.onNext(new DefaultHttpContent(buf)); |
| 153 | + subscriber.onComplete(); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void cancel() { |
| 158 | + delivered = true; |
| 159 | + } |
| 160 | + }); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments