-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_test.go
More file actions
651 lines (582 loc) · 18.6 KB
/
client_test.go
File metadata and controls
651 lines (582 loc) · 18.6 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package billionverify
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestNewClient(t *testing.T) {
t.Run("requires API key", func(t *testing.T) {
_, err := NewClient(Config{APIKey: ""})
if err == nil {
t.Error("expected error for empty API key")
}
if _, ok := err.(*AuthenticationError); !ok {
t.Errorf("expected AuthenticationError, got %T", err)
}
})
t.Run("creates client with default options", func(t *testing.T) {
client, err := NewClient(Config{APIKey: "test-key"})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client.apiKey != "test-key" {
t.Errorf("expected API key 'test-key', got %s", client.apiKey)
}
if client.baseURL != defaultBaseURL {
t.Errorf("expected base URL %s, got %s", defaultBaseURL, client.baseURL)
}
})
t.Run("creates client with custom options", func(t *testing.T) {
client, err := NewClient(Config{
APIKey: "test-key",
BaseURL: "https://custom.api.com",
Timeout: 60 * time.Second,
Retries: 5,
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if client.baseURL != "https://custom.api.com" {
t.Errorf("expected custom base URL, got %s", client.baseURL)
}
if client.retries != 5 {
t.Errorf("expected 5 retries, got %d", client.retries)
}
})
}
func TestHealth(t *testing.T) {
t.Run("checks health successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/health" {
t.Errorf("expected path /health, got %s", r.URL.Path)
}
if r.Method != http.MethodGet {
t.Errorf("expected GET method, got %s", r.Method)
}
// Health endpoint should not require API key
if r.Header.Get("EV-API-KEY") != "" {
t.Error("health endpoint should not send API key")
}
response := HealthResponse{
Status: "ok",
Time: 1705319400,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.Health(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Status != "ok" {
t.Errorf("expected status ok, got %s", result.Status)
}
})
}
func TestVerify(t *testing.T) {
t.Run("verifies email successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/verify/single" {
t.Errorf("expected path /v1/verify/single, got %s", r.URL.Path)
}
if r.Method != http.MethodPost {
t.Errorf("expected POST method, got %s", r.Method)
}
if r.Header.Get("EV-API-KEY") != "test-key" {
t.Errorf("expected EV-API-KEY header")
}
// Verify request body uses check_smtp not smtp_check
var reqBody map[string]interface{}
json.NewDecoder(r.Body).Decode(&reqBody)
if _, ok := reqBody["check_smtp"]; !ok {
t.Error("expected check_smtp in request body")
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"email": "test@example.com",
"status": "valid",
"score": 0.95,
"is_deliverable": true,
"is_disposable": false,
"is_catchall": false,
"is_role": false,
"is_free": false,
"domain": "example.com",
"domain_age": 10,
"mx_records": []string{"mail.example.com"},
"smtp_check": true,
"reason": "accepted",
"response_time": 250,
"credits_used": 1,
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.Verify(context.Background(), "test@example.com", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Email != "test@example.com" {
t.Errorf("expected email test@example.com, got %s", result.Email)
}
if result.Status != StatusValid {
t.Errorf("expected status valid, got %s", result.Status)
}
if result.Score != 0.95 {
t.Errorf("expected score 0.95, got %f", result.Score)
}
if !result.IsDeliverable {
t.Error("expected is_deliverable to be true")
}
if result.Domain != "example.com" {
t.Errorf("expected domain example.com, got %s", result.Domain)
}
})
t.Run("handles authentication error", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": map[string]string{
"code": "INVALID_API_KEY",
"message": "Invalid API key",
},
})
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "invalid-key", BaseURL: server.URL})
_, err := client.Verify(context.Background(), "test@example.com", nil)
if err == nil {
t.Fatal("expected error")
}
if _, ok := err.(*AuthenticationError); !ok {
t.Errorf("expected AuthenticationError, got %T", err)
}
})
t.Run("handles validation error", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": map[string]string{
"code": "INVALID_EMAIL",
"message": "Invalid email format",
},
})
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
_, err := client.Verify(context.Background(), "invalid", nil)
if err == nil {
t.Fatal("expected error")
}
if _, ok := err.(*ValidationError); !ok {
t.Errorf("expected ValidationError, got %T", err)
}
})
t.Run("handles insufficient credits error with HTTP 402", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusPaymentRequired)
json.NewEncoder(w).Encode(map[string]interface{}{
"error": map[string]string{
"code": "INSUFFICIENT_CREDITS",
"message": "Not enough credits",
},
})
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
_, err := client.Verify(context.Background(), "test@example.com", nil)
if err == nil {
t.Fatal("expected error")
}
if _, ok := err.(*InsufficientCreditsError); !ok {
t.Errorf("expected InsufficientCreditsError, got %T", err)
}
})
}
func TestVerifyBatch(t *testing.T) {
t.Run("verifies batch successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/verify/bulk" {
t.Errorf("expected path /v1/verify/bulk, got %s", r.URL.Path)
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"results": []map[string]interface{}{
{
"email": "user1@example.com",
"status": "valid",
"score": 0.95,
"is_deliverable": true,
"credits_used": 1,
},
{
"email": "user2@example.com",
"status": "invalid",
"score": 0.0,
"is_deliverable": false,
"credits_used": 0,
},
},
"total_emails": 2,
"valid_emails": 1,
"invalid_emails": 1,
"credits_used": 1,
"process_time": 1500,
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.VerifyBatch(context.Background(), []string{
"user1@example.com",
"user2@example.com",
}, nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Results) != 2 {
t.Errorf("expected 2 results, got %d", len(result.Results))
}
if result.TotalEmails != 2 {
t.Errorf("expected total 2, got %d", result.TotalEmails)
}
if result.ValidEmails != 1 {
t.Errorf("expected 1 valid email, got %d", result.ValidEmails)
}
})
t.Run("rejects more than 50 emails", func(t *testing.T) {
client, _ := NewClient(Config{APIKey: "test-key"})
emails := make([]string, 51)
for i := range emails {
emails[i] = "test@example.com"
}
_, err := client.VerifyBatch(context.Background(), emails, nil)
if err == nil {
t.Fatal("expected error")
}
if _, ok := err.(*ValidationError); !ok {
t.Errorf("expected ValidationError, got %T", err)
}
})
}
func TestGetFileJobStatus(t *testing.T) {
t.Run("gets job status successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/verify/file/job_123" {
t.Errorf("expected path /v1/verify/file/job_123, got %s", r.URL.Path)
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"job_id": "job_123",
"status": "processing",
"file_name": "emails.csv",
"total_emails": 100,
"processed_emails": 50,
"progress_percent": 50,
"valid_emails": 40,
"invalid_emails": 5,
"unknown_emails": 5,
"role_emails": 0,
"catchall_emails": 0,
"credits_used": 50,
"created_at": "2026-02-04T10:30:00Z",
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.GetFileJobStatus(context.Background(), "job_123", nil)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.JobID != "job_123" {
t.Errorf("expected job ID job_123, got %s", result.JobID)
}
if result.ProgressPercent != 50 {
t.Errorf("expected progress 50, got %d", result.ProgressPercent)
}
})
t.Run("supports long-polling timeout parameter", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
timeout := r.URL.Query().Get("timeout")
if timeout != "30" {
t.Errorf("expected timeout=30, got %s", timeout)
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"job_id": "job_123",
"status": "completed",
"total_emails": 100,
"processed_emails": 100,
"progress_percent": 100,
"created_at": "2026-02-04T10:30:00Z",
"completed_at": "2026-02-04T10:32:00Z",
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.GetFileJobStatus(context.Background(), "job_123", &FileJobStatusOptions{Timeout: 30})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.Status != JobStatusCompleted {
t.Errorf("expected completed status, got %s", result.Status)
}
})
}
func TestGetCredits(t *testing.T) {
t.Run("gets credits successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/credits" {
t.Errorf("expected path /v1/credits, got %s", r.URL.Path)
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"account_id": "abc123",
"api_key_id": "key_xyz",
"api_key_name": "Default API Key",
"credits_balance": 9500,
"credits_consumed": 500,
"credits_added": 10000,
"last_updated": "2026-02-04T10:30:00Z",
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.GetCredits(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.CreditsBalance != 9500 {
t.Errorf("expected credits_balance 9500, got %d", result.CreditsBalance)
}
if result.AccountID != "abc123" {
t.Errorf("expected account_id abc123, got %s", result.AccountID)
}
if result.APIKeyName != "Default API Key" {
t.Errorf("expected api_key_name 'Default API Key', got %s", result.APIKeyName)
}
})
}
func TestWebhooks(t *testing.T) {
t.Run("creates webhook successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/webhooks" {
t.Errorf("expected path /v1/webhooks, got %s", r.URL.Path)
}
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"id": "webhook_123",
"url": "https://example.com/webhook",
"events": []string{"file.completed", "file.failed"},
"secret": "test-secret",
"is_active": true,
"created_at": "2026-02-04T10:30:00Z",
"updated_at": "2026-02-04T10:30:00Z",
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.CreateWebhook(context.Background(), WebhookConfig{
URL: "https://example.com/webhook",
Events: []WebhookEvent{EventFileCompleted, EventFileFailed},
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result.ID != "webhook_123" {
t.Errorf("expected ID webhook_123, got %s", result.ID)
}
if result.Secret != "test-secret" {
t.Errorf("expected secret test-secret, got %s", result.Secret)
}
if !result.IsActive {
t.Error("expected is_active to be true")
}
})
t.Run("lists webhooks successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := map[string]interface{}{
"success": true,
"code": "0",
"message": "Success",
"data": map[string]interface{}{
"webhooks": []map[string]interface{}{
{
"id": "webhook_123",
"url": "https://example.com/webhook",
"events": []string{"file.completed"},
"is_active": true,
"created_at": "2026-02-04T10:30:00Z",
"updated_at": "2026-02-04T10:30:00Z",
},
},
"total": 1,
},
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
result, err := client.ListWebhooks(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result.Webhooks) != 1 {
t.Errorf("expected 1 webhook, got %d", len(result.Webhooks))
}
if result.Total != 1 {
t.Errorf("expected total 1, got %d", result.Total)
}
})
t.Run("deletes webhook successfully", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/v1/webhooks/webhook_123" {
t.Errorf("expected path /v1/webhooks/webhook_123, got %s", r.URL.Path)
}
w.WriteHeader(http.StatusNoContent)
}))
defer server.Close()
client, _ := NewClient(Config{APIKey: "test-key", BaseURL: server.URL})
err := client.DeleteWebhook(context.Background(), "webhook_123")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
})
}
func TestVerifyWebhookSignature(t *testing.T) {
t.Run("verifies valid signature", func(t *testing.T) {
payload := `{"event":"test"}`
secret := "test-secret"
// Pre-computed valid signature
signature := "sha256=ad386d9a61a0540a089d2955a07280771439f9f8c41a4b94cd404a740061c3d9"
result := VerifyWebhookSignature(payload, signature, secret)
if !result {
t.Error("expected signature to be valid")
}
})
t.Run("rejects invalid signature", func(t *testing.T) {
payload := `{"event":"test"}`
secret := "test-secret"
signature := "sha256=invalid"
result := VerifyWebhookSignature(payload, signature, secret)
if result {
t.Error("expected signature to be invalid")
}
})
}
func TestErrors(t *testing.T) {
t.Run("AuthenticationError", func(t *testing.T) {
err := NewAuthenticationError("")
if err.Code != "INVALID_API_KEY" {
t.Errorf("expected code INVALID_API_KEY, got %s", err.Code)
}
if err.StatusCode != 401 {
t.Errorf("expected status 401, got %d", err.StatusCode)
}
})
t.Run("RateLimitError", func(t *testing.T) {
err := NewRateLimitError("Rate limited", 60)
if err.RetryAfter != 60 {
t.Errorf("expected retry after 60, got %d", err.RetryAfter)
}
})
t.Run("ValidationError", func(t *testing.T) {
err := NewValidationError("Invalid input", "details here")
if err.Details != "details here" {
t.Errorf("expected details 'details here', got %s", err.Details)
}
})
t.Run("InsufficientCreditsError with HTTP 402", func(t *testing.T) {
err := NewInsufficientCreditsError("")
if err.Code != "INSUFFICIENT_CREDITS" {
t.Errorf("expected code INSUFFICIENT_CREDITS, got %s", err.Code)
}
if err.StatusCode != 402 {
t.Errorf("expected status 402, got %d", err.StatusCode)
}
})
t.Run("NotFoundError", func(t *testing.T) {
err := NewNotFoundError("")
if err.Code != "NOT_FOUND" {
t.Errorf("expected code NOT_FOUND, got %s", err.Code)
}
})
t.Run("TimeoutError", func(t *testing.T) {
err := NewTimeoutError("Request timed out")
if err.Message != "Request timed out" {
t.Errorf("expected message 'Request timed out', got %s", err.Message)
}
})
}
func TestStatusEnums(t *testing.T) {
t.Run("verification status enums", func(t *testing.T) {
statuses := []VerificationStatus{
StatusValid,
StatusInvalid,
StatusUnknown,
StatusRisky,
StatusDisposable,
StatusCatchall,
StatusRole,
}
expected := []string{"valid", "invalid", "unknown", "risky", "disposable", "catchall", "role"}
for i, status := range statuses {
if string(status) != expected[i] {
t.Errorf("expected status %s, got %s", expected[i], status)
}
}
})
t.Run("webhook event enums", func(t *testing.T) {
events := []WebhookEvent{
EventFileCompleted,
EventFileFailed,
}
expected := []string{"file.completed", "file.failed"}
for i, event := range events {
if string(event) != expected[i] {
t.Errorf("expected event %s, got %s", expected[i], event)
}
}
})
}