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

     1  package cwire
     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/cwire.json
    11  // These also validate the format of the external API: request.imp[i].ext.bidder
    12  // TestValidParams makes sure that the cwire 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.BidderCWire, json.RawMessage(validParam)); err != nil {
    22  			t.Errorf("Schema rejected cwire params: %s", validParam)
    23  		}
    24  	}
    25  }
    26  
    27  // TestInvalidParams makes sure that the cwire 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.BidderCWire, json.RawMessage(invalidParam)); err == nil {
    36  			t.Errorf("Schema allowed unexpected params: %s", invalidParam)
    37  		}
    38  	}
    39  }
    40  
    41  var validParams = []string{
    42  	`{"cwcreative":"3746","cwdebug":true,"cwfeatures":["feat1","feat2"]}`,
    43  	`{"cwcreative":"3746","cwdebug":true}`,
    44  	`{"cwcreative":"3746"}`,
    45  	`{"cwdebug":true,"cwfeatures":["feat1","feat2"]}`,
    46  	`{"cwdebug":true}`,
    47  	`{"cwfeatures":["feat1","feat2"]}`,
    48  	`{"cwfeatures":["feat1"]}`,
    49  	`{"cwfeatures":[]}`,
    50  	`{"pageId":321,"cwcreative":"3746"}`,
    51  	`{"pageId":321}`,
    52  	`{"placementId":123,"cwcreative":"3746"}`,
    53  	`{"placementId":123,"cwdebug":true,"cwfeatures":["feat1","feat2"]}`,
    54  	`{"placementId":123,"pageId":321,"cwcreative":"3746","cwdebug":true,"cwfeatures":["feat1","feat2"]}`,
    55  	`{"placementId":123,"pageId":321,"cwcreative":"3746"}`,
    56  	`{"placementId":123,"pageId":321}`,
    57  	`{"placementId":123}`,
    58  	`{}`,
    59  }
    60  
    61  var invalidParams = []string{
    62  	`4.2`,
    63  	`5`,
    64  	`[]`,
    65  	``,
    66  	`null`,
    67  	`true`,
    68  	`{"cwcreative":1234}`,
    69  	`{"placementId":"abc"}`,
    70  	`{"cwdebug":"TRUE"}`,
    71  	`{"cwdebug":"FALSE"}`,
    72  	`{"cwfeatures":[1,2,3]}`,
    73  }