Skip to content

Commit 19da5cd

Browse files
authored
feat: support to reuse the cookies (#301)
Co-authored-by: rick <[email protected]>
1 parent 1bc6b66 commit 19da5cd

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

cmd/run.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,11 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
320320
return
321321
}
322322

323+
suiteRunner := runner.GetTestSuiteRunner(testSuite)
324+
suiteRunner.WithTestReporter(o.reporter)
325+
suiteRunner.WithSecure(testSuite.Spec.Secure)
326+
suiteRunner.WithOutputWriter(os.Stdout)
327+
suiteRunner.WithWriteLevel(o.level)
323328
for _, testCase := range testSuite.Items {
324329
if !testCase.InScope(o.caseItems) {
325330
continue
@@ -338,12 +343,7 @@ func (o *runOption) runSuite(loader testing.Loader, dataContext map[string]inter
338343
ctxWithTimeout, _ := context.WithTimeout(ctx, o.requestTimeout)
339344
ctxWithTimeout = context.WithValue(ctxWithTimeout, runner.ContextKey("").ParentDir(), loader.GetContext())
340345

341-
runner := runner.GetTestSuiteRunner(testSuite)
342-
runner.WithTestReporter(o.reporter)
343-
runner.WithSecure(testSuite.Spec.Secure)
344-
runner.WithOutputWriter(os.Stdout)
345-
runner.WithWriteLevel(o.level)
346-
if output, err = runner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
346+
if output, err = suiteRunner.RunTestCase(&testCase, dataContext, ctxWithTimeout); err != nil && !o.requestIgnoreError {
347347
err = fmt.Errorf("failed to run '%s', %v", testCase.Name, err)
348348
return
349349
} else {

pkg/runner/http.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,15 @@ func (r ReportResultSlice) Swap(i, j int) {
7878
type simpleTestCaseRunner struct {
7979
UnimplementedRunner
8080
simpleResponse SimpleResponse
81+
cookies []*http.Cookie
8182
}
8283

8384
// NewSimpleTestCaseRunner creates the instance of the simple test case runner
8485
func NewSimpleTestCaseRunner() TestCaseRunner {
8586
runner := &simpleTestCaseRunner{
8687
UnimplementedRunner: NewDefaultUnimplementedRunner(),
8788
simpleResponse: SimpleResponse{},
89+
cookies: []*http.Cookie{},
8890
}
8991
return runner
9092
}
@@ -155,6 +157,9 @@ func (r *simpleTestCaseRunner) RunTestCase(testcase *testing.TestCase, dataConte
155157
return
156158
}
157159

160+
for _, cookie := range r.cookies {
161+
request.AddCookie(cookie)
162+
}
158163
r.log.Info("start to send request to %s\n", testcase.Request.API)
159164

160165
// 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
203208
} else {
204209
r.log.Trace(fmt.Sprintf("skip to read the body due to it is not struct content: %q\n", respType))
205210
}
211+
212+
r.cookies = append(r.cookies, resp.Cookies()...)
206213
return
207214
}
208215

0 commit comments

Comments
 (0)