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

     1  package middleware_test
     2  
     3  import (
     4  	"net/http"
     5  	"regexp"
     6  	"strings"
     7  
     8  	"context"
     9  
    10  	"github.com/goadesign/goa"
    11  	"github.com/goadesign/goa/middleware"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("RequireHeader", func() {
    17  	var ctx context.Context
    18  	var req *http.Request
    19  	var rw http.ResponseWriter
    20  	var service *goa.Service
    21  	headerName := "Some-Header"
    22  
    23  	BeforeEach(func() {
    24  		var err error
    25  		service = newService(nil)
    26  		req, err = http.NewRequest("POST", "/foo/bar", strings.NewReader(`{"payload":42}`))
    27  		Ω(err).ShouldNot(HaveOccurred())
    28  		rw = new(testResponseWriter)
    29  		ctx = newContext(service, rw, req, nil)
    30  	})
    31  
    32  	It("matches a header value", func() {
    33  		req.Header.Set(headerName, "some value")
    34  		var newCtx context.Context
    35  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    36  			newCtx = ctx
    37  			return service.Send(ctx, http.StatusOK, "ok")
    38  		}
    39  		t := middleware.RequireHeader(
    40  			service,
    41  			regexp.MustCompile("^/foo"),
    42  			headerName,
    43  			regexp.MustCompile("^some value$"),
    44  			http.StatusUnauthorized)(h)
    45  		err := t(ctx, rw, req)
    46  		Ω(err).ShouldNot(HaveOccurred())
    47  		Ω(goa.ContextResponse(newCtx).Status).Should(Equal(http.StatusOK))
    48  	})
    49  
    50  	It("responds with failure on mismatch", func() {
    51  		req.Header.Set(headerName, "some other value")
    52  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    53  			panic("unreachable")
    54  		}
    55  		t := middleware.RequireHeader(
    56  			service,
    57  			regexp.MustCompile("^/foo"),
    58  			headerName,
    59  			regexp.MustCompile("^some value$"),
    60  			http.StatusUnauthorized)(h)
    61  		err := t(ctx, rw, req)
    62  		Ω(err).ShouldNot(HaveOccurred())
    63  		Ω(goa.ContextResponse(ctx).Status).Should(Equal(http.StatusUnauthorized))
    64  	})
    65  
    66  	It("responds with failure when header is missing", func() {
    67  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    68  			panic("unreachable")
    69  		}
    70  		t := middleware.RequireHeader(
    71  			service,
    72  			regexp.MustCompile("^/foo"),
    73  			headerName,
    74  			regexp.MustCompile("^some value$"),
    75  			http.StatusUnauthorized)(h)
    76  		err := t(ctx, rw, req)
    77  		Ω(err).ShouldNot(HaveOccurred())
    78  		Ω(goa.ContextResponse(ctx).Status).Should(Equal(http.StatusUnauthorized))
    79  	})
    80  
    81  	It("passes through for a non-matching path", func() {
    82  		var newCtx context.Context
    83  		req.Header.Set(headerName, "bogus")
    84  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
    85  			newCtx = ctx
    86  			return service.Send(ctx, http.StatusOK, "ok")
    87  		}
    88  		t := middleware.RequireHeader(
    89  			service,
    90  			regexp.MustCompile("^/baz"),
    91  			headerName,
    92  			regexp.MustCompile("^some value$"),
    93  			http.StatusUnauthorized)(h)
    94  		err := t(ctx, rw, req)
    95  		Ω(err).ShouldNot(HaveOccurred())
    96  		Ω(goa.ContextResponse(newCtx).Status).Should(Equal(http.StatusOK))
    97  	})
    98  
    99  	It("matches value for a nil path pattern", func() {
   100  		req.Header.Set(headerName, "bogus")
   101  		h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
   102  			panic("unreachable")
   103  		}
   104  		t := middleware.RequireHeader(
   105  			service,
   106  			nil,
   107  			headerName,
   108  			regexp.MustCompile("^some value$"),
   109  			http.StatusNotFound)(h)
   110  		err := t(ctx, rw, req)
   111  		Ω(err).ShouldNot(HaveOccurred())
   112  		Ω(goa.ContextResponse(ctx).Status).Should(Equal(http.StatusNotFound))
   113  	})
   114  })