Skip to content

Commit

Permalink
Wrapper Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode committed Feb 7, 2024
1 parent 5d17394 commit 27742dd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 28 deletions.
1 change: 1 addition & 0 deletions endpoints/cookie_sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ func TestExtractGDPRSignal(t *testing.T) {
}
}

// todo
func TestExtractPrivacyPolicies(t *testing.T) {
type testInput struct {
request cookieSyncRequest
Expand Down
7 changes: 4 additions & 3 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2045,9 +2045,10 @@ func setRegsImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) err
return fmt.Errorf("request.regs.ext is invalid: %v", err)
}

if len(regExt.GetGPC()) == 0 {
secGPC := httpReq.Header.Get(httputil.HeaderSecGPC)
regExt.SetGPC(secGPC)
if regExt.GetGPC() == nil {
if secGPC := httpReq.Header.Get(httputil.HeaderSecGPC); len(secGPC) > 0 {
regExt.SetGPC(&secGPC)
}
}
return nil
}
Expand Down
17 changes: 12 additions & 5 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -682,40 +682,47 @@ func TestSetRegsImplicitly(t *testing.T) {
expectedError string
}{
{
name: "nil",
name: "regs-nil",
gpcHeader: "",
regs: nil,
expectedRegs: nil,
expectedError: "",
},
{
name: "nil-gpc",
name: "regs-nil-fromheader",
gpcHeader: "1",
regs: nil,
expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)},
expectedError: "",
},
{
name: "empty",
name: "regs-empty",
gpcHeader: "",
regs: &openrtb2.Regs{},
expectedRegs: &openrtb2.Regs{},
expectedError: "",
},
{
name: "empty-gpc",
name: "regs-empty-fromheader",
gpcHeader: "1",
regs: &openrtb2.Regs{},
expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)},
expectedError: "",
},
{
name: "existing",
gpcHeader: "",
gpcHeader: "1",
regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"0"}`)},
expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"0"}`)},
expectedError: "",
},
{
name: "existing-empty",
gpcHeader: "1",
regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":""}`)},
expectedRegs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":""}`)},
expectedError: "",
},
{
name: "empty-gpc",
gpcHeader: "1",
Expand Down
14 changes: 7 additions & 7 deletions exchange/bidder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,6 @@ import (
"sync"
"time"

"github.com/golang/glog"
"github.com/prebid/openrtb/v20/adcom1"
nativeRequests "github.com/prebid/openrtb/v20/native1/request"
nativeResponse "github.com/prebid/openrtb/v20/native1/response"
"github.com/prebid/openrtb/v20/openrtb2"
"golang.org/x/net/context/ctxhttp"

"github.com/prebid/prebid-server/v2/adapters"
"github.com/prebid/prebid-server/v2/bidadjustment"
"github.com/prebid/prebid-server/v2/config"
Expand All @@ -37,6 +30,13 @@ import (
"github.com/prebid/prebid-server/v2/util/httputil"
"github.com/prebid/prebid-server/v2/util/jsonutil"
"github.com/prebid/prebid-server/v2/version"

"github.com/golang/glog"
"github.com/prebid/openrtb/v20/adcom1"
nativeRequests "github.com/prebid/openrtb/v20/native1/request"
nativeResponse "github.com/prebid/openrtb/v20/native1/response"
"github.com/prebid/openrtb/v20/openrtb2"
"golang.org/x/net/context/ctxhttp"
)

// AdaptedBidder defines the contract needed to participate in an Auction within an Exchange.
Expand Down
10 changes: 5 additions & 5 deletions openrtb_ext/request_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1168,7 +1168,7 @@ type RegExt struct {
extDirty bool
gdpr *int8
gdprDirty bool
gpc string
gpc *string
gpcDirty bool
usPrivacy string
usPrivacyDirty bool
Expand Down Expand Up @@ -1228,8 +1228,8 @@ func (re *RegExt) marshal() (json.RawMessage, error) {
}

if re.gpcDirty {
if len(re.gpc) > 0 {
rawjson, err := json.Marshal(re.gpc)
if re.gpc != nil {
rawjson, err := json.Marshal(*re.gpc)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -1287,12 +1287,12 @@ func (re *RegExt) SetGDPR(gdpr *int8) {
re.gdprDirty = true
}

func (re *RegExt) GetGPC() string {
func (re *RegExt) GetGPC() *string {
gpc := re.gpc
return gpc
}

func (re *RegExt) SetGPC(gpc string) {
func (re *RegExt) SetGPC(gpc *string) {
re.gpc = gpc
re.gpcDirty = true
}
Expand Down
10 changes: 5 additions & 5 deletions openrtb_ext/request_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1366,7 +1366,7 @@ func TestCloneRegExt(t *testing.T) {
ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)},
extDirty: true,
gdpr: ptrutil.ToPtr[int8](1),
gpc: "anyGPC",
gpc: ptrutil.ToPtr("anyGPC"),
gpcDirty: true,
usPrivacy: "priv",
usPrivacyDirty: true,
Expand All @@ -1375,7 +1375,7 @@ func TestCloneRegExt(t *testing.T) {
ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)},
extDirty: true,
gdpr: ptrutil.ToPtr[int8](1),
gpc: "anyGPC",
gpc: ptrutil.ToPtr("anyGPC"),
gpcDirty: true,
usPrivacy: "priv",
usPrivacyDirty: true,
Expand All @@ -1388,7 +1388,7 @@ func TestCloneRegExt(t *testing.T) {
ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)},
extDirty: true,
gdpr: ptrutil.ToPtr[int8](1),
gpc: "anyGPC",
gpc: ptrutil.ToPtr("anyGPC"),
gpcDirty: true,
usPrivacy: "priv",
usPrivacyDirty: true,
Expand All @@ -1397,7 +1397,7 @@ func TestCloneRegExt(t *testing.T) {
ext: map[string]json.RawMessage{"A": json.RawMessage(`X`), "B": json.RawMessage(`Y`)},
extDirty: true,
gdpr: ptrutil.ToPtr[int8](1),
gpc: "anyGPC",
gpc: ptrutil.ToPtr("anyGPC"),
gpcDirty: true,
usPrivacy: "priv",
usPrivacyDirty: true,
Expand All @@ -1408,7 +1408,7 @@ func TestCloneRegExt(t *testing.T) {
regExt.extDirty = false
regExt.gdpr = nil
regExt.gdprDirty = true
regExt.gpc = "mutated"
regExt.gpc = ptrutil.ToPtr("mutated")
regExt.gpcDirty = false
regExt.usPrivacy = "Other"
},
Expand Down
7 changes: 5 additions & 2 deletions privacy/rule_condition.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ func getGPC(request ActivityRequest) string {
}

if request.IsBidRequest() && request.bidRequest.Regs != nil {
regExt, _ := request.bidRequest.GetRegExt()
return regExt.GetGPC()
if regExt, err := request.bidRequest.GetRegExt(); err == nil {
if v := regExt.GetGPC(); v != nil {
return *v
}
}
}

return ""
Expand Down
10 changes: 10 additions & 0 deletions privacy/rule_condition_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,16 @@ func TestGetGPC(t *testing.T) {
request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{"gpc":"1"}`)}}}},
expected: "1",
},
{
name: "request-regs-missing",
request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`{}`)}}}},
expected: "",
},
{
name: "request-regs-malformed",
request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: &openrtb2.Regs{Ext: json.RawMessage(`malformed`)}}}},
expected: "",
},
{
name: "request-regs-nil",
request: ActivityRequest{bidRequest: &openrtb_ext.RequestWrapper{BidRequest: &openrtb2.BidRequest{Regs: nil}}},
Expand Down
2 changes: 1 addition & 1 deletion util/httputil/headers.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package httputil

const (
HeaderSecGPC = "Sec-GPC"
HeaderSecGPC = "Sec-Gpc"
)

0 comments on commit 27742dd

Please sign in to comment.