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