Skip to content

Commit

Permalink
feat: support to reference in the request body (#79)
Browse files Browse the repository at this point in the history
* feat: support to reference in the request body

* add more test cases of answer platform

---------

Co-authored-by: Rick <linuxsuren@users.noreply.github.com>
  • Loading branch information
LinuxSuRen and LinuxSuRen authored May 30, 2023
1 parent 7680987 commit 09b5860
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 14 deletions.
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
28 changes: 18 additions & 10 deletions pkg/server/remote_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
20 changes: 20 additions & 0 deletions pkg/server/remote_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
97 changes: 97 additions & 0 deletions sample/answer.yaml
Original file line number Diff line number Diff line change
@@ -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}}"
}
21 changes: 21 additions & 0 deletions sample/halo.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 09b5860

Please sign in to comment.