github.com/erda-project/erda-infra@v1.0.9/providers/httpserver/router_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package httpserver 16 17 import ( 18 "testing" 19 "time" 20 21 "github.com/go-playground/validator" 22 23 "github.com/erda-project/erda-infra/base/logs/logrusx" 24 "github.com/erda-project/erda-infra/providers/httpserver/server" 25 ) 26 27 func Test_removeParamName(t *testing.T) { 28 tests := []struct { 29 name string 30 path string 31 want string 32 }{ 33 { 34 path: "/abc", 35 want: "/abc", 36 }, 37 { 38 path: "/abc/:def", 39 want: "/abc/*", 40 }, 41 { 42 path: "/abc/:def/ghi", 43 want: "/abc/*/ghi", 44 }, 45 { 46 path: "/ab/:c/*/:de/f/**", 47 want: "/ab/*/*/*/f/**", 48 }, 49 { 50 path: "/ab/:c/*/:de/f/**/:G", 51 want: "/ab/*/*/*/f/**/*", 52 }, 53 } 54 for _, tt := range tests { 55 t.Run(tt.name, func(t *testing.T) { 56 if got := removeParamName(tt.path); got != tt.want { 57 t.Errorf("removeParamName() = %v, want %v", got, tt.want) 58 } 59 }) 60 } 61 } 62 63 func Test_routerManager_Started(t *testing.T) { 64 srv := server.New(false, &dataBinder{}, &structValidator{validator: validator.New()}) 65 p := &provider{server: srv, Cfg: &config{PrintRoutes: false}, Log: logrusx.New(), startedChan: make(chan struct{})} 66 rm := &routerManager{p: p} 67 go func() { 68 // blocking http server start 69 _ = p.Start() 70 }() 71 time.Sleep(time.Second) 72 select { 73 case <-rm.Started(): 74 // started 75 default: 76 t.Fatalf("should started") 77 } 78 }