From b60f3ae7f5c84c90ba23d1b7ef40bc4b45ab68fb Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 7 Dec 2021 14:39:32 +0800 Subject: [PATCH] refactor: NewStep --- step.go | 101 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 62 insertions(+), 39 deletions(-) diff --git a/step.go b/step.go index 2498a3e7..d0a6e542 100644 --- a/step.go +++ b/step.go @@ -10,135 +10,144 @@ func NewConfig(name string) *TConfig { } } +// WithVariables sets variables for current testcase. func (c *TConfig) WithVariables(variables map[string]interface{}) *TConfig { c.Variables = variables return c } +// SetBaseURL sets base URL for current testcase. func (c *TConfig) SetBaseURL(baseURL string) *TConfig { c.BaseURL = baseURL return c } +// SetVerifySSL sets whether to verify SSL for current testcase. func (c *TConfig) SetVerifySSL(verify bool) *TConfig { c.Verify = verify return c } +// WithParameters sets parameters for current testcase. func (c *TConfig) WithParameters(parameters map[string]interface{}) *TConfig { c.Parameters = parameters return c } +// ExportVars specifies variable names to export for current testcase. func (c *TConfig) ExportVars(vars ...string) *TConfig { c.Export = vars return c } +// SetWeight sets weight for current testcase, which is used in load testing. func (c *TConfig) SetWeight(weight int) *TConfig { c.Weight = weight return c } // NewStep returns a new constructed teststep with specified step name. -func NewStep(name string) *step { - return &step{ - TStep: &TStep{ - Name: name, - Variables: make(map[string]interface{}), - }, +func NewStep(name string) *TStep { + return &TStep{ + Name: name, + Variables: make(map[string]interface{}), } } -type step struct { - *TStep -} - -func (s *step) WithVariables(variables map[string]interface{}) *step { - s.TStep.Variables = variables +// WithVariables sets variables for current teststep. +func (s *TStep) WithVariables(variables map[string]interface{}) *TStep { + s.Variables = variables return s } -func (s *step) SetupHook(hook string) *step { - s.TStep.SetupHooks = append(s.TStep.SetupHooks, hook) +// SetupHook adds a setup hook for current teststep. +func (s *TStep) SetupHook(hook string) *TStep { + s.SetupHooks = append(s.SetupHooks, hook) return s } -func (s *step) GET(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// GET makes a HTTP GET request. +func (s *TStep) GET(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpGET, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) HEAD(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// HEAD makes a HTTP HEAD request. +func (s *TStep) HEAD(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpHEAD, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) POST(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// POST makes a HTTP POST request. +func (s *TStep) POST(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpPOST, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) PUT(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// PUT makes a HTTP PUT request. +func (s *TStep) PUT(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpPUT, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) DELETE(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// DELETE makes a HTTP DELETE request. +func (s *TStep) DELETE(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpDELETE, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) OPTIONS(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// OPTIONS makes a HTTP OPTIONS request. +func (s *TStep) OPTIONS(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpOPTIONS, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -func (s *step) PATCH(url string) *requestWithOptionalArgs { - s.TStep.Request = &Request{ +// PATCH makes a HTTP PATCH request. +func (s *TStep) PATCH(url string) *requestWithOptionalArgs { + s.Request = &Request{ Method: httpPATCH, URL: url, } return &requestWithOptionalArgs{ - step: s.TStep, + step: s, } } -// call referenced testcase -func (s *step) CallRefCase(tc *TestCase) *testcaseWithOptionalArgs { - s.TStep.TestCase = tc +// CallRefCase calls a referenced testcase. +func (s *TStep) CallRefCase(tc *TestCase) *testcaseWithOptionalArgs { + s.TestCase = tc return &testcaseWithOptionalArgs{ - step: s.TStep, + step: s, } } @@ -147,62 +156,74 @@ type requestWithOptionalArgs struct { step *TStep } +// SetVerify sets whether to verify SSL for current HTTP request. func (s *requestWithOptionalArgs) SetVerify(verify bool) *requestWithOptionalArgs { s.step.Request.Verify = verify return s } +// SetTimeout sets timeout for current HTTP request. func (s *requestWithOptionalArgs) SetTimeout(timeout float32) *requestWithOptionalArgs { s.step.Request.Timeout = timeout return s } +// SetProxies sets proxies for current HTTP request. func (s *requestWithOptionalArgs) SetProxies(proxies map[string]string) *requestWithOptionalArgs { // TODO return s } +// SetAllowRedirects sets whether to allow redirects for current HTTP request. func (s *requestWithOptionalArgs) SetAllowRedirects(allowRedirects bool) *requestWithOptionalArgs { s.step.Request.AllowRedirects = allowRedirects return s } +// SetAuth sets auth for current HTTP request. func (s *requestWithOptionalArgs) SetAuth(auth map[string]string) *requestWithOptionalArgs { // TODO return s } +// WithParams sets HTTP request params for current step. func (s *requestWithOptionalArgs) WithParams(params map[string]interface{}) *requestWithOptionalArgs { s.step.Request.Params = params return s } +// WithHeaders sets HTTP request headers for current step. func (s *requestWithOptionalArgs) WithHeaders(headers map[string]string) *requestWithOptionalArgs { s.step.Request.Headers = headers return s } +// WithCookies sets HTTP request cookies for current step. func (s *requestWithOptionalArgs) WithCookies(cookies map[string]string) *requestWithOptionalArgs { s.step.Request.Cookies = cookies return s } +// WithBody sets HTTP request body for current step. func (s *requestWithOptionalArgs) WithBody(body interface{}) *requestWithOptionalArgs { s.step.Request.Body = body return s } +// TeardownHook adds a teardown hook for current teststep. func (s *requestWithOptionalArgs) TeardownHook(hook string) *requestWithOptionalArgs { s.step.TeardownHooks = append(s.step.TeardownHooks, hook) return s } +// Validate switches to step validation. func (s *requestWithOptionalArgs) Validate() *stepRequestValidation { return &stepRequestValidation{ step: s.step, } } +// Extract switches to step extraction. func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction { s.step.Extract = make(map[string]string) return &stepRequestExtraction{ @@ -230,11 +251,13 @@ type testcaseWithOptionalArgs struct { step *TStep } +// TeardownHook adds a teardown hook for current teststep. func (s *testcaseWithOptionalArgs) TeardownHook(hook string) *testcaseWithOptionalArgs { s.step.TeardownHooks = append(s.step.TeardownHooks, hook) return s } +// Export specifies variable names to export from referenced testcase for current step. func (s *testcaseWithOptionalArgs) Export(names ...string) *testcaseWithOptionalArgs { s.step.Export = append(s.step.Export, names...) return s