diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2023-02-07 18:51:32 +0100 |
|---|---|---|
| committer | Aleksandr Nogikh <wp32pw@gmail.com> | 2023-02-16 11:29:36 +0100 |
| commit | 48bd4c36893ce38635fa891b2bd4f80fd87fa278 (patch) | |
| tree | 83793ae523130b7babff4d64c93bdad2b1bf1de4 /dashboard | |
| parent | 3256fb23a09736243944db98d8608b490afb9d60 (diff) | |
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.
Diffstat (limited to 'dashboard')
| -rw-r--r-- | dashboard/app/util_test.go | 35 |
1 files changed, 16 insertions, 19 deletions
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{ |
