Skip to content

Commit 09b5860

Browse files
authored
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 <[email protected]>
1 parent 7680987 commit 09b5860

File tree

6 files changed

+160
-14
lines changed

6 files changed

+160
-14
lines changed

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ require (
77
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883
88
github.com/antonmedv/expr v1.12.1
99
github.com/ghodss/yaml v1.0.0
10-
github.com/golang/protobuf v1.5.2
1110
github.com/h2non/gock v1.2.0
1211
github.com/invopop/jsonschema v0.7.0
1312
github.com/linuxsuren/go-fake-runtime v0.0.0-20230426144714-1a7a0d160d3f
@@ -17,12 +16,14 @@ require (
1716
github.com/xeipuuv/gojsonschema v1.2.0
1817
golang.org/x/sync v0.1.0
1918
google.golang.org/grpc v1.54.0
19+
google.golang.org/protobuf v1.28.1
2020
)
2121

2222
require (
2323
github.com/Masterminds/goutils v1.1.1 // indirect
2424
github.com/Masterminds/semver/v3 v3.2.0 // indirect
2525
github.com/davecgh/go-spew v1.1.1 // indirect
26+
github.com/golang/protobuf v1.5.2 // indirect
2627
github.com/google/uuid v1.3.0 // indirect
2728
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
2829
github.com/huandu/xstrings v1.3.3 // indirect
@@ -43,7 +44,6 @@ require (
4344
golang.org/x/sys v0.6.0 // indirect
4445
golang.org/x/text v0.8.0 // indirect
4546
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
46-
google.golang.org/protobuf v1.30.0 // indirect
4747
gopkg.in/yaml.v2 v2.4.0 // indirect
4848
gopkg.in/yaml.v3 v3.0.1 // indirect
4949
)

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ google.golang.org/grpc v1.54.0 h1:EhTqbhiYeixwWQtAEZAxmV9MGqcjEU2mFx52xCzNyag=
126126
google.golang.org/grpc v1.54.0/go.mod h1:PUSEXI6iWghWaB6lXM4knEgpJNu2qUcKfDtNci3EC2g=
127127
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
128128
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
129-
google.golang.org/protobuf v1.30.0 h1:kPPoIgf3TsEvrm0PFe15JQ+570QVxYzEvvHqChK+cng=
130-
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
129+
google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w=
130+
google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
131131
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
132132
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
133133
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

pkg/server/remote_server.go

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,8 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
162162
}
163163
}
164164

165-
for _, sub := range reg.FindStringSubmatch(testcase.Request.API) {
166-
// remove {{ and }}
167-
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
168-
api := left.ReplaceAllString(sub, "")
169-
170-
expectName = targetReg.FindString(api)
171-
expectName = strings.TrimPrefix(expectName, ".")
172-
expectNames.Push(expectName)
173-
}
174-
}
165+
findExpectNames(testcase.Request.API, expectNames)
166+
findExpectNames(testcase.Request.Body, expectNames)
175167

176168
fmt.Println("expect test case names", expectNames.GetAll())
177169
for _, item := range suite.Items {
@@ -183,6 +175,22 @@ func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (
183175
return
184176
}
185177

178+
func findExpectNames(target string, expectNames *UniqueSlice[string]) {
179+
reg, _ := regexp.Compile(`(.*?\{\{.*\.\w*.*?\}\})`)
180+
targetReg, _ := regexp.Compile(`\.\w*`)
181+
182+
for _, sub := range reg.FindStringSubmatch(target) {
183+
// remove {{ and }}
184+
if left, leftErr := regexp.Compile(`.*\{\{`); leftErr == nil {
185+
body := left.ReplaceAllString(sub, "")
186+
187+
expectName := targetReg.FindString(body)
188+
expectName = strings.TrimPrefix(expectName, ".")
189+
expectNames.Push(expectName)
190+
}
191+
}
192+
}
193+
186194
// UniqueSlice represents an unique slice
187195
type UniqueSlice[T comparable] struct {
188196
data []T

pkg/server/remote_server_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ func TestFindParentTestCases(t *testing.T) {
9191
expect: []atesting.TestCase{{
9292
Name: "login",
9393
}},
94+
}, {
95+
name: "body",
96+
testcase: &atesting.TestCase{
97+
Request: atesting.Request{
98+
Body: `{{.login.data}}`,
99+
},
100+
},
101+
suite: &atesting.TestSuite{
102+
Items: []atesting.TestCase{{
103+
Name: "login",
104+
}, {
105+
Name: "user",
106+
Request: atesting.Request{
107+
Body: `{{.login.data}}`,
108+
},
109+
}},
110+
},
111+
expect: []atesting.TestCase{{
112+
Name: "login",
113+
}},
94114
}, {
95115
name: "empty cases",
96116
testcase: &atesting.TestCase{},

sample/answer.yaml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#!api-testing
2+
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
3+
# see also https://github.com/answerdev/answer
4+
name: Answer
5+
api: http://localhost:9080/answer/api/v1
6+
items:
7+
- name: login
8+
request:
9+
api: /user/login/email
10+
method: POST
11+
header:
12+
Content-Type: application/json
13+
body: |
14+
{
15+
"e_mail": "[email protected]",
16+
"pass": "admin123"
17+
}
18+
- name: status
19+
request:
20+
api: /notification/status
21+
method: GET
22+
header:
23+
Content-Type: application/json
24+
Authorization: "{{.login.data.access_token}}"
25+
- name: question
26+
request:
27+
api: /question
28+
method: POST
29+
header:
30+
Content-Type: application/json
31+
Authorization: "{{.login.data.access_token}}"
32+
body: |
33+
{
34+
"title": "{{randomKubernetesName}}",
35+
"content": "good-body",
36+
"tags": [
37+
{
38+
"slug_name": "test",
39+
"display_name": "test",
40+
"original_text": "",
41+
"parsed_text": ""
42+
}
43+
]
44+
}
45+
expect:
46+
bodyFieldsExpect:
47+
data/content: good-body
48+
- name: answer
49+
request:
50+
api: /answer
51+
method: POST
52+
header:
53+
Authorization: "{{.login.data.access_token}}"
54+
Content-Type: application/json
55+
body: |
56+
{
57+
"question_id": "{{.question.data.id}}",
58+
"content": "12121212",
59+
"html": "<p>12121212</p>\n"
60+
}
61+
- name: acceptance
62+
before:
63+
items:
64+
- sleep("1s")
65+
request:
66+
api: /answer/acceptance
67+
method: POST
68+
header:
69+
Authorization: "{{.login.data.access_token}}"
70+
Content-Type: application/json
71+
body: |
72+
{
73+
"question_id": "{{.question.data.id}}",
74+
"answer_id": "{{.answer.data.info.id}}"
75+
}
76+
- name: delAnswer
77+
request:
78+
api: /answer
79+
method: DELETE
80+
header:
81+
Authorization: "{{.login.data.access_token}}"
82+
Content-Type: application/json
83+
body: |
84+
{
85+
"id": "{{.answer.data.info.id}}"
86+
}
87+
- name: delQuestion
88+
request:
89+
api: /question
90+
method: DELETE
91+
header:
92+
Content-Type: application/json
93+
Authorization: "{{.login.data.access_token}}"
94+
body: |
95+
{
96+
"id": "{{.question.data.id}}"
97+
}

sample/halo.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!api-testing
2+
# yaml-language-server: $schema=https://gitee.com/linuxsuren/api-testing/raw/master/sample/api-testing-schema.json
3+
# see also https://github.com/halo-dev/halo
4+
name: Halo
5+
api: https://demo.halo.run
6+
items:
7+
- name: publickey
8+
request:
9+
api: /login/public-key
10+
method: GET
11+
- name: login
12+
request:
13+
api: /login
14+
method: POST
15+
body: |
16+
{
17+
"username": "demo",
18+
"password": "P@ssw0rd123"
19+
}
20+
expect:
21+
statusCode: 500

0 commit comments

Comments
 (0)