Skip to content

Commit e5018bd

Browse files
committed
fix: downgrade okhttp for compatibility
1 parent 1f006b0 commit e5018bd

File tree

3 files changed

+103
-4
lines changed

3 files changed

+103
-4
lines changed

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ repositories {
2020

2121
dependencies {
2222
// HTTP Client
23-
api 'com.squareup.okhttp3:okhttp-jvm:5.3.2'
23+
api 'com.squareup.okhttp3:okhttp:4.9.3'
2424

2525
// JSON Processing
2626
implementation 'com.fasterxml.jackson.core:jackson-databind:2.21.1'
2727

2828
// Testing
2929
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.1'
30-
testImplementation 'com.squareup.okhttp3:mockwebserver:5.3.2'
30+
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.3'
3131
}
3232

3333
test {

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
<maven.compiler.source>1.8</maven.compiler.source>
4040
<maven.compiler.target>1.8</maven.compiler.target>
4141
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
42-
<okhttp.version>5.3.2</okhttp.version>
42+
<okhttp.version>4.9.3</okhttp.version>
4343
<jackson.version>2.21.0</jackson.version>
4444
<junit.version>5.10.1</junit.version>
4545
</properties>
@@ -48,7 +48,7 @@
4848
<!-- HTTP Client -->
4949
<dependency>
5050
<groupId>com.squareup.okhttp3</groupId>
51-
<artifactId>okhttp-jvm</artifactId>
51+
<artifactId>okhttp</artifactId>
5252
<version>${okhttp.version}</version>
5353
</dependency>
5454

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)