From 48bd4c36893ce38635fa891b2bd4f80fd87fa278 Mon Sep 17 00:00:00 2001 From: Aleksandr Nogikh Date: Tue, 7 Feb 2023 18:51:32 +0100 Subject: dashboard: directly transform request context Instead of exposing a map with the list of variables to set, let users specify a callback to transform context.Context before making a request. --- dashboard/app/util_test.go | 35 ++++++++++++++++------------------- 1 file changed, 16 insertions(+), 19 deletions(-) (limited to 'dashboard') diff --git a/dashboard/app/util_test.go b/dashboard/app/util_test.go index d8d0a02d0..36069dbc0 100644 --- a/dashboard/app/util_test.go +++ b/dashboard/app/util_test.go @@ -33,15 +33,15 @@ import ( ) type Ctx struct { - t *testing.T - inst aetest.Instance - ctx context.Context - mockedTime time.Time - emailSink chan *aemail.Message - contextVars map[interface{}]interface{} - client *apiClient - client2 *apiClient - publicClient *apiClient + t *testing.T + inst aetest.Instance + ctx context.Context + mockedTime time.Time + emailSink chan *aemail.Message + transformContext func(context.Context) context.Context + client *apiClient + client2 *apiClient + publicClient *apiClient } var skipDevAppserverTests = func() bool { @@ -68,11 +68,11 @@ func NewCtx(t *testing.T) *Ctx { t.Fatal(err) } c := &Ctx{ - t: t, - inst: inst, - mockedTime: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), - contextVars: make(map[interface{}]interface{}), - emailSink: make(chan *aemail.Message, 100), + t: t, + inst: inst, + mockedTime: time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC), + emailSink: make(chan *aemail.Message, 100), + transformContext: func(c context.Context) context.Context { return c }, } c.client = c.makeClient(client1, password1, true) c.client2 = c.makeClient(client2, password2, true) @@ -248,6 +248,7 @@ func (c *Ctx) httpRequest(method, url, body string, access AccessLevel) (*httpte c.t.Fatal(err) } r = registerRequest(r, c) + r = r.WithContext(c.transformContext(r.Context())) if access == AccessAdmin || access == AccessUser { user := &user.User{ Email: "user@syzkaller.com", @@ -376,11 +377,7 @@ type apiClient struct { func (c *Ctx) makeClient(client, key string, failOnErrors bool) *apiClient { doer := func(r *http.Request) (*http.Response, error) { r = registerRequest(r, c) - newCtx := r.Context() - for key, val := range c.contextVars { - newCtx = context.WithValue(newCtx, key, val) - } - r = r.WithContext(newCtx) + r = r.WithContext(c.transformContext(r.Context())) w := httptest.NewRecorder() http.DefaultServeMux.ServeHTTP(w, r) res := &http.Response{ -- cgit mrf-deployment