Skip to content

Commit 19a07a2

Browse files
authored
feat: support finding the parent test cases (#32)
1 parent a55b86b commit 19a07a2

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

pkg/server/remote_server.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
context "context"
77
"fmt"
88
"os"
9+
"regexp"
910
"strings"
1011

1112
"github.com/linuxsuren/api-testing/pkg/render"
@@ -76,7 +77,8 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er
7677
}
7778

7879
if targetTestcase != nil {
79-
suite.Items = []testing.TestCase{*targetTestcase}
80+
parentCases := findParentTestCases(targetTestcase, suite)
81+
suite.Items = append(parentCases, *targetTestcase)
8082
} else {
8183
err = fmt.Errorf("cannot found testcase %s", task.CaseName)
8284
return
@@ -124,3 +126,27 @@ func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply,
124126
reply = &HelloReply{Message: version.GetVersion()}
125127
return
126128
}
129+
130+
func findParentTestCases(testcase *testing.TestCase, suite *testing.TestSuite) (testcases []testing.TestCase) {
131+
reg, matchErr := regexp.Compile(`.*\{\{\.\w*\..*}\}.*`)
132+
targetReg, targetErr := regexp.Compile(`\{\{\.\w*\.`)
133+
134+
if matchErr == nil && targetErr == nil {
135+
expectName := ""
136+
for _, val := range testcase.Request.Header {
137+
if matched := reg.MatchString(val); matched {
138+
expectName = targetReg.FindString(val)
139+
expectName = strings.TrimPrefix(expectName, "{{.")
140+
expectName = strings.TrimSuffix(expectName, ".")
141+
break
142+
}
143+
}
144+
145+
for _, item := range suite.Items {
146+
if item.Name == expectName {
147+
testcases = append(testcases, item)
148+
}
149+
}
150+
}
151+
return
152+
}

pkg/server/remote_server_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
_ "embed"
99

1010
"github.com/h2non/gock"
11+
atesting "github.com/linuxsuren/api-testing/pkg/testing"
1112
"github.com/stretchr/testify/assert"
1213
)
1314

@@ -58,6 +59,42 @@ func TestRemoteServer(t *testing.T) {
5859
assert.Nil(t, err)
5960
}
6061

62+
func TestFindParentTestCases(t *testing.T) {
63+
tests := []struct {
64+
name string
65+
testcase *atesting.TestCase
66+
suite *atesting.TestSuite
67+
expect []atesting.TestCase
68+
}{{
69+
name: "normal",
70+
testcase: &atesting.TestCase{
71+
Request: atesting.Request{
72+
Header: map[string]string{
73+
"Authorization": "Bearer {{.login.data.access_token}}",
74+
},
75+
},
76+
},
77+
suite: &atesting.TestSuite{
78+
Items: []atesting.TestCase{{
79+
Name: "login",
80+
}},
81+
},
82+
expect: []atesting.TestCase{{
83+
Name: "login",
84+
}},
85+
}, {
86+
name: "empty cases",
87+
testcase: &atesting.TestCase{},
88+
suite: &atesting.TestSuite{},
89+
}}
90+
for _, tt := range tests {
91+
t.Run(tt.name, func(t *testing.T) {
92+
result := findParentTestCases(tt.testcase, tt.suite)
93+
assert.Equal(t, tt.expect, result)
94+
})
95+
}
96+
}
97+
6198
//go:embed testdata/simple.yaml
6299
var simpleSuite string
63100

0 commit comments

Comments
 (0)