| 
6 | 6 | 	context "context"  | 
7 | 7 | 	"fmt"  | 
8 | 8 | 	"os"  | 
 | 9 | +	"regexp"  | 
9 | 10 | 	"strings"  | 
10 | 11 | 
 
  | 
11 | 12 | 	"github.com/linuxsuren/api-testing/pkg/render"  | 
@@ -76,7 +77,8 @@ func (s *server) Run(ctx context.Context, task *TestTask) (reply *HelloReply, er  | 
76 | 77 | 		}  | 
77 | 78 | 
 
  | 
78 | 79 | 		if targetTestcase != nil {  | 
79 |  | -			suite.Items = []testing.TestCase{*targetTestcase}  | 
 | 80 | +			parentCases := findParentTestCases(targetTestcase, suite)  | 
 | 81 | +			suite.Items = append(parentCases, *targetTestcase)  | 
80 | 82 | 		} else {  | 
81 | 83 | 			err = fmt.Errorf("cannot found testcase %s", task.CaseName)  | 
82 | 84 | 			return  | 
@@ -124,3 +126,27 @@ func (s *server) GetVersion(ctx context.Context, in *Empty) (reply *HelloReply,  | 
124 | 126 | 	reply = &HelloReply{Message: version.GetVersion()}  | 
125 | 127 | 	return  | 
126 | 128 | }  | 
 | 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 | +}  | 
0 commit comments