github.com/prebid/prebid-server/v2@v2.18.0/version/xprebidheader_test.go (about) 1 package version 2 3 import ( 4 "testing" 5 6 "github.com/prebid/openrtb/v20/openrtb2" 7 "github.com/stretchr/testify/assert" 8 9 "github.com/prebid/prebid-server/v2/openrtb_ext" 10 "github.com/prebid/prebid-server/v2/util/jsonutil" 11 ) 12 13 func TestBuildXPrebidHeader(t *testing.T) { 14 testCases := []struct { 15 description string 16 version string 17 result string 18 }{ 19 { 20 description: "No Version", 21 version: "", 22 result: "pbs-go/unknown", 23 }, 24 { 25 description: "Version", 26 version: "0.100.0", 27 result: "pbs-go/0.100.0", 28 }, 29 } 30 31 for _, test := range testCases { 32 result := BuildXPrebidHeader(test.version) 33 assert.Equal(t, test.result, result, test.description) 34 } 35 } 36 37 func TestBuildXPrebidHeaderForRequest(t *testing.T) { 38 testCases := []struct { 39 description string 40 version string 41 requestExt *openrtb_ext.ExtRequest 42 requestAppExt *openrtb_ext.ExtApp 43 result string 44 }{ 45 { 46 description: "No versions", 47 version: "", 48 result: "pbs-go/unknown", 49 }, 50 { 51 description: "pbs", 52 version: "test-version", 53 result: "pbs-go/test-version", 54 }, 55 { 56 description: "prebid.js", 57 version: "test-version", 58 requestExt: &openrtb_ext.ExtRequest{ 59 Prebid: openrtb_ext.ExtRequestPrebid{ 60 Channel: &openrtb_ext.ExtRequestPrebidChannel{ 61 Name: "pbjs", 62 Version: "test-pbjs-version", 63 }, 64 }, 65 }, 66 result: "pbs-go/test-version,pbjs/test-pbjs-version", 67 }, 68 { 69 description: "unknown prebid.js", 70 version: "test-version", 71 requestExt: &openrtb_ext.ExtRequest{ 72 Prebid: openrtb_ext.ExtRequestPrebid{ 73 Channel: &openrtb_ext.ExtRequestPrebidChannel{ 74 Name: "pbjs", 75 }, 76 }, 77 }, 78 result: "pbs-go/test-version,pbjs/unknown", 79 }, 80 { 81 description: "channel without a name", 82 version: "test-version", 83 requestExt: &openrtb_ext.ExtRequest{ 84 Prebid: openrtb_ext.ExtRequestPrebid{ 85 Channel: &openrtb_ext.ExtRequestPrebidChannel{ 86 Version: "test-version", 87 }, 88 }, 89 }, 90 result: "pbs-go/test-version", 91 }, 92 { 93 description: "prebid-mobile", 94 version: "test-version", 95 requestAppExt: &openrtb_ext.ExtApp{ 96 Prebid: openrtb_ext.ExtAppPrebid{ 97 Source: "prebid-mobile", 98 Version: "test-prebid-mobile-version", 99 }, 100 }, 101 result: "pbs-go/test-version,prebid-mobile/test-prebid-mobile-version", 102 }, 103 { 104 description: "app ext without a source", 105 version: "test-version", 106 requestAppExt: &openrtb_ext.ExtApp{ 107 Prebid: openrtb_ext.ExtAppPrebid{ 108 Version: "test-version", 109 }, 110 }, 111 result: "pbs-go/test-version", 112 }, 113 { 114 description: "Version found in both req.Ext and req.App.Ext", 115 version: "test-version", 116 requestExt: &openrtb_ext.ExtRequest{ 117 Prebid: openrtb_ext.ExtRequestPrebid{ 118 Channel: &openrtb_ext.ExtRequestPrebidChannel{ 119 Name: "pbjs", 120 Version: "test-pbjs-version", 121 }, 122 }, 123 }, 124 requestAppExt: &openrtb_ext.ExtApp{ 125 Prebid: openrtb_ext.ExtAppPrebid{ 126 Source: "prebid-mobile", 127 Version: "test-prebid-mobile-version", 128 }, 129 }, 130 result: "pbs-go/test-version,pbjs/test-pbjs-version,prebid-mobile/test-prebid-mobile-version", 131 }, 132 } 133 134 for _, test := range testCases { 135 req := &openrtb2.BidRequest{} 136 if test.requestExt != nil { 137 reqExt, err := jsonutil.Marshal(test.requestExt) 138 assert.NoError(t, err, test.description+":err marshalling reqExt") 139 req.Ext = reqExt 140 } 141 if test.requestAppExt != nil { 142 reqAppExt, err := jsonutil.Marshal(test.requestAppExt) 143 assert.NoError(t, err, test.description+":err marshalling reqAppExt") 144 req.App = &openrtb2.App{Ext: reqAppExt} 145 } 146 result := BuildXPrebidHeaderForRequest(req, test.version) 147 assert.Equal(t, test.result, result, test.description+":result") 148 } 149 }