Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smaato: Rework multi imp support and add adpod support #1902

Merged
merged 15 commits into from
Jul 14, 2021
Merged
41 changes: 18 additions & 23 deletions adapters/smaato/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smaato
import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/errortypes"
"net/url"
"strings"
)
Expand All @@ -22,32 +23,26 @@ type img struct {
Ctaurl string `json:"ctaurl"`
}

func extractAdmImage(adapterResponseAdm string) (string, error) {
var imgMarkup string
var err error

func extractAdmImage(adMarkup string) (string, error) {
var imageAd imageAd
err = json.Unmarshal([]byte(adapterResponseAdm), &imageAd)
var image = imageAd.Image

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
if err := json.Unmarshal([]byte(adMarkup), &imageAd); err != nil {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
}
}

for _, impression := range image.Impressiontrackers {

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
var clickEvent strings.Builder
for _, clicktracker := range imageAd.Image.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'.replace(/\\+/g, ' ')), " +
"{cache: 'no-cache'});")
}

imgMarkup = fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));"><img src="%s" width="%d" height="%d"/>%s</div>`,
&clickEvent, url.QueryEscape(image.Img.Ctaurl), image.
Img.URL, image.Img.W, image.Img.
H, &impressionTracker)
var impressionTracker strings.Builder
for _, impression := range imageAd.Image.Impressiontrackers {
impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
return imgMarkup, err

return fmt.Sprintf(`<div style="cursor:pointer" onclick="%s;window.open(decodeURIComponent('%s'.replace(/\+/g, ' ')));">`+
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
`<img src="%s" width="%d" height="%d"/>%s`+
`</div>`, &clickEvent, url.QueryEscape(imageAd.Image.Img.Ctaurl), imageAd.Image.Img.URL, imageAd.Image.Img.W, imageAd.Image.Img.H, &impressionTracker), nil
}
56 changes: 34 additions & 22 deletions adapters/smaato/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,52 @@ import (
"testing"
)

func TestRenderAdMarkup(t *testing.T) {
func TestExtractAdmImage(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
adMarkup string
}
expectedResult := `<div style="cursor:pointer"` +
` onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});;window.open(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fctaurl%2F1'.replace(/\+/g, ' ')));">` +
`<img src="//prebid-test.smaatolabs.net/img/320x50.jpg" width="350" height="50"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`

tests := []struct {
testName string
args args
result string
testName string
args args
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
expectedAdMarkup string
expectedError string
}{
{"imageTest", args{"Img",
"{\"image\":{\"img\":{\"url\":\"//prebid-test.smaatolabs.net/img/320x50.jpg\"," +
{"extract image",
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
args{"{\"image\":{\"img\":{\"url\":\"//prebid-test.smaatolabs.net/img/320x50.jpg\"," +
"\"w\":350,\"h\":50,\"ctaurl\":\"//prebid-test.smaatolabs.net/track/ctaurl/1\"}," +
"\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
`<div style="cursor:pointer"` +
` onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'.replace(/\+/g, ' ')),` +
` {cache: 'no-cache'});;window.open(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fctaurl%2F1'.replace(/\+/g, ' ')));">` +
`<img src="//prebid-test.smaatolabs.net/img/320x50.jpg" width="350" height="50"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`,
"",
},
{"invalid adMarkup",
args{"{"},
"",
"Invalid ad markup {.",
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
adMarkup, err := extractAdmImage(tt.args.adMarkup)
if tt.expectedError != "" {
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
if err == nil {
t.Errorf("extractAdmImage() expectedError %v", tt.expectedError)
} else if err.Error() != tt.expectedError {
t.Errorf("extractAdmImage() err = %v, expectedError %v", err, tt.expectedError)
}
} else if err != nil {
t.Errorf("extractAdmImage() unexpected err = %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)

if adMarkup != tt.expectedAdMarkup {
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("extractAdmImage() adMarkup = %v, expectedAdMarkup %v", adMarkup, tt.expectedAdMarkup)
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions adapters/smaato/params_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ func TestInvalidParams(t *testing.T) {

var validParams = []string{
`{"publisherId":"test-id-1234-smaato","adspaceId": "1123581321"}`,
`{"publisherId":"test-id-1234-smaato","adbreakId": "4123581321"}`,
`{"publisherId":"test-id-1234-smaato","adspaceId": "1123581321","adbreakId": "4123581321"}`,
}

var invalidParams = []string{
Expand Down
38 changes: 16 additions & 22 deletions adapters/smaato/richmedia.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package smaato
import (
"encoding/json"
"fmt"
"github.com/prebid/prebid-server/errortypes"
"net/url"
"strings"
)
Expand All @@ -22,31 +23,24 @@ type richmedia struct {
Clicktrackers []string `json:"clicktrackers"`
}

func extractAdmRichMedia(adapterResponseAdm string) (string, error) {
var richMediaMarkup string
var err error

func extractAdmRichMedia(adMarkup string) (string, error) {
var richMediaAd richMediaAd
err = json.Unmarshal([]byte(adapterResponseAdm), &richMediaAd)
var richMedia = richMediaAd.RichMedia

if err == nil {
var clickEvent strings.Builder
var impressionTracker strings.Builder

for _, clicktracker := range richMedia.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
"{cache: 'no-cache'});")
if err := json.Unmarshal([]byte(adMarkup), &richMediaAd); err != nil {
return "", &errortypes.BadServerResponse{
Message: fmt.Sprintf("Invalid ad markup %s.", adMarkup),
}
for _, impression := range richMedia.Impressiontrackers {
}

impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
var clickEvent strings.Builder
var impressionTracker strings.Builder

richMediaMarkup = fmt.Sprintf(`<div onclick="%s">%s%s</div>`,
&clickEvent,
richMedia.MediaData.Content,
&impressionTracker)
for _, clicktracker := range richMediaAd.RichMedia.Clicktrackers {
clickEvent.WriteString("fetch(decodeURIComponent('" + url.QueryEscape(clicktracker) + "'), " +
"{cache: 'no-cache'});")
}
for _, impression := range richMediaAd.RichMedia.Impressiontrackers {
impressionTracker.WriteString(fmt.Sprintf(`<img src="%s" alt="" width="0" height="0"/>`, impression))
}
return richMediaMarkup, err

return fmt.Sprintf(`<div onclick="%s">%s%s</div>`, &clickEvent, richMediaAd.RichMedia.MediaData.Content, &impressionTracker), nil
}
52 changes: 33 additions & 19 deletions adapters/smaato/richmedia_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,47 @@ import (

func TestExtractAdmRichMedia(t *testing.T) {
type args struct {
adType adMarkupType
adapterResponseAdm string
adMarkup string
}
expectedResult := `<div onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'),` +
` {cache: 'no-cache'});"><div>hello</div><img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`
tests := []struct {
testName string
args args
result string
testName string
args args
mansinahar marked this conversation as resolved.
Show resolved Hide resolved
expectedAdMarkup string
expectedError string
}{
{"richmediaTest", args{"Richmedia", "{\"richmedia\":{\"mediadata\":{\"content\":\"<div>hello</div>\"," +
"" + "\"w\":350," +
"\"h\":50},\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
expectedResult,
{"extract richmedia",
args{"{\"richmedia\":{\"mediadata\":{\"content\":\"<div>hello</div>\"," +
"" + "\"w\":350," +
"\"h\":50},\"impressiontrackers\":[\"//prebid-test.smaatolabs.net/track/imp/1\",\"//prebid-test.smaatolabs.net/track/imp/2\"]," +
"\"clicktrackers\":[\"//prebid-test.smaatolabs.net/track/click/1\",\"//prebid-test.smaatolabs.net/track/click/2\"]}}"},
`<div onclick="fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F1'),` +
` {cache: 'no-cache'});fetch(decodeURIComponent('%2F%2Fprebid-test.smaatolabs.net%2Ftrack%2Fclick%2F2'),` +
` {cache: 'no-cache'});"><div>hello</div><img src="//prebid-test.smaatolabs.net/track/imp/1" alt="" width="0" height="0"/>` +
`<img src="//prebid-test.smaatolabs.net/track/imp/2" alt="" width="0" height="0"/></div>`,
"",
},
{"invalid adMarkup",
args{"{"},
"",
"Invalid ad markup {.",
},
}

for _, tt := range tests {
t.Run(tt.testName, func(t *testing.T) {
got, err := renderAdMarkup(tt.args.adType, tt.args.adapterResponseAdm)
if err != nil {
t.Errorf("error rendering ad markup: %v", err)
adMarkup, err := extractAdmRichMedia(tt.args.adMarkup)
if tt.expectedError != "" {
if err == nil {
t.Errorf("extractAdmRichMedia() expectedError %v", tt.expectedError)
} else if err.Error() != tt.expectedError {
t.Errorf("extractAdmRichMedia() err = %v, expectedError %v", err, tt.expectedError)
}
} else if err != nil {
t.Errorf("extractAdmRichMedia() unexpected err = %v", err)
}
if got != tt.result {
t.Errorf("renderAdMarkup() got = %v, result %v", got, tt.result)

if adMarkup != tt.expectedAdMarkup {
t.Errorf("extractAdmRichMedia() adMarkup = %v, expectedAdMarkup %v", adMarkup, tt.expectedAdMarkup)
}
})
}
Expand Down
Loading