github.com/zak-blake/goa@v1.4.1/middleware/timeout_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"net/http"
     5  	"strings"
     6  	"time"
     7  
     8  	"context"
     9  
    10  	"github.com/goadesign/goa/middleware"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Timeout", func() {
    16  	It("sets a deadline", func() {
    17  		service := newService(nil)
    18  
    19  		req, err := http.NewRequest("POST", "/goo", strings.NewReader(`{"payload":42}`))
    20  		Ω(err).ShouldNot(HaveOccurred())
    21  		rw := new(testResponseWriter)
    22  		ctx := newContext(service, rw, req, nil)
    23  		var newCtx context.Context
    24  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    25  			newCtx = ctx
    26  			return service.Send(ctx, 200, "ok")
    27  		}
    28  		t := middleware.Timeout(time.Duration(1))(h)
    29  		err = t(ctx, rw, req)
    30  		Ω(err).ShouldNot(HaveOccurred())
    31  		_, ok := newCtx.Deadline()
    32  		Ω(ok).Should(BeTrue())
    33  	})
    34  })