TestSMTPAPI_Wait verifies the long-poll wait endpoint.
(t *testing.T)
| 507 | |
| 508 | // TestSMTPAPI_Wait verifies the long-poll wait endpoint. |
| 509 | func TestSMTPAPI_Wait(t *testing.T) { |
| 510 | mux, store, mod := setupAPITest(t) |
| 511 | |
| 512 | t.Run("timeout", func(t *testing.T) { |
| 513 | r := httptest.NewRequest("GET", "/api/smtp/messages/wait?timeout=50ms&to=nobody@example.com", nil) |
| 514 | w := httptest.NewRecorder() |
| 515 | mux.ServeHTTP(w, r) |
| 516 | |
| 517 | if w.Code != http.StatusRequestTimeout { |
| 518 | t.Errorf("expected 408, got %d", w.Code) |
| 519 | } |
| 520 | }) |
| 521 | |
| 522 | t.Run("existing matching event", func(t *testing.T) { |
| 523 | storeSMTPEvent(t, store, "wait-uuid1", ParsedEmail{ |
| 524 | Subject: "Existing", |
| 525 | To: []EmailAddress{{Email: "wait-user@example.com"}}, |
| 526 | }, tsAgo(time.Second), "default") |
| 527 | |
| 528 | r := httptest.NewRequest("GET", "/api/smtp/messages/wait?to=wait-user@example.com&timeout=1s", nil) |
| 529 | w := httptest.NewRecorder() |
| 530 | mux.ServeHTTP(w, r) |
| 531 | |
| 532 | if w.Code != 200 { |
| 533 | t.Errorf("expected 200, got %d", w.Code) |
| 534 | } |
| 535 | }) |
| 536 | |
| 537 | t.Run("new event arrives", func(t *testing.T) { |
| 538 | cursor := tsNow() |
| 539 | |
| 540 | resultCh := make(chan *httptest.ResponseRecorder, 1) |
| 541 | go func() { |
| 542 | url := fmt.Sprintf("/api/smtp/messages/wait?to=newuser@example.com&timeout=5s&since=%.6f", cursor) |
| 543 | r := httptest.NewRequest("GET", url, nil) |
| 544 | w := httptest.NewRecorder() |
| 545 | mux.ServeHTTP(w, r) |
| 546 | resultCh <- w |
| 547 | }() |
| 548 | |
| 549 | // Give the goroutine time to subscribe before the event arrives. |
| 550 | time.Sleep(30 * time.Millisecond) |
| 551 | |
| 552 | newEv := event.Event{ |
| 553 | UUID: "wait-uuid2", |
| 554 | Type: "smtp", |
| 555 | Payload: mustMarshalJSON(ParsedEmail{ |
| 556 | Subject: "New Arrival", |
| 557 | To: []EmailAddress{{Email: "newuser@example.com"}}, |
| 558 | }), |
| 559 | Timestamp: tsNow(), |
| 560 | Project: "default", |
| 561 | } |
| 562 | store.Store(context.Background(), newEv) |
| 563 | mod.OnEventStored(newEv) |
| 564 | |
| 565 | select { |
| 566 | case w := <-resultCh: |
nothing calls this directly
no test coverage detected