forked from lettermint/lettermint-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmailEndpointTest.java
More file actions
203 lines (168 loc) · 7.61 KB
/
EmailEndpointTest.java
File metadata and controls
203 lines (168 loc) · 7.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package co.lettermint;
import co.lettermint.models.SendEmailResponse;
import okhttp3.mockwebserver.MockResponse;
import okhttp3.mockwebserver.MockWebServer;
import okhttp3.mockwebserver.RecordedRequest;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
class EmailEndpointTest {
private MockWebServer mockWebServer;
private Lettermint lettermint;
@BeforeEach
void setUp() throws IOException {
mockWebServer = new MockWebServer();
mockWebServer.start();
String baseUrl = mockWebServer.url("/v1").toString();
lettermint = new Lettermint("test-api-token", baseUrl);
}
@AfterEach
void tearDown() throws IOException {
mockWebServer.shutdown();
}
@Test
void testSimpleEmail() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_123\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
SendEmailResponse response = lettermint.email()
.from("sender@example.com")
.to("recipient@example.com")
.subject("Test Subject")
.text("Hello World")
.send();
assertEquals("msg_123", response.getMessageId());
assertEquals("queued", response.getStatus());
RecordedRequest request = mockWebServer.takeRequest();
assertEquals("POST", request.getMethod());
assertEquals("/v1/send", request.getPath());
assertTrue(request.getHeader("Content-Type").startsWith("application/json"));
assertEquals("test-api-token", request.getHeader("x-lettermint-token"));
assertTrue(request.getHeader("User-Agent").startsWith("Lettermint/"));
}
@Test
void testEmailWithAllOptions() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_456\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
Map<String, String> headers = new HashMap<>();
headers.put("X-Custom-Header", "custom-value");
Map<String, Object> metadata = new HashMap<>();
metadata.put("userId", "user_123");
metadata.put("campaign", "welcome");
SendEmailResponse response = lettermint.email()
.from("John Doe <sender@example.com>")
.to("recipient1@example.com", "recipient2@example.com")
.cc("cc@example.com")
.bcc("bcc@example.com")
.replyTo("reply@example.com")
.subject("Welcome!")
.html("<p>Hello <b>World</b></p>")
.text("Hello World")
.headers(headers)
.attach("document.pdf", "base64content")
.attach("logo.png", "base64logo", "logo-cid")
.route("route-slug-123")
.metadata(metadata)
.tag("welcome", "onboarding")
.idempotencyKey("unique-key-123")
.send();
assertEquals("msg_456", response.getMessageId());
RecordedRequest request = mockWebServer.takeRequest();
assertEquals("unique-key-123", request.getHeader("Idempotency-Key"));
String body = request.getBody().readUtf8();
assertTrue(body.contains("\"from\":\"John Doe <sender@example.com>\""));
assertTrue(body.contains("\"to\":[\"recipient1@example.com\",\"recipient2@example.com\"]"));
assertTrue(body.contains("\"cc\":[\"cc@example.com\"]"));
assertTrue(body.contains("\"bcc\":[\"bcc@example.com\"]"));
assertTrue(body.contains("\"reply_to\":\"reply@example.com\""));
assertTrue(body.contains("\"subject\":\"Welcome!\""));
assertTrue(body.contains("\"html\":\"<p>Hello <b>World</b></p>\""));
assertTrue(body.contains("\"text\":\"Hello World\""));
assertTrue(body.contains("\"route\":\"route-slug-123\""));
assertTrue(body.contains("\"tags\":[\"welcome\",\"onboarding\"]"));
}
@Test
void testRecipientsReplaceWhenCalledMultipleTimes() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_789\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
lettermint.email()
.from("sender@example.com")
.to("first@example.com")
.to("replaced@example.com")
.subject("Test")
.text("Test")
.send();
RecordedRequest request = mockWebServer.takeRequest();
String body = request.getBody().readUtf8();
assertTrue(body.contains("\"to\":[\"replaced@example.com\"]"));
assertFalse(body.contains("first@example.com"));
}
@Test
void testEndpointResetsAfterSend() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_1\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_2\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
lettermint.email()
.from("sender@example.com")
.to("recipient@example.com")
.subject("First Email")
.text("First content")
.send();
lettermint.email()
.from("sender@example.com")
.to("other@example.com")
.subject("Second Email")
.text("Second content")
.send();
mockWebServer.takeRequest();
RecordedRequest secondRequest = mockWebServer.takeRequest();
String body = secondRequest.getBody().readUtf8();
assertTrue(body.contains("\"subject\":\"Second Email\""));
assertFalse(body.contains("First Email"));
}
@Test
void testSingleHeaderMethod() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_123\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
lettermint.email()
.from("sender@example.com")
.to("recipient@example.com")
.subject("Test")
.text("Test")
.header("X-First", "value1")
.header("X-Second", "value2")
.send();
RecordedRequest request = mockWebServer.takeRequest();
String body = request.getBody().readUtf8();
assertTrue(body.contains("\"X-First\":\"value1\""));
assertTrue(body.contains("\"X-Second\":\"value2\""));
}
@Test
void testSingleMetadataMethod() throws Exception {
mockWebServer.enqueue(new MockResponse()
.setBody("{\"message_id\": \"msg_123\", \"status\": \"queued\"}")
.setHeader("Content-Type", "application/json"));
lettermint.email()
.from("sender@example.com")
.to("recipient@example.com")
.subject("Test")
.text("Test")
.metadata("key1", "value1")
.metadata("key2", 123)
.send();
RecordedRequest request = mockWebServer.takeRequest();
String body = request.getBody().readUtf8();
assertTrue(body.contains("\"key1\":\"value1\""));
assertTrue(body.contains("\"key2\":123"));
}
}