github.com/prebid/prebid-server/v2@v2.18.0/analytics/pubstack/config_test.go (about) 1 package pubstack 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "net/url" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestFetchConfig(t *testing.T) { 13 configResponse := `{ 14 "scopeId": "scopeId", 15 "endpoint": "https://pubstack.io", 16 "features": { 17 "auction": true, 18 "cookiesync": true, 19 "amp": true, 20 "setuid": false, 21 "video": false 22 } 23 }` 24 25 server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 26 defer req.Body.Close() 27 res.Write([]byte(configResponse)) 28 })) 29 30 defer server.Close() 31 32 endpoint, _ := url.Parse(server.URL) 33 cfg, _ := fetchConfig(server.Client(), endpoint) 34 35 assert.Equal(t, "scopeId", cfg.ScopeID) 36 assert.Equal(t, "https://pubstack.io", cfg.Endpoint) 37 assert.Equal(t, true, cfg.Features[auction]) 38 assert.Equal(t, true, cfg.Features[cookieSync]) 39 assert.Equal(t, true, cfg.Features[amp]) 40 assert.Equal(t, false, cfg.Features[setUID]) 41 assert.Equal(t, false, cfg.Features[video]) 42 } 43 44 func TestFetchConfig_Error(t *testing.T) { 45 configResponse := `{ 46 "error": "scopeId", 47 }` 48 49 server := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) { 50 defer req.Body.Close() 51 res.Write([]byte(configResponse)) 52 })) 53 54 defer server.Close() 55 56 endpoint, _ := url.Parse(server.URL) 57 cfg, err := fetchConfig(server.Client(), endpoint) 58 59 assert.Nil(t, cfg) 60 assert.Error(t, err) 61 } 62 63 func TestIsSameAs(t *testing.T) { 64 copyConfig := func(conf Configuration) *Configuration { 65 newConfig := conf 66 newConfig.Features = make(map[string]bool) 67 for k := range conf.Features { 68 newConfig.Features[k] = conf.Features[k] 69 } 70 return &newConfig 71 } 72 73 a := &Configuration{ 74 ScopeID: "scopeId", 75 Endpoint: "endpoint", 76 Features: map[string]bool{ 77 "auction": true, 78 "cookiesync": true, 79 "amp": true, 80 "setuid": false, 81 "video": false, 82 }, 83 } 84 85 assert.True(t, a.isSameAs(copyConfig(*a))) 86 87 b := copyConfig(*a) 88 b.ScopeID = "anotherId" 89 assert.False(t, a.isSameAs(b)) 90 91 b = copyConfig(*a) 92 b.Endpoint = "anotherEndpoint" 93 assert.False(t, a.isSameAs(b)) 94 95 b = copyConfig(*a) 96 b.Features["auction"] = true 97 assert.True(t, a.isSameAs(b)) 98 b.Features["auction"] = false 99 assert.False(t, a.isSameAs(b)) 100 } 101 102 func TestClone(t *testing.T) { 103 config := &Configuration{ 104 ScopeID: "scopeId", 105 Endpoint: "endpoint", 106 Features: map[string]bool{ 107 "auction": true, 108 "cookiesync": true, 109 "amp": true, 110 "setuid": false, 111 "video": false, 112 }, 113 } 114 115 clone := config.clone() 116 117 assert.Equal(t, config, clone) 118 assert.NotSame(t, config, clone) 119 } 120 121 func TestDisableAllFeatures(t *testing.T) { 122 config := &Configuration{ 123 ScopeID: "scopeId", 124 Endpoint: "endpoint", 125 Features: map[string]bool{ 126 "auction": true, 127 "cookiesync": true, 128 "amp": true, 129 "setuid": false, 130 "video": false, 131 }, 132 } 133 134 expected := &Configuration{ 135 ScopeID: "scopeId", 136 Endpoint: "endpoint", 137 Features: map[string]bool{ 138 "auction": false, 139 "cookiesync": false, 140 "amp": false, 141 "setuid": false, 142 "video": false, 143 }, 144 } 145 146 disabled := config.disableAllFeatures() 147 148 assert.Equal(t, expected, disabled) 149 assert.Same(t, config, disabled) 150 }