From 3793d4c0471da364d657f1773dade1f8d572e0ed Mon Sep 17 00:00:00 2001 From: ShriprasadM Date: Thu, 28 Jan 2021 23:52:22 +0530 Subject: [PATCH] requestheaders: new parameter inside debug.httpcalls. to log request header details (#1659) * Added support for logging requestheaders inside httpCalls.requestheaders * Reverterd test case change * Modified outgoing mock request for appnexus, to send some request header information. Modified sample mock response such that ext.debug.httpcalls.appnexus.requestheaders will return the information of passed request headers * Addressed code review comments given by SyntaxNode. Also Moved RequestHeaders next to RequestBidy in openrtb_ext.ExtHttpCall Co-authored-by: Shriprasad --- exchange/bidder.go | 14 ++++++++------ exchange/bidder_test.go | 11 +++++++++++ .../request-multi-bidders-debug-info.json | 4 ++++ openrtb_ext/response.go | 9 +++++---- 4 files changed, 28 insertions(+), 10 deletions(-) diff --git a/exchange/bidder.go b/exchange/bidder.go index e4867b0eeac..6e84c260f03 100644 --- a/exchange/bidder.go +++ b/exchange/bidder.go @@ -310,17 +310,19 @@ func getAssetByID(id int64, assets []nativeRequests.Asset) (nativeRequests.Asset func makeExt(httpInfo *httpCallInfo) *openrtb_ext.ExtHttpCall { if httpInfo.err == nil { return &openrtb_ext.ExtHttpCall{ - Uri: httpInfo.request.Uri, - RequestBody: string(httpInfo.request.Body), - ResponseBody: string(httpInfo.response.Body), - Status: httpInfo.response.StatusCode, + Uri: httpInfo.request.Uri, + RequestBody: string(httpInfo.request.Body), + ResponseBody: string(httpInfo.response.Body), + Status: httpInfo.response.StatusCode, + RequestHeaders: httpInfo.request.Headers, } } else if httpInfo.request == nil { return &openrtb_ext.ExtHttpCall{} } else { return &openrtb_ext.ExtHttpCall{ - Uri: httpInfo.request.Uri, - RequestBody: string(httpInfo.request.Body), + Uri: httpInfo.request.Uri, + RequestBody: string(httpInfo.request.Body), + RequestHeaders: httpInfo.request.Headers, } } } diff --git a/exchange/bidder_test.go b/exchange/bidder_test.go index 8266daaa172..93aaf241a01 100644 --- a/exchange/bidder_test.go +++ b/exchange/bidder_test.go @@ -886,6 +886,9 @@ func TestBadRequestLogging(t *testing.T) { if ext.Status != 0 { t.Errorf("The Status code should be 0. Got %d", ext.Status) } + if len(ext.RequestHeaders) > 0 { + t.Errorf("The request headers should be empty. Got %s", ext.RequestHeaders) + } } // TestBadResponseLogging makes sure that openrtb_ext works properly if we don't get a sensible HTTP response. @@ -894,6 +897,9 @@ func TestBadResponseLogging(t *testing.T) { request: &adapters.RequestData{ Uri: "test.com", Body: []byte("request body"), + Headers: http.Header{ + "header-1": []string{"value-1"}, + }, }, err: errors.New("Bad response"), } @@ -910,6 +916,7 @@ func TestBadResponseLogging(t *testing.T) { if ext.Status != 0 { t.Errorf("The Status code should be 0. Got %d", ext.Status) } + assert.Equal(t, info.request.Headers, http.Header(ext.RequestHeaders), "The request headers should be \"header-1:value-1\"") } // TestSuccessfulResponseLogging makes sure that openrtb_ext works properly if the HTTP request is successful. @@ -918,6 +925,9 @@ func TestSuccessfulResponseLogging(t *testing.T) { request: &adapters.RequestData{ Uri: "test.com", Body: []byte("request body"), + Headers: http.Header{ + "header-1": []string{"value-1", "value-2"}, + }, }, response: &adapters.ResponseData{ StatusCode: 200, @@ -937,6 +947,7 @@ func TestSuccessfulResponseLogging(t *testing.T) { if ext.Status != info.response.StatusCode { t.Errorf("The Status code should be 0. Got %d", ext.Status) } + assert.Equal(t, info.request.Headers, http.Header(ext.RequestHeaders), "The request headers should be \"%s\". Got %s", info.request.Headers, ext.RequestHeaders) } func TestMobileNativeTypes(t *testing.T) { diff --git a/exchange/exchangetest/request-multi-bidders-debug-info.json b/exchange/exchangetest/request-multi-bidders-debug-info.json index ec174f75b36..5061798bf38 100644 --- a/exchange/exchangetest/request-multi-bidders-debug-info.json +++ b/exchange/exchangetest/request-multi-bidders-debug-info.json @@ -65,6 +65,7 @@ { "uri": "appnexusTest.com", "requestbody": "appnexusTestRequestBody", + "requestheaders": { "header_1" : ["value_11", "value_12"], "header_2" : ["value_21"] }, "responsebody": "appnexusTestResponseBody", "status": 200 } @@ -94,6 +95,7 @@ { "uri": "audienceNetworkTest.com", "requestbody": "audienceNetworkTestRequestBody", + "requestheaders": null, "responsebody": "audienceNetworkTestResponseBody", "status": 200 } @@ -149,6 +151,7 @@ { "uri": "appnexusTest.com", "requestbody": "appnexusTestRequestBody", + "requestheaders": { "header_1" : ["value_11", "value_12"], "header_2" : ["value_21"] }, "responsebody": "appnexusTestResponseBody", "status": 200 } @@ -157,6 +160,7 @@ { "uri": "audienceNetworkTest.com", "requestbody": "audienceNetworkTestRequestBody", + "requestheaders": null, "responsebody": "audienceNetworkTestResponseBody", "status": 200 } diff --git a/openrtb_ext/response.go b/openrtb_ext/response.go index 589dad40113..02370d19376 100644 --- a/openrtb_ext/response.go +++ b/openrtb_ext/response.go @@ -55,10 +55,11 @@ type ExtBidderError struct { // ExtHttpCall defines the contract for a bidresponse.ext.debug.httpcalls.{bidder}[i] type ExtHttpCall struct { - Uri string `json:"uri"` - RequestBody string `json:"requestbody"` - ResponseBody string `json:"responsebody"` - Status int `json:"status"` + Uri string `json:"uri"` + RequestBody string `json:"requestbody"` + RequestHeaders map[string][]string `json:"requestheaders"` + ResponseBody string `json:"responsebody"` + Status int `json:"status"` } // CookieStatus describes the allowed values for bidresponse.ext.usersync.{bidder}.status