github.com/avenga/couper@v1.12.2/config/runtime/access_control_test.go (about) 1 package runtime_test 2 3 import ( 4 "context" 5 "fmt" 6 "sort" 7 "strings" 8 "testing" 9 10 "github.com/avenga/couper/cache" 11 "github.com/avenga/couper/config/configload" 12 "github.com/avenga/couper/config/runtime" 13 "github.com/avenga/couper/internal/test" 14 ) 15 16 func TestDuplicateEndpoint(t *testing.T) { 17 tests := []struct { 18 name string 19 hcl string 20 endpoints []string 21 }{ 22 { 23 "shared API base path: create catch-all", 24 `api { 25 access_control = ["a"] 26 } 27 api { 28 access_control = ["a"] 29 }`, 30 []string{"/**"}, 31 }, 32 { 33 "shared API base path: create catch-all", 34 `base_path = "/p" 35 api { 36 access_control = ["a"] 37 } 38 api { 39 access_control = ["a"] 40 }`, 41 []string{"/p/**"}, 42 }, 43 { 44 "shared API base path: create catch-all", 45 `api {} 46 api { 47 access_control = ["a"] 48 }`, 49 []string{"/**"}, 50 }, 51 { 52 "shared API base path w/o access control: no catch-all", 53 `api { 54 } 55 api { 56 }`, 57 []string{}, 58 }, 59 { 60 "shared API base path: create catch-all", 61 `access_control = ["a"] 62 api {} 63 api {} 64 api { 65 base_path = "/p" 66 endpoint "/**" { 67 response {} 68 } 69 }`, 70 []string{"/**", "/p/**"}, 71 }, 72 { 73 "unique base paths: create catch-all twice", 74 `access_control = ["a"] 75 api { 76 base_path = "/" 77 } 78 api { 79 base_path = "/p" 80 }`, 81 []string{"/**", "/p/**"}, 82 }, 83 { 84 "unique base paths: create catch-all twice", 85 `access_control = ["a"] 86 api { 87 base_path = "/p" 88 } 89 api { 90 base_path = "/" 91 }`, 92 []string{"/**", "/p/**"}, 93 }, 94 { 95 "user defined /** endpoint in 1st API: no extra catch-all", 96 `api { 97 endpoint "/**" { 98 access_control = ["a"] 99 response {} 100 } 101 } 102 api { 103 access_control = ["a"] 104 } 105 `, 106 []string{"/**"}, 107 }, 108 { 109 "user defined /** endpoint in 2nd API: no extra catch-all", 110 `access_control = ["a"] 111 api {} 112 api { 113 endpoint "/**" { 114 response {} 115 } 116 } 117 `, 118 []string{"/**"}, 119 }, 120 { 121 "files + api: catch-all", 122 `files { 123 base_path = "/public" 124 document_root = "." 125 } 126 api { 127 access_control = ["a"] 128 } 129 `, 130 []string{"/**"}, 131 }, 132 { 133 "files + api, same base path: no catch-all", 134 `files { 135 document_root = "." 136 } 137 api { 138 access_control = ["a"] 139 } 140 `, 141 []string{}, 142 }, 143 { 144 "files + api, same base path: no catch-all", 145 `files { 146 base_path = "/p" 147 document_root = "." 148 } 149 api { 150 base_path = "/p" 151 access_control = ["a"] 152 endpoint "/**" { 153 response {} 154 } 155 } 156 `, 157 []string{"/p/**"}, 158 }, 159 { 160 "spa + api: catch-all", 161 `spa { 162 base_path = "/public" 163 bootstrap_file = "access_control_test.go" 164 paths = [] 165 } 166 api { 167 access_control = ["a"] 168 }`, 169 []string{"/**"}, 170 }, 171 { 172 "spa + api, same base path: no catch-all", 173 `spa { 174 base_path = "/path" 175 bootstrap_file = "access_control_test.go" 176 paths = [] 177 } 178 api { 179 base_path = "/path" 180 access_control = ["a"] 181 } 182 `, 183 []string{}, 184 }, 185 { 186 "files + api, same base path: no catch-all", 187 `spa { 188 base_path = "/p" 189 bootstrap_file = "access_control_test.go" 190 paths = [] 191 } 192 api { 193 base_path = "/p" 194 access_control = ["a"] 195 endpoint "/**" { 196 response {} 197 } 198 } 199 `, 200 []string{"/p/**"}, 201 }, 202 } 203 204 template := ` 205 server { 206 %% 207 } 208 definitions { 209 jwt "a" { 210 signature_algorithm = "HS256" 211 key = "asdf" 212 } 213 } 214 ` 215 216 for _, tt := range tests { 217 t.Run(tt.name, func(subT *testing.T) { 218 conf, err := configload.LoadBytes([]byte(strings.Replace(template, "%%", tt.hcl, -1)), "couper.hcl") 219 if err != nil { 220 subT.Error(err) 221 return 222 } 223 log, _ := test.NewLogger() 224 logger := log.WithContext(context.TODO()) 225 tmpStoreCh := make(chan struct{}) 226 defer close(tmpStoreCh) 227 228 ctx, cancel := context.WithCancel(conf.Context) 229 conf.Context = ctx 230 defer cancel() 231 232 server, err := runtime.NewServerConfiguration(conf, logger, cache.New(logger, tmpStoreCh)) 233 234 if err != nil { 235 subT.Error("expected no error, got:", err) 236 return 237 } 238 239 endpointMap := server[8080]["*"].EndpointRoutes 240 var endpoints sort.StringSlice 241 for endpoint := range endpointMap { 242 endpoints = append(endpoints, endpoint) 243 } 244 endpoints.Sort() 245 if fmt.Sprint(tt.endpoints) != fmt.Sprint(endpoints) { 246 subT.Errorf("unexpected endpoints, want: %v, got: %v", tt.endpoints, endpoints) 247 return 248 } 249 }) 250 } 251 }