github.com/GuanceCloud/cliutils@v1.1.21/testutil/http_test.go (about) 1 // Unless explicitly stated otherwise all files in this repository are licensed 2 // under the MIT License. 3 // This product includes software developed at Guance Cloud (https://www.guance.com/). 4 // Copyright 2021-present Guance, Inc. 5 6 package testutil 7 8 import ( 9 "fmt" 10 "net/http" 11 "sync" 12 "testing" 13 "time" 14 15 "github.com/gin-gonic/gin" 16 ) 17 18 func TestHTTPServer(t *testing.T) { 19 opt := &HTTPServerOptions{ 20 Bind: ":12345", 21 Exit: make(chan interface{}), 22 Routes: map[string]func(*gin.Context){ 23 "/route1": func(*gin.Context) { fmt.Printf("on route1") }, 24 "/route2": func(*gin.Context) { fmt.Printf("on route2") }, 25 }, 26 } 27 28 wg := sync.WaitGroup{} 29 30 wg.Add(1) 31 32 go func() { 33 defer wg.Done() 34 NewHTTPServer(t, opt) 35 }() 36 37 time.Sleep(time.Second) 38 39 _, err := http.Get("http://:12345/route1") 40 if err != nil { 41 t.Error(err) 42 } 43 44 _, err = http.Post("http://:12345/route2", "", nil) 45 if err != nil { 46 t.Error(err) 47 } 48 49 close(opt.Exit) 50 wg.Wait() 51 }