github.com/avenga/couper@v1.12.2/config/runtime/server_internal_test.go (about) 1 package runtime 2 3 import ( 4 "context" 5 "reflect" 6 "testing" 7 8 logrustest "github.com/sirupsen/logrus/hooks/test" 9 10 "github.com/avenga/couper/cache" 11 "github.com/avenga/couper/config" 12 "github.com/avenga/couper/config/runtime/server" 13 "github.com/avenga/couper/eval" 14 ) 15 16 func TestServer_getEndpointsList(t *testing.T) { 17 srvConf := &config.Server{ 18 APIs: []*config.API{ 19 { 20 Endpoints: []*config.Endpoint{ 21 {Pattern: "/api/1"}, 22 {Pattern: "/api/2"}, 23 }, 24 }, 25 }, 26 Endpoints: []*config.Endpoint{ 27 {Pattern: "/free/1"}, 28 {Pattern: "/free/2"}, 29 }, 30 } 31 32 serverOptions, _ := server.NewServerOptions(nil, nil) 33 endpoints, _ := newEndpointMap(srvConf, serverOptions) 34 if l := len(endpoints); l != 4 { 35 t.Fatalf("Expected 4 endpointes, given %d", l) 36 } 37 38 checks := map[string]HandlerKind{ 39 "/api/1": api, 40 "/api/2": api, 41 "/free/1": endpoint, 42 "/free/2": endpoint, 43 } 44 45 for pattern, kind := range checks { 46 var exist bool 47 for endpoint, parent := range endpoints { 48 if endpoint.Pattern == pattern { 49 if kind == api && parent == nil { 50 t.Errorf("Expected an api endpoint for path pattern: %q", pattern) 51 } 52 exist = true 53 break 54 } 55 } 56 if !exist { 57 t.Errorf("Expected an endpoint for path pattern: %q", pattern) 58 } 59 } 60 } 61 62 func TestServer_validatePortHosts(t *testing.T) { 63 type args struct { 64 conf *config.Couper 65 configuredPort int 66 } 67 68 tests := []struct { 69 name string 70 args args 71 wantErr bool 72 }{ 73 { 74 "Same host/port in one server", 75 args{ 76 &config.Couper{ 77 Servers: []*config.Server{ 78 {Hosts: []string{"*", "*", "*:9090", "*:9090", "*:8080"}}, 79 }, 80 }, 8080, 81 }, 82 false, 83 }, 84 { 85 "Same host/port in two servers with *", 86 args{ 87 &config.Couper{ 88 Servers: []*config.Server{ 89 {Hosts: []string{"*"}}, 90 {Hosts: []string{"*"}}, 91 }, 92 }, 8080, 93 }, 94 true, 95 }, 96 { 97 "Same host/port in two servers with *:<port>", 98 args{ 99 &config.Couper{ 100 Servers: []*config.Server{ 101 {Hosts: []string{"*:8080"}}, 102 {Hosts: []string{"*:8080"}}, 103 }, 104 }, 8080, 105 }, 106 true, 107 }, 108 { 109 "Same host/port in two servers with example.com", 110 args{ 111 &config.Couper{ 112 Servers: []*config.Server{ 113 {Hosts: []string{"example.com", "couper.io"}}, 114 {Hosts: []string{"example.com", "couper.io"}}, 115 }, 116 }, 8080, 117 }, 118 true, 119 }, 120 { 121 "Same host/port in two servers with example.com:<port>", 122 args{ 123 &config.Couper{ 124 Servers: []*config.Server{ 125 {Hosts: []string{"example.com:9090"}}, 126 {Hosts: []string{"example.com:9090"}}, 127 }, 128 }, 8080, 129 }, 130 true, 131 }, 132 { 133 "Same port w/ different host in two servers", 134 args{ 135 &config.Couper{ 136 Servers: []*config.Server{ 137 {Hosts: []string{"*", "example.com:9090"}}, 138 {Hosts: []string{"couper.io:9090"}}, 139 }, 140 }, 8080, 141 }, 142 false, 143 }, 144 { 145 "Host is mandatory for multiple servers", 146 args{ 147 &config.Couper{ 148 Servers: []*config.Server{ 149 {Hosts: []string{"*"}}, 150 {}, 151 }, 152 }, 8080, 153 }, 154 true, 155 }, 156 { 157 "Host is optional for single server", 158 args{ 159 &config.Couper{ 160 Servers: []*config.Server{ 161 {}, 162 }, 163 }, 8080, 164 }, 165 false, 166 }, 167 { 168 "Invalid host format", 169 args{ 170 &config.Couper{ 171 Servers: []*config.Server{ 172 {Hosts: []string{"_"}}, 173 }, 174 }, 8080, 175 }, 176 true, 177 }, 178 } 179 for _, tt := range tests { 180 t.Run(tt.name, func(subT *testing.T) { 181 tt.args.conf.Context = eval.NewDefaultContext() 182 tt.args.conf.Settings = config.NewDefaultSettings() 183 184 logger, _ := logrustest.NewNullLogger() 185 log := logger.WithContext(context.Background()) 186 187 quitCh := make(chan struct{}) 188 defer close(quitCh) 189 190 memStore := cache.New(log, quitCh) 191 192 if _, err := NewServerConfiguration(tt.args.conf, log, memStore); (err != nil) != tt.wantErr { 193 subT.Errorf("validatePortHosts() error = %v, wantErr %v", err, tt.wantErr) 194 } 195 }) 196 } 197 } 198 199 func TestServer_GetCORS(t *testing.T) { 200 parentCORS := &config.CORS{MaxAge: "123"} 201 parent := &config.Server{ 202 CORS: parentCORS, 203 } 204 currCORS := &config.CORS{MaxAge: "321"} 205 curr := &config.Server{ 206 CORS: currCORS, 207 } 208 209 if got := whichCORS(parent, &config.Server{}); got != parentCORS { 210 t.Errorf("Unexpected CORS given: %#v", got) 211 } 212 213 if got := whichCORS(parent, curr); got != currCORS { 214 t.Errorf("Unexpected CORS given: %#v", got) 215 } 216 217 currCORS.Disable = true 218 219 if got := whichCORS(parent, curr); got != nil { 220 t.Errorf("Unexpected CORS given: %#v", got) 221 } 222 } 223 224 func TestServer_ParseBodyLimit(t *testing.T) { 225 i, err := parseBodyLimit("non-size") 226 if err == nil { 227 t.Error("Unexpected NIL-error given") 228 } 229 if i != -1 { 230 t.Errorf("Unexpected size given: %#v", i) 231 } 232 233 i, err = parseBodyLimit("") 234 if err != nil { 235 t.Error("Unexpected error given") 236 } 237 if i != 64000000 { 238 t.Errorf("Unexpected size given: %#v", i) 239 } 240 241 i, err = parseBodyLimit("1K") 242 if err != nil { 243 t.Error("Unexpected error given") 244 } 245 if i != 1000 { 246 t.Errorf("Unexpected size given: %#v", i) 247 } 248 } 249 250 func TestServer_NewAC(t *testing.T) { 251 srvConf := &config.Server{ 252 AccessControl: []string{"s1", "s2"}, 253 DisableAccessControl: []string{"s1"}, 254 } 255 apiConf := &config.API{ 256 AccessControl: []string{"a1", "a2"}, 257 DisableAccessControl: []string{"a1"}, 258 } 259 260 got := newAC(srvConf, nil) 261 exp := config.AccessControl{ 262 AccessControl: []string{"s1", "s2"}, 263 DisableAccessControl: []string{"s1"}, 264 } 265 if !reflect.DeepEqual(got, exp) { 266 t.Errorf("want\n%#v\ngot\n%#v", exp, got) 267 } 268 269 got = newAC(srvConf, apiConf) 270 exp = config.AccessControl{ 271 AccessControl: []string{"s1", "s2", "a1", "a2"}, 272 DisableAccessControl: []string{"s1", "a1"}, 273 } 274 if !reflect.DeepEqual(got, exp) { 275 t.Errorf("want\n%#v\ngot\n%#v", exp, got) 276 } 277 }