github.com/goldeneggg/goa@v1.3.1/middleware/recover_test.go (about)

     1  package middleware_test
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"context"
     8  
     9  	"github.com/goadesign/goa"
    10  	"github.com/goadesign/goa/middleware"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Recover", func() {
    16  	var h goa.Handler
    17  	var err error
    18  
    19  	JustBeforeEach(func() {
    20  		rg := middleware.Recover()(h)
    21  		err = rg(nil, nil, nil)
    22  	})
    23  
    24  	Context("with a handler that panics with a string", func() {
    25  		BeforeEach(func() {
    26  			h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    27  				panic("boom")
    28  			}
    29  		})
    30  
    31  		It("creates an error from the panic message", func() {
    32  			Ω(err).Should(HaveOccurred())
    33  			Ω(err.Error()).Should(HavePrefix("panic: boom\n"))
    34  		})
    35  	})
    36  
    37  	Context("with a handler that panics with an error", func() {
    38  		BeforeEach(func() {
    39  			h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    40  				panic(fmt.Errorf("boom"))
    41  			}
    42  		})
    43  
    44  		It("creates an error from the panic error message", func() {
    45  			Ω(err).Should(HaveOccurred())
    46  			Ω(err.Error()).Should(HavePrefix("panic: boom\n"))
    47  		})
    48  	})
    49  
    50  	Context("with a handler that panics with something else", func() {
    51  		BeforeEach(func() {
    52  			h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    53  				panic(42)
    54  			}
    55  		})
    56  
    57  		It("creates a generic error message", func() {
    58  			Ω(err).Should(HaveOccurred())
    59  			Ω(err.Error()).Should(HavePrefix("unknown panic\n"))
    60  		})
    61  	})
    62  })