From 4c3858ca08b7eef7c6378842761d354a0748db2d Mon Sep 17 00:00:00 2001 From: dbemiller <27972385+dbemiller@users.noreply.github.com> Date: Tue, 18 Sep 2018 11:18:26 -0400 Subject: [PATCH] Fixed the Domain and Page for AMP requests. (#685) --- endpoints/openrtb2/amp_auction.go | 9 +++++++++ endpoints/openrtb2/amp_auction_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/endpoints/openrtb2/amp_auction.go b/endpoints/openrtb2/amp_auction.go index 2aa1b1b4ea0..2dbc8ac0016 100644 --- a/endpoints/openrtb2/amp_auction.go +++ b/endpoints/openrtb2/amp_auction.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "net/http" + "net/url" "strconv" "strings" "time" @@ -315,6 +316,14 @@ func (deps *endpointDeps) overrideWithParams(httpRequest *http.Request, req *ope } else { req.Site.Page = canonicalURL } + // Fixes #683 + if parsedURL, err := url.Parse(canonicalURL); err == nil { + domain := parsedURL.Host + if colonIndex := strings.LastIndex(domain, ":"); colonIndex != -1 { + domain = domain[:colonIndex] + } + req.Site.Domain = domain + } } slot := httpRequest.FormValue("slot") diff --git a/endpoints/openrtb2/amp_auction_test.go b/endpoints/openrtb2/amp_auction_test.go index 59d62b73c6a..ee39f5a3809 100644 --- a/endpoints/openrtb2/amp_auction_test.go +++ b/endpoints/openrtb2/amp_auction_test.go @@ -6,12 +6,14 @@ import ( "fmt" "net/http" "net/http/httptest" + "net/url" "reflect" "strconv" "testing" "github.com/mxmCherry/openrtb" analyticsConf "github.com/prebid/prebid-server/analytics/config" + "github.com/stretchr/testify/assert" "github.com/prebid/prebid-server/config" "github.com/prebid/prebid-server/exchange" @@ -74,6 +76,29 @@ func TestGoodAmpRequests(t *testing.T) { } } +// Prevents #683 +func TestAMPPageInfo(t *testing.T) { + const page = "http://test.somepage.co.uk:1234?myquery=1&other=2" + stored := map[string]json.RawMessage{ + "1": json.RawMessage(validRequest(t, "site.json")), + } + theMetrics := pbsmetrics.NewMetrics(metrics.NewRegistry(), openrtb_ext.BidderList()) + exchange := &mockAmpExchange{} + endpoint, _ := NewAmpEndpoint(exchange, newParamsValidator(t), &mockAmpStoredReqFetcher{stored}, &config.Configuration{MaxRequestSize: maxSize}, theMetrics, analyticsConf.NewPBSAnalytics(&config.Analytics{})) + request := httptest.NewRequest("GET", fmt.Sprintf("/openrtb2/auction/amp?tag_id=1&curl=%s", url.QueryEscape(page)), nil) + recorder := httptest.NewRecorder() + endpoint(recorder, request, nil) + + if !assert.NotNil(t, exchange.lastRequest, "Endpoint responded with %d: %s", recorder.Code, recorder.Body.String()) { + return + } + if !assert.NotNil(t, exchange.lastRequest.Site) { + return + } + assert.Equal(t, page, exchange.lastRequest.Site.Page) + assert.Equal(t, "test.somepage.co.uk", exchange.lastRequest.Site.Domain) +} + // TestBadRequests makes sure we return 400's on bad requests. func TestAmpBadRequests(t *testing.T) { files := fetchFiles(t, "sample-requests/invalid-whole")