From 09b58601c68331bff135d586540e30e5c08b8870 Mon Sep 17 00:00:00 2001 From: Rick <1450685+LinuxSuRen@users.noreply.github.com> Date: Tue, 30 May 2023 21:44:22 +0800 Subject: [PATCH] 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 --- go.mod | 4 +- go.sum | 4 +- pkg/server/remote_server.go | 28 +++++---- pkg/server/remote_server_test.go | 20 +++++++ sample/answer.yaml | 97 ++++++++++++++++++++++++++++++++ sample/halo.yaml | 21 +++++++ 6 files changed, 160 insertions(+), 14 deletions(-) create mode 100644 sample/answer.yaml create mode 100644 sample/halo.yaml diff --git a/go.mod b/go.mod index b4cd471..f251126 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 523adeb..f67129d 100644 --- a/go.sum +++ b/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= diff --git a/pkg/server/remote_server.go b/pkg/server/remote_server.go index dc52b4a..2ee6c6b 100644 --- a/pkg/server/remote_server.go +++ b/pkg/server/remote_server.go @@ -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 diff --git a/pkg/server/remote_server_test.go b/pkg/server/remote_server_test.go index ab8dc3c..84e0f93 100644 --- a/pkg/server/remote_server_test.go +++ b/pkg/server/remote_server_test.go @@ -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{}, diff --git a/sample/answer.yaml b/sample/answer.yaml new file mode 100644 index 0000000..9f3eadb --- /dev/null +++ b/sample/answer.yaml @@ -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": "

12121212

\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}}" + } diff --git a/sample/halo.yaml b/sample/halo.yaml new file mode 100644 index 0000000..a355626 --- /dev/null +++ b/sample/halo.yaml @@ -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