github.com/prebid/prebid-server/v2@v2.18.0/openrtb_ext/imp_appnexus_test.go (about)

     1  package openrtb_ext
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/prebid/prebid-server/v2/util/jsonutil"
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestKeywordsUnmarshalJSON(t *testing.T) {
    11  	type keywords struct {
    12  		Keywords ExtImpAppnexusKeywords `json:"keywords"`
    13  	}
    14  
    15  	type testCase struct {
    16  		input    []byte
    17  		expected string
    18  		desc     string
    19  	}
    20  
    21  	validTestCases := []testCase{
    22  		{input: []byte(`{"keywords" : { "pets": ["dog"] }}`), expected: "pets=dog", desc: "dynamic json object"},
    23  		{input: []byte(`{"keywords" : { "foo":[] }}`), expected: "foo", desc: "dynamic json object with empty value array"},
    24  		{input: []byte(`{"keywords" : [{"key": "foo", "value": ["bar","baz"]},{"key": "valueless"}]}`), expected: "foo=bar,foo=baz,valueless", desc: "array of objects"},
    25  		{input: []byte(`{"keywords" : "foo=bar,foo=baz,valueless"}`), expected: "foo=bar,foo=baz,valueless", desc: "string keywords"},
    26  		{input: []byte(`{"keywords" : ""}`), expected: "", desc: "empty string"},
    27  		{input: []byte(`{"keywords" : {}}`), expected: "", desc: "empty keywords object"},
    28  		{input: []byte(`{"keywords" : [{}]}`), expected: "", desc: "empty keywords object array"},
    29  		{input: []byte(`{"keywords": []}`), expected: "", desc: "empty keywords array"},
    30  	}
    31  
    32  	for _, test := range validTestCases {
    33  		var keywords keywords
    34  		assert.NoError(t, jsonutil.UnmarshalValid(test.input, &keywords), test.desc)
    35  		assert.Equal(t, test.expected, keywords.Keywords.String())
    36  	}
    37  
    38  	invalidTestCases := []testCase{
    39  		{input: []byte(`{"keywords": [{]}`), desc: "invalid keywords array"},
    40  		{input: []byte(`{"keywords" : {"}}`), desc: "invalid keywords object"},
    41  	}
    42  
    43  	for _, test := range invalidTestCases {
    44  		var keywords keywords
    45  		assert.Error(t, jsonutil.UnmarshalValid(test.input, &keywords), test.desc)
    46  	}
    47  }