Skip to content

Commit

Permalink
UOE-11628: Fix: Invalid Price value added in VAST version more than 3…
Browse files Browse the repository at this point in the history
….0 (#974)
  • Loading branch information
Pubmatic-Supriya-Patil authored Dec 3, 2024
1 parent c68f2cf commit 0c2db28
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 14 deletions.
20 changes: 13 additions & 7 deletions adapters/vastbidder/etree_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,7 @@ func (p *etreeXMLParser) Parse(vastXML []byte) (err error) {
}

func (p *etreeXMLParser) GetPricingDetails() (price float64, currency string) {
var node *etree.Element

if int(p.vastVersion) == 2 {
node = p.adElement.FindElement("./Extensions/Extension/Price")
} else {
node = p.adElement.SelectElement("Pricing")
}
node := p.getPricingNode()

if node == nil {
return 0.0, ""
Expand All @@ -81,6 +75,18 @@ func (p *etreeXMLParser) GetPricingDetails() (price float64, currency string) {
return priceValue, currency
}

func (p *etreeXMLParser) getPricingNode() *etree.Element {
node := p.adElement.SelectElement("Pricing")
if node == nil {
node = p.adElement.FindElement("./Extensions/Extension/Pricing")
}
if node == nil {
node = p.adElement.FindElement("./Extensions/Extension/Price")
}

return node
}

func (p *etreeXMLParser) GetAdvertiser() (advertisers []string) {
switch int(p.vastVersion) {
case vastVersion2x, vastVersion3x:
Expand Down
100 changes: 100 additions & 0 deletions adapters/vastbidder/etree_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,24 @@ func getPricingDetailsTestCases() []struct {
wantPrice: 12.05,
wantCurrency: "USD",
},
{
name: "vast_2.0_empty_pricing",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Pricing></Pricing></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 0,
wantCurrency: "",
},
{
name: "vast_2.0_cdata_pricing",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Pricing><![CDATA[ 12.05 ]]></Pricing></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 12.05,
wantCurrency: "USD",
},
{
name: "vast_2.0_pricing",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Pricing>12.05</Pricing></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 12.05,
wantCurrency: "USD",
},
{
name: "vast_2.0_empty_currency",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Price currency="">12.05</Price></Extension></Extensions></Wrapper></Ad></VAST>`,
Expand Down Expand Up @@ -354,6 +372,30 @@ func getPricingDetailsTestCases() []struct {
wantPrice: 5,
wantCurrency: "USD",
},
{
name: "vast_2.0_pricing_price_all_location",
vastXML: `<VAST><Ad><Wrapper><Pricing><![CDATA[ 15.05 ]]></Pricing><Extensions><Extension><Pricing><![CDATA[ 12.05 ]]></Pricing><Price><![CDATA[ 10.05 ]]></Price></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 15.05,
wantCurrency: "USD",
},
{
name: "vast_3.0_pricing_price_all_location",
vastXML: `<VAST version="3.0"><Ad><Wrapper><Pricing><![CDATA[ 15.05 ]]></Pricing><Extensions><Extension><Pricing><![CDATA[ 12.05 ]]></Pricing><Price><![CDATA[ 10.05 ]]></Price></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 15.05,
wantCurrency: "USD",
},
{
name: "vast_2.0_pricing_price_extension",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Pricing><![CDATA[ 12.05 ]]></Pricing><Price><![CDATA[ 10.05 ]]></Price></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 12.05,
wantCurrency: "USD",
},
{
name: "vast_3.0_pricing_price_extension",
vastXML: `<VAST version="3.0"><Ad><Wrapper><Extensions><Extension><Pricing><![CDATA[ 12.05 ]]></Pricing><Price><![CDATA[ 10.05 ]]></Price></Extension></Extensions></Wrapper></Ad></VAST>`,
wantPrice: 12.05,
wantCurrency: "USD",
},
// TODO: Add test cases.
}
}
Expand Down Expand Up @@ -422,3 +464,61 @@ func Test_etreeXMLParser_GetPricingDetails(t *testing.T) {
})
}
}

func getPricingNodeTestCases() []struct {
name string
vastXML string
wantNil bool
wantPrice string
} {
return []struct {
name string
vastXML string
wantNil bool
wantPrice string
}{
{
name: "no_pricing_node",
vastXML: `<VAST><Ad><Wrapper></Wrapper></Ad></VAST>`,
wantNil: true,
},
{
name: "pricing_node_present",
vastXML: `<VAST><Ad><Wrapper><Pricing>12.50</Pricing></Wrapper></Ad></VAST>`,
wantNil: false,
wantPrice: "12.50",
},
{
name: "pricing_node_in_extension_pricing",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Pricing>12.10</Pricing></Extension></Extensions></Wrapper></Ad></VAST>`,
wantNil: false,
wantPrice: "12.10",
},
{
name: "price_node_in_extension_price",
vastXML: `<VAST><Ad><Wrapper><Extensions><Extension><Price>12.05</Price></Extension></Extensions></Wrapper></Ad></VAST>`,
wantNil: false,
wantPrice: "12.05",
},
}
}

func Test_etreeXMLParser_getPricingNode(t *testing.T) {
for _, tt := range getPricingNodeTestCases() {
t.Run(tt.name, func(t *testing.T) {
parser := newETreeXMLParser()
err := parser.Parse([]byte(tt.vastXML))
if !assert.NoError(t, err) {
return
}

node := parser.getPricingNode()
if tt.wantNil {
assert.Nil(t, node)
} else {
assert.NotNil(t, node)
assert.Equal(t, tt.wantPrice, node.Text())
}
})
}
}
20 changes: 13 additions & 7 deletions adapters/vastbidder/fastxml_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,7 @@ func (p *fastXMLParser) Parse(vastXML []byte) (err error) {
}

func (p *fastXMLParser) GetPricingDetails() (price float64, currency string) {
var node *fastxml.Element

if int(p.vastVersion) == 2 {
node = p.reader.SelectElement(p.adElement, "Extensions", "Extension", "Price")
} else {
node = p.reader.SelectElement(p.adElement, "Pricing")
}
node := p.getPricingNode()

if node == nil {
return 0.0, ""
Expand All @@ -82,6 +76,18 @@ func (p *fastXMLParser) GetPricingDetails() (price float64, currency string) {
return priceValue, currency
}

func (p *fastXMLParser) getPricingNode() *fastxml.Element {
node := p.reader.SelectElement(p.adElement, "Pricing")
if node == nil {
node = p.reader.SelectElement(p.adElement, "Extensions", "Extension", "Pricing")
}
if node == nil {
node = p.reader.SelectElement(p.adElement, "Extensions", "Extension", "Price")
}

return node
}

func (p *fastXMLParser) GetAdvertiser() (advertisers []string) {
switch int(p.vastVersion) {
case vastVersion2x, vastVersion3x:
Expand Down
20 changes: 20 additions & 0 deletions adapters/vastbidder/fastxml_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package vastbidder

import (
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -71,3 +72,22 @@ func Test_fastXMLParser_GetPricingDetails(t *testing.T) {
})
}
}

func Test_fastXMLParser_getPricingNode(t *testing.T) {
for _, tt := range getPricingNodeTestCases() {
t.Run(tt.name, func(t *testing.T) {
parser := newFastXMLParser()
err := parser.Parse([]byte(tt.vastXML))
if !assert.NoError(t, err) {
return
}
node := parser.getPricingNode()
if tt.wantNil {
assert.Nil(t, node)
} else {
assert.NotNil(t, node)
assert.Equal(t, tt.wantPrice, strings.TrimSpace(parser.reader.RawText(node)))
}
})
}
}

0 comments on commit 0c2db28

Please sign in to comment.