|
| 1 | +/* |
| 2 | + * Copyright (c) 2010-2025 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved. |
| 3 | + */ |
| 4 | +package com.marklogic.client.impl; |
| 5 | + |
| 6 | +import com.marklogic.client.impl.okhttp.RetryIOExceptionInterceptor; |
| 7 | +import okhttp3.MediaType; |
| 8 | +import okhttp3.MultipartBody; |
| 9 | +import okhttp3.OkHttpClient; |
| 10 | +import okhttp3.Request; |
| 11 | +import org.junit.jupiter.api.Test; |
| 12 | + |
| 13 | +import java.io.ByteArrayInputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStream; |
| 16 | +import java.nio.charset.StandardCharsets; |
| 17 | +import java.util.concurrent.atomic.AtomicInteger; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 22 | + |
| 23 | +class StreamingOutputImplTest { |
| 24 | + |
| 25 | + @Test |
| 26 | + void multipartBodyWithStreamingPartIsNotRetried() { |
| 27 | + StreamingOutputImpl streamingBody = new StreamingOutputImpl( |
| 28 | + outputStream -> outputStream.write("content".getBytes(StandardCharsets.UTF_8)), |
| 29 | + null, |
| 30 | + MediaType.get("application/xml") |
| 31 | + ); |
| 32 | + MultipartBody multipartBody = new MultipartBody.Builder() |
| 33 | + .setType(MultipartBody.FORM) |
| 34 | + .addFormDataPart("body", null, streamingBody) |
| 35 | + .build(); |
| 36 | + assertTrue(multipartBody.isOneShot()); |
| 37 | + |
| 38 | + AtomicInteger attempts = new AtomicInteger(); |
| 39 | + OkHttpClient client = new OkHttpClient.Builder() |
| 40 | + .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
| 41 | + .addInterceptor(chain -> { |
| 42 | + attempts.incrementAndGet(); |
| 43 | + throw new IOException("unexpected end of stream"); |
| 44 | + }) |
| 45 | + .build(); |
| 46 | + Request request = new Request.Builder() |
| 47 | + .url("http://localhost/") |
| 48 | + .post(multipartBody) |
| 49 | + .build(); |
| 50 | + |
| 51 | + assertThrows(IOException.class, () -> client.newCall(request).execute()); |
| 52 | + assertEquals(1, attempts.get()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void multipartBodyWithInputStreamPartIsNotRetried() { |
| 57 | + InputStream inputStream = new ByteArrayInputStream("content".getBytes(StandardCharsets.UTF_8)); |
| 58 | + OkHttpServices.ObjectRequestBody inputStreamBody = |
| 59 | + new OkHttpServices.ObjectRequestBody(inputStream, MediaType.get("application/xml")); |
| 60 | + MultipartBody multipartBody = new MultipartBody.Builder() |
| 61 | + .setType(MultipartBody.FORM) |
| 62 | + .addFormDataPart("body", null, inputStreamBody) |
| 63 | + .build(); |
| 64 | + assertTrue(multipartBody.isOneShot(), |
| 65 | + "A multipart body containing an InputStream-backed part must be one-shot so the retry interceptor does " + |
| 66 | + "not replay an already-consumed stream."); |
| 67 | + |
| 68 | + AtomicInteger attempts = new AtomicInteger(); |
| 69 | + OkHttpClient client = new OkHttpClient.Builder() |
| 70 | + .addInterceptor(new RetryIOExceptionInterceptor(3, 0, 1, 0)) |
| 71 | + .addInterceptor(chain -> { |
| 72 | + attempts.incrementAndGet(); |
| 73 | + throw new IOException("unexpected end of stream"); |
| 74 | + }) |
| 75 | + .build(); |
| 76 | + Request request = new Request.Builder() |
| 77 | + .url("http://localhost/") |
| 78 | + .post(multipartBody) |
| 79 | + .build(); |
| 80 | + |
| 81 | + assertThrows(IOException.class, () -> client.newCall(request).execute()); |
| 82 | + assertEquals(1, attempts.get()); |
| 83 | + } |
| 84 | +} |
0 commit comments