Skip to content

Commit

Permalink
PBS Version In Response Header (prebid#2129)
Browse files Browse the repository at this point in the history
  • Loading branch information
SyntaxNode authored and shunj-nb committed Nov 8, 2022
1 parent 9cd2a0d commit 6f13b9d
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 254 deletions.
2 changes: 2 additions & 0 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/prebid/prebid-server/stored_requests/backends/empty_fetcher"
"github.com/prebid/prebid-server/usersync"
"github.com/prebid/prebid-server/util/iputil"
"github.com/prebid/prebid-server/version"

"github.com/buger/jsonparser"
"github.com/golang/glog"
Expand Down Expand Up @@ -133,6 +134,7 @@ func (deps *endpointDeps) AmpAuction(w http.ResponseWriter, r *http.Request, _ h
// and "Access-Control-Allow-Credentials" are handled in CORS middleware
w.Header().Set("AMP-Access-Control-Allow-Source-Origin", origin)
w.Header().Set("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin")
w.Header().Set("X-Prebid", version.BuildXPrebidHeader(version.Ver))

req, errL := deps.parseAmpRequest(r)
ao.Errors = append(ao.Errors, errL...)
Expand Down
68 changes: 65 additions & 3 deletions endpoints/openrtb2/amp_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ func TestBuildAmpObject(t *testing.T) {

for _, test := range testCases {
// Set up test, declare a new mock logger every time
actualAmpObject, endpoint := AmpObjectTestSetup(t, test.inTagId, test.inStoredRequest, false)
actualAmpObject, endpoint := ampObjectTestSetup(t, test.inTagId, test.inStoredRequest, false)
// Run test
endpoint(recorder, request, nil)

Expand Down Expand Up @@ -1332,13 +1332,13 @@ func TestIdGeneration(t *testing.T) {

for _, test := range testCases {
// Set up and run test
actualAmpObject, endpoint := AmpObjectTestSetup(t, "test", test.givenInStoredRequest, test.givenGenerateRequestID)
actualAmpObject, endpoint := ampObjectTestSetup(t, "test", test.givenInStoredRequest, test.givenGenerateRequestID)
endpoint(recorder, request, nil)
assert.Equalf(t, test.expectedID, actualAmpObject.Request.ID, "Bid Request ID is incorrect: %s\n", test.description)
}
}

func AmpObjectTestSetup(t *testing.T, inTagId string, inStoredRequest json.RawMessage, generateRequestID bool) (*analytics.AmpObject, httprouter.Handle) {
func ampObjectTestSetup(t *testing.T, inTagId string, inStoredRequest json.RawMessage, generateRequestID bool) (*analytics.AmpObject, httprouter.Handle) {
actualAmpObject := analytics.AmpObject{}
logger := newMockLogger(&actualAmpObject)

Expand All @@ -1363,3 +1363,65 @@ func AmpObjectTestSetup(t *testing.T, inTagId string, inStoredRequest json.RawMe
)
return &actualAmpObject, endpoint
}

func TestAmpAuctionResponseHeaders(t *testing.T) {
testCases := []struct {
description string
requestURLArguments string
expectedStatus int
expectedHeaders func(http.Header)
}{
{
description: "Success Response",
requestURLArguments: "?tag_id=1&__amp_source_origin=foo",
expectedStatus: 200,
expectedHeaders: func(h http.Header) {
h.Set("AMP-Access-Control-Allow-Source-Origin", "foo")
h.Set("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin")
h.Set("X-Prebid", "pbs-go/unknown")
h.Set("Content-Type", "text/plain; charset=utf-8")
},
},
{
description: "Failure Response",
requestURLArguments: "?tag_id=invalid&__amp_source_origin=foo",
expectedStatus: 400,
expectedHeaders: func(h http.Header) {
h.Set("AMP-Access-Control-Allow-Source-Origin", "foo")
h.Set("Access-Control-Expose-Headers", "AMP-Access-Control-Allow-Source-Origin")
h.Set("X-Prebid", "pbs-go/unknown")
},
},
}

storedRequests := map[string]json.RawMessage{
"1": json.RawMessage(validRequest(t, "site.json")),
}
exchange := &nobidExchange{}
endpoint, _ := NewAmpEndpoint(
fakeUUIDGenerator{},
exchange,
newParamsValidator(t),
&mockAmpStoredReqFetcher{storedRequests},
empty_fetcher.EmptyFetcher{},
&config.Configuration{MaxRequestSize: maxSize},
&metricsConfig.NilMetricsEngine{},
analyticsConf.NewPBSAnalytics(&config.Analytics{}),
map[string]string{},
[]byte{},
openrtb_ext.BuildBidderMap(),
)

for _, test := range testCases {
httpReq := httptest.NewRequest("GET", fmt.Sprintf("/openrtb2/auction/amp"+test.requestURLArguments), nil)
recorder := httptest.NewRecorder()

endpoint(recorder, httpReq, nil)

expectedHeaders := http.Header{}
test.expectedHeaders(expectedHeaders)

assert.Equal(t, test.expectedStatus, recorder.Result().StatusCode, test.description+":statuscode")
assert.Equal(t, expectedHeaders, recorder.Result().Header, test.description+":statuscode")
}
}
4 changes: 3 additions & 1 deletion endpoints/openrtb2/auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"time"

"github.com/prebid/prebid-server/firstpartydata"
"github.com/prebid/prebid-server/version"

"github.com/buger/jsonparser"
jsonpatch "github.com/evanphx/json-patch"
Expand Down Expand Up @@ -148,6 +149,8 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
deps.analytics.LogAuctionObject(&ao)
}()

w.Header().Set("X-Prebid", version.BuildXPrebidHeader(version.Ver))

req, impExtInfoMap, errL := deps.parseRequest(r)
if errortypes.ContainsFatalError(errL) && writeError(errL, w, &labels) {
return
Expand Down Expand Up @@ -237,7 +240,6 @@ func (deps *endpointDeps) Auction(w http.ResponseWriter, r *http.Request, _ http
enc := json.NewEncoder(w)
enc.SetEscapeHTML(false)

// Fixes #328
w.Header().Set("Content-Type", "application/json")

// If an error happens when encoding the response, there isn't much we can do.
Expand Down
55 changes: 55 additions & 0 deletions endpoints/openrtb2/auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3590,6 +3590,61 @@ func TestValidateNativeAssetData(t *testing.T) {
}
}

func TestAuctionResponseHeaders(t *testing.T) {
testCases := []struct {
description string
requestBody string
expectedStatus int
expectedHeaders func(http.Header)
}{
{
description: "Success Response",
requestBody: validRequest(t, "site.json"),
expectedStatus: 200,
expectedHeaders: func(h http.Header) {
h.Set("X-Prebid", "pbs-go/unknown")
h.Set("Content-Type", "application/json")
},
},
{
description: "Failure Response",
requestBody: "{}",
expectedStatus: 400,
expectedHeaders: func(h http.Header) {
h.Set("X-Prebid", "pbs-go/unknown")
},
},
}

exchange := &nobidExchange{}
endpoint, _ := NewEndpoint(
fakeUUIDGenerator{},
exchange,
newParamsValidator(t),
empty_fetcher.EmptyFetcher{},
empty_fetcher.EmptyFetcher{},
&config.Configuration{MaxRequestSize: maxSize},
&metricsConfig.NilMetricsEngine{},
analyticsConf.NewPBSAnalytics(&config.Analytics{}),
map[string]string{},
[]byte{},
openrtb_ext.BuildBidderMap(),
empty_fetcher.EmptyFetcher{})

for _, test := range testCases {
httpReq := httptest.NewRequest("POST", "/openrtb2/auction", strings.NewReader(test.requestBody))
recorder := httptest.NewRecorder()

endpoint(recorder, httpReq, nil)

expectedHeaders := http.Header{}
test.expectedHeaders(expectedHeaders)

assert.Equal(t, test.expectedStatus, recorder.Result().StatusCode, test.description+":statuscode")
assert.Equal(t, expectedHeaders, recorder.Result().Header, test.description+":statuscode")
}
}

// warningsCheckExchange is a well-behaved exchange which stores all incoming warnings.
type warningsCheckExchange struct {
auctionRequest exchange.AuctionRequest
Expand Down
3 changes: 3 additions & 0 deletions endpoints/openrtb2/video_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/prebid/prebid-server/errortypes"
"github.com/prebid/prebid-server/util/iputil"
"github.com/prebid/prebid-server/util/uuidutil"
"github.com/prebid/prebid-server/version"

"github.com/golang/glog"
"github.com/julienschmidt/httprouter"
Expand Down Expand Up @@ -153,6 +154,8 @@ func (deps *endpointDeps) VideoAuctionEndpoint(w http.ResponseWriter, r *http.Re
deps.analytics.LogVideoObject(&vo)
}()

w.Header().Set("X-Prebid", version.BuildXPrebidHeader(version.Ver))

lr := &io.LimitedReader{
R: r.Body,
N: deps.cfg.MaxRequestSize,
Expand Down
Loading

0 comments on commit 6f13b9d

Please sign in to comment.