|
| 1 | +package co.lettermint; |
| 2 | + |
| 3 | +import co.lettermint.models.SendEmailResponse; |
| 4 | +import okhttp3.mockwebserver.MockResponse; |
| 5 | +import okhttp3.mockwebserver.MockWebServer; |
| 6 | +import okhttp3.mockwebserver.RecordedRequest; |
| 7 | +import okio.ByteString; |
| 8 | +import org.junit.jupiter.api.AfterEach; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | + |
| 12 | +import java.io.IOException; |
| 13 | + |
| 14 | +import static org.junit.jupiter.api.Assertions.*; |
| 15 | + |
| 16 | +/** |
| 17 | + * Verifies OkHttp/Okio compatibility after downgrade to 4.9.3. |
| 18 | + * Exercises ByteString and request/response serialization paths |
| 19 | + * that previously caused NoSuchFieldError with Okio 3.x. |
| 20 | + */ |
| 21 | +class OkHttpCompatibilityTest { |
| 22 | + |
| 23 | + private MockWebServer server; |
| 24 | + private Lettermint lettermint; |
| 25 | + |
| 26 | + @BeforeEach |
| 27 | + void setUp() throws IOException { |
| 28 | + server = new MockWebServer(); |
| 29 | + server.start(); |
| 30 | + lettermint = new Lettermint("test-token", server.url("/v1").toString()); |
| 31 | + } |
| 32 | + |
| 33 | + @AfterEach |
| 34 | + void tearDown() throws IOException { |
| 35 | + server.shutdown(); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + void okioByteStringIsAccessible() { |
| 40 | + // This is the exact code path that fails with NoSuchFieldError |
| 41 | + // when Okio 2.x and 3.x jars conflict on the classpath |
| 42 | + ByteString bs = ByteString.encodeUtf8("hello"); |
| 43 | + assertEquals("hello", bs.utf8()); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void fullRoundTripRequest() throws Exception { |
| 48 | + server.enqueue(new MockResponse() |
| 49 | + .setBody("{\"message_id\": \"msg_compat\", \"status\": \"queued\"}") |
| 50 | + .setHeader("Content-Type", "application/json")); |
| 51 | + |
| 52 | + SendEmailResponse response = lettermint.email() |
| 53 | + .from("test@example.com") |
| 54 | + .to("recipient@example.com") |
| 55 | + .subject("OkHttp 4.9.3 compat test") |
| 56 | + .text("Verifying request body serialization through Okio") |
| 57 | + .html("<p>With HTML body too</p>") |
| 58 | + .send(); |
| 59 | + |
| 60 | + assertEquals("msg_compat", response.getMessageId()); |
| 61 | + assertEquals("queued", response.getStatus()); |
| 62 | + |
| 63 | + RecordedRequest recorded = server.takeRequest(); |
| 64 | + assertEquals("POST", recorded.getMethod()); |
| 65 | + |
| 66 | + // readUtf8() goes through Okio's Buffer/ByteString internally |
| 67 | + String body = recorded.getBody().readUtf8(); |
| 68 | + assertTrue(body.contains("\"subject\":\"OkHttp 4.9.3 compat test\"")); |
| 69 | + assertTrue(body.contains("\"text\":\"Verifying request body serialization through Okio\"")); |
| 70 | + assertTrue(body.contains("\"html\":\"<p>With HTML body too</p>\"")); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void multipleSequentialRequests() throws Exception { |
| 75 | + for (int i = 0; i < 3; i++) { |
| 76 | + server.enqueue(new MockResponse() |
| 77 | + .setBody("{\"message_id\": \"msg_" + i + "\", \"status\": \"queued\"}") |
| 78 | + .setHeader("Content-Type", "application/json")); |
| 79 | + } |
| 80 | + |
| 81 | + for (int i = 0; i < 3; i++) { |
| 82 | + SendEmailResponse response = lettermint.email() |
| 83 | + .from("test@example.com") |
| 84 | + .to("r@example.com") |
| 85 | + .subject("Msg " + i) |
| 86 | + .text("Body " + i) |
| 87 | + .send(); |
| 88 | + |
| 89 | + assertEquals("msg_" + i, response.getMessageId()); |
| 90 | + } |
| 91 | + |
| 92 | + // Verify all 3 requests went through with correct bodies |
| 93 | + for (int i = 0; i < 3; i++) { |
| 94 | + RecordedRequest req = server.takeRequest(); |
| 95 | + String body = req.getBody().readUtf8(); |
| 96 | + assertTrue(body.contains("\"subject\":\"Msg " + i + "\"")); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments