From e3e2851c24efd832df69cf4eae80b5eea1caa35a Mon Sep 17 00:00:00 2001 From: Marten Seemann Date: Sat, 24 Aug 2019 15:16:10 +0700 Subject: [PATCH] add an integration test for HTTP request cancelations --- integrationtests/self/http_test.go | 36 ++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/integrationtests/self/http_test.go b/integrationtests/self/http_test.go index 9403be94a..ff44c3437 100644 --- a/integrationtests/self/http_test.go +++ b/integrationtests/self/http_test.go @@ -3,6 +3,7 @@ package self_test import ( "bytes" "compress/gzip" + "context" "crypto/tls" "fmt" "io/ioutil" @@ -20,6 +21,11 @@ import ( "github.com/onsi/gomega/gbytes" ) +type streamCancelError interface { + Canceled() bool + ErrorCode() protocol.ApplicationErrorCode +} + var _ = Describe("HTTP tests", func() { var client *http.Client @@ -184,6 +190,36 @@ var _ = Describe("HTTP tests", func() { Expect(err).ToNot(HaveOccurred()) Expect(string(body)).To(Equal("Hello, World!\n")) }) + + It("cancels requests", func() { + handlerCalled := make(chan struct{}) + http.HandleFunc("/cancel", func(w http.ResponseWriter, r *http.Request) { + defer GinkgoRecover() + defer close(handlerCalled) + for { + if _, err := w.Write([]byte("foobar")); err != nil { + Expect(r.Context().Done()).To(BeClosed()) + serr, ok := err.(streamCancelError) + Expect(ok).To(BeTrue()) + Expect(serr.Canceled()).To(BeTrue()) + Expect(serr.ErrorCode()).To(BeEquivalentTo(5)) + return + } + } + }) + + req, err := http.NewRequest(http.MethodGet, "https://localhost:"+testserver.Port()+"/cancel", nil) + Expect(err).ToNot(HaveOccurred()) + ctx, cancel := context.WithCancel(context.Background()) + req = req.WithContext(ctx) + resp, err := client.Do(req) + Expect(err).ToNot(HaveOccurred()) + Expect(resp.StatusCode).To(Equal(200)) + cancel() + Eventually(handlerCalled).Should(BeClosed()) + _, err = resp.Body.Read([]byte{0}) + Expect(err).To(HaveOccurred()) + }) }) } })