package upstream import ( "encoding/json" "net/http" "net/http/httptest" "testing" "time" ) func TestEmitProtocolLogPostsSafeOutboundPacketEvent(t *testing.T) { events := make(chan protocolLogEvent, 1) api := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/gateway/events/protocol-log" { t.Errorf("unexpected path: %s", r.URL.Path) http.Error(w, "unexpected path", http.StatusNotFound) return } var event protocolLogEvent if err := json.NewDecoder(r.Body).Decode(&event); err != nil { t.Errorf("decode protocol event: %v", err) http.Error(w, "invalid event", http.StatusBadRequest) return } events <- event w.WriteHeader(http.StatusOK) })) defer api.Close() conn := &connection{apiBaseURL: api.URL, httpClient: api.Client()} conn.emitProtocolLog(protocolLogEvent{ Protocol: "cmpp", Direction: "platform_to_channel", EventType: "submit", Status: "success", ChannelID: "channel-1", MessageID: "MSG-1", Phone: "18821203795", PayloadBytes: 32, Detail: map[string]any{"sequenceId": 7}, }) select { case event := <-events: if event.Direction != "platform_to_channel" || event.EventType != "submit" || event.Status != "success" { t.Fatalf("unexpected protocol event: %+v", event) } if event.MessageID != "MSG-1" || event.Phone != "18821203795" || event.PayloadBytes != 32 { t.Fatalf("unexpected event identity: %+v", event) } case <-time.After(time.Second): t.Fatal("timed out waiting for protocol event") } }