MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestProcessStreamPrimingEvent

Function TestProcessStreamPrimingEvent

mcp/streamable_test.go:2569–2615  ·  view source on GitHub ↗

TestProcessStreamPrimingEvent verifies that the streamable client correctly ignores SSE events with empty data buffers, which are used as priming events (e.g. SEP-1699).

(t *testing.T)

Source from the content-addressed store, hash-verified

2567// TestProcessStreamPrimingEvent verifies that the streamable client correctly ignores
2568// SSE events with empty data buffers, which are used as priming events (e.g. SEP-1699).
2569func TestProcessStreamPrimingEvent(t *testing.T) {
2570 // We create a mock response with a priming event (empty data, with an ID),
2571 // followed by a normal event.
2572 sseData := `id: 123
2573
2574id: 124
2575data: {"jsonrpc":"2.0","id":1,"result":{}}
2576
2577`
2578
2579 ctx := t.Context()
2580 resp := &http.Response{
2581 StatusCode: http.StatusOK,
2582 Header: http.Header{"Content-Type": []string{"text/event-stream"}},
2583 Body: io.NopCloser(strings.NewReader(sseData)),
2584 }
2585
2586 incoming := make(chan jsonrpc.Message, 10)
2587 done := make(chan struct{})
2588
2589 conn := &streamableClientConn{
2590 ctx: ctx,
2591 done: done,
2592 incoming: incoming,
2593 failed: make(chan struct{}),
2594 logger: ensureLogger(nil),
2595 }
2596
2597 lastID, _, clientClosed := conn.processStream(ctx, "test", resp, nil)
2598
2599 if clientClosed {
2600 t.Fatalf("processStream was unexpectedly closed by client")
2601 }
2602
2603 if lastID != "124" {
2604 t.Errorf("lastEventID = %q, want %q", lastID, "124")
2605 }
2606
2607 select {
2608 case msg := <-incoming:
2609 if res, ok := msg.(*jsonrpc.Response); !(ok && res.ID == jsonrpc2.Int64ID(1)) {
2610 t.Errorf("got unexpected message: %v", msg)
2611 }
2612 default:
2613 t.Errorf("expected a JSON-RPC message to be produced")
2614 }
2615}
2616
2617// TestScanEventsPingFiltering is a unit test for the low-level event scanning
2618// with ping events to verify scanEvents properly parses all event types.

Callers

nothing calls this directly

Calls 3

processStreamMethod · 0.95
Int64IDFunction · 0.92
ensureLoggerFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…