github.1485827954.workers.dev/newrelic/newrelic-client-go@v1.1.0/internal/http/compress_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package http 5 6 import ( 7 "io" 8 "io/ioutil" 9 "testing" 10 11 "github.com/stretchr/testify/assert" 12 13 mock "github.com/newrelic/newrelic-client-go/pkg/testhelpers" 14 ) 15 16 var testCompressionCases = []struct { 17 compress bool 18 data []byte 19 gzip []byte 20 }{ 21 { 22 compress: false, 23 data: []byte("test"), 24 }, 25 { 26 compress: false, 27 data: []byte(`abcdefghijklmnopqrstuvwxyz1234567890`), 28 }, 29 { 30 compress: true, 31 data: []byte(`{"data": "json", "maybe":"didn't check", "handcrafted":true, "example": { "sub": "object", "arry": [ "why", "not" ] }, "todo": [ "make", "it", "larger" }`), 32 gzip: []byte{ 33 0x1f, 0x8b, 0x8, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0xff, 0x24, 0x8c, 0x4d, 0xae, 0xc2, 0x30, 0xc, 0x6, 0xaf, 0xf2, 34 0xe9, 0xdb, 0xbc, 0x4d, 0x4f, 0x90, 0xab, 0x3c, 0xb1, 0x70, 0x63, 0x43, 0x7f, 0x13, 0xe4, 0xba, 0x82, 0xaa, 0xea, 0xdd, 0x51, 0x60, 0x3b, 0xa3, 35 0x99, 0x93, 0x2a, 0x21, 0x4c, 0xe0, 0xb4, 0xd5, 0xc2, 0xe, 0x5c, 0xe5, 0xe8, 0x8d, 0x89, 0x3a, 0x6a, 0xf9, 0xb, 0xe4, 0xc1, 0xf2, 0xdc, 0xf8, 36 0x20, 0x45, 0xb3, 0xcb, 0x3d, 0x4c, 0x99, 0xc2, 0x77, 0xeb, 0x40, 0x7b, 0xcb, 0xfa, 0x5c, 0x8c, 0x9, 0x27, 0xb8, 0xed, 0x7d, 0xfb, 0xd4, 0x7e, 37 0xb2, 0x1c, 0xad, 0x10, 0xf7, 0x83, 0x9, 0xff, 0xe0, 0x6b, 0x38, 0x1a, 0x28, 0x35, 0x88, 0x1b, 0xae, 0xe, 0x8c, 0xaa, 0xf5, 0xe7, 0x56, 0x99, 38 0xad, 0xc9, 0xf1, 0xdb, 0x2c, 0xe2, 0xf, 0x73, 0xe2, 0xfa, 0x4, 0x0, 0x0, 0xff, 0xff, 0x60, 0x64, 0xbb, 0x7c, 0x99, 0x0, 0x0, 0x0, 39 }, 40 }, 41 } 42 43 func TestNoneCompressor(t *testing.T) { 44 t.Parallel() 45 46 tc := mock.NewTestConfig(t, nil) 47 c := NewClient(tc) 48 49 req, err := c.NewRequest("POST", c.config.Region().RestURL("path"), nil, nil, nil) 50 assert.NoError(t, err) 51 52 compress := NoneCompressor{} 53 var bodyReader io.Reader 54 55 for _, d := range testCompressionCases { 56 req.SetHeader("content-encoding", "<invalid>") 57 58 bodyReader, err = compress.Compress(req, d.data) 59 assert.NoError(t, err) 60 assert.Equal(t, "", req.GetHeader("content-encoding")) 61 62 res, err := ioutil.ReadAll(bodyReader) 63 assert.NoError(t, err) 64 assert.Equal(t, d.data, res) 65 } 66 } 67 68 func TestGzipCompressor(t *testing.T) { 69 t.Parallel() 70 71 tc := mock.NewTestConfig(t, nil) 72 c := NewClient(tc) 73 74 req, err := c.NewRequest("POST", c.config.Region().RestURL("path"), nil, nil, nil) 75 assert.NoError(t, err) 76 77 compress := GzipCompressor{} 78 var bodyReader io.Reader 79 80 for _, d := range testCompressionCases { 81 req.SetHeader("content-encoding", "<invalid>") 82 83 bodyReader, err = compress.Compress(req, d.data) 84 assert.NoError(t, err) 85 86 res, err := ioutil.ReadAll(bodyReader) 87 assert.NoError(t, err) 88 89 if d.compress { 90 assert.Equal(t, "gzip", req.GetHeader("content-encoding")) 91 assert.Equal(t, d.gzip, res) 92 } else { 93 assert.Equal(t, "", req.GetHeader("content-encoding")) 94 assert.Equal(t, d.data, res) 95 } 96 } 97 }