diff options
-rw-r--r-- | http/client/client.go | 16 | ||||
-rw-r--r-- | http/client/client_test.go | 50 |
2 files changed, 65 insertions, 1 deletions
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))) +} |