MCPcopy Create free account
hub / github.com/buggregator/server / TestSMTPAPI_Wait

Function TestSMTPAPI_Wait

modules/smtp/api_test.go:509–574  ·  view source on GitHub ↗

TestSMTPAPI_Wait verifies the long-poll wait endpoint.

(t *testing.T)

Source from the content-addressed store, hash-verified

507
508// TestSMTPAPI_Wait verifies the long-poll wait endpoint.
509func 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:

Callers

nothing calls this directly

Calls 9

setupAPITestFunction · 0.85
storeSMTPEventFunction · 0.85
tsAgoFunction · 0.85
tsNowFunction · 0.85
mustMarshalJSONFunction · 0.85
ServeHTTPMethod · 0.80
StoreMethod · 0.65
OnEventStoredMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected