@@ -154,6 +154,129 @@ func TestCreateCampaign_RequestBody(t *testing.T) {
154154 }
155155}
156156
157+ const updateCampaignResponse = `{
158+ "success": true,
159+ "campaignId": "cmp_abc123",
160+ "emailMessageId": "em_abc123",
161+ "name": "Renamed",
162+ "status": "Draft",
163+ "createdAt": "2026-04-01T10:00:00Z",
164+ "updatedAt": "2026-04-25T10:00:00Z"
165+ }`
166+
167+ func TestUpdateCampaign (t * testing.T ) {
168+ tests := []struct {
169+ name string
170+ statusCode int
171+ body string
172+ wantAPIErr * APIError
173+ wantErrMsg string
174+ }{
175+ {
176+ name : "success" ,
177+ statusCode : http .StatusOK ,
178+ body : updateCampaignResponse ,
179+ },
180+ {
181+ name : "not found" ,
182+ statusCode : http .StatusNotFound ,
183+ body : `{"success":false,"message":"Campaign not found"}` ,
184+ wantAPIErr : & APIError {StatusCode : http .StatusNotFound , Message : "Campaign not found" },
185+ },
186+ {
187+ name : "not in draft" ,
188+ statusCode : http .StatusConflict ,
189+ body : `{"success":false,"message":"Campaign is not in draft status"}` ,
190+ wantAPIErr : & APIError {StatusCode : http .StatusConflict , Message : "Campaign is not in draft status" },
191+ },
192+ {
193+ name : "invalid json" ,
194+ statusCode : http .StatusOK ,
195+ body : `not json` ,
196+ wantErrMsg : "failed to decode response" ,
197+ },
198+ }
199+
200+ for _ , tt := range tests {
201+ t .Run (tt .name , func (t * testing.T ) {
202+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
203+ w .WriteHeader (tt .statusCode )
204+ w .Write ([]byte (tt .body ))
205+ }))
206+ defer server .Close ()
207+
208+ client := NewClient (server .URL , "test-key" , false )
209+ result , err := client .UpdateCampaign ("cmp_abc123" , UpdateCampaignRequest {Name : "Renamed" })
210+
211+ if tt .wantAPIErr != nil {
212+ var apiErr * APIError
213+ if ! errors .As (err , & apiErr ) {
214+ t .Fatalf ("expected *APIError, got %T: %v" , err , err )
215+ }
216+ if apiErr .StatusCode != tt .wantAPIErr .StatusCode {
217+ t .Errorf ("StatusCode = %d, want %d" , apiErr .StatusCode , tt .wantAPIErr .StatusCode )
218+ }
219+ if apiErr .Message != tt .wantAPIErr .Message {
220+ t .Errorf ("Message = %q, want %q" , apiErr .Message , tt .wantAPIErr .Message )
221+ }
222+ return
223+ }
224+
225+ if tt .wantErrMsg != "" {
226+ if err == nil {
227+ t .Fatalf ("expected error containing %q, got nil" , tt .wantErrMsg )
228+ }
229+ if ! strings .Contains (err .Error (), tt .wantErrMsg ) {
230+ t .Errorf ("error = %q, want it to contain %q" , err .Error (), tt .wantErrMsg )
231+ }
232+ return
233+ }
234+
235+ if err != nil {
236+ t .Fatalf ("unexpected error: %v" , err )
237+ }
238+ if result .CampaignID != "cmp_abc123" {
239+ t .Errorf ("CampaignID = %q, want cmp_abc123" , result .CampaignID )
240+ }
241+ if result .Name != "Renamed" {
242+ t .Errorf ("Name = %q, want Renamed" , result .Name )
243+ }
244+ })
245+ }
246+ }
247+
248+ func TestUpdateCampaign_RequestBodyAndPath (t * testing.T ) {
249+ var (
250+ gotPath string
251+ gotMethod string
252+ body map [string ]any
253+ )
254+ server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
255+ gotPath = r .URL .Path
256+ gotMethod = r .Method
257+ b , _ := io .ReadAll (r .Body )
258+ json .Unmarshal (b , & body )
259+ w .WriteHeader (http .StatusOK )
260+ w .Write ([]byte (updateCampaignResponse ))
261+ }))
262+ defer server .Close ()
263+
264+ client := NewClient (server .URL , "test-key" , false )
265+ if _ , err := client .UpdateCampaign ("cmp_abc123" , UpdateCampaignRequest {Name : "Renamed" }); err != nil {
266+ t .Fatalf ("unexpected error: %v" , err )
267+ }
268+
269+ if gotMethod != http .MethodPost {
270+ t .Errorf ("method = %q, want POST" , gotMethod )
271+ }
272+ if gotPath != "/campaigns/cmp_abc123" {
273+ t .Errorf ("path = %q, want /campaigns/cmp_abc123" , gotPath )
274+ }
275+ if body ["name" ] != "Renamed" {
276+ t .Errorf ("name = %v, want Renamed" , body ["name" ])
277+ }
278+ }
279+
157280func TestGetCampaign (t * testing.T ) {
158281 body := `{
159282 "success": true,
0 commit comments