refactor: NewStep

This commit is contained in:
debugtalk
2021-12-07 14:39:32 +08:00
parent d31c30476f
commit bf7e331f62

101
step.go
View File

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