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

make bidderInfo endpoint case insensitive #3136

Merged
merged 1 commit into from
Sep 26, 2023
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
20 changes: 19 additions & 1 deletion endpoints/info/bidders_detail.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ func NewBiddersDetailEndpoint(bidders config.BidderInfos, aliases map[string]str
return func(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
bidder := ps.ByName("bidderName")

if response, ok := responses[bidder]; ok {
coreBidderName, found := getNormalisedBidderName(bidder, aliases)
if !found {
w.WriteHeader(http.StatusNotFound)
}
if response, ok := responses[coreBidderName]; ok {
w.Header().Set("Content-Type", "application/json")
if _, err := w.Write(response); err != nil {
glog.Errorf("error writing response to /info/bidders/%s: %v", bidder, err)
Expand All @@ -38,6 +42,20 @@ func NewBiddersDetailEndpoint(bidders config.BidderInfos, aliases map[string]str
}
}

func getNormalisedBidderName(bidderName string, aliases map[string]string) (string, bool) {
if strings.ToLower(bidderName) == "all" {
return "all", true
}
coreBidderName, ok := openrtb_ext.NormalizeBidderName(bidderName)
if !ok { //check default aliases if not found in coreBidders
if _, isDefaultAlias := aliases[bidderName]; isDefaultAlias {
return bidderName, true
}
return "", false
}
return coreBidderName.String(), true
}

func prepareBiddersDetailResponse(bidders config.BidderInfos, aliases map[string]string) (map[string][]byte, error) {
details, err := mapDetails(bidders, aliases)
if err != nil {
Expand Down
62 changes: 40 additions & 22 deletions endpoints/info/bidders_detail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package info

import (
"bytes"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -365,22 +366,22 @@ func TestMapMediaTypes(t *testing.T) {
func TestBiddersDetailHandler(t *testing.T) {
bidderAInfo := config.BidderInfo{Endpoint: "https://secureEndpoint.com", Disabled: false, Maintainer: &config.MaintainerInfo{Email: "bidderA"}}
bidderAResponse := []byte(`{"status":"ACTIVE","usesHttps":true,"maintainer":{"email":"bidderA"}}`)
aliasAResponse := []byte(`{"status":"ACTIVE","usesHttps":true,"maintainer":{"email":"bidderA"},"aliasOf":"a"}`)
aliasAResponse := []byte(`{"status":"ACTIVE","usesHttps":true,"maintainer":{"email":"bidderA"},"aliasOf":"appnexus"}`)

bidderBInfo := config.BidderInfo{Endpoint: "http://unsecureEndpoint.com", Disabled: false, Maintainer: &config.MaintainerInfo{Email: "bidderB"}}
bidderBResponse := []byte(`{"status":"ACTIVE","usesHttps":false,"maintainer":{"email":"bidderB"}}`)

allResponse := bytes.Buffer{}
allResponse.WriteString(`{"a":`)
allResponse.Write(bidderAResponse)
allResponse.WriteString(`,"aAlias":`)
allResponse.WriteString(`{"aAlias":`)
allResponse.Write(aliasAResponse)
allResponse.WriteString(`,"b":`)
allResponse.WriteString(`,"appnexus":`)
allResponse.Write(bidderAResponse)
allResponse.WriteString(`,"rubicon":`)
allResponse.Write(bidderBResponse)
allResponse.WriteString(`}`)

bidders := config.BidderInfos{"a": bidderAInfo, "b": bidderBInfo}
aliases := map[string]string{"aAlias": "a"}
bidders := config.BidderInfos{"appnexus": bidderAInfo, "rubicon": bidderBInfo}
aliases := map[string]string{"aAlias": "appnexus"}

handler := NewBiddersDetailEndpoint(bidders, aliases)

Expand All @@ -393,14 +394,21 @@ func TestBiddersDetailHandler(t *testing.T) {
}{
{
description: "Bidder A",
givenBidder: "a",
givenBidder: "appnexus",
expectedStatus: http.StatusOK,
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: bidderAResponse,
},
{
description: "Bidder B",
givenBidder: "b",
givenBidder: "rubicon",
expectedStatus: http.StatusOK,
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: bidderBResponse,
},
{
description: "Bidder B - case insensitive",
givenBidder: "RUBICON",
expectedStatus: http.StatusOK,
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: bidderBResponse,
Expand All @@ -412,6 +420,13 @@ func TestBiddersDetailHandler(t *testing.T) {
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: aliasAResponse,
},
{
description: "Bidder A Alias - case insensitive",
givenBidder: "aAlias",
expectedStatus: http.StatusOK,
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: aliasAResponse,
},
{
description: "All Bidders",
givenBidder: "all",
Expand All @@ -420,11 +435,11 @@ func TestBiddersDetailHandler(t *testing.T) {
expectedResponse: allResponse.Bytes(),
},
{
description: "All Bidders - Wrong Case",
givenBidder: "ALL",
expectedStatus: http.StatusNotFound,
expectedHeaders: http.Header{},
expectedResponse: []byte{},
description: "All Bidders - Case insensitive",
givenBidder: "All",
expectedStatus: http.StatusOK,
expectedHeaders: http.Header{"Content-Type": []string{"application/json"}},
expectedResponse: allResponse.Bytes(),
},
{
description: "Invalid Bidder",
Expand All @@ -436,16 +451,19 @@ func TestBiddersDetailHandler(t *testing.T) {
}

for _, test := range testCases {
responseRecorder := httptest.NewRecorder()
handler(responseRecorder, nil, httprouter.Params{{"bidderName", test.givenBidder}})
t.Run(test.description, func(t *testing.T) {
responseRecorder := httptest.NewRecorder()
handler(responseRecorder, nil, httprouter.Params{{"bidderName", test.givenBidder}})

result := responseRecorder.Result()
assert.Equal(t, result.StatusCode, test.expectedStatus, test.description+":statuscode")
result := responseRecorder.Result()
assert.Equal(t, result.StatusCode, test.expectedStatus, test.description+":statuscode")

resultBody, _ := io.ReadAll(result.Body)
assert.Equal(t, test.expectedResponse, resultBody, test.description+":body")
resultBody, _ := io.ReadAll(result.Body)
fmt.Println(string(test.expectedResponse))
assert.Equal(t, test.expectedResponse, resultBody, test.description+":body")

resultHeaders := result.Header
assert.Equal(t, test.expectedHeaders, resultHeaders, test.description+":headers")
resultHeaders := result.Header
assert.Equal(t, test.expectedHeaders, resultHeaders, test.description+":headers")
})
}
}
Loading