Skip to content

Commit

Permalink
fix TestAmpBadRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
pm-nilesh-chate committed Feb 26, 2024
1 parent 829c797 commit 990faea
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 25 deletions.
60 changes: 35 additions & 25 deletions endpoints/openrtb2/amp_auction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1028,40 +1028,50 @@ func TestAMPSiteExt(t *testing.T) {

// TestBadRequests makes sure we return 400's on bad requests.
func TestAmpBadRequests(t *testing.T) {
dir := "sample-requests/invalid-whole"
dir := "sample-requests/amp/invalid-whole"
files, err := os.ReadDir(dir)
assert.NoError(t, err, "Failed to read folder: %s", dir)

badRequests := make(map[string]json.RawMessage, len(files))
for index, file := range files {
badRequests[strconv.Itoa(100+index)] = readFile(t, "sample-requests/invalid-whole/"+file.Name())
}
filename := file.Name()
fileData := readFile(t, "sample-requests/amp/invalid-whole/"+filename)

endpoint, _ := NewAmpEndpoint(
fakeUUIDGenerator{},
&mockAmpExchange{},
newParamsValidator(t),
&mockAmpStoredReqFetcher{badRequests},
empty_fetcher.EmptyFetcher{},
&config.Configuration{MaxRequestSize: maxSize},
&metricsConfig.NilMetricsEngine{},
analyticsBuild.New(&config.Analytics{}),
map[string]string{},
[]byte{},
openrtb_ext.BuildBidderMap(),
empty_fetcher.EmptyFetcher{},
hooks.EmptyPlanBuilder{},
nil,
)
for requestID := range badRequests {
request := httptest.NewRequest("GET", fmt.Sprintf("/openrtb2/auction/amp?tag_id=%s", requestID), nil)
test, err := parseTestData(fileData, filename)
if !assert.NoError(t, err) {
return
}

requestID := strconv.Itoa(100 + index)

endpoint, _ := NewAmpEndpoint(
fakeUUIDGenerator{},
&mockAmpExchange{},
newParamsValidator(t),
&mockAmpStoredReqFetcher{data: map[string]json.RawMessage{
requestID: test.BidRequest,
}},
&mockAccountFetcher{
data: map[string]json.RawMessage{"test_pub": json.RawMessage("{}")},
},
&config.Configuration{MaxRequestSize: maxSize},
&metricsConfig.NilMetricsEngine{},
analyticsBuild.New(&config.Analytics{}),
map[string]string{},
[]byte{},
openrtb_ext.BuildBidderMap(),
empty_fetcher.EmptyFetcher{},
hooks.EmptyPlanBuilder{},
nil,
)

request := httptest.NewRequest("GET", fmt.Sprintf("/openrtb2/auction/amp?account=test_pub&tag_id=%s", requestID), nil)
recorder := httptest.NewRecorder()

endpoint(recorder, request, nil)

if recorder.Code != http.StatusBadRequest {
t.Errorf("Expected status %d. Got %d. Input was: %s", http.StatusBadRequest, recorder.Code, fmt.Sprintf("/openrtb2/auction/amp?config=%s", requestID))
}
response := recorder.Body.String()
assert.Equal(t, test.ExpectedReturnCode, recorder.Code, filename)
assert.Contains(t, response, test.ExpectedErrorMessage, "Actual: %s \nExpected: %s. Filename: %s \n", response, test.ExpectedErrorMessage, filename)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"description": "Bid request with negative tmax value. Expect error",
"mockBidRequest": {
"tmax": -2,
"id": "some-request-id",
"site": {
"page": "test.somepage.com",
"ext": {
"amp": 1
}
},
"imp": [
{
"id": "my-imp-id",
"banner": {
"format": [
{
"w": 300,
"h": 600
}
]
},
"pmp": {
"deals": [
{
"id": "some-deal-id"
}
]
},
"ext": {
"appnexus": {
"placementId": 12883451
}
}
}
],
"ext": {
"prebid": {
"targeting": {
"pricegranularity": "low"
},
"cache": {
"bids": {}
}
}
}
},
"expectedReturnCode": 400,
"expectedErrorMessage": "Invalid request: request.tmax must be nonnegative. Got -2\n"
}

0 comments on commit 990faea

Please sign in to comment.