From b9fb2ea80e9852ae6465fcb3b822ca9ffa3306bd Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Fri, 11 Oct 2024 13:17:50 +0200 Subject: dashboard/api: add Client type --- dashboard/app/util_test.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'dashboard/app/util_test.go') diff --git a/dashboard/app/util_test.go b/dashboard/app/util_test.go index f30581095..f865ff612 100644 --- a/dashboard/app/util_test.go +++ b/dashboard/app/util_test.go @@ -26,6 +26,7 @@ import ( "time" "github.com/google/go-cmp/cmp" + "github.com/google/syzkaller/dashboard/api" "github.com/google/syzkaller/dashboard/dashapi" "github.com/google/syzkaller/pkg/email" "github.com/google/syzkaller/pkg/subsystem" @@ -496,18 +497,6 @@ 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) - r = r.WithContext(c.transformContext(r.Context())) - w := httptest.NewRecorder() - http.DefaultServeMux.ServeHTTP(w, r) - res := &http.Response{ - StatusCode: w.Code, - Status: http.StatusText(w.Code), - Body: io.NopCloser(w.Result().Body), - } - return res, nil - } logger := func(msg string, args ...interface{}) { c.t.Logf("%v: "+msg, append([]interface{}{caller(3)}, args...)...) } @@ -516,7 +505,7 @@ func (c *Ctx) makeClient(client, key string, failOnErrors bool) *apiClient { c.t.Fatalf("\n%v: %v", caller(2), err) } } - dash, err := dashapi.NewCustom(client, "", key, c.inst.NewRequest, doer, logger, errorHandler) + dash, err := dashapi.NewCustom(client, "", key, c.inst.NewRequest, c.httpDoer(), logger, errorHandler) if err != nil { panic(fmt.Sprintf("Impossible error: %v", err)) } @@ -526,6 +515,25 @@ func (c *Ctx) makeClient(client, key string, failOnErrors bool) *apiClient { } } +func (c *Ctx) makeAPIClient() *api.Client { + return api.NewTestClient(c.inst.NewRequest, c.httpDoer()) +} + +func (c *Ctx) httpDoer() func(*http.Request) (*http.Response, error) { + return func(r *http.Request) (*http.Response, error) { + r = registerRequest(r, c) + r = r.WithContext(c.transformContext(r.Context())) + w := httptest.NewRecorder() + http.DefaultServeMux.ServeHTTP(w, r) + res := &http.Response{ + StatusCode: w.Code, + Status: http.StatusText(w.Code), + Body: io.NopCloser(w.Result().Body), + } + return res, nil + } +} + func (client *apiClient) pollBugs(expect int) []*dashapi.BugReport { resp, _ := client.ReportingPollBugs("test") if len(resp.Reports) != expect { -- cgit mrf-deployment