From 66dfcec0ad2d84bded78b05cd56cfef36782e4e8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 2 Aug 2022 16:18:47 +0800 Subject: [PATCH] [Backport release-1.5] Feat: add http status and code from http cmd run (#4528) * Feat: add http status and code from http cmd run Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit 0b96b2e60ae9a7a8e7c2d575ec88bb5efdfca037) * Feat: fix unit test error Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit 25f22915030173e883998db920d6afdac986c63b) * Feat: status is not necessary Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit ef3b8ac82b61e153e4fdcb6a88cffaeb89e7bc7c) * Feat: make reviewable Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit 684f5e9ae2cd21613475a0c16a166cdbc93a84b0) * Feat: add unit test Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit 6c39f602eaa046e19c4d3b30d552a2f8be6775f9) * Feat: make reviewable Signed-off-by: suxiang <704427617@qq.com> (cherry picked from commit 20ae7f2e15902123ec230328e796b1d0e656546f) Co-authored-by: suxiang <704427617@qq.com> --- pkg/builtin/http/http.go | 7 ++++--- pkg/workflow/providers/http/do_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pkg/builtin/http/http.go b/pkg/builtin/http/http.go index 01f8c7b3d..bc60fceef 100644 --- a/pkg/builtin/http/http.go +++ b/pkg/builtin/http/http.go @@ -128,9 +128,10 @@ func (c *HTTPCmd) Run(meta *registry.Meta) (res interface{}, err error) { b, err := io.ReadAll(resp.Body) // parse response body and headers return map[string]interface{}{ - "body": string(b), - "header": resp.Header, - "trailer": resp.Trailer, + "body": string(b), + "header": resp.Header, + "trailer": resp.Trailer, + "statusCode": resp.StatusCode, }, err } diff --git a/pkg/workflow/providers/http/do_test.go b/pkg/workflow/providers/http/do_test.go index 0a0f6fa6f..6f42c51f4 100644 --- a/pkg/workflow/providers/http/do_test.go +++ b/pkg/workflow/providers/http/do_test.go @@ -57,17 +57,20 @@ func TestHttpDo(t *testing.T) { body: string header?: [string]: [...string] trailer?: [string]: [...string] + statusCode: number }) ` testCases := map[string]struct { request string expectedBody string + statusCode int64 }{ "hello": { request: baseTemplate + ` method: "GET" url: "http://127.0.0.1:1229/hello"`, expectedBody: `hello`, + statusCode: 200, }, "echo": { @@ -79,6 +82,7 @@ request:{ header: "Content-Type": "text/plain; charset=utf-8" }`, expectedBody: `I am vela`, + statusCode: 200, }, "json": { request: ` @@ -95,6 +99,7 @@ request:{ header: "Content-Type": "application/json; charset=utf-8" }` + baseTemplate, expectedBody: `{"name":"foo","score":100}`, + statusCode: 200, }, } @@ -109,6 +114,11 @@ request:{ ret, err := body.CueValue().String() assert.NilError(t, err, tName) assert.Equal(t, ret, tCase.expectedBody, tName) + statusCode, err := v.LookupValue("response", "statusCode") + assert.NilError(t, err, tName) + code, err := statusCode.CueValue().Int64() + assert.NilError(t, err, tName) + assert.Equal(t, code, tCase.statusCode, tName) } }