github.com/anycable/anycable-go@v1.5.1/server/request_info_test.go (about) 1 package server 2 3 import ( 4 "crypto/tls" 5 "net/http/httptest" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestFetchUID(t *testing.T) { 13 t.Run("Without request id", func(t *testing.T) { 14 req := httptest.NewRequest("GET", "/", nil) 15 uid, _ := FetchUID(req) 16 17 assert.NotEqual(t, "", uid) 18 }) 19 20 t.Run("With request id", func(t *testing.T) { 21 req := httptest.NewRequest("GET", "/", nil) 22 req.Header.Set("x-request-id", "external-request-id") 23 24 uid, _ := FetchUID(req) 25 26 assert.Equal(t, "external-request-id", uid) 27 }) 28 } 29 30 func TestRequestInfo(t *testing.T) { 31 t.Run("With just path", func(t *testing.T) { 32 req := httptest.NewRequest("GET", "/cable", nil) 33 info, err := NewRequestInfo(req, nil) 34 35 require.NoError(t, err) 36 assert.Equal(t, "http://example.com/cable", info.URL) 37 }) 38 39 t.Run("With just path + TLS", func(t *testing.T) { 40 req := httptest.NewRequest("GET", "/cable", nil) 41 req.TLS = &tls.ConnectionState{} 42 info, err := NewRequestInfo(req, nil) 43 44 require.NoError(t, err) 45 assert.Equal(t, "https://example.com/cable", info.URL) 46 }) 47 48 t.Run("With fully qualified URL", func(t *testing.T) { 49 req := httptest.NewRequest("GET", "ws://anycable.io/cable", nil) 50 info, err := NewRequestInfo(req, nil) 51 52 require.NoError(t, err) 53 assert.Equal(t, "ws://anycable.io/cable", info.URL) 54 }) 55 56 t.Run("With params", func(t *testing.T) { 57 req := httptest.NewRequest("GET", "ws://anycable.io/cable?pi=3&pp=no&pi=5", nil) 58 info, err := NewRequestInfo(req, nil) 59 60 require.NoError(t, err) 61 assert.Equal(t, "5", info.Param("pi")) 62 assert.Equal(t, "no", info.Param("pp")) 63 64 blank_info := RequestInfo{} 65 assert.Equal(t, "", blank_info.Param("pi")) 66 }) 67 68 t.Run("With AnyCable Headers", func(t *testing.T) { 69 req := httptest.NewRequest("GET", "ws://anycable.io/cable?pi=3&pp=no&pi=5", nil) 70 req.Header.Set("x-anycable-sid", "432") 71 req.Header.Set("X-AnyCable-Ping-Interval", "10") 72 info, err := NewRequestInfo(req, nil) 73 74 require.NoError(t, err) 75 assert.Equal(t, "432", info.AnyCableHeader("X-ANYCABLE-SID")) 76 assert.Equal(t, "10", info.AnyCableHeader("x-anycable-ping-interval")) 77 78 blank_info := RequestInfo{} 79 assert.Equal(t, "", blank_info.AnyCableHeader("SID")) 80 }) 81 }