Skip to content

Commit

Permalink
Fixed the Domain and Page for AMP requests. (#685)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbemiller authored and hhhjort committed Sep 18, 2018
1 parent 05664b4 commit 4c3858c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
9 changes: 9 additions & 0 deletions endpoints/openrtb2/amp_auction.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions endpoints/openrtb2/amp_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 4c3858c

Please sign in to comment.