From: Maciej Wereski Date: Tue, 26 Jun 2018 10:08:08 +0000 (+0200) Subject: HTTP API Client: Add method to check request state X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F15%2F182615%2F7;p=tools%2Fboruta.git HTTP API Client: Add method to check request state GetRequestState() is convenient method for checking status of request. It uses HEAD HTTP method, so it's faster than calling GetRequestInfo and checking boruta.ReqInfo.State. As HEAD method is used it may be harder to debug when an issue occurs, only HTTP status code is returned. Change-Id: Ia2c33e0294e7840d7acec1896090f29c38163913 Signed-off-by: Maciej Wereski --- diff --git a/http/client/client.go b/http/client/client.go index b656f9e..a6e2f45 100644 --- a/http/client/client.go +++ b/http/client/client.go @@ -352,3 +352,19 @@ func (client *BorutaClient) Deregister(uuid boruta.WorkerUUID) error { } return processResponse(resp, nil) } + +// GetRequestState is convenient way to check state of a request with given reqID. +// When error occurs then returned boruta.ReqState will make no sense. Developer +// should always check for an error before proceeding with actions dependent on +// request state. +func (client *BorutaClient) GetRequestState(reqID boruta.ReqID) (boruta.ReqState, error) { + path := client.url + "reqs/" + strconv.Itoa(int(reqID)) + resp, err := http.Head(path) + if err != nil { + return boruta.FAILED, err + } + if resp.StatusCode != http.StatusNoContent { + return boruta.FAILED, errors.New("bad HTTP status: " + resp.Status) + } + return boruta.ReqState(resp.Header.Get("Boruta-Request-State")), nil +} diff --git a/http/client/client_test.go b/http/client/client_test.go index 1fa244d..c94ef8b 100644 --- a/http/client/client_test.go +++ b/http/client/client_test.go @@ -176,7 +176,7 @@ func prepareServer(method string, tests []*testCase) *httptest.Server { break } } - if test.status != http.StatusNoContent { + if test.status != http.StatusNoContent && r.Method != http.MethodHead { // Find appropriate file with reply. fpath += test.name + "-" + r.Method switch test.contentType { @@ -196,6 +196,12 @@ func prepareServer(method string, tests []*testCase) *httptest.Server { } w.Header().Set("Content-Type", test.contentType) } + // Set custom Boruta HTTP headers. + if test.header != nil { + for k := range test.header { + w.Header().Set(k, test.header.Get(k)) + } + } w.WriteHeader(test.status) if test.status != http.StatusNoContent { w.Write(data) @@ -988,3 +994,45 @@ func TestDeregister(t *testing.T) { client.url = "http://nosuchaddress.fail" assert.NotNil(client.Deregister(validUUID)) } + +func TestGetRequestState(t *testing.T) { + prefix := "get-request-state-" + path := "/api/v1/reqs/" + + header := make(http.Header) + header.Set("Boruta-Request-State", string(DONE)) + + tests := []*testCase{ + &testCase{ + // valid request + name: prefix + "valid", + path: path + "1", + status: http.StatusNoContent, + header: header, + }, + &testCase{ + // missing request + name: prefix + "missing", + path: path + "2", + contentType: contentJSON, + status: http.StatusNotFound, + }, + } + + srv := prepareServer(http.MethodHead, tests) + defer srv.Close() + assert, client := initTest(t, srv.URL) + + // valid request + state, err := client.GetRequestState(ReqID(1)) + assert.Nil(err) + assert.Equal(DONE, state) + + // missing request + state, err = client.GetRequestState(ReqID(2)) + assert.Equal(errors.New("bad HTTP status: 404 Not Found"), err) + + // http.Head failure + client.url = "http://nosuchaddress.fail" + assert.NotNil(client.GetRequestState(ReqID(1))) +}