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

GPC: Set extension based on header #3895

Merged
merged 10 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ const observeBrowsingTopics = "Observe-Browsing-Topics"
const observeBrowsingTopicsValue = "?1"

var (
dntKey string = http.CanonicalHeaderKey("DNT")
dntDisabled int8 = 0
dntEnabled int8 = 1
notAmp int8 = 0
dntKey string = http.CanonicalHeaderKey("DNT")
secGPCHdrKey string = http.CanonicalHeaderKey("Sec-GPC")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: secGPCKey suffices

dntDisabled int8 = 0
dntEnabled int8 = 1
notAmp int8 = 0
)

var accountIdSearchPath = [...]struct {
Expand Down Expand Up @@ -1522,7 +1523,7 @@ func setAuctionTypeImplicitly(r *openrtb_ext.RequestWrapper) {
}

func setGPCImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) error {
secGPC := httpReq.Header.Get("Sec-GPC")
secGPC := httpReq.Header.Get(secGPCHdrKey)
fmt.Printf("Sec-GPC Header: %s\n", secGPC)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove print statement


if secGPC != "1" {
Expand All @@ -1538,6 +1539,20 @@ func setGPCImplicitly(httpReq *http.Request, r *openrtb_ext.RequestWrapper) erro
regExt.SetGPC(&gpc)
fmt.Printf("GPC set to: %s\n", *regExt.GetGPC())

// Ręczna aktualizacja pola Regs.Ext
regExtBytes, err := json.Marshal(regExt)
if err != nil {
return err
}

// Debugging - sprawdzenie, co jest w regExtBytes
fmt.Printf("Zawartość regExtBytes przed przypisaniem: %s\n", string(regExtBytes))

r.BidRequest.Regs.Ext = regExtBytes // Zaktualizowanie pola Regs.Ext

// Debugging - sprawdzenie, co jest w r.BidRequest.Regs.Ext
fmt.Printf("Zaktualizowane Regs.Ext: %s\n", string(r.BidRequest.Regs.Ext))

return nil
}

Expand Down
41 changes: 25 additions & 16 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5620,44 +5620,53 @@ func TestValidateOrFillCookieDeprecation(t *testing.T) {

func TestSetGPCImplicitly(t *testing.T) {
testCases := []struct {
description string
headerValue string
expectedGPC string
expectError bool
description string
headerValue string
expectedGPC string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this because expectedRegs should contain the expected GPC value.

expectError bool
regs *openrtb2.Regs
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: move regs up a line so all of the expected fields are at the end of the struct definition. Please also do this in the actual test cases.

expectedRegs *openrtb2.Regs
}{
{
description: "Sec-GPC header set to '1', GPC should be set to '1'",
przemkaczmarek marked this conversation as resolved.
Show resolved Hide resolved
headerValue: "1",
expectedGPC: "1",
expectError: false,
regs: &openrtb2.Regs{
Ext: []byte(`{}`),
},
expectedRegs: &openrtb2.Regs{
Ext: []byte(`{"gpc":"1"}`),
},
},
Copy link
Collaborator

@bsardo bsardo Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add these two test cases to ensure that we don't create a regs or regs.ext object if we're not writing to the gpc field:

{
	description: "regs_nil_and_header_not_set",
	header:      "",
	regs:        nil,
	expectError: false,
	expectedRegs: nil,
},
{
	description: "regs_ext_is_nil_and_header_not_set",
	header:      "",
	regs:        &openrtb2.Regs{
		Ext: nil,
	},
	expectError: false,
	expectedRegs: &openrtb2.Regs{
		Ext: nil,
	},
},

}

for _, test := range testCases {
t.Run(test.description, func(t *testing.T) {
httpReq := &http.Request{
Header: http.Header{
"Sec-GPC": []string{test.headerValue},
http.CanonicalHeaderKey("Sec-GPC"): []string{test.headerValue},
},
}

r := &openrtb_ext.RequestWrapper{
BidRequest: &openrtb2.BidRequest{}, // what I should add here???
BidRequest: &openrtb2.BidRequest{
Regs: test.regs,
},
}

// Wywołanie funkcji testowanej
err := setGPCImplicitly(httpReq, r)
if (err != nil) != test.expectError {
t.Errorf("Unexpected error state: got %v, expected error: %v", err, test.expectError)
}

regExt, _ := r.GetRegExt()
gpcValue := regExt.GetGPC()

if gpcValue != nil && *gpcValue != test.expectedGPC {
t.Errorf("Expected GPC %v, but got %v", test.expectedGPC, *gpcValue)
} else if gpcValue == nil && test.expectedGPC != "" {
t.Errorf("Expected GPC to be set, but it was nil")
// Weryfikacja błędu
if test.expectError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}

// Weryfikacja JSON
assert.JSONEq(t, string(test.expectedRegs.Ext), string(r.BidRequest.Regs.Ext))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order to handle the two new test cases I requested you can modify this as such:

if test.expectedRegs == nil {
	assert.Nil(t, r.BidRequest.Regs)
} else if test.expectedRegs.Ext == nil {
	assert.Nil(t, r.BidRequest.Regs.Ext)
} else {
	assert.JSONEq(t, string(test.expectedRegs.Ext), string(r.BidRequest.Regs.Ext))
}

})
}
}
Expand Down
Loading