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##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-DOMAIN##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro7=##PBS-LIMITADTRACKING##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1##&macro10=##PBS-BIDDER##&macro11=##PBS-INTEGRATION##&macro12=##PBS-VASTCRTID##&macro15=##PBS-AUCTIONID##&macro16=##PBS-CHANNEL##&macro17=##PBS-EVENTTYPE##&macro18=##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&macro2=testbundle&macro3=testdomain&macro4=publishertestdomain&macro5=pageurltest&macro6=testpublisherID&macro7=10&macro8=yes&macro9=value1&macro10=test&macro11=&macro12=123&macro15=123&macro16=test1&macro17=vast&macro18=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##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-APPBUNDLE##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro6=##PBS-LIMITADTRACKING##&macro7=##PBS-GDPRCONSENT##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1CUST1##&macro10=##PBS-MACRO-CUSTOMMACR1CUST2##",
    91  	"http://google.com?macro1=##PBS-BIDID##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-APPBUNDLE##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro6=##PBS-LIMITADTRACKING##&macro7=##PBS-GDPRCONSENT##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1CUST1##&macro10=##PBS-MACRO-CUSTOMMACR1CUST2##",
    92  	"http://pubmatic.com?macro1=##PBS-BIDID##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-APPBUNDLE##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro6=##PBS-LIMITADTRACKING##&macro7=##PBS-GDPRCONSENT##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1CUST1##&macro10=##PBS-MACRO-CUSTOMMACR1CUST2##",
    93  	"http://testbidder.com?macro1=##PBS-BIDID##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-APPBUNDLE##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro6=##PBS-LIMITADTRACKING##&macro7=##PBS-GDPRCONSENT##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1CUST1##&macro10=##PBS-MACRO-CUSTOMMACR1CUST2##",
    94  	"http://dummybidder.com?macro1=##PBS-BIDID##&macro2=##PBS-APPBUNDLE##&macro3=##PBS-APPBUNDLE##&macro4=##PBS-PUBDOMAIN##&macro5=##PBS-PAGEURL##&macro6=##PBS-ACCOUNTID##&macro6=##PBS-LIMITADTRACKING##&macro7=##PBS-GDPRCONSENT##&macro8=##PBS-GDPRCONSENT##&macro9=##PBS-MACRO-CUSTOMMACR1CUST1##&macro10=##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  }