github.com/Tyktechnologies/tyk@v2.9.5+incompatible/gateway/analytics_test.go (about) 1 package gateway 2 3 import ( 4 "testing" 5 6 "github.com/TykTechnologies/tyk/config" 7 ) 8 9 func TestGeoIPLookup(t *testing.T) { 10 testCases := [...]struct { 11 in string 12 wantErr bool 13 }{ 14 {"", false}, 15 {"foobar", true}, 16 {"1.2.3.4", false}, 17 } 18 for _, tc := range testCases { 19 _, err := geoIPLookup(tc.in) 20 switch { 21 case tc.wantErr && err == nil: 22 t.Errorf("geoIPLookup(%q) did not error", tc.in) 23 case !tc.wantErr && err != nil: 24 t.Errorf("geoIPLookup(%q) errored", tc.in) 25 } 26 } 27 } 28 29 func TestURLReplacer(t *testing.T) { 30 defer ResetTestConfig() 31 globalConf := config.Global() 32 globalConf.AnalyticsConfig.NormaliseUrls.Enabled = true 33 globalConf.AnalyticsConfig.NormaliseUrls.NormaliseUUIDs = true 34 globalConf.AnalyticsConfig.NormaliseUrls.NormaliseNumbers = true 35 globalConf.AnalyticsConfig.NormaliseUrls.Custom = []string{"ihatethisstring"} 36 config.SetGlobal(globalConf) 37 38 recordUUID1 := AnalyticsRecord{Path: "/15873a748894492162c402d67e92283b/search"} 39 recordUUID2 := AnalyticsRecord{Path: "/CA761232-ED42-11CE-BACD-00AA0057B223/search"} 40 recordUUID3 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"} 41 recordUUID4 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"} 42 recordID1 := AnalyticsRecord{Path: "/widgets/123456/getParams"} 43 recordCust := AnalyticsRecord{Path: "/widgets/123456/getParams/ihatethisstring"} 44 45 globalConf.AnalyticsConfig.NormaliseUrls.CompiledPatternSet = initNormalisationPatterns() 46 config.SetGlobal(globalConf) 47 48 recordUUID1.NormalisePath(&globalConf) 49 recordUUID2.NormalisePath(&globalConf) 50 recordUUID3.NormalisePath(&globalConf) 51 recordUUID4.NormalisePath(&globalConf) 52 recordID1.NormalisePath(&globalConf) 53 recordCust.NormalisePath(&globalConf) 54 55 if recordUUID1.Path != "/{uuid}/search" { 56 t.Error("Path not altered, is:") 57 t.Error(recordUUID1.Path) 58 t.Error(config.Global().AnalyticsConfig.NormaliseUrls) 59 } 60 61 if recordUUID2.Path != "/{uuid}/search" { 62 t.Error("Path not altered, is:") 63 t.Error(recordUUID2.Path) 64 } 65 66 if recordUUID3.Path != "/{uuid}/search" { 67 t.Error("Path not altered, is:") 68 t.Error(recordUUID3.Path) 69 } 70 71 if recordUUID4.Path != "/{uuid}/search" { 72 t.Error("Path not altered, is:") 73 t.Error(recordUUID4.Path) 74 } 75 76 if recordID1.Path != "/widgets/{id}/getParams" { 77 t.Error("Path not altered, is:") 78 t.Error(recordID1.Path) 79 } 80 81 if recordCust.Path != "/widgets/{id}/getParams/{var}" { 82 t.Error("Path not altered, is:") 83 t.Error(recordCust.Path) 84 } 85 } 86 87 func BenchmarkURLReplacer(b *testing.B) { 88 b.ReportAllocs() 89 90 defer ResetTestConfig() 91 92 globalConf := config.Global() 93 globalConf.AnalyticsConfig.NormaliseUrls.Enabled = true 94 globalConf.AnalyticsConfig.NormaliseUrls.NormaliseUUIDs = true 95 globalConf.AnalyticsConfig.NormaliseUrls.NormaliseNumbers = true 96 globalConf.AnalyticsConfig.NormaliseUrls.Custom = []string{"ihatethisstring"} 97 globalConf.AnalyticsConfig.NormaliseUrls.CompiledPatternSet = initNormalisationPatterns() 98 config.SetGlobal(globalConf) 99 100 for i := 0; i < b.N; i++ { 101 recordUUID1 := AnalyticsRecord{Path: "/15873a748894492162c402d67e92283b/search"} 102 recordUUID2 := AnalyticsRecord{Path: "/CA761232-ED42-11CE-BACD-00AA0057B223/search"} 103 recordUUID3 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"} 104 recordUUID4 := AnalyticsRecord{Path: "/ca761232-ed42-11ce-BAcd-00aa0057b223/search"} 105 recordID1 := AnalyticsRecord{Path: "/widgets/123456/getParams"} 106 recordCust := AnalyticsRecord{Path: "/widgets/123456/getParams/ihatethisstring"} 107 108 recordUUID1.NormalisePath(&globalConf) 109 recordUUID2.NormalisePath(&globalConf) 110 recordUUID3.NormalisePath(&globalConf) 111 recordUUID4.NormalisePath(&globalConf) 112 recordID1.NormalisePath(&globalConf) 113 recordCust.NormalisePath(&globalConf) 114 } 115 } 116 117 func TestTagHeaders(t *testing.T) { 118 req := TestReq(t, "GET", "/tagmeplease", nil) 119 req.Header.Set("Content-Type", "application/json") 120 req.Header.Set("X-Tag-Me", "1") 121 req.Header.Set("X-Tag-Me2", "2") 122 req.Header.Set("X-Tag-Me3", "3") 123 req.Header.Set("X-Ignore-Me", "4") 124 125 existingTags := []string{"first", "second"} 126 existingTags = tagHeaders(req, []string{ 127 "x-tag-me", 128 "x-tag-me2", 129 "x-tag-me3"}, 130 existingTags) 131 132 if len(existingTags) == 2 { 133 t.Fatal("Existing tags have not been expanded") 134 } 135 136 if len(existingTags) != 5 { 137 t.Fatalf("Wrong number of tags, got %v, wanted %v", len(existingTags), 5) 138 } 139 140 check := map[string]bool{ 141 "x-tag-me-1": true, 142 "x-tag-me2-2": true, 143 "x-tag-me3-3": true, 144 } 145 146 for _, t := range existingTags { 147 _, ok := check[t] 148 if ok { 149 delete(check, t) 150 } 151 } 152 153 if len(check) != 0 { 154 t.Fatalf("Header values not proerly set, got: %v, remnainder: %v", existingTags, check) 155 } 156 157 } 158 159 func BenchmarkTagHeaders(b *testing.B) { 160 b.ReportAllocs() 161 162 req := TestReq(b, "GET", "/tagmeplease", nil) 163 req.Header.Set("Content-Type", "application/json") 164 req.Header.Set("X-Tag-Me", "1") 165 req.Header.Set("X-Tag-Me2", "2") 166 req.Header.Set("X-Tag-Me3", "3") 167 req.Header.Set("X-Ignore-Me", "4") 168 169 existingTags := []string{"first", "second"} 170 171 var newExistingTags []string 172 for i := 0; i < b.N; i++ { 173 newExistingTags = tagHeaders( 174 req, 175 []string{ 176 "x-tag-me", 177 "x-tag-me2", 178 "x-tag-me3", 179 }, 180 existingTags, 181 ) 182 if len(newExistingTags) == 2 { 183 b.Fatal("Existing tags have not been expanded") 184 } 185 } 186 }