From: Maciej Wereski Date: Fri, 10 Nov 2017 12:46:06 +0000 (+0100) Subject: HTTP API Client: Update request X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d0d0682addc204d47e4f1c37ee451bd51c526aea;p=tools%2Fboruta.git HTTP API Client: Update request Change-Id: I7bac5be49bfd0f2a410720ec008c31c5d35cabd1 Signed-off-by: Maciej Wereski --- diff --git a/http/client/client.go b/http/client/client.go index a2862ae..45f1870 100644 --- a/http/client/client.go +++ b/http/client/client.go @@ -182,7 +182,27 @@ func (client *BorutaClient) CloseRequest(reqID boruta.ReqID) error { // UpdateRequest prepares JSON with fields that should be changed for given // request ID. func (client *BorutaClient) UpdateRequest(reqInfo *boruta.ReqInfo) error { - return util.ErrNotImplemented + if reqInfo == nil { + return errors.New("nil reqInfo passed") + } + req, err := json.Marshal(&struct { + boruta.Priority + Deadline time.Time + ValidAfter time.Time + }{ + Priority: reqInfo.Priority, + Deadline: reqInfo.Deadline, + ValidAfter: reqInfo.ValidAfter, + }) + if err != nil { + return err + } + path := client.url + "reqs/" + strconv.Itoa(int(reqInfo.ID)) + resp, err := http.Post(path, contentType, bytes.NewReader(req)) + if err != nil { + return err + } + return processResponse(resp, nil) } // GetRequestInfo queries Boruta server for details about given request ID. diff --git a/http/client/client_test.go b/http/client/client_test.go index 0fbd03d..6e4970c 100644 --- a/http/client/client_test.go +++ b/http/client/client_test.go @@ -396,11 +396,67 @@ func TestCloseRequest(t *testing.T) { } func TestUpdateRequest(t *testing.T) { - assert, client := initTest(t, "") - assert.NotNil(client) + prefix := "update-req-" + path := "/api/v1/reqs/" + + validAfter := time.Now() + deadline := validAfter.AddDate(0, 0, 2) + priority := Priority(4) + + reqJSON := string(jsonMustMarshal(&struct { + Priority + Deadline time.Time + ValidAfter time.Time + }{ + Priority: priority, + Deadline: deadline, + ValidAfter: validAfter, + })) + + reqinfo := req + + reqinfo.Priority = priority + reqinfo.Deadline = deadline + reqinfo.ValidAfter = validAfter + tests := []*testCase{ + &testCase{ + // valid request + name: prefix + "valid", + path: path + "1", + json: reqJSON, + contentType: contentJSON, + status: http.StatusNoContent, + }, + &testCase{ + // missing request + name: prefix + "missing", + path: path + "2", + json: reqJSON, + contentType: contentJSON, + status: http.StatusNotFound, + }, + } + + srv := prepareServer(http.MethodPost, tests) + defer srv.Close() + assert, client := initTest(t, srv.URL) + + // valid + reqinfo.ID = ReqID(1) + assert.Nil(client.UpdateRequest(&reqinfo)) + + // missing + reqinfo.ID = ReqID(2) + assert.Equal(errReqNotFound, client.UpdateRequest(&reqinfo)) + + // bad arguments err := client.UpdateRequest(nil) - assert.Equal(util.ErrNotImplemented, err) + assert.Equal(errors.New("nil reqInfo passed"), err) + + // http.Post failure + client.url = "http://nosuchaddress.fail" + assert.NotNil(client.UpdateRequest(&reqinfo)) } func TestGetRequestInfo(t *testing.T) {