MCPcopy
hub / github.com/cloudflare/cloudflared / TestWebsocketWrapper

Function TestWebsocketWrapper

carrier/websocket_test.go:81–123  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

79}
80
81func TestWebsocketWrapper(t *testing.T) {
82 listener, err := hello.CreateTLSListener("localhost:0")
83 require.NoError(t, err)
84
85 serverErrorChan := make(chan error)
86 helloSvrCtx, cancelHelloSvr := context.WithCancel(context.Background())
87 defer func() { <-serverErrorChan }()
88 defer cancelHelloSvr()
89 go func() {
90 log := zerolog.Nop()
91 serverErrorChan <- hello.StartHelloWorldServer(&log, listener, helloSvrCtx.Done())
92 }()
93
94 tlsConfig := websocketClientTLSConfig(t)
95 d := gws.Dialer{TLSClientConfig: tlsConfig, HandshakeTimeout: time.Minute}
96 testAddr := fmt.Sprintf("https://%s/ws", listener.Addr().String())
97 req := testRequest(t, testAddr, nil)
98 conn, resp, err := clientConnect(req, &d)
99 require.NoError(t, err)
100 assert.Equal(t, "websocket", resp.Header.Get("Upgrade"))
101
102 // Websocket now connected to test server so lets check our wrapper
103 wrapper := cfwebsocket.GorillaConn{Conn: conn}
104 buf := make([]byte, 100)
105 wrapper.Write([]byte("abc"))
106 n, err := wrapper.Read(buf)
107 require.NoError(t, err)
108 require.Equal(t, n, 3)
109 require.Equal(t, "abc", string(buf[:n]))
110
111 // Test partial read, read 1 of 3 bytes in one read and the other 2 in another read
112 wrapper.Write([]byte("abc"))
113 buf = buf[:1]
114 n, err = wrapper.Read(buf)
115 require.NoError(t, err)
116 require.Equal(t, n, 1)
117 require.Equal(t, "a", string(buf[:n]))
118 buf = buf[:cap(buf)]
119 n, err = wrapper.Read(buf)
120 require.NoError(t, err)
121 require.Equal(t, n, 2)
122 require.Equal(t, "bc", string(buf[:n]))
123}

Callers

nothing calls this directly

Calls 11

WriteMethod · 0.95
ReadMethod · 0.95
CreateTLSListenerFunction · 0.92
StartHelloWorldServerFunction · 0.92
websocketClientTLSConfigFunction · 0.85
clientConnectFunction · 0.85
AddrMethod · 0.80
GetMethod · 0.80
testRequestFunction · 0.70
StringMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected