diff --git a/helper.go b/helper.go index 397a95a..e77cfaf 100644 --- a/helper.go +++ b/helper.go @@ -457,12 +457,12 @@ func AppRunner(shoudClear bool, runApp func()) { ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) defer stop() - defer func() { - v := recover() - fmt.Println("Recovered:", v) - }() + // defer func() { + // v := recover() + // fmt.Println("Recovered:", v) + // }() - go runApp() + runApp() <-ctx.Done() //do stuff after ending @@ -472,8 +472,9 @@ func AppRunner(shoudClear bool, runApp func()) { } func handlerOutput(w http.ResponseWriter, r *http.Request, str ...string) { fmt.Fprintln(w, str[0]+": "+r.URL.Path+" "+str[1]) + } -func StartTestHTTPServer(port int, name string) { +func StartTestHTTPServer(port int, name string, headers ...string) { p := strconv.Itoa(port) go func() { @@ -482,13 +483,23 @@ func StartTestHTTPServer(port int, name string) { // fmt.Println("Test server is running at http://" + GetIP() + ":" + p) r := mux.NewRouter() r.Path("/").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if name == "albert" { + AlbertHandler(w, r) + return + } handlerOutput(w, r, name, "/") }) r.Path("/test1").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handlerOutput(w, r, name, "/test1") }) - r.Path("/test2").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - handlerOutput(w, r, name, "/test2") + r.PathPrefix("/api/users/").Path("/data").HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + for _, h := range headers { + h := strings.Split(h, ":") + + w.Header().Add(h[0], h[1]) + } + w.Header().Add("test", "1234") + handlerOutput(w, r, name, "/api/users/data OK!") }) err := http.ListenAndServe(":"+p, r) @@ -553,3 +564,40 @@ func IsKeyExistsInMap[V comparable](mp map[string]V, key string) (V, bool, error return val, ok, nil } } + +func HttpClientWithSkipVerify() *http.Client { + tr := &http.Transport{ + TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, + } + return &http.Client{Transport: tr} +} + +func AlbertHandler(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html") // Important: set the correct content type + html := ` + + + +Albert Einstein Photo + + + +Albert Einstein + + +` + w.Write([]byte(html)) // Write the HTML string to the response +}