Add CloseClientConnections function to ghttp Signed-off-by: Michael Fraenkel <fraenkel@us.ibm.com>
diff --git a/ghttp/test_server.go b/ghttp/test_server.go index 2748fea..71cd341 100644 --- a/ghttp/test_server.go +++ b/ghttp/test_server.go
@@ -172,6 +172,9 @@ //Close() should be called at the end of each test. It spins down and cleans up the test server. func (s *Server) Close() { + s.writeLock.Lock() + defer s.writeLock.Unlock() + server := s.HTTPTestServer s.HTTPTestServer = nil server.Close() @@ -305,3 +308,10 @@ existingHandler := s.GetHandler(index) s.SetHandler(index, CombineHandlers(existingHandler, handler)) } + +func (s *Server) CloseClientConnections() { + s.writeLock.Lock() + defer s.writeLock.Unlock() + + s.HTTPTestServer.CloseClientConnections() +}
diff --git a/ghttp/test_server_test.go b/ghttp/test_server_test.go index d992fb4..68ff0d4 100644 --- a/ghttp/test_server_test.go +++ b/ghttp/test_server_test.go
@@ -5,6 +5,7 @@ "io/ioutil" "net/http" "regexp" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" . "github.com/onsi/gomega/ghttp" @@ -25,6 +26,32 @@ s.Close() }) + Describe("closing client connections", func() { + It("closes", func() { + s.AppendHandlers( + func(w http.ResponseWriter, req *http.Request) { + w.Write([]byte("hello")) + }, + func(w http.ResponseWriter, req *http.Request) { + s.CloseClientConnections() + }, + ) + + resp, err := http.Get(s.URL()) + Ω(err).ShouldNot(HaveOccurred()) + Ω(resp.StatusCode).Should(Equal(200)) + + body, err := ioutil.ReadAll(resp.Body) + resp.Body.Close() + Ω(err).ShouldNot(HaveOccurred()) + Ω(body).Should(Equal([]byte("hello"))) + + resp, err = http.Get(s.URL()) + Ω(err).Should(HaveOccurred()) + Ω(resp).Should(BeNil()) + }) + }) + Describe("allowing unhandled requests", func() { Context("when true", func() { BeforeEach(func() {