thomasfinstad / terraform-provider-vyos-rolling

Terraform provider for VyOS with a focus on automatic resource generation
6 stars 0 forks source link

change to using test server https://pkg.go.dev/net/http/httptest#Server #175

Closed github-actions[bot] closed 7 months ago

github-actions[bot] commented 7 months ago

otherwise it will close the server before response is sent

Run as a go func as being in this handler and shutting down the server is self-blocking

https://github.com/thomasfinstad/terraform-provider-vyos/blob/775e5d4f6f379eae2063f408c17784f225b78ca8/internal/terraform/tests/api/api-mock.go#L235


}

// Server starts and maintains the http server until all exchanges are matched
// TODO change to using test server https://pkg.go.dev/net/http/httptest#Server
func Server(srv *http.Server, el *ExchangeList) {
    mux := http.NewServeMux()
    mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {

        if !el.handle(w, r) {
            log.Printf("MOCK SRV:Did not match the next expected exchange pattern:\n")
            log.Printf("MOCK SRV:URI: %s\n", r.RequestURI)
            log.Printf("MOCK SRV:Key: %s\n", r.FormValue("key"))
            log.Printf("MOCK SRV:Ops: %s\n", r.FormValue("data"))

            w.WriteHeader(http.StatusNotFound)
            w.Write([]byte("NO MATCH"))
            if f, ok := w.(http.Flusher); ok {
                f.Flush()
            }
            log.Printf("MOCK SRV:Flushed\n")

            go srv.Shutdown(context.TODO())
            log.Printf("MOCK SRV:Srv Shutdown\n")
        }

        if len(el.Unmatched()) == 0 {
            // Only works if the response.reply() does a Flush() on the http.ResponseWriter,
            //   otherwise it will close the server before response is sent
            // Run as a go func as being in this handler and shutting down the server is self-blocking
            go srv.Shutdown(context.TODO())
            log.Printf("MOCK SRV:Srv Shutdown\n")
        }
    })