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

If Device.UA is not present in request body, init it with user-agent from header #1219

Merged
merged 3 commits into from
Mar 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

{
"accountid": "555888777",
"podconfig": {
"durationrangesec": [
30
],
"requireexactduration": true,
"pods": [
{
"podid": 1,
"adpoddurationsec": 180,
"configid": "fba10607-0c12-43d1-ad07-b8a513bc75d6"
},
{
"podid": 2,
"adpoddurationsec": 150,
"configid": "8b452b41-2681-4a20-9086-6f16ffad7773"
}
]
},
"site": {
"page": "prebid.com"
},
"user": {
"buyeruids": {
"appnexus": "unique_id_an",
"rubicon": "unique_id_rubi"
},
"gdpr": {
"consentrequired": false,
"consentstring": "something"
},
"yob": 1991,
"gender": "F",
"keywords": "Hotels, Travelling"
},
"device": {
"ua": "TestHeaderSample",
"ip": "123.145.167.10",
"devicetype": 1,
"dnt": 33,
"ifa": "AA000DFE74168477C70D291f574D344790E0BB11",
"lmt": 44,
"os": "mac os",
"w": 640,
"h": 480,
"didsha1": "didsha1",
"didmd5": "didmd5",
"dpidsha1": "dpidsha1",
"dpidmd5": "dpidmd5",
"macsha1": "macsha1",
"macmd5": "macmd5"
},
"includebrandcategory":{
"primaryadserver": 1,
"publisher": ""
},
"video": {
"w": 640,
"h": 480,
"mimes": [
"video/mp4"
],
"protocols": [
2,3,5,6
]
},
"content": {
"episode": 6,
"title": "episodeName",
"series": "TvName",
"season": "season3",
"len": 900,
"livestream": 0
},
"cacheconfig": {
"ttl": 42
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@

{
"accountid": "555888777",
"podconfig": {
"durationrangesec": [
30
],
"requireexactduration": true,
"pods": [
{
"podid": 1,
"adpoddurationsec": 180,
"configid": "fba10607-0c12-43d1-ad07-b8a513bc75d6"
},
{
"podid": 2,
"adpoddurationsec": 150,
"configid": "8b452b41-2681-4a20-9086-6f16ffad7773"
}
]
},
"site": {
"page": "prebid.com"
},
"user": {
"buyeruids": {
"appnexus": "unique_id_an",
"rubicon": "unique_id_rubi"
},
"gdpr": {
"consentrequired": false,
"consentstring": "something"
},
"yob": 1991,
"gender": "F",
"keywords": "Hotels, Travelling"
},
"includebrandcategory":{
"primaryadserver": 1,
"publisher": ""
},
"video": {
"w": 640,
"h": 480,
"mimes": [
"video/mp4"
],
"protocols": [
2,3,5,6
]
},
"content": {
"episode": 6,
"title": "episodeName",
"series": "TvName",
"season": "season3",
"len": 900,
"livestream": 0
},
"cacheconfig": {
"ttl": 42
}
}
9 changes: 7 additions & 2 deletions endpoints/openrtb2/video_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
}
}
//unmarshal and validate combined result
videoBidReq, errL, podErrors := deps.parseVideoRequest(resolvedRequest)
videoBidReq, errL, podErrors := deps.parseVideoRequest(resolvedRequest, r.Header)
if len(errL) > 0 {
handleError(&labels, w, errL, &vo)
return
Expand Down Expand Up @@ -556,14 +556,19 @@ func createBidExtension(videoRequest *openrtb_ext.BidRequestVideo) ([]byte, erro
return reqJSON, nil
}

func (deps *endpointDeps) parseVideoRequest(request []byte) (req *openrtb_ext.BidRequestVideo, errs []error, podErrors []PodError) {
func (deps *endpointDeps) parseVideoRequest(request []byte, headers http.Header) (req *openrtb_ext.BidRequestVideo, errs []error, podErrors []PodError) {
req = &openrtb_ext.BidRequestVideo{}

if err := json.Unmarshal(request, &req); err != nil {
errs = []error{err}
return
}

//if Device.UA is not present in request body, init it with user-agent from request header if it's present
if req.Device.UA == "" {
req.Device.UA = headers.Get("User-Agent")
}

errL, podErrors := deps.validateVideoRequest(req)
if len(errL) > 0 {
errs = append(errs, errL...)
Expand Down
75 changes: 75 additions & 0 deletions endpoints/openrtb2/video_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/http/httptest"
"strings"
"testing"
Expand Down Expand Up @@ -745,6 +746,80 @@ func TestHandleErrorMetrics(t *testing.T) {
assert.Equal(t, "request missing required field: PodConfig.Pods", mod.videoObjects[0].Errors[1].Error(), "Second error in AnalyticsObject should have message regarding Pods")
}

func TestParseVideoRequestWithUserAgentAndHeader(t *testing.T) {
ex := &mockExchangeVideo{}
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_with_device_user_agent.json")
if err != nil {
t.Fatalf("Failed to fetch a valid request: %v", err)
}

headers := http.Header{}
headers.Add("User-Agent", "TestHeader")

deps := mockDeps(t, ex)
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)

assert.Equal(t, "TestHeaderSample", req.Device.UA, "Header should be taken from original request")
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")

}

func TestParseVideoRequestWithUserAgentAndEmptyHeader(t *testing.T) {
ex := &mockExchangeVideo{}
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_with_device_user_agent.json")
if err != nil {
t.Fatalf("Failed to fetch a valid request: %v", err)
}

headers := http.Header{}

deps := mockDeps(t, ex)
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)

assert.Equal(t, "TestHeaderSample", req.Device.UA, "Header should be taken from original request")
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")

}

func TestParseVideoRequestWithoutUserAgentWithHeader(t *testing.T) {
ex := &mockExchangeVideo{}
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_without_device_user_agent.json")
if err != nil {
t.Fatalf("Failed to fetch a valid request: %v", err)
}

headers := http.Header{}
headers.Add("User-Agent", "TestHeader")

deps := mockDeps(t, ex)
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)

assert.Equal(t, "TestHeader", req.Device.UA, "Device.ua should be taken from request header")
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")

}

func TestParseVideoRequestWithoutUserAgentAndEmptyHeader(t *testing.T) {
ex := &mockExchangeVideo{}
reqData, err := ioutil.ReadFile("sample-requests/video/video_valid_sample_without_device_user_agent.json")
if err != nil {
t.Fatalf("Failed to fetch a valid request: %v", err)
}

headers := http.Header{}

deps := mockDeps(t, ex)
req, valErr, podErr := deps.parseVideoRequest(reqData, headers)

assert.Equal(t, "", req.Device.UA, "Device.ua should be empty")
assert.Equal(t, []error(nil), valErr, "No validation errors should be returned")
assert.Equal(t, make([]PodError, 0), podErr, "No pod errors should be returned")

}

func mockDepsWithMetrics(t *testing.T, ex *mockExchangeVideo) (*endpointDeps, *pbsmetrics.Metrics, *mockAnalyticsModule) {
theMetrics := pbsmetrics.NewMetrics(metrics.NewRegistry(), openrtb_ext.BidderList(), config.DisabledMetrics{})
mockModule := &mockAnalyticsModule{}
Expand Down