github.com/prebid/prebid-server/v2@v2.18.0/adapters/cpmstar/params_test.go (about)

     1  package cpmstar
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/prebid/prebid-server/v2/openrtb_ext"
     8  )
     9  
    10  // This file actually intends to test static/bidder-params/cpmstar.json
    11  // These also validate the format of the external API: request.imp[i].ext.prebid.bidder.cpmstar
    12  // TestValidParams makes sure that the Cpmstar schema accepts all imp.ext fields which we intend to support.
    13  
    14  func TestValidParams(t *testing.T) {
    15  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    16  	if err != nil {
    17  		t.Fatalf("Failed to fetch the json-schemas. %v", err)
    18  	}
    19  
    20  	for _, validParam := range validParams {
    21  		if err := validator.Validate(openrtb_ext.BidderCpmstar, json.RawMessage(validParam)); err != nil {
    22  			t.Errorf("Schema rejected Cpmstar params: %s", validParam)
    23  		}
    24  	}
    25  }
    26  
    27  // TestInvalidParams makes sure that the Cpmstar schema rejects all the imp.ext fields we don't support.
    28  func TestInvalidParams(t *testing.T) {
    29  	validator, err := openrtb_ext.NewBidderParamsValidator("../../static/bidder-params")
    30  	if err != nil {
    31  		t.Fatalf("Failed to fetch the json-schemas. %v", err)
    32  	}
    33  
    34  	for _, invalidParam := range invalidParams {
    35  		if err := validator.Validate(openrtb_ext.BidderCpmstar, json.RawMessage(invalidParam)); err == nil {
    36  			t.Errorf("Schema allowed unexpected params: %s", invalidParam)
    37  		}
    38  	}
    39  }
    40  
    41  var validParams = []string{
    42  	`{"placementId": 154}`,
    43  	`{"placementId": 154, "subpoolId": 123}`,
    44  }
    45  
    46  var invalidParams = []string{
    47  	`{}`,
    48  	`null`,
    49  	`true`,
    50  	`154`,
    51  	`{"placementId": "154"}`, // placementId should be numeric
    52  	`{"placementId": 154, "subpoolId": "123"}`, // placementId and subpoolId should both be numeric
    53  	`{"invalid_param": 123}`,
    54  }