g := gomega.NewWithT(t) server := ghttp.NewServer() defer server.Close() response, err := http.Get(server.URL()) g.Expect(err).NotTo(gomega.HaveOccurred()) g.Expect(response.StatusCode).To(gomega.Equal(http.StatusOK))
g := gomega.NewWithT(t) server := ghttp.NewServer() defer server.Close() go func() { time.Sleep(time.Second) server.Close() }() _, err := http.Get(server.URL()) g.Expect(err).To(gomega.HaveOccurred())This example demonstrates how to forcefully close the server while a request is in progress. A goroutine is started that waits for one second before calling Server.Close(), and an HTTP GET request is made to the server. Since the server is closed before the request can complete, an error is expected. Overall, the github.com/onsi/gomega/ghttp package is a useful library for writing HTTP server tests in Go, with Server.Close being a helpful method for controlling the server's lifecycle.