github.com/prebid/prebid-server/v2@v2.18.0/usersync/encoder_decoder_test.go (about) 1 package usersync 2 3 import ( 4 "net/http" 5 "testing" 6 "time" 7 8 "github.com/stretchr/testify/assert" 9 ) 10 11 func TestEncoderDecoder(t *testing.T) { 12 encoder := Base64Encoder{} 13 decoder := Base64Decoder{} 14 15 testCases := []struct { 16 name string 17 givenRequest *http.Request 18 givenHttpCookie *http.Cookie 19 givenCookie *Cookie 20 givenDecoder Decoder 21 expectedCookie *Cookie 22 }{ 23 { 24 name: "simple-cookie", 25 givenCookie: &Cookie{ 26 uids: map[string]UIDEntry{ 27 "adnxs": { 28 UID: "UID", 29 Expires: time.Time{}, 30 }, 31 }, 32 optOut: false, 33 }, 34 expectedCookie: &Cookie{ 35 uids: map[string]UIDEntry{ 36 "adnxs": { 37 UID: "UID", 38 }, 39 }, 40 optOut: false, 41 }, 42 }, 43 { 44 name: "empty-cookie", 45 givenCookie: &Cookie{}, 46 expectedCookie: &Cookie{ 47 uids: map[string]UIDEntry{}, 48 optOut: false, 49 }, 50 }, 51 { 52 name: "nil-cookie", 53 givenCookie: nil, 54 expectedCookie: &Cookie{ 55 uids: map[string]UIDEntry{}, 56 optOut: false, 57 }, 58 }, 59 } 60 61 for _, test := range testCases { 62 t.Run(test.name, func(t *testing.T) { 63 encodedCookie, err := encoder.Encode(test.givenCookie) 64 assert.NoError(t, err) 65 decodedCookie := decoder.Decode(encodedCookie) 66 67 assert.Equal(t, test.expectedCookie.uids, decodedCookie.uids) 68 assert.Equal(t, test.expectedCookie.optOut, decodedCookie.optOut) 69 }) 70 } 71 } 72 73 func TestEncoder(t *testing.T) { 74 encoder := Base64Encoder{} 75 76 testCases := []struct { 77 name string 78 givenCookie *Cookie 79 expectedEncodedCookie string 80 }{ 81 { 82 name: "simple-cookie", 83 givenCookie: &Cookie{ 84 uids: map[string]UIDEntry{ 85 "adnxs": { 86 UID: "UID", 87 Expires: time.Time{}, 88 }, 89 }, 90 optOut: false, 91 }, 92 expectedEncodedCookie: "eyJ0ZW1wVUlEcyI6eyJhZG54cyI6eyJ1aWQiOiJVSUQiLCJleHBpcmVzIjoiMDAwMS0wMS0wMVQwMDowMDowMFoifX19", 93 }, 94 { 95 name: "empty-cookie", 96 givenCookie: &Cookie{}, 97 expectedEncodedCookie: "e30=", 98 }, 99 { 100 name: "nil-cookie", 101 givenCookie: nil, 102 expectedEncodedCookie: "bnVsbA==", 103 }, 104 } 105 106 for _, test := range testCases { 107 t.Run(test.name, func(t *testing.T) { 108 encodedCookie, err := encoder.Encode(test.givenCookie) 109 assert.NoError(t, err) 110 111 assert.Equal(t, test.expectedEncodedCookie, encodedCookie) 112 }) 113 } 114 } 115 116 func TestDecoder(t *testing.T) { 117 decoder := Base64Decoder{} 118 119 testCases := []struct { 120 name string 121 givenEncodedCookie string 122 expectedCookie *Cookie 123 }{ 124 { 125 name: "simple-encoded-cookie", 126 givenEncodedCookie: "eyJ0ZW1wVUlEcyI6eyJhZG54cyI6eyJ1aWQiOiJVSUQiLCJleHBpcmVzIjoiMDAwMS0wMS0wMVQwMDowMDowMFoifX19", 127 expectedCookie: &Cookie{ 128 uids: map[string]UIDEntry{ 129 "adnxs": { 130 UID: "UID", 131 }, 132 }, 133 optOut: false, 134 }, 135 }, 136 { 137 name: "nil-encoded-cookie", 138 givenEncodedCookie: "", 139 expectedCookie: NewCookie(), 140 }, 141 } 142 143 for _, test := range testCases { 144 t.Run(test.name, func(t *testing.T) { 145 decodedCookie := decoder.Decode(test.givenEncodedCookie) 146 assert.Equal(t, test.expectedCookie, decodedCookie) 147 }) 148 } 149 }