graphqlws

package
v0.0.0-...-870d405 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

README

graphql-ws (subscriptions-transport-ws) proxy scenarios

Protocol: subscriptions-transport-ws (subprotocol graphql-ws)

These scenarios verify the router's behavior as a protocol-translating proxy. The client speaks the legacy graphql-ws protocol while the upstream subgraph speaks the modern graphql-transport-ws protocol. The router translates message types in both directions:

  • Client start becomes upstream subscribe
  • Client stop becomes upstream complete
  • Upstream next becomes client data
  • Upstream error becomes client data with {"errors":[...]} payload

The router may interleave ka (keep-alive) messages at any time; scenarios use a readSkippingKA helper to drain them transparently.

The test harness must register a backend named "subgraph-a".

Connection phase (connect.go)

Scenario Asserts
ConnectionInitAndAck connection_init receives connection_ack with the legacy graphql-ws subprotocol

Operation lifecycle (lifecycle.go)

Scenario Asserts
StartDataCompleteRoundTrip Full lifecycle: client start is translated to upstream subscribe, upstream next arrives as client data, upstream complete arrives as client complete
MultipleDataMessagesBeforeComplete Three upstream next messages arrive as three data messages on the client, in order
StopCancelsSubscription Client stop is translated to upstream complete with the remapped upstream ID
ServerErrorInDataPayload Upstream error is delivered to the client as a data message with {"errors":[...]} in the payload (per the subscriptions-transport-ws convention)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllowlistedHeadersForwardedToBackend = speedtrap.Scenario{
	Name: "allowlisted headers forwarded to backend",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		headers := http.Header{
			"Authorization":  {"Bearer tok"},
			"X-Custom-Trace": {"abc123"},
		}
		c, err := s.Client(
			speedtrap.WithClientSubprotocol("graphql-ws"),
			speedtrap.WithClientHeaders(headers),
		)
		require.NoError(s, err)
		require.NoError(s, c.Send(`{"type":"connection_init"}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"hdr"}}}`))

		b, err := s.Backend("subgraph-a").Accept()
		require.NoError(s, err)

		require.Equal(s, "Bearer tok", b.UpgradeHeaders.Get("Authorization"))
		require.Equal(s, "abc123", b.UpgradeHeaders.Get("X-Custom-Trace"))

		msg, err = b.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_init"}`, msg)
		require.NoError(s, b.Send(`{"type":"connection_ack"}`))

		msg, err = b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"hdr"}}}`)
		subID := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

AllowlistedHeadersForwardedToBackend verifies that client upgrade headers matching the router's header propagation rules are forwarded as HTTP headers on the upstream WebSocket dial (legacy protocol variant).

View Source
var BackendCloseFrameDuringActiveSubscription = speedtrap.Scenario{
	Name: "backend close frame during active subscription",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"bclose"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"bclose"}}}`)

		require.NoError(s, b.SendClose(1011, "internal error"))

		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"type":"data","id":"1","payload":{"errors":[{"message":"upstream service error","extensions":{"code":"<<PRESENCE>>","closeCode":1011,"closeReason":"internal error"}}]}}`)
	},
}

BackendCloseFrameDuringActiveSubscription verifies that when the backend sends a close frame during an active subscription, the client receives an error for the affected subscription.

View Source
var BackendNeverAcksConnectionInitTimesOut = speedtrap.Scenario{
	Name: "backend never acks connection init times out",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"noack"}}}`))

		b, err := s.Backend("subgraph-a").Accept()
		require.NoError(s, err)
		msg, err := b.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_init"}`, msg)

		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"type":"data","id":"1","payload":{"errors":[{"message":"upstream service error","extensions":{"code":"<<PRESENCE>>"}}]}}`)
	},
}

BackendNeverAcksConnectionInitTimesOut verifies that when the backend accepts the connection but never sends connection_ack, the client receives an error for the pending subscribe.

View Source
var BackendTCPDropDuringActiveSubscription = speedtrap.Scenario{
	Name: "backend TCP drop during active subscription",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"drop"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"drop"}}}`)

		require.NoError(s, b.Drop())

		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"type":"data","id":"1","payload":{"errors":[{"message":"upstream service error","extensions":{"code":"<<PRESENCE>>"}}]}}`)
	},
}

BackendTCPDropDuringActiveSubscription verifies that when the backend forcibly drops TCP during an active subscription, the client receives an error for the affected subscription.

View Source
var ClientCloseFrameCleansUpBackendSubscription = speedtrap.Scenario{
	Name: "client close frame cleans up backend subscription",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"cclose"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"cclose"}}}`)
		subID := extractID(msg)

		require.NoError(s, c.SendClose(1000, "going away"))

		msg, err = b.Read()
		if err != nil {
			return
		}
		require.JSONEq(s, fmt.Sprintf(`{"type":"complete","id":"%s"}`, subID), msg)
	},
}

ClientCloseFrameCleansUpBackendSubscription verifies the proxy cleans up the backend subscription when the client sends a close frame.

View Source
var ClientTCPDropCleansUpBackendSubscription = speedtrap.Scenario{
	Name: "client TCP drop cleans up backend subscription",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"cdrop"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"cdrop"}}}`)
		subID := extractID(msg)

		require.NoError(s, c.Drop())

		msg, err = b.Read()
		if err != nil {
			return
		}
		require.JSONEq(s, fmt.Sprintf(`{"type":"complete","id":"%s"}`, subID), msg)
	},
}

ClientTCPDropCleansUpBackendSubscription verifies the proxy cleans up the backend subscription when the client forcibly drops TCP.

View Source
var ConnectionInitAndAck = speedtrap.Scenario{
	Name: "connection init and ack",
	Run: func(s *speedtrap.S) {
		c, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)

		require.NoError(s, c.Send(`{"type":"connection_init"}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)
	},
}

ConnectionInitAndAck verifies the basic connection_init → connection_ack handshake for the legacy graphql-ws (subscriptions-transport-ws) protocol.

View Source
var ConnectionInitPayloadForwardedToBackend = speedtrap.Scenario{
	Name: "connection init payload forwarded to backend",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)
		require.NoError(s, c.Send(`{"type":"connection_init","payload":{"token":"abc"}}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"init"}}}`))

		b, err := s.Backend("subgraph-a").Accept()
		require.NoError(s, err)
		msg, err = b.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_init","payload":{"token":"abc"}}`, msg)
		require.NoError(s, b.Send(`{"type":"connection_ack"}`))

		msg, err = b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"init"}}}`)
		subID := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

ConnectionInitPayloadForwardedToBackend verifies that the connection_init payload from the client is forwarded to the backend (legacy protocol variant).

View Source
var ConnectionTerminateCleansUp = speedtrap.Scenario{
	Name: "connection terminate cleans up",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"term"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"term"}}}`)
		subID := extractID(msg)

		require.NoError(s, c.Send(`{"type":"connection_terminate"}`))

		msg, err = b.Read()
		require.NoError(s, err)
		require.JSONEq(s, fmt.Sprintf(`{"type":"complete","id":"%s"}`, subID), msg)

		_, err = c.Read()
		require.Error(s, err, "expected client connection to be closed after connection_terminate")
	},
}

ConnectionTerminateCleansUp verifies that sending connection_terminate causes the proxy to clean up backend subscriptions and close the connection.

View Source
var DifferentInitPayloadsGetSeparateUpstreamConnections = speedtrap.Scenario{
	Name: "different init payloads get separate upstream connections",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c1, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)
		require.NoError(s, c1.Send(`{"type":"connection_init","payload":{"token":"aaa"}}`))
		msg, err := c1.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c1.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"sep1"}}}`))

		c2, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)
		require.NoError(s, c2.Send(`{"type":"connection_init","payload":{"token":"bbb"}}`))
		msg, err = c2.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c2.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"sep2"}}}`))

		conns := make([]*speedtrap.ConnectionHandle, 2)
		for i := range conns {
			conns[i], err = s.Backend("subgraph-a").Accept()
			require.NoError(s, err)
		}

		var b1, b2 *speedtrap.ConnectionHandle
		token := func(t speedtrap.Tagged) string { return gjson.Get(t.Msg, "payload.token").String() }
		speedtrap.ReadSwitch(s, speedtrap.MergeMessages(conns...), token,
			speedtrap.Case("aaa", func(t speedtrap.Tagged) {
				require.JSONEq(s, `{"type":"connection_init","payload":{"token":"aaa"}}`, t.Msg)
				require.NoError(s, t.Conn.Send(`{"type":"connection_ack"}`))
				b1 = t.Conn
			}),
			speedtrap.Case("bbb", func(t speedtrap.Tagged) {
				require.JSONEq(s, `{"type":"connection_init","payload":{"token":"bbb"}}`, t.Msg)
				require.NoError(s, t.Conn.Send(`{"type":"connection_ack"}`))
				b2 = t.Conn
			}),
		)

		msg, err = b1.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"sep1"}}}`)
		subID1 := extractID(msg)

		msg, err = b2.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"sep2"}}}`)
		subID2 := extractID(msg)

		require.NoError(s, b1.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID1)))
		msg = readSkippingKA(s, c1)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)

		require.NoError(s, b2.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID2)))
		msg = readSkippingKA(s, c2)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

DifferentInitPayloadsGetSeparateUpstreamConnections verifies that two client connections with different connection_init payloads result in separate upstream connections (legacy protocol variant).

View Source
var DuplicateConnectionInitClosesSocket = speedtrap.Scenario{
	Name: "duplicate connection init closes socket with 4429",
	Run: func(s *speedtrap.S) {
		c, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)

		require.NoError(s, c.Send(`{"type":"connection_init"}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c.Send(`{"type":"connection_init"}`))

		cf, err := c.ReadControl()
		require.NoError(s, err)
		require.Equal(s, ws.OpClose, cf.OpCode)
		code, reason := cf.CloseData()
		require.Equal(s, ws.StatusCode(4429), code, "expected close code 4429, got %d", int(code))
		require.Equal(s, "Too many initialisation requests", reason)
	},
}

DuplicateConnectionInitClosesSocket verifies that sending a second connection_init after acknowledgment closes the connection with 4429. This mirrors graphql-transport-ws behavior rather than the reference server which silently re-acks. See PROTOCOL_AMENDMENTS.md.

View Source
var DuplicateStartIDClosesSocket = speedtrap.Scenario{
	Name: "duplicate start ID closes socket",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"dup"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"dup"}}}`)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"dup"}}}`))

		_, err = c.Read()
		require.Error(s, err, "expected connection to be closed after duplicate start ID")
	},
}

DuplicateStartIDClosesSocket verifies that sending a "start" with an ID that is already active causes the server to close the connection.

NOTE: This diverges from the subscriptions-transport-ws reference server (v0.11.0), which silently completes the existing subscription and starts a new one with the same ID (replace semantics). We intentionally treat duplicate IDs as an error to match the graphql-transport-ws behavior (close with 4409) and avoid the complexity of implicit replacement.

View Source
var MultipleConcurrentSubscriptions = speedtrap.Scenario{
	Name: "multiple concurrent subscriptions on one connection",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"a"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		subID1 := extractID(msg)

		require.NoError(s, c.Send(`{"id":"2","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"b"}}}`))

		_, err = s.Backend("subgraph-a").TryAccept()
		require.Error(s, err, "expected no second upstream connection (multiplexing)")

		msg, err = b.Read()
		require.NoError(s, err)
		subID2 := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"a"}}}}`, subID1)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"data","payload":{"data":{"streamA":{"key":"a"}}}}`)

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"b"}}}}`, subID2)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"2","type":"data","payload":{"data":{"streamA":{"key":"b"}}}}`)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID1)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID2)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"2","type":"complete"}`)
	},
}

MultipleConcurrentSubscriptions verifies that the proxy correctly handles multiple active subscriptions on a single legacy graphql-ws connection.

View Source
var MultipleDataMessagesBeforeComplete = speedtrap.Scenario{
	Name: "multiple data messages before complete",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key contents}}","variables":{"key":"multi"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		subID := extractID(msg)

		for i := 1; i <= 3; i++ {
			require.NoError(s, b.Send(fmt.Sprintf(
				`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"multi","contents":%d}}}}`, subID, i)))
		}

		for i := 1; i <= 3; i++ {
			msg = readSkippingKA(s, c)
			ja.Assertf(msg, `{"id":"1","type":"data","payload":{"data":{"streamA":{"key":"multi","contents":%d}}}}`, i)
		}

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

MultipleDataMessagesBeforeComplete verifies the proxy forwards multiple data messages for a single subscription before the final complete.

View Source
var MultipleSubscriptionsShareOneUpstreamConnection = speedtrap.Scenario{
	Name: "multiple subscriptions share one upstream connection",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"mux1"}}}`))
		require.NoError(s, c.Send(`{"id":"2","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"mux2"}}}`))

		b := backendHandshake(s)

		// Read both subscribes (order is non-deterministic)
		var subIDMux1, subIDMux2 string
		varKey := func(msg string) string { return gjson.Get(msg, "payload.variables.key").String() }
		speedtrap.ReadSwitch(s, b.Messages(), varKey,
			speedtrap.Case("mux1", func(msg string) {
				ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"mux1"}}}`)
				subIDMux1 = extractID(msg)
			}),
			speedtrap.Case("mux2", func(msg string) {
				ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"mux2"}}}`)
				subIDMux2 = extractID(msg)
			}),
		)

		_, err := s.Backend("subgraph-a").TryAccept()
		require.Error(s, err, "expected no second upstream connection (multiplexing)")

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"mux1"}}}}`, subIDMux1)))
		msg := readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"data","payload":{"data":{"streamA":{"key":"mux1"}}}}`)

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"mux2"}}}}`, subIDMux2)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"2","type":"data","payload":{"data":{"streamA":{"key":"mux2"}}}}`)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subIDMux1)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subIDMux2)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"2","type":"complete"}`)
	},
}

MultipleSubscriptionsShareOneUpstreamConnection verifies that the proxy multiplexes multiple subscriptions from a single client connection onto one upstream backend connection (legacy protocol variant).

View Source
var NonAllowlistedHeadersFilteredOut = speedtrap.Scenario{
	Name: "non-allowlisted headers filtered out",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		headers := http.Header{
			"Authorization":     {"Bearer tok"},
			"X-Secret-Internal": {"should-be-stripped"},
		}
		c, err := s.Client(
			speedtrap.WithClientSubprotocol("graphql-ws"),
			speedtrap.WithClientHeaders(headers),
		)
		require.NoError(s, err)
		require.NoError(s, c.Send(`{"type":"connection_init"}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"filter"}}}`))

		b, err := s.Backend("subgraph-a").Accept()
		require.NoError(s, err)

		require.Equal(s, "Bearer tok", b.UpgradeHeaders.Get("Authorization"))
		require.Empty(s, b.UpgradeHeaders.Get("X-Secret-Internal"))

		msg, err = b.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_init"}`, msg)
		require.NoError(s, b.Send(`{"type":"connection_ack"}`))

		msg, err = b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"filter"}}}`)
		subID := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

NonAllowlistedHeadersFilteredOut verifies that client upgrade headers NOT matching the router's header propagation rules are stripped before the upstream WebSocket dial (legacy protocol variant).

View Source
var OneSubscriptionErrorDoesNotAffectAnother = speedtrap.Scenario{
	Name: "one subscription error does not affect another",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"err1"}}}`))
		require.NoError(s, c.Send(`{"id":"2","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"err2"}}}`))

		b := backendHandshake(s)

		// Read both subscribes (order is non-deterministic)
		var subID1, subID2 string
		varKey := func(msg string) string { return gjson.Get(msg, "payload.variables.key").String() }
		speedtrap.ReadSwitch(s, b.Messages(), varKey,
			speedtrap.Case("err1", func(msg string) { subID1 = extractID(msg) }),
			speedtrap.Case("err2", func(msg string) { subID2 = extractID(msg) }),
		)

		_, err := s.Backend("subgraph-a").TryAccept()
		require.Error(s, err, "expected no second upstream connection (multiplexing)")

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"error","payload":[{"message":"sub1 failed"}]}`, subID1)))

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"err2"}}}}`, subID2)))

		isKA := func(msg string) bool { return msg == `{"type":"ka"}` }
		idType := func(msg string) string { return extractID(msg) + ":" + extractType(msg) }
		speedtrap.ReadSwitch(s, speedtrap.Filter(c.Messages(), isKA), idType,
			speedtrap.Case("1:data", func(msg string) {
				ja.Assertf(msg, `{"id":"1","type":"data","payload":{"errors":[{"message":"sub1 failed"}]}}`)
			}),
			speedtrap.Case("1:complete", func(msg string) {
				ja.Assertf(msg, `{"id":"1","type":"complete"}`)
			}),
			speedtrap.Case("2:data", func(msg string) {
				ja.Assertf(msg, `{"id":"2","type":"data","payload":{"data":{"streamA":{"key":"err2"}}}}`)
			}),
		)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID2)))

		msg := readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"2","type":"complete"}`)
	},
}

OneSubscriptionErrorDoesNotAffectAnother verifies that an error on one subscription does not disrupt a concurrent subscription on the same connection.

Scenarios contains all subscriptions-transport-ws ("graphql-ws" subprotocol) proxy scenarios. The test harness must register a backend named "subgraph-a" that speaks graphql-transport-ws (the router translates between protocols).

View Source
var ServerErrorInDataPayload = speedtrap.Scenario{
	Name: "server error delivered in data payload",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"err"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		subID := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"error","payload":[{"message":"something went wrong"}]}`, subID)))

		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"data","payload":{"errors":[{"message":"something went wrong"}]}}`)

		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

ServerErrorInDataPayload verifies that when the backend sends a terminal error, the router delivers it to the legacy client as a "data" message with errors in the payload followed by a "complete" frame. subscriptions-transport-ws reserves "error" for pre-execution failures, so terminal runtime errors are expressed as data+errors plus a terminal complete.

View Source
var StartDataCompleteRoundTrip = speedtrap.Scenario{
	Name: "start data complete round-trip",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key contents}}","variables":{"key":"test"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key contents}}","variables":{"key":"test"}}}`)
		subID := extractID(msg)

		require.NoError(s, b.Send(fmt.Sprintf(
			`{"id":"%s","type":"next","payload":{"data":{"streamA":{"key":"test","contents":null}}}}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"data","payload":{"data":{"streamA":{"key":"test","contents":null}}}}`)

		require.NoError(s, b.Send(fmt.Sprintf(`{"id":"%s","type":"complete"}`, subID)))
		msg = readSkippingKA(s, c)
		ja.Assertf(msg, `{"id":"1","type":"complete"}`)
	},
}

StartDataCompleteRoundTrip verifies the full start → data → complete lifecycle through the proxy. The client speaks legacy graphql-ws while the backend speaks graphql-transport-ws (the router translates).

View Source
var StopCancelsSubscription = speedtrap.Scenario{
	Name: "stop cancels upstream subscription",
	Run: func(s *speedtrap.S) {
		ja := jsonassert.New(s)

		c := clientHandshake(s)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"stop"}}}`))

		b := backendHandshake(s)
		msg, err := b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"subscribe","id":"<<PRESENCE>>","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"stop"}}}`)
		subID := extractID(msg)

		require.NoError(s, c.Send(`{"id":"1","type":"stop"}`))

		msg, err = b.Read()
		require.NoError(s, err)
		ja.Assertf(msg, `{"type":"complete","id":"%s"}`, subID)
	},
}

StopCancelsSubscription verifies that the client sending a "stop" message causes the proxy to forward a "complete" to the upstream backend.

View Source
var SubscribeBeforeAckClosesSocket = speedtrap.Scenario{
	Name: "subscribe before connection ack closes socket with 4401",
	Run: func(s *speedtrap.S) {
		c, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)

		require.NoError(s, c.Send(`{"id":"1","type":"start","payload":{"query":"subscription($key: String){streamA(key: $key){key}}","variables":{"key":"noack"}}}`))

		cf, err := c.ReadControl()
		require.NoError(s, err)
		require.Equal(s, ws.OpClose, cf.OpCode)
		code, _ := cf.CloseData()
		require.Equal(s, ws.StatusCode(4401), code, "expected close code 4401, got %d", int(code))
	},
}

SubscribeBeforeAckClosesSocket verifies that sending a start message before the connection is acknowledged closes the connection with 4401. This mirrors graphql-transport-ws behavior rather than the reference server which processes the subscription without a handshake. See PROTOCOL_AMENDMENTS.md.

View Source
var UnknownMessageTypeClosesSocket = speedtrap.Scenario{
	Name: "unknown message type closes socket with 4400",
	Run: func(s *speedtrap.S) {
		c, err := s.Client(speedtrap.WithClientSubprotocol("graphql-ws"))
		require.NoError(s, err)

		require.NoError(s, c.Send(`{"type":"connection_init"}`))
		msg, err := c.Read()
		require.NoError(s, err)
		require.JSONEq(s, `{"type":"connection_ack"}`, msg)

		require.NoError(s, c.Send(`{"type":"bogus"}`))

		cf, err := c.ReadControl()
		require.NoError(s, err)
		require.Equal(s, ws.OpClose, cf.OpCode)
		code, _ := cf.CloseData()
		require.Equal(s, ws.StatusCode(4400), code, "expected close code 4400, got %d", int(code))
	},
}

UnknownMessageTypeClosesSocket verifies that sending an unknown message type closes the connection with 4400. This mirrors graphql-transport-ws behavior rather than the reference server which returns an error message and keeps the connection alive. See PROTOCOL_AMENDMENTS.md.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址