From 990faea1aa6b785315dc12288ec5f736a560f280 Mon Sep 17 00:00:00 2001 From: Nilesh Chate Date: Mon, 26 Feb 2024 14:10:58 +0530 Subject: [PATCH] fix TestAmpBadRequests --- endpoints/openrtb2/amp_auction_test.go | 60 +++++++++++-------- .../amp/invalid-whole/tmax-negative.json | 50 ++++++++++++++++ 2 files changed, 85 insertions(+), 25 deletions(-) create mode 100644 endpoints/openrtb2/sample-requests/amp/invalid-whole/tmax-negative.json diff --git a/endpoints/openrtb2/amp_auction_test.go b/endpoints/openrtb2/amp_auction_test.go index 7d87b65301d..b30603c0f8c 100644 --- a/endpoints/openrtb2/amp_auction_test.go +++ b/endpoints/openrtb2/amp_auction_test.go @@ -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) } } diff --git a/endpoints/openrtb2/sample-requests/amp/invalid-whole/tmax-negative.json b/endpoints/openrtb2/sample-requests/amp/invalid-whole/tmax-negative.json new file mode 100644 index 00000000000..8fe54bc073a --- /dev/null +++ b/endpoints/openrtb2/sample-requests/amp/invalid-whole/tmax-negative.json @@ -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" + } \ No newline at end of file