|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.core.http.netty; |
| 5 | + |
| 6 | +import com.azure.core.http.HttpClient; |
| 7 | +import com.azure.core.http.HttpHeaderName; |
| 8 | +import com.azure.core.http.HttpMethod; |
| 9 | +import com.azure.core.http.HttpRequest; |
| 10 | +import com.azure.core.http.HttpResponse; |
| 11 | +import com.azure.core.util.Context; |
| 12 | +import org.junit.jupiter.params.ParameterizedTest; |
| 13 | +import org.junit.jupiter.params.provider.ValueSource; |
| 14 | + |
| 15 | +import java.io.ByteArrayOutputStream; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.io.OutputStream; |
| 19 | +import java.net.ServerSocket; |
| 20 | +import java.net.Socket; |
| 21 | +import java.net.URI; |
| 22 | +import java.net.URL; |
| 23 | +import java.nio.charset.StandardCharsets; |
| 24 | +import java.util.Locale; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.TimeUnit; |
| 27 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +/** |
| 34 | + * Verifies that the Netty client surfaces the observation of an interim {@code 100 Continue} response through an |
| 35 | + * {@link AtomicBoolean} holder the caller places in the request {@link Context}. The holder is keyed by an opt-in |
| 36 | + * string contract any caller may use; the Netty client sets it to {@code true} when the service sends the interim |
| 37 | + * response. |
| 38 | + */ |
| 39 | +public class NettyExpectContinueObservationTests { |
| 40 | + private static final byte[] BODY = new byte[1024]; |
| 41 | + |
| 42 | + @ParameterizedTest |
| 43 | + @ValueSource(booleans = { true, false }) |
| 44 | + public void interimContinueIsSurfacedThroughContextHolder(boolean sync) throws Exception { |
| 45 | + try (ContinueTestServer server = new ContinueTestServer(true)) { |
| 46 | + server.start(); |
| 47 | + AtomicBoolean holder = sendWithHolder(server, sync); |
| 48 | + assertTrue(server.awaitRequest(), "request never reached the server"); |
| 49 | + assertTrue(holder.get(), "100 Continue was not surfaced through the context holder"); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @ParameterizedTest |
| 54 | + @ValueSource(booleans = { true, false }) |
| 55 | + public void holderStaysFalseWhenServerSendsNoContinue(boolean sync) throws Exception { |
| 56 | + try (ContinueTestServer server = new ContinueTestServer(false)) { |
| 57 | + server.start(); |
| 58 | + AtomicBoolean holder = sendWithHolder(server, sync); |
| 59 | + assertTrue(server.awaitRequest(), "request never reached the server"); |
| 60 | + assertFalse(holder.get(), "holder should stay false when no 100 Continue is sent"); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // Must match the private key NettyAsyncHttpClient reads for the opt-in 100-continue observation contract. |
| 65 | + private static final String EXPECT_CONTINUE_RECEIVED_KEY = "azure-http-client-expect-continue-received"; |
| 66 | + |
| 67 | + private static AtomicBoolean sendWithHolder(ContinueTestServer server, boolean sync) throws IOException { |
| 68 | + HttpClient client = new NettyAsyncHttpClientBuilder().build(); |
| 69 | + AtomicBoolean holder = new AtomicBoolean(false); |
| 70 | + Context context = new Context(EXPECT_CONTINUE_RECEIVED_KEY, holder); |
| 71 | + |
| 72 | + HttpRequest request = new HttpRequest(HttpMethod.PUT, server.url()).setBody(BODY); |
| 73 | + request.getHeaders().set(HttpHeaderName.EXPECT, "100-continue"); |
| 74 | + |
| 75 | + HttpResponse response = sync ? client.sendSync(request, context) : client.send(request, context).block(); |
| 76 | + assertEquals(201, response.getStatusCode()); |
| 77 | + response.close(); |
| 78 | + return holder; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Minimal HTTP/1.1 server that optionally answers the 100-continue handshake, then reads the body and returns 201. |
| 83 | + */ |
| 84 | + private static final class ContinueTestServer implements AutoCloseable { |
| 85 | + private final ServerSocket serverSocket; |
| 86 | + private final boolean sendContinue; |
| 87 | + private final CountDownLatch requestHandled = new CountDownLatch(1); |
| 88 | + private volatile Thread thread; |
| 89 | + |
| 90 | + ContinueTestServer(boolean sendContinue) throws IOException { |
| 91 | + this.serverSocket = new ServerSocket(0); |
| 92 | + this.sendContinue = sendContinue; |
| 93 | + } |
| 94 | + |
| 95 | + URL url() throws IOException { |
| 96 | + return URI.create("http://localhost:" + serverSocket.getLocalPort() + "/resource").toURL(); |
| 97 | + } |
| 98 | + |
| 99 | + void start() { |
| 100 | + thread = new Thread(this::serve, "continue-test-server"); |
| 101 | + thread.setDaemon(true); |
| 102 | + thread.start(); |
| 103 | + } |
| 104 | + |
| 105 | + boolean awaitRequest() throws InterruptedException { |
| 106 | + return requestHandled.await(30, TimeUnit.SECONDS); |
| 107 | + } |
| 108 | + |
| 109 | + private void serve() { |
| 110 | + try (Socket socket = serverSocket.accept()) { |
| 111 | + socket.setTcpNoDelay(true); |
| 112 | + InputStream in = socket.getInputStream(); |
| 113 | + OutputStream out = socket.getOutputStream(); |
| 114 | + |
| 115 | + Parsed parsed = readHeaders(in); |
| 116 | + |
| 117 | + if (sendContinue && parsed.expectHeader != null) { |
| 118 | + out.write("HTTP/1.1 100 Continue\r\n\r\n".getBytes(StandardCharsets.UTF_8)); |
| 119 | + out.flush(); |
| 120 | + } |
| 121 | + |
| 122 | + readBody(in, parsed.contentLength); |
| 123 | + |
| 124 | + out.write("HTTP/1.1 201 Created\r\nContent-Length: 0\r\n\r\n".getBytes(StandardCharsets.UTF_8)); |
| 125 | + out.flush(); |
| 126 | + } catch (IOException ex) { |
| 127 | + // The test assertions report the failure. |
| 128 | + } finally { |
| 129 | + requestHandled.countDown(); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private static Parsed readHeaders(InputStream in) throws IOException { |
| 134 | + ByteArrayOutputStream headerBytes = new ByteArrayOutputStream(); |
| 135 | + int consecutiveNewlines = 0; |
| 136 | + int b; |
| 137 | + while (consecutiveNewlines < 2 && (b = in.read()) != -1) { |
| 138 | + headerBytes.write(b); |
| 139 | + if (b == '\n') { |
| 140 | + consecutiveNewlines++; |
| 141 | + } else if (b != '\r') { |
| 142 | + consecutiveNewlines = 0; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + Parsed parsed = new Parsed(); |
| 147 | + for (String line : new String(headerBytes.toByteArray(), StandardCharsets.UTF_8).split("\r\n")) { |
| 148 | + int colon = line.indexOf(':'); |
| 149 | + if (colon < 0) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + String name = line.substring(0, colon).trim().toLowerCase(Locale.ROOT); |
| 153 | + String value = line.substring(colon + 1).trim(); |
| 154 | + if ("expect".equals(name)) { |
| 155 | + parsed.expectHeader = value.toLowerCase(Locale.ROOT); |
| 156 | + } else if ("content-length".equals(name)) { |
| 157 | + parsed.contentLength = Integer.parseInt(value); |
| 158 | + } |
| 159 | + } |
| 160 | + return parsed; |
| 161 | + } |
| 162 | + |
| 163 | + private static void readBody(InputStream in, int remaining) throws IOException { |
| 164 | + byte[] buffer = new byte[Math.max(remaining, 1)]; |
| 165 | + int read = 0; |
| 166 | + while (read < remaining) { |
| 167 | + int count = in.read(buffer, 0, Math.min(buffer.length, remaining - read)); |
| 168 | + if (count < 0) { |
| 169 | + break; |
| 170 | + } |
| 171 | + read += count; |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void close() throws IOException { |
| 177 | + serverSocket.close(); |
| 178 | + if (thread != null) { |
| 179 | + thread.interrupt(); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private static final class Parsed { |
| 184 | + private String expectHeader; |
| 185 | + private int contentLength; |
| 186 | + } |
| 187 | + } |
| 188 | +} |
0 commit comments