feat: support to reference in the request body (#79)
* feat: support to reference in the request body * add more test cases of answer platform --------- Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
This commit is contained in:
parent
768098757f
commit
09b58601c6
4
go.mod
4
go.mod
|
@ -7,7 +7,6 @@ require (
|
|||
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
|
||||
github.com/antonmedv/expr v1.12.1
|
||||
github.com/ghodss/yaml v1.0.0
|
||||
github.com/golang/protobuf v1.5.2
|
||||
github.com/h2non/gock v1.2.0
|
||||
github.com/invopop/jsonschema v0.7.0
|
||||
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f
|
||||
|
@ -17,12 +16,14 @@ require (
|
|||
github.com/xeipuuv/gojsonschema v1.2.0
|
||||
golang.org/x/sync v0.1.0
|
||||
google.golang.org/grpc v1.54.0
|
||||
google.golang.org/protobuf v1.28.1
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.2.0 // indirect
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
|
||||
github.com/huandu/xstrings v1.3.3 // indirect
|
||||
|
@ -43,7 +44,6 @@ require (
|
|||
golang.org/x/sys v0.6.0 // indirect
|
||||
golang.org/x/text v0.8.0 // indirect
|
||||
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
|
||||
google.golang.org/protobuf v1.30.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -126,8 +126,8 @@ google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag=
|
|||
google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
|
||||
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
|
||||
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
|
|
@ -162,16 +162,8 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
|
|||
}
|
||||
}
|
||||
|
||||
for _, sub := range reg.FindStringSubmatch(testcase.Request.API) {
|
||||
// remove {{ and }}
|
||||
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
|
||||
api := left.ReplaceAllString(sub, "")
|
||||
|
||||
expectName = targetReg.FindString(api)
|
||||
expectName = strings.TrimPrefix(expectName, ".")
|
||||
expectNames.Push(expectName)
|
||||
}
|
||||
}
|
||||
findExpectNames(testcase.Request.API, expectNames)
|
||||
findExpectNames(testcase.Request.Body, expectNames)
|
||||
|
||||
fmt.Println("expect test case names", expectNames.GetAll())
|
||||
for _, item := range suite.Items {
|
||||
|
@ -183,6 +175,22 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
|
|||
return
|
||||
}
|
||||
|
||||
func findExpectNames(target string, expectNames *UniqueSlice[string]) {
|
||||
reg, _ := regexp.Compile(`(.*?\{\{.*\.\w*.*?\}\})`)
|
||||
targetReg, _ := regexp.Compile(`\.\w*`)
|
||||
|
||||
for _, sub := range reg.FindStringSubmatch(target) {
|
||||
// remove {{ and }}
|
||||
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
|
||||
body := left.ReplaceAllString(sub, "")
|
||||
|
||||
expectName := targetReg.FindString(body)
|
||||
expectName = strings.TrimPrefix(expectName, ".")
|
||||
expectNames.Push(expectName)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// UniqueSlice represents an unique slice
|
||||
type UniqueSlice[T comparable] struct {
|
||||
data []T
|
||||
|
|
|
@ -91,6 +91,26 @@ func TestFindParentTestCases(t *testing.T) {
|
|||
expect: []atesting.TestCase{{
|
||||
Name: "login",
|
||||
}},
|
||||
}, {
|
||||
name: "body",
|
||||
testcase: &atesting.TestCase{
|
||||
Request: atesting.Request{
|
||||
Body: `{{.login.data}}`,
|
||||
},
|
||||
},
|
||||
suite: &atesting.TestSuite{
|
||||
Items: []atesting.TestCase{{
|
||||
Name: "login",
|
||||
}, {
|
||||
Name: "user",
|
||||
Request: atesting.Request{
|
||||
Body: `{{.login.data}}`,
|
||||
},
|
||||
}},
|
||||
},
|
||||
expect: []atesting.TestCase{{
|
||||
Name: "login",
|
||||
}},
|
||||
}, {
|
||||
name: "empty cases",
|
||||
testcase: &atesting.TestCase{},
|
||||
|
|
|
@ -0,0 +1,97 @@
|
|||
#!api-testing
|
||||
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
|
||||
# see also https://github.com/answerdev/answer
|
||||
name: Answer
|
||||
api: http://localhost:9080/answer/api/v1
|
||||
items:
|
||||
- name: login
|
||||
request:
|
||||
api: /user/login/email
|
||||
method: POST
|
||||
header:
|
||||
Content-Type: application/json
|
||||
body: |
|
||||
{
|
||||
"e_mail": "admin@ad.com",
|
||||
"pass": "admin123"
|
||||
}
|
||||
- name: status
|
||||
request:
|
||||
api: /notification/status
|
||||
method: GET
|
||||
header:
|
||||
Content-Type: application/json
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
- name: question
|
||||
request:
|
||||
api: /question
|
||||
method: POST
|
||||
header:
|
||||
Content-Type: application/json
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
body: |
|
||||
{
|
||||
"title": "{{randomKubernetesName}}",
|
||||
"content": "good-body",
|
||||
"tags": [
|
||||
{
|
||||
"slug_name": "test",
|
||||
"display_name": "test",
|
||||
"original_text": "",
|
||||
"parsed_text": ""
|
||||
}
|
||||
]
|
||||
}
|
||||
expect:
|
||||
bodyFieldsExpect:
|
||||
data/content: good-body
|
||||
- name: answer
|
||||
request:
|
||||
api: /answer
|
||||
method: POST
|
||||
header:
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
Content-Type: application/json
|
||||
body: |
|
||||
{
|
||||
"question_id": "{{.question.data.id}}",
|
||||
"content": "12121212",
|
||||
"html": "<p>12121212</p>\n"
|
||||
}
|
||||
- name: acceptance
|
||||
before:
|
||||
items:
|
||||
- sleep("1s")
|
||||
request:
|
||||
api: /answer/acceptance
|
||||
method: POST
|
||||
header:
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
Content-Type: application/json
|
||||
body: |
|
||||
{
|
||||
"question_id": "{{.question.data.id}}",
|
||||
"answer_id": "{{.answer.data.info.id}}"
|
||||
}
|
||||
- name: delAnswer
|
||||
request:
|
||||
api: /answer
|
||||
method: DELETE
|
||||
header:
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
Content-Type: application/json
|
||||
body: |
|
||||
{
|
||||
"id": "{{.answer.data.info.id}}"
|
||||
}
|
||||
- name: delQuestion
|
||||
request:
|
||||
api: /question
|
||||
method: DELETE
|
||||
header:
|
||||
Content-Type: application/json
|
||||
Authorization: "{{.login.data.access_token}}"
|
||||
body: |
|
||||
{
|
||||
"id": "{{.question.data.id}}"
|
||||
}
|
|
@ -0,0 +1,21 @@
|
|||
#!api-testing
|
||||
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
|
||||
# see also https://github.com/halo-dev/halo
|
||||
name: Halo
|
||||
api: https://demo.halo.run
|
||||
items:
|
||||
- name: publickey
|
||||
request:
|
||||
api: /login/public-key
|
||||
method: GET
|
||||
- name: login
|
||||
request:
|
||||
api: /login
|
||||
method: POST
|
||||
body: |
|
||||
{
|
||||
"username": "demo",
|
||||
"password": "P@ssw0rd123"
|
||||
}
|
||||
expect:
|
||||
statusCode: 500
|
Loading…
Reference in New Issue