diff --git a/boomer.go b/boomer.go index 8ffabd3d..5d53b285 100644 --- a/boomer.go +++ b/boomer.go @@ -65,9 +65,9 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task { stepData, err := runner.runStep(step, config) elapsed := time.Since(start).Nanoseconds() / int64(time.Millisecond) if err == nil { - b.RecordSuccess(step.Type(), step.Name(), elapsed, stepData.ResponseLength) + b.RecordSuccess(step.getType(), step.name(), elapsed, stepData.responseLength) } else { - b.RecordFailure(step.Type(), step.Name(), elapsed, err.Error()) + b.RecordFailure(step.getType(), step.name(), elapsed, err.Error()) } } }, diff --git a/convert.go b/convert.go index 978818c7..e25f8bee 100644 --- a/convert.go +++ b/convert.go @@ -15,7 +15,7 @@ func (tc *TestCase) ToTCase() (*TCase, error) { Config: tc.Config, } for _, step := range tc.TestSteps { - tCase.TestSteps = append(tCase.TestSteps, step.ToStruct()) + tCase.TestSteps = append(tCase.TestSteps, step.toStruct()) } return &tCase, nil } diff --git a/docs/cmd/hrp.md b/docs/cmd/hrp.md index d7c0b4ef..21663901 100644 --- a/docs/cmd/hrp.md +++ b/docs/cmd/hrp.md @@ -22,4 +22,4 @@ Copyright 2021 debugtalk * [hrp har2case](hrp_har2case.md) - Convert HAR to json/yaml testcase files * [hrp run](hrp_run.md) - run API test -###### Auto generated by spf13/cobra on 2-Dec-2021 +###### Auto generated by spf13/cobra on 7-Dec-2021 diff --git a/docs/cmd/hrp_boom.md b/docs/cmd/hrp_boom.md index 734db534..8c906a3a 100644 --- a/docs/cmd/hrp_boom.md +++ b/docs/cmd/hrp_boom.md @@ -39,4 +39,4 @@ hrp boom [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 2-Dec-2021 +###### Auto generated by spf13/cobra on 7-Dec-2021 diff --git a/docs/cmd/hrp_har2case.md b/docs/cmd/hrp_har2case.md index 41c62b78..0932d403 100644 --- a/docs/cmd/hrp_har2case.md +++ b/docs/cmd/hrp_har2case.md @@ -23,4 +23,4 @@ hrp har2case harPath... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 2-Dec-2021 +###### Auto generated by spf13/cobra on 7-Dec-2021 diff --git a/docs/cmd/hrp_run.md b/docs/cmd/hrp_run.md index 9c38ebfd..18ba8a02 100644 --- a/docs/cmd/hrp_run.md +++ b/docs/cmd/hrp_run.md @@ -30,4 +30,4 @@ hrp run path... [flags] * [hrp](hrp.md) - One-stop solution for HTTP(S) testing. -###### Auto generated by spf13/cobra on 2-Dec-2021 +###### Auto generated by spf13/cobra on 7-Dec-2021 diff --git a/extract.go b/extract.go index 5dc8d839..56bc7b2c 100644 --- a/extract.go +++ b/extract.go @@ -18,14 +18,14 @@ func (s *stepRequestExtraction) Validate() *stepRequestValidation { } } -func (s *stepRequestExtraction) Name() string { +func (s *stepRequestExtraction) name() string { return s.step.Name } -func (s *stepRequestExtraction) Type() string { +func (s *stepRequestExtraction) getType() string { return fmt.Sprintf("request-%v", s.step.Request.Method) } -func (s *stepRequestExtraction) ToStruct() *TStep { +func (s *stepRequestExtraction) toStruct() *TStep { return s.step } diff --git a/har2case/core.go b/har2case/core.go index 04c1a312..0ecbbb4f 100644 --- a/har2case/core.go +++ b/har2case/core.go @@ -147,8 +147,8 @@ func (h *HAR) prepareTestStep(entry *Entry) (*hrp.TStep, error) { tStep := &TStep{ TStep: hrp.TStep{ - Request: &hrp.TRequest{}, - Validators: make([]hrp.TValidator, 0), + Request: &hrp.Request{}, + Validators: make([]hrp.Validator, 0), }, } if err := tStep.makeRequestMethod(entry); err != nil { @@ -180,7 +180,7 @@ type TStep struct { } func (s *TStep) makeRequestMethod(entry *Entry) error { - s.Request.Method = hrp.EnumHTTPMethod(entry.Request.Method) + s.Request.Method = entry.Request.Method return nil } @@ -258,7 +258,7 @@ func (s *TStep) makeRequestBody(entry *Entry) error { func (s *TStep) makeValidate(entry *Entry) error { // make validator for response status code - s.Validators = append(s.Validators, hrp.TValidator{ + s.Validators = append(s.Validators, hrp.Validator{ Check: "status_code", Assert: "equals", Expect: entry.Response.Status, @@ -269,7 +269,7 @@ func (s *TStep) makeValidate(entry *Entry) error { for _, header := range entry.Response.Headers { // assert Content-Type if strings.EqualFold(header.Name, "Content-Type") { - s.Validators = append(s.Validators, hrp.TValidator{ + s.Validators = append(s.Validators, hrp.Validator{ Check: "headers.Content-Type", Assert: "equals", Expect: header.Value, @@ -318,7 +318,7 @@ func (s *TStep) makeValidate(entry *Entry) error { case []interface{}: continue default: - s.Validators = append(s.Validators, hrp.TValidator{ + s.Validators = append(s.Validators, hrp.Validator{ Check: fmt.Sprintf("body.%s", key), Assert: "equals", Expect: v, diff --git a/internal/version/init.go b/internal/version/init.go index e5b480e1..a29e664d 100644 --- a/internal/version/init.go +++ b/internal/version/init.go @@ -1,3 +1,3 @@ package version -const VERSION = "v0.2.1" +const VERSION = "v0.3.0" diff --git a/models.go b/models.go index 9aafdc60..0c8b797a 100644 --- a/models.go +++ b/models.go @@ -1,19 +1,17 @@ package hrp -type EnumHTTPMethod string - const ( - GET EnumHTTPMethod = "GET" - HEAD EnumHTTPMethod = "HEAD" - POST EnumHTTPMethod = "POST" - PUT EnumHTTPMethod = "PUT" - DELETE EnumHTTPMethod = "DELETE" - OPTIONS EnumHTTPMethod = "OPTIONS" - PATCH EnumHTTPMethod = "PATCH" + httpGET string = "GET" + httpHEAD string = "HEAD" + httpPOST string = "POST" + httpPUT string = "PUT" + httpDELETE string = "DELETE" + httpOPTIONS string = "OPTIONS" + httpPATCH string = "PATCH" ) type TConfig struct { - Name string `json:"name" yaml:"name"` + Name string `json:"name" yaml:"name"` // required Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"` BaseURL string `json:"base_url,omitempty" yaml:"base_url,omitempty"` Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` @@ -22,9 +20,9 @@ type TConfig struct { Weight int `json:"weight,omitempty" yaml:"weight,omitempty"` } -type TRequest struct { - Method EnumHTTPMethod `json:"method" yaml:"method"` - URL string `json:"url" yaml:"url"` +type Request struct { + Method string `json:"method" yaml:"method"` // required + URL string `json:"url" yaml:"url"` // required Params map[string]interface{} `json:"params,omitempty" yaml:"params,omitempty"` Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"` Cookies map[string]string `json:"cookies,omitempty" yaml:"cookies,omitempty"` @@ -34,44 +32,48 @@ type TRequest struct { Verify bool `json:"verify,omitempty" yaml:"verify,omitempty"` } -type TValidator struct { - Check string `json:"check,omitempty" yaml:"check,omitempty"` // get value with jmespath - Assert string `json:"assert,omitempty" yaml:"assert,omitempty"` - Expect interface{} `json:"expect,omitempty" yaml:"expect,omitempty"` - Message string `json:"msg,omitempty" yaml:"msg,omitempty"` +type Validator struct { + Check string `json:"check" yaml:"check"` // get value with jmespath + Assert string `json:"assert" yaml:"assert"` + Expect interface{} `json:"expect" yaml:"expect"` + Message string `json:"msg,omitempty" yaml:"msg,omitempty"` // optional } +// TStep represents teststep data structure. +// Each step maybe two different type: make one HTTP request or reference another testcase. type TStep struct { - Name string `json:"name" yaml:"name"` - Transaction string `json:"transaction,omitempty" yaml:"transaction,omitempty"` - Request *TRequest `json:"request,omitempty" yaml:"request,omitempty"` + Name string `json:"name" yaml:"name"` // required + Request *Request `json:"request,omitempty" yaml:"request,omitempty"` TestCase *TestCase `json:"testcase,omitempty" yaml:"testcase,omitempty"` Variables map[string]interface{} `json:"variables,omitempty" yaml:"variables,omitempty"` SetupHooks []string `json:"setup_hooks,omitempty" yaml:"setup_hooks,omitempty"` TeardownHooks []string `json:"teardown_hooks,omitempty" yaml:"teardown_hooks,omitempty"` Extract map[string]string `json:"extract,omitempty" yaml:"extract,omitempty"` - Validators []TValidator `json:"validate,omitempty" yaml:"validate,omitempty"` + Validators []Validator `json:"validate,omitempty" yaml:"validate,omitempty"` Export []string `json:"export,omitempty" yaml:"export,omitempty"` } -// used for testcase json loading and dumping +// TCase represents testcase data structure. +// Each testcase includes one public config and several sequential teststeps. type TCase struct { Config TConfig `json:"config" yaml:"config"` TestSteps []*TStep `json:"teststeps" yaml:"teststeps"` } -// interface for all types of steps +// IStep represents interface for all types for teststeps type IStep interface { - Name() string - Type() string - ToStruct() *TStep + name() string + getType() string + toStruct() *TStep } +// ITestCase represents interface for all types for testcases type ITestCase interface { ToTestCase() (*TestCase, error) ToTCase() (*TCase, error) } +// TestCase is a container for one testcase. // used for testcase runner type TestCase struct { Config TConfig @@ -86,11 +88,11 @@ type TestCasePath struct { Path string } -type TestCaseSummary struct{} +type testCaseSummary struct{} -type StepData struct { - Name string // step name - Success bool // step execution result - ResponseLength int64 // response body length - ExportVars map[string]interface{} // extract variables +type stepData struct { + name string // step name + success bool // step execution result + responseLength int64 // response body length + exportVars map[string]interface{} // extract variables } diff --git a/response.go b/response.go index 80d0e5cc..1fa9857b 100644 --- a/response.go +++ b/response.go @@ -12,7 +12,7 @@ import ( "github.com/httprunner/hrp/internal/builtin" ) -func NewResponseObject(t *testing.T, resp *http.Response) (*ResponseObject, error) { +func newResponseObject(t *testing.T, resp *http.Response) (*responseObject, error) { // prepare response headers headers := make(map[string]string) for k, v := range resp.Header { @@ -58,7 +58,7 @@ func NewResponseObject(t *testing.T, resp *http.Response) (*ResponseObject, erro return nil, err } - return &ResponseObject{ + return &responseObject{ t: t, respObjMeta: data, }, nil @@ -71,13 +71,13 @@ type respObjMeta struct { Body interface{} `json:"body"` } -type ResponseObject struct { +type responseObject struct { t *testing.T respObjMeta interface{} validationResults map[string]interface{} } -func (v *ResponseObject) Extract(extractors map[string]string) map[string]interface{} { +func (v *responseObject) Extract(extractors map[string]string) map[string]interface{} { if extractors == nil { return nil } @@ -93,7 +93,7 @@ func (v *ResponseObject) Extract(extractors map[string]string) map[string]interf return extractMapping } -func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[string]interface{}) (err error) { +func (v *responseObject) Validate(validators []Validator, variablesMapping map[string]interface{}) (err error) { for _, validator := range validators { // parse check value checkItem := validator.Check @@ -133,7 +133,7 @@ func (v *ResponseObject) Validate(validators []TValidator, variablesMapping map[ return nil } -func (v *ResponseObject) searchJmespath(expr string) interface{} { +func (v *responseObject) searchJmespath(expr string) interface{} { checkValue, err := jmespath.Search(expr, v.respObjMeta) if err != nil { log.Error().Str("expr", expr).Err(err).Msg("search jmespath failed") diff --git a/runner.go b/runner.go index dfc2cf24..e143b6d8 100644 --- a/runner.go +++ b/runner.go @@ -113,12 +113,12 @@ func (r *Runner) runCase(testcase *TestCase) error { return nil } -func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err error) { - log.Info().Str("step", step.Name()).Msg("run step start") +func (r *Runner) runStep(step IStep, config *TConfig) (stepResult *stepData, err error) { + log.Info().Str("step", step.name()).Msg("run step start") // copy step to avoid data racing copiedStep := &TStep{} - if err = copier.Copy(copiedStep, step.ToStruct()); err != nil { + if err = copier.Copy(copiedStep, step.toStruct()); err != nil { log.Error().Err(err).Msg("copy step data failed") return } @@ -142,7 +142,7 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e // run referenced testcase log.Info().Str("testcase", copiedStep.Name).Msg("run referenced testcase") // TODO: override testcase config - stepData, err = r.runStepTestCase(copiedStep) + stepResult, err = r.runStepTestCase(copiedStep) if err != nil { log.Error().Err(err).Msg("run referenced testcase step failed") return @@ -150,7 +150,7 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e } else { // run request copiedStep.Request.URL = buildURL(config.BaseURL, copiedStep.Request.URL) // avoid data racing - stepData, err = r.runStepRequest(copiedStep) + stepResult, err = r.runStepRequest(copiedStep) if err != nil { log.Error().Err(err).Msg("run request step failed") return @@ -158,23 +158,23 @@ func (r *Runner) runStep(step IStep, config *TConfig) (stepData *StepData, err e } // update extracted variables - for k, v := range stepData.ExportVars { + for k, v := range stepResult.exportVars { r.sessionVariables[k] = v } log.Info(). - Str("step", step.Name()). - Bool("success", stepData.Success). - Interface("exportVars", stepData.ExportVars). + Str("step", step.name()). + Bool("success", stepResult.success). + Interface("exportVars", stepResult.exportVars). Msg("run step end") return } -func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { - stepData = &StepData{ - Name: step.Name, - Success: false, - ResponseLength: 0, +func (r *Runner) runStepRequest(step *TStep) (stepResult *stepData, err error) { + stepResult = &stepData{ + name: step.Name, + success: false, + responseLength: 0, } rawUrl := step.Request.URL @@ -310,7 +310,7 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { } // new response object - respObj, err := NewResponseObject(r.t, resp) + respObj, err := newResponseObject(r.t, resp) if err != nil { err = errors.Wrap(err, "init ResponseObject error") return @@ -319,7 +319,7 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { // extract variables from response extractors := step.Extract extractMapping := respObj.Extract(extractors) - stepData.ExportVars = extractMapping + stepResult.exportVars = extractMapping // override step variables with extracted variables stepVariables := mergeVariables(step.Variables, extractMapping) @@ -330,15 +330,15 @@ func (r *Runner) runStepRequest(step *TStep) (stepData *StepData, err error) { return } - stepData.Success = true - stepData.ResponseLength = resp.ContentLength + stepResult.success = true + stepResult.responseLength = resp.ContentLength return } -func (r *Runner) runStepTestCase(step *TStep) (stepData *StepData, err error) { - stepData = &StepData{ - Name: step.Name, - Success: false, +func (r *Runner) runStepTestCase(step *TStep) (stepResult *stepData, err error) { + stepResult = &stepData{ + name: step.Name, + success: false, } testcase := step.TestCase err = r.runCase(testcase) @@ -371,8 +371,8 @@ func (r *Runner) parseConfig(config *TConfig) error { return nil } -func (r *Runner) GetSummary() *TestCaseSummary { - return &TestCaseSummary{} +func (r *Runner) GetSummary() *testCaseSummary { + return &testCaseSummary{} } func setBodyBytes(req *http.Request, data []byte) { diff --git a/step.go b/step.go index 3508ca13..d86fa897 100644 --- a/step.go +++ b/step.go @@ -26,8 +26,8 @@ func (s *step) SetupHook(hook string) *step { } func (s *step) GET(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: GET, + s.TStep.Request = &Request{ + Method: httpGET, URL: url, } return &requestWithOptionalArgs{ @@ -36,8 +36,8 @@ func (s *step) GET(url string) *requestWithOptionalArgs { } func (s *step) HEAD(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: HEAD, + s.TStep.Request = &Request{ + Method: httpHEAD, URL: url, } return &requestWithOptionalArgs{ @@ -46,8 +46,8 @@ func (s *step) HEAD(url string) *requestWithOptionalArgs { } func (s *step) POST(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: POST, + s.TStep.Request = &Request{ + Method: httpPOST, URL: url, } return &requestWithOptionalArgs{ @@ -56,8 +56,8 @@ func (s *step) POST(url string) *requestWithOptionalArgs { } func (s *step) PUT(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: PUT, + s.TStep.Request = &Request{ + Method: httpPUT, URL: url, } return &requestWithOptionalArgs{ @@ -66,8 +66,8 @@ func (s *step) PUT(url string) *requestWithOptionalArgs { } func (s *step) DELETE(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: DELETE, + s.TStep.Request = &Request{ + Method: httpDELETE, URL: url, } return &requestWithOptionalArgs{ @@ -76,8 +76,8 @@ func (s *step) DELETE(url string) *requestWithOptionalArgs { } func (s *step) OPTIONS(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: OPTIONS, + s.TStep.Request = &Request{ + Method: httpOPTIONS, URL: url, } return &requestWithOptionalArgs{ @@ -86,8 +86,8 @@ func (s *step) OPTIONS(url string) *requestWithOptionalArgs { } func (s *step) PATCH(url string) *requestWithOptionalArgs { - s.TStep.Request = &TRequest{ - Method: PATCH, + s.TStep.Request = &Request{ + Method: httpPATCH, URL: url, } return &requestWithOptionalArgs{ @@ -171,18 +171,18 @@ func (s *requestWithOptionalArgs) Extract() *stepRequestExtraction { } } -func (s *requestWithOptionalArgs) Name() string { +func (s *requestWithOptionalArgs) name() string { if s.step.Name != "" { return s.step.Name } return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL) } -func (s *requestWithOptionalArgs) Type() string { +func (s *requestWithOptionalArgs) getType() string { return fmt.Sprintf("request-%v", s.step.Request.Method) } -func (s *requestWithOptionalArgs) ToStruct() *TStep { +func (s *requestWithOptionalArgs) toStruct() *TStep { return s.step } @@ -201,17 +201,17 @@ func (s *testcaseWithOptionalArgs) Export(names ...string) *testcaseWithOptional return s } -func (s *testcaseWithOptionalArgs) Name() string { +func (s *testcaseWithOptionalArgs) name() string { if s.step.Name != "" { return s.step.Name } return s.step.TestCase.Config.Name } -func (s *testcaseWithOptionalArgs) Type() string { +func (s *testcaseWithOptionalArgs) getType() string { return "testcase" } -func (s *testcaseWithOptionalArgs) ToStruct() *TStep { +func (s *testcaseWithOptionalArgs) toStruct() *TStep { return s.step } diff --git a/step_test.go b/step_test.go index 1f2e56e1..40206391 100644 --- a/step_test.go +++ b/step_test.go @@ -28,7 +28,7 @@ var ( func TestRunRequestGetToStruct(t *testing.T) { tStep := stepGET.step - if tStep.Request.Method != GET { + if tStep.Request.Method != httpGET { t.Fatalf("tStep.Request.Method != GET") } if tStep.Request.URL != "/get" { @@ -50,7 +50,7 @@ func TestRunRequestGetToStruct(t *testing.T) { func TestRunRequestPostDataToStruct(t *testing.T) { tStep := stepPOSTData.step - if tStep.Request.Method != POST { + if tStep.Request.Method != httpPOST { t.Fatalf("tStep.Request.Method != POST") } if tStep.Request.URL != "/post" { diff --git a/validate.go b/validate.go index f4975ca3..5ae23cbe 100644 --- a/validate.go +++ b/validate.go @@ -9,61 +9,61 @@ type stepRequestValidation struct { step *TStep } -func (s *stepRequestValidation) Name() string { +func (s *stepRequestValidation) name() string { if s.step.Name != "" { return s.step.Name } return fmt.Sprintf("%s %s", s.step.Request.Method, s.step.Request.URL) } -func (s *stepRequestValidation) Type() string { +func (s *stepRequestValidation) getType() string { return fmt.Sprintf("request-%v", s.step.Request.Method) } -func (s *stepRequestValidation) ToStruct() *TStep { +func (s *stepRequestValidation) toStruct() *TStep { return s.step } func (s *stepRequestValidation) AssertEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation { - validator := TValidator{ + v := Validator{ Check: jmesPath, Assert: "equals", Expect: expected, Message: msg, } - s.step.Validators = append(s.step.Validators, validator) + s.step.Validators = append(s.step.Validators, v) return s } func (s *stepRequestValidation) AssertStartsWith(jmesPath string, expected interface{}, msg string) *stepRequestValidation { - validator := TValidator{ + v := Validator{ Check: jmesPath, Assert: "startswith", Expect: expected, Message: msg, } - s.step.Validators = append(s.step.Validators, validator) + s.step.Validators = append(s.step.Validators, v) return s } func (s *stepRequestValidation) AssertEndsWith(jmesPath string, expected interface{}, msg string) *stepRequestValidation { - validator := TValidator{ + v := Validator{ Check: jmesPath, Assert: "endswith", Expect: expected, Message: msg, } - s.step.Validators = append(s.step.Validators, validator) + s.step.Validators = append(s.step.Validators, v) return s } func (s *stepRequestValidation) AssertLengthEqual(jmesPath string, expected interface{}, msg string) *stepRequestValidation { - validator := TValidator{ + v := Validator{ Check: jmesPath, Assert: "length_equals", Expect: expected, Message: msg, } - s.step.Validators = append(s.step.Validators, validator) + s.step.Validators = append(s.step.Validators, v) return s }