|
| 1 | +package com.algolia.manual; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.*; |
| 4 | + |
| 5 | +import com.algolia.api.SearchClient; |
| 6 | +import com.algolia.config.ClientOptions; |
| 7 | +import java.io.IOException; |
| 8 | +import java.nio.charset.StandardCharsets; |
| 9 | +import java.util.LinkedHashMap; |
| 10 | +import java.util.Map; |
| 11 | +import okhttp3.Interceptor; |
| 12 | +import okhttp3.MediaType; |
| 13 | +import okhttp3.Protocol; |
| 14 | +import okhttp3.RequestBody; |
| 15 | +import okhttp3.Response; |
| 16 | +import okhttp3.ResponseBody; |
| 17 | +import okio.Buffer; |
| 18 | +import org.junit.jupiter.api.DisplayName; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | +import org.junit.jupiter.api.Timeout; |
| 21 | + |
| 22 | +/** |
| 23 | + * Pins the wire contract from API-141 / CR-12179: request bodies are pre-serialized to UTF-8 bytes |
| 24 | + * and declare their exact length, so requests carry {@code Content-Length} instead of {@code |
| 25 | + * Transfer-Encoding: chunked}. |
| 26 | + */ |
| 27 | +class RequestBodyContentLengthTest { |
| 28 | + |
| 29 | + static final class BodyCaptureInterceptor implements Interceptor { |
| 30 | + |
| 31 | + Long contentLength; |
| 32 | + String bodyUtf8; |
| 33 | + |
| 34 | + @Override |
| 35 | + public Response intercept(Chain chain) throws IOException { |
| 36 | + RequestBody body = chain.request().body(); |
| 37 | + if (body != null) { |
| 38 | + contentLength = body.contentLength(); |
| 39 | + Buffer buffer = new Buffer(); |
| 40 | + body.writeTo(buffer); |
| 41 | + bodyUtf8 = buffer.readUtf8(); |
| 42 | + } |
| 43 | + return new Response.Builder() |
| 44 | + .code(200) |
| 45 | + .request(chain.request()) |
| 46 | + .protocol(Protocol.HTTP_2) |
| 47 | + .message("") |
| 48 | + .body(ResponseBody.create("{}", MediaType.parse("application/json"))) |
| 49 | + .build(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + private static SearchClient clientWith(Interceptor interceptor) { |
| 54 | + return new SearchClient( |
| 55 | + "appId", |
| 56 | + "apiKey", |
| 57 | + ClientOptions.builder().setRequesterConfig(requester -> requester.addInterceptor(interceptor)).build() |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + @Timeout(10) |
| 63 | + @DisplayName("request bodies declare their exact UTF-8 byte length instead of chunked encoding") |
| 64 | + void requestBodiesDeclareExactUtf8Length() throws Exception { |
| 65 | + BodyCaptureInterceptor capture = new BodyCaptureInterceptor(); |
| 66 | + Map<String, String> payload = new LinkedHashMap<String, String>(); |
| 67 | + payload.put("message", "café"); |
| 68 | + |
| 69 | + try (SearchClient client = clientWith(capture)) { |
| 70 | + client.customPost("1/test", null, payload); |
| 71 | + String expected = "{\"message\":\"café\"}"; |
| 72 | + assertEquals(expected, capture.bodyUtf8); |
| 73 | + assertEquals(Long.valueOf(expected.getBytes(StandardCharsets.UTF_8).length), capture.contentLength); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + @Timeout(10) |
| 79 | + @DisplayName("bodyless POST sends a fixed-length empty JSON object") |
| 80 | + void bodylessPostSendsFixedLengthEmptyObject() throws Exception { |
| 81 | + BodyCaptureInterceptor capture = new BodyCaptureInterceptor(); |
| 82 | + |
| 83 | + try (SearchClient client = clientWith(capture)) { |
| 84 | + client.customPost("1/test"); |
| 85 | + assertEquals("{}", capture.bodyUtf8); |
| 86 | + assertEquals(Long.valueOf(2L), capture.contentLength); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments