github.com/prebid/prebid-server/v2@v2.18.0/macros/string_index_based_replacer_test.go (about) 1 package macros 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/prebid/openrtb/v20/openrtb2" 8 "github.com/prebid/prebid-server/v2/exchange/entities" 9 "github.com/prebid/prebid-server/v2/openrtb_ext" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestStringIndexBasedReplace(t *testing.T) { 14 15 type args struct { 16 url string 17 getMacroProvider func() *MacroProvider 18 } 19 tests := []struct { 20 name string 21 args args 22 want string 23 wantErr bool 24 }{ 25 { 26 name: "success", 27 args: args{ 28 url: "http://tracker.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-DOMAIN##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o7=##PBS-LIMITADTRACKING##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1##¯o10=##PBS-BIDDER##¯o11=##PBS-INTEGRATION##¯o12=##PBS-VASTCRTID##¯o15=##PBS-AUCTIONID##¯o16=##PBS-CHANNEL##¯o17=##PBS-EVENTTYPE##¯o18=##PBS-VASTEVENT##", 29 getMacroProvider: func() *MacroProvider { 30 macroProvider := NewProvider(req) 31 macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, "test") 32 macroProvider.PopulateEventMacros("123", "vast", "firstQuartile") 33 return macroProvider 34 }, 35 }, 36 want: "http://tracker.com?macro1=bidId123¯o2=testbundle¯o3=testdomain¯o4=publishertestdomain¯o5=pageurltest¯o6=testpublisherID¯o7=10¯o8=yes¯o9=value1¯o10=test¯o11=¯o12=123¯o15=123¯o16=test1¯o17=vast¯o18=firstQuartile", 37 }, 38 { 39 name: "url does not have macro", 40 args: args{ 41 url: "http://tracker.com", 42 getMacroProvider: func() *MacroProvider { 43 macroProvider := NewProvider(req) 44 macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, "test") 45 macroProvider.PopulateEventMacros("123", "vast", "firstQuartile") 46 return macroProvider 47 }, 48 }, 49 want: "http://tracker.com", 50 }, 51 { 52 name: "macro not found", 53 args: args{ 54 url: "http://tracker.com?macro1=##PBS-test1##", 55 getMacroProvider: func() *MacroProvider { 56 macroProvider := NewProvider(&openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{}}) 57 macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, "test") 58 macroProvider.PopulateEventMacros("123", "vast", "firstQuartile") 59 return macroProvider 60 }, 61 }, 62 want: "http://tracker.com?macro1=", 63 }, 64 { 65 name: "tracker url is empty", 66 args: args{ 67 url: "", 68 getMacroProvider: func() *MacroProvider { 69 macroProvider := NewProvider(&openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{}}) 70 macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, "test") 71 macroProvider.PopulateEventMacros("123", "vast", "firstQuartile") 72 return macroProvider 73 }, 74 }, 75 want: "", 76 }, 77 } 78 for _, tt := range tests { 79 t.Run(tt.name, func(t *testing.T) { 80 replacer := NewStringIndexBasedReplacer() 81 builder := strings.Builder{} 82 replacer.Replace(&builder, tt.args.url, tt.args.getMacroProvider()) 83 assert.Equal(t, tt.want, builder.String(), tt.name) 84 }) 85 } 86 } 87 88 var lmt int8 = 10 89 var benchmarkURL = []string{ 90 "http://tracker1.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-APPBUNDLE##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o6=##PBS-LIMITADTRACKING##¯o7=##PBS-GDPRCONSENT##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1CUST1##¯o10=##PBS-MACRO-CUSTOMMACR1CUST2##", 91 "http://google.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-APPBUNDLE##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o6=##PBS-LIMITADTRACKING##¯o7=##PBS-GDPRCONSENT##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1CUST1##¯o10=##PBS-MACRO-CUSTOMMACR1CUST2##", 92 "http://pubmatic.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-APPBUNDLE##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o6=##PBS-LIMITADTRACKING##¯o7=##PBS-GDPRCONSENT##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1CUST1##¯o10=##PBS-MACRO-CUSTOMMACR1CUST2##", 93 "http://testbidder.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-APPBUNDLE##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o6=##PBS-LIMITADTRACKING##¯o7=##PBS-GDPRCONSENT##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1CUST1##¯o10=##PBS-MACRO-CUSTOMMACR1CUST2##", 94 "http://dummybidder.com?macro1=##PBS-BIDID##¯o2=##PBS-APPBUNDLE##¯o3=##PBS-APPBUNDLE##¯o4=##PBS-PUBDOMAIN##¯o5=##PBS-PAGEURL##¯o6=##PBS-ACCOUNTID##¯o6=##PBS-LIMITADTRACKING##¯o7=##PBS-GDPRCONSENT##¯o8=##PBS-GDPRCONSENT##¯o9=##PBS-MACRO-CUSTOMMACR1CUST1##¯o10=##PBS-MACRO-CUSTOMMACR1CUST2##", 95 } 96 97 var req *openrtb_ext.RequestWrapper = &openrtb_ext.RequestWrapper{ 98 BidRequest: &openrtb2.BidRequest{ 99 ID: "123", 100 Site: &openrtb2.Site{ 101 Domain: "testdomain", 102 Publisher: &openrtb2.Publisher{ 103 Domain: "publishertestdomain", 104 ID: "testpublisherID", 105 }, 106 Page: "pageurltest", 107 }, 108 App: &openrtb2.App{ 109 Domain: "testdomain", 110 Bundle: "testbundle", 111 Publisher: &openrtb2.Publisher{ 112 Domain: "publishertestdomain", 113 ID: "testpublisherID", 114 }, 115 }, 116 Device: &openrtb2.Device{ 117 Lmt: &lmt, 118 }, 119 User: &openrtb2.User{Ext: []byte(`{"consent":"yes" }`)}, 120 Ext: []byte(`{"prebid":{"channel": {"name":"test1"},"macros":{"CUSTOMMACR1":"value1","CUSTOMMACR2":"value2","CUSTOMMACR3":"value3"}}}`), 121 }, 122 } 123 124 var bid *openrtb2.Bid = &openrtb2.Bid{ID: "bidId123", CID: "campaign_1", CrID: "creative_1"} 125 126 func BenchmarkStringIndexBasedReplacer(b *testing.B) { 127 replacer := NewStringIndexBasedReplacer() 128 builder := &strings.Builder{} 129 for n := 0; n < b.N; n++ { 130 for _, url := range benchmarkURL { 131 macroProvider := NewProvider(req) 132 macroProvider.PopulateBidMacros(&entities.PbsOrtbBid{Bid: bid}, "test") 133 macroProvider.PopulateEventMacros("123", "vast", "firstQuartile") 134 replacer.Replace(builder, url, macroProvider) 135 } 136 } 137 }