diff options
| author | Aleksandr Nogikh <nogikh@google.com> | 2022-07-15 09:25:31 +0000 |
|---|---|---|
| committer | Aleksandr Nogikh <wp32pw@gmail.com> | 2022-07-15 16:04:04 +0200 |
| commit | 4ad7e28c82c4f736c1066bb2dda270fc053478c1 (patch) | |
| tree | f8cdc2be88788891d2b5215b1690c15420da4ae3 /dashboard/dashapi/dashapi.go | |
| parent | 7444fb485bce374cbba8280de84f0c0a18cb39d0 (diff) | |
dashapi: add query mocking functionality
At the moment it's only possible to do by emulating http requests, which
looks like an overkill for simple query mocking in unit tests.
Diffstat (limited to 'dashboard/dashapi/dashapi.go')
| -rw-r--r-- | dashboard/dashapi/dashapi.go | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/dashboard/dashapi/dashapi.go b/dashboard/dashapi/dashapi.go index bcd964852..028448b33 100644 --- a/dashboard/dashapi/dashapi.go +++ b/dashboard/dashapi/dashapi.go @@ -23,12 +23,17 @@ import ( ) type Dashboard struct { - Client string - Addr string - Key string - ctor RequestCtor - doer RequestDoer - logger RequestLogger + Client string + Addr string + Key string + ctor RequestCtor + doer RequestDoer + logger RequestLogger + // Yes, we have the ability to set custom constructor, doer and logger, but + // there are also cases when we just want to mock the whole request processing. + // Implementing that on top of http.Request/http.Response would complicate the + // code too much. + mocker RequestMocker errorHandler func(error) } @@ -36,10 +41,17 @@ func New(client, addr, key string) (*Dashboard, error) { return NewCustom(client, addr, key, http.NewRequest, http.DefaultClient.Do, nil, nil) } +func NewMock(mocker RequestMocker) *Dashboard { + return &Dashboard{ + mocker: mocker, + } +} + type ( RequestCtor func(method, url string, body io.Reader) (*http.Request, error) RequestDoer func(req *http.Request) (*http.Response, error) RequestLogger func(msg string, args ...interface{}) + RequestMocker func(method string, req, resp interface{}) error ) // key == "" indicates that the ambient GCE service account authority @@ -592,6 +604,9 @@ func (dash *Dashboard) Query(method string, req, reply interface{}) error { if dash.logger != nil { dash.logger("API(%v): %#v", method, req) } + if dash.mocker != nil { + return dash.mocker(method, req, reply) + } err := dash.queryImpl(method, req, reply) if err != nil { if dash.logger != nil { |
