[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 0b96b2e60a)

* Feat: fix unit test error

Signed-off-by: suxiang <704427617@qq.com>
(cherry picked from commit 25f2291503)

* Feat: status is not necessary

Signed-off-by: suxiang <704427617@qq.com>
(cherry picked from commit ef3b8ac82b)

* Feat: make reviewable

Signed-off-by: suxiang <704427617@qq.com>
(cherry picked from commit 684f5e9ae2)

* Feat: add unit test

Signed-off-by: suxiang <704427617@qq.com>
(cherry picked from commit 6c39f602ea)

* Feat: make reviewable

Signed-off-by: suxiang <704427617@qq.com>
(cherry picked from commit 20ae7f2e15)

Co-authored-by: suxiang <704427617@qq.com>
This commit is contained in:
github-actions[bot]
2022-08-02 16:18:47 +08:00
committed by GitHub
co-authored by suxiang
parent 37dd176dd3
commit 66dfcec0ad
2 changed files with 14 additions and 3 deletions
+4 -3
View File
@@ -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
}
+10
View File
@@ -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)
}
}