feat: support to reuse the cookies (#301)

Co-authored-by: rick <LinuxSuRen@users.noreply.github.com>
This commit is contained in:
Rick 2023-12-01 17:34:43 +08:00 committed by GitHub
parent 1bc6b66520
commit 19da5cdc32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 6 deletions

View File

@ -320,6 +320,11 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
return
}
suiteRunner := runner.GetTestSuiteRunner(testSuite)
suiteRunner.WithTestReporter(o.reporter)
suiteRunner.WithSecure(testSuite.Spec.Secure)
suiteRunner.WithOutputWriter(os.Stdout)
suiteRunner.WithWriteLevel(o.level)
for _, testCase := range testSuite.Items {
if !testCase.InScope(o.caseItems) {
continue
@ -338,12 +343,7 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
ctxWithTimeout, _ := context.WithTimeout(ctx, o.requestTimeout)
ctxWithTimeout = context.WithValue(ctxWithTimeout, runner.ContextKey("").ParentDir(), loader.GetContext())
runner := runner.GetTestSuiteRunner(testSuite)
runner.WithTestReporter(o.reporter)
runner.WithSecure(testSuite.Spec.Secure)
runner.WithOutputWriter(os.Stdout)
runner.WithWriteLevel(o.level)
if output, err = runner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
if output, err = suiteRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
err = fmt.Errorf("failed to run '%s', %v", testCase.Name, err)
return
} else {

View File

@ -78,6 +78,7 @@ func (r ReportResultSlice) Swap(i, j int) {
type simpleTestCaseRunner struct {
UnimplementedRunner
simpleResponse SimpleResponse
cookies []*http.Cookie
}
// NewSimpleTestCaseRunner creates the instance of the simple test case runner
@ -85,6 +86,7 @@ func NewSimpleTestCaseRunner() TestCaseRunner {
runner := &simpleTestCaseRunner{
UnimplementedRunner: NewDefaultUnimplementedRunner(),
simpleResponse: SimpleResponse{},
cookies: []*http.Cookie{},
}
return runner
}
@ -155,6 +157,9 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
return
}
for _, cookie := range r.cookies {
request.AddCookie(cookie)
}
r.log.Info("start to send request to %s\n", testcase.Request.API)
// TODO only do this for unit testing, should remove it once we have a better way
@ -203,6 +208,8 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
} else {
r.log.Trace(fmt.Sprintf("skip to read the body due to it is not struct content: %q\n", respType))
}
r.cookies = append(r.cookies, resp.Cookies()...)
return
}