// extractRequestID obtains the request ID either from a goa client or middleware func extractRequestID(ctx context.Context) string { reqID := middleware.ContextRequestID(ctx) if reqID == "" { return client.ContextRequestID(ctx) } return reqID }
req.Header.Set(middleware.RequestIDHeader, reqID) rw = new(testResponseWriter) params = url.Values{"query": []string{"value"}} service.Encoder.Register(goa.NewJSONEncoder, "*/*") ctx = newContext(service, rw, req, params) }) It("sets the request ID in the context", func() { var newCtx context.Context h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { newCtx = ctx return service.Send(ctx, 200, "ok") } rg := middleware.RequestID()(h) Ω(rg(ctx, rw, req)).ShouldNot(HaveOccurred()) Ω(middleware.ContextRequestID(newCtx)).Should(Equal(reqID)) }) It("truncates request ID when it exceeds a default limit", func() { var newCtx context.Context h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { newCtx = ctx return service.Send(ctx, 200, "ok") } tooLong := makeRequestID(2 * middleware.DefaultRequestIDLengthLimit) expected := makeRequestID(middleware.DefaultRequestIDLengthLimit) req.Header.Set(middleware.RequestIDHeader, tooLong) rg := middleware.RequestID()(h) Ω(rg(ctx, rw, req)).ShouldNot(HaveOccurred()) Ω(middleware.ContextRequestID(newCtx)).Should(Equal(expected)) })
var ctx context.Context var rw http.ResponseWriter var req *http.Request var params url.Values var service *goa.Service BeforeEach(func() { service = newService(nil) var err error req, err = http.NewRequest("GET", "/goo", nil) Ω(err).ShouldNot(HaveOccurred()) req.Header.Set("X-Request-Id", reqID) rw = new(testResponseWriter) params = url.Values{"query": []string{"value"}} service.Encoder(goa.NewJSONEncoder, "*/*") ctx = newContext(service, rw, req, params) }) It("sets the request ID in the context", func() { var newCtx context.Context h := func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error { newCtx = ctx return service.Send(ctx, 200, "ok") } rg := middleware.RequestID()(h) Ω(rg(ctx, rw, req)).ShouldNot(HaveOccurred()) Ω(middleware.ContextRequestID(newCtx)).Should(Equal(reqID)) }) })