Skip to content

Commit

Permalink
review comment 2
Browse files Browse the repository at this point in the history
  • Loading branch information
AvinashKapre committed Apr 26, 2024
1 parent f4f053d commit 8b12b65
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
7 changes: 5 additions & 2 deletions analytics/pubmatic/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pubmatic

import (
"encoding/json"
"fmt"
"errors"
"net/http"
"net/url"
"strconv"
Expand Down Expand Up @@ -85,18 +85,21 @@ func RestoreBidResponse(rctx *models.RequestCtx, ao analytics.AuctionObject) err
if ao.Response.NBR != nil {
return nil
}

signalData := map[string]string{}
if err := json.Unmarshal(ao.Response.SeatBid[0].Bid[0].Ext, &signalData); err != nil {
return err
}

if val, ok := signalData[models.SignalData]; !ok || val == "" {
return fmt.Errorf("signal data not found in the response")
return errors.New("signal data not found in the response")
}

orignalResponse := &openrtb2.BidResponse{}
if err := json.Unmarshal([]byte(signalData[models.SignalData]), orignalResponse); err != nil {
return err
}

*ao.Response = *orignalResponse
return nil
}
23 changes: 13 additions & 10 deletions analytics/pubmatic/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func TestRestoreBidResponse(t *testing.T) {
name string
args args
want *openrtb2.BidResponse
WantErr bool
wantErr string
}{
{
name: "Endpoint is not AppLovinMax",
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestRestoreBidResponse(t *testing.T) {
},
},
},
WantErr: true,
wantErr: "unexpected end of JSON input",
},
{
name: "signaldata not present in ext",
Expand All @@ -319,7 +319,7 @@ func TestRestoreBidResponse(t *testing.T) {
Bid: []openrtb2.Bid{
{
ID: "123",
Ext: json.RawMessage(`"signalData": "{\"matchedimpression\":{\"appnexus\":50,\"pubmatic\":50}}\r\n"`),
Ext: json.RawMessage(`{"signalData1": "{\"matchedimpression\":{\"appnexus\":50,\"pubmatic\":50}}\r\n"}`),
},
},
},
Expand All @@ -337,13 +337,13 @@ func TestRestoreBidResponse(t *testing.T) {
Bid: []openrtb2.Bid{
{
ID: "123",
Ext: json.RawMessage(`"signalData": "{\"matchedimpression\":{\"appnexus\":50,\"pubmatic\":50}}\r\n"`),
Ext: json.RawMessage(`{"signalData1": "{\"matchedimpression\":{\"appnexus\":50,\"pubmatic\":50}}\r\n"}`),
},
},
},
},
},
WantErr: true,
wantErr: "signal data not found in the response",
},
{
name: "failed to unmarshal signaldata",
Expand All @@ -360,7 +360,7 @@ func TestRestoreBidResponse(t *testing.T) {
{
ID: "bid-id-1",
ImpID: "imp_1",
Ext: json.RawMessage(`{"signaldata":{"id":123}"}`),
Ext: json.RawMessage(`{"signaldata": "{"id":123}"}"`),
},
},
},
Expand All @@ -382,12 +382,13 @@ func TestRestoreBidResponse(t *testing.T) {
{
ID: "bid-id-1",
ImpID: "imp_1",
Ext: json.RawMessage(`{"signaldata":{"id":123}"}`)},
Ext: json.RawMessage(`{"signaldata": "{"id":123}"}"`),
},
},
},
},
},
WantErr: true,
wantErr: `invalid character 'i' after object key:value pair`,
},
{
name: "valid AppLovinMax Response",
Expand Down Expand Up @@ -437,8 +438,10 @@ func TestRestoreBidResponse(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := RestoreBidResponse(tt.args.rctx, tt.args.ao)
assert.Equal(t, tt.WantErr, err != nil)
assert.Equal(t, tt.want, tt.args.ao.Response)
if err != nil {
assert.Equal(t, tt.wantErr, err.Error(), tt.name)
}
assert.Equal(t, tt.want, tt.args.ao.Response, tt.name)
})
}
}
2 changes: 1 addition & 1 deletion analytics/pubmatic/pubmatic.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (ow HTTPLogger) LogAuctionObject(ao *analytics.AuctionObject) {

err := RestoreBidResponse(rCtx, *ao)
if err != nil {
glog.V(3).Info("Failed to restore bid response for pub:[%d], profile:[%d], version:[%d], err:[%s].", rCtx.PubID, rCtx.ProfileID, rCtx.VersionID, err.Error())
glog.Error("Failed to restore bid response for pub:[%d], profile:[%d], version:[%d], err:[%s].", rCtx.PubID, rCtx.ProfileID, rCtx.VersionID, err.Error())
}

url, headers := GetLogAuctionObjectAsURL(*ao, rCtx, false, false)
Expand Down

0 comments on commit 8b12b65

Please sign in to comment.