github.com/anycable/anycable-go@v1.5.1/server/headers_extractor_test.go (about) 1 package server 2 3 import ( 4 "net/http/httptest" 5 "testing" 6 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestHeadersExtractor_FromRequest(t *testing.T) { 11 req := httptest.NewRequest("GET", "/", nil) 12 req.Header.Set("Cookie", "yummy_cookie=raisin;tasty_cookie=strawberry") 13 req.Header.Set("X-Api-Token", "42") 14 req.Header.Set("Accept-Language", "ru") 15 16 t.Run("Without specified headers", func(t *testing.T) { 17 extractor := DefaultHeadersExtractor{} 18 headers := extractor.FromRequest(req) 19 20 assert.Len(t, headers, 1) 21 assert.Equal(t, "192.0.2.1", headers["REMOTE_ADDR"]) 22 }) 23 24 t.Run("With specified headers", func(t *testing.T) { 25 extractor := DefaultHeadersExtractor{Headers: []string{"cookie", "x-api-token", "x-jid"}} 26 headers := extractor.FromRequest(req) 27 28 assert.Len(t, headers, 3) 29 30 assert.Empty(t, headers["accept-language"]) 31 assert.Equal(t, "42", headers["x-api-token"]) 32 assert.Equal(t, "yummy_cookie=raisin;tasty_cookie=strawberry", headers["cookie"]) 33 assert.Equal(t, "192.0.2.1", headers["REMOTE_ADDR"]) 34 35 _, ok := headers["x-jid"] 36 assert.False(t, ok) 37 }) 38 39 t.Run("With specified headers and cookie filter", func(t *testing.T) { 40 extractor := DefaultHeadersExtractor{Headers: []string{"cookie"}, Cookies: []string{"yummy_cookie"}} 41 headers := extractor.FromRequest(req) 42 43 assert.Len(t, headers, 2) 44 45 assert.Empty(t, headers["accept-language"]) 46 assert.Equal(t, "yummy_cookie=raisin;", headers["cookie"]) 47 assert.Equal(t, "192.0.2.1", headers["REMOTE_ADDR"]) 48 }) 49 }