HTTP API Client: Add method to check request state 15/182615/7
authorMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 26 Jun 2018 10:08:08 +0000 (12:08 +0200)
committerMaciej Wereski <m.wereski@partner.samsung.com>
Tue, 10 Jul 2018 08:05:40 +0000 (10:05 +0200)
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 <m.wereski@partner.samsung.com>
http/client/client.go
http/client/client_test.go

index b656f9e..a6e2f45 100644 (file)
@@ -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
+}
index 1fa244d..c94ef8b 100644 (file)
@@ -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)))
+}