From d31c30476fffa971741a574280e822aec8eda7f4 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Tue, 7 Dec 2021 13:29:06 +0800 Subject: [PATCH] refactor: NewConfig --- boomer.go | 2 +- boomer_test.go | 7 ++----- examples/demo_test.go | 10 ++++------ examples/extract_test.go | 16 ++++++--------- examples/function_test.go | 12 +++++------ examples/request_test.go | 8 +++----- examples/validate_test.go | 8 +++----- examples/variables_test.go | 41 ++++++++++++++------------------------ har2case/core.go | 9 +++------ models.go | 4 ++-- runner.go | 2 +- runner_test.go | 13 ++++-------- step.go | 38 +++++++++++++++++++++++++++++++++++ step_test.go | 4 +--- 14 files changed, 88 insertions(+), 86 deletions(-) diff --git a/boomer.go b/boomer.go index 5d53b28..22c9a41 100644 --- a/boomer.go +++ b/boomer.go @@ -58,7 +58,7 @@ func (b *Boomer) convertBoomerTask(testcase *TestCase) *boomer.Task { Weight: testcase.Config.Weight, Fn: func() { runner := NewRunner(nil).SetDebug(b.debug) - config := &testcase.Config + config := testcase.Config for _, step := range testcase.TestSteps { var err error start := time.Now() diff --git a/boomer_test.go b/boomer_test.go index bc58729..511ebfe 100644 --- a/boomer_test.go +++ b/boomer_test.go @@ -7,10 +7,7 @@ import ( func TestBoomerStandaloneRun(t *testing.T) { testcase1 := &TestCase{ - Config: TConfig{ - Name: "TestCase1", - BaseURL: "http://httpbin.org", - }, + Config: NewConfig("TestCase1").SetBaseURL("http://httpbin.org"), TestSteps: []IStep{ NewStep("headers"). GET("/headers"). @@ -22,7 +19,7 @@ func TestBoomerStandaloneRun(t *testing.T) { Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"), - NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}), + NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3")}), }, } testcase2 := &TestCasePath{demoTestCaseJSONPath} diff --git a/examples/demo_test.go b/examples/demo_test.go index 768ed47..0fb4e56 100644 --- a/examples/demo_test.go +++ b/examples/demo_test.go @@ -8,17 +8,15 @@ import ( ) var demoTestCase = &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "demo with complex mechanisms", - BaseURL: "https://postman-echo.com", - Variables: map[string]interface{}{ // global level variables + Config: hrp.NewConfig("demo with complex mechanisms"). + SetBaseURL("https://postman-echo.com"). + WithVariables(map[string]interface{}{ // global level variables "n": 5, "a": 12.3, "b": 3.45, "varFoo1": "${gen_random_string($n)}", "varFoo2": "${max($a, $b)}", // 12.3; eval with built-in function - }, - }, + }), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ // step level variables diff --git a/examples/extract_test.go b/examples/extract_test.go index b555566..ec72277 100644 --- a/examples/extract_test.go +++ b/examples/extract_test.go @@ -9,11 +9,9 @@ import ( // reference extracted variables for validation in the same step func TestCaseExtractStepSingle(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with variables", - BaseURL: "https://postman-echo.com", - Verify: false, - }, + Config: hrp.NewConfig("run request with variables"). + SetBaseURL("https://postman-echo.com"). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ @@ -46,11 +44,9 @@ func TestCaseExtractStepSingle(t *testing.T) { // reference extracted variables from previous step func TestCaseExtractStepAssociation(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with variables", - BaseURL: "https://postman-echo.com", - Verify: false, - }, + Config: hrp.NewConfig("run request with variables"). + SetBaseURL("https://postman-echo.com"). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ diff --git a/examples/function_test.go b/examples/function_test.go index 946c8a1..cd2c2d9 100644 --- a/examples/function_test.go +++ b/examples/function_test.go @@ -8,16 +8,14 @@ import ( func TestCaseCallFunction(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with functions", - BaseURL: "https://postman-echo.com", - Verify: false, - Variables: map[string]interface{}{ + Config: hrp.NewConfig("run request with functions"). + SetBaseURL("https://postman-echo.com"). + WithVariables(map[string]interface{}{ "n": 5, "a": 12.3, "b": 3.45, - }, - }, + }). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). GET("/get"). diff --git a/examples/request_test.go b/examples/request_test.go index 43a8f41..6312df0 100644 --- a/examples/request_test.go +++ b/examples/request_test.go @@ -8,11 +8,9 @@ import ( func TestCaseBasicRequest(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "request methods testcase in hardcode", - BaseURL: "https://postman-echo.com", - Verify: false, - }, + Config: hrp.NewConfig("request methods testcase in hardcode"). + SetBaseURL("https://postman-echo.com"). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). GET("/get"). diff --git a/examples/validate_test.go b/examples/validate_test.go index e1ddb45..24d60e2 100644 --- a/examples/validate_test.go +++ b/examples/validate_test.go @@ -8,11 +8,9 @@ import ( func TestCaseValidateStep(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with validation", - BaseURL: "https://postman-echo.com", - Verify: false, - }, + Config: hrp.NewConfig("run request with validation"). + SetBaseURL("https://postman-echo.com"). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ diff --git a/examples/variables_test.go b/examples/variables_test.go index 7fc70ba..9fb4c0f 100644 --- a/examples/variables_test.go +++ b/examples/variables_test.go @@ -8,16 +8,13 @@ import ( func TestCaseConfigVariables(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with variables", - BaseURL: "https://postman-echo.com", - Variables: map[string]interface{}{ + Config: hrp.NewConfig("run request with variables"). + SetBaseURL("https://postman-echo.com"). + WithVariables(map[string]interface{}{ "var1": "bar1", "agent": "HttpRunnerPlus", "expectedStatusCode": 200, - }, - Verify: false, - }, + }).SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). GET("/get"). @@ -41,11 +38,9 @@ func TestCaseConfigVariables(t *testing.T) { func TestCaseStepVariables(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with variables", - BaseURL: "https://postman-echo.com", - Verify: false, - }, + Config: hrp.NewConfig("run request with variables"). + SetBaseURL("https://postman-echo.com"). + SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ @@ -74,16 +69,13 @@ func TestCaseStepVariables(t *testing.T) { func TestCaseOverrideConfigVariables(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with variables", - BaseURL: "https://postman-echo.com", - Variables: map[string]interface{}{ + Config: hrp.NewConfig("run request with variables"). + SetBaseURL("https://postman-echo.com"). + WithVariables(map[string]interface{}{ "var1": "bar0", "agent": "HttpRunnerPlus", "expectedStatusCode": 200, - }, - Verify: false, - }, + }).SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ @@ -112,18 +104,15 @@ func TestCaseOverrideConfigVariables(t *testing.T) { func TestCaseParseVariables(t *testing.T) { testcase := &hrp.TestCase{ - Config: hrp.TConfig{ - Name: "run request with functions", - BaseURL: "https://postman-echo.com", - Verify: false, - Variables: map[string]interface{}{ + Config: hrp.NewConfig("run request with functions"). + SetBaseURL("https://postman-echo.com"). + WithVariables(map[string]interface{}{ "n": 5, "a": 12.3, "b": 3.45, "varFoo1": "${gen_random_string($n)}", "varFoo2": "${max($a, $b)}", // 12.3 - }, - }, + }).SetVerifySSL(false), TestSteps: []hrp.IStep{ hrp.NewStep("get with params"). WithVariables(map[string]interface{}{ diff --git a/har2case/core.go b/har2case/core.go index 0ecbbb4..0e48957 100644 --- a/har2case/core.go +++ b/har2case/core.go @@ -86,7 +86,7 @@ func (h *HAR) makeTestCase() (*hrp.TCase, error) { } tCase := &hrp.TCase{ - Config: *h.prepareConfig(), + Config: h.prepareConfig(), TestSteps: teststeps, } return tCase, nil @@ -114,11 +114,8 @@ func (h *HAR) load() (*Har, error) { } func (h *HAR) prepareConfig() *hrp.TConfig { - return &hrp.TConfig{ - Name: "testcase description", - Variables: make(map[string]interface{}), - Verify: false, - } + return hrp.NewConfig("testcase description"). + SetVerifySSL(false) } func (h *HAR) prepareTestSteps() ([]*hrp.TStep, error) { diff --git a/models.go b/models.go index 0c8b797..8aac4ec 100644 --- a/models.go +++ b/models.go @@ -56,7 +56,7 @@ type TStep struct { // TCase represents testcase data structure. // Each testcase includes one public config and several sequential teststeps. type TCase struct { - Config TConfig `json:"config" yaml:"config"` + Config *TConfig `json:"config" yaml:"config"` TestSteps []*TStep `json:"teststeps" yaml:"teststeps"` } @@ -76,7 +76,7 @@ type ITestCase interface { // TestCase is a container for one testcase. // used for testcase runner type TestCase struct { - Config TConfig + Config *TConfig TestSteps []IStep } diff --git a/runner.go b/runner.go index dc9206f..58eb6c2 100644 --- a/runner.go +++ b/runner.go @@ -99,7 +99,7 @@ func (r *runner) Run(testcases ...ITestCase) error { } func (r *runner) runCase(testcase *TestCase) error { - config := &testcase.Config + config := testcase.Config if err := r.parseConfig(config); err != nil { return err } diff --git a/runner_test.go b/runner_test.go index cd48eec..686d67e 100644 --- a/runner_test.go +++ b/runner_test.go @@ -6,10 +6,8 @@ import ( func TestHttpRunner(t *testing.T) { testcase1 := &TestCase{ - Config: TConfig{ - Name: "TestCase1", - BaseURL: "http://httpbin.org", - }, + Config: NewConfig("TestCase1"). + SetBaseURL("http://httpbin.org"), TestSteps: []IStep{ NewStep("headers"). GET("/headers"). @@ -21,14 +19,11 @@ func TestHttpRunner(t *testing.T) { Validate(). AssertEqual("status_code", 200, "check status code"). AssertEqual("headers.\"Content-Type\"", "application/json", "check http response Content-Type"), - NewStep("TestCase3").CallRefCase(&TestCase{Config: TConfig{Name: "TestCase3"}}), + NewStep("TestCase3").CallRefCase(&TestCase{Config: NewConfig("TestCase3")}), }, } testcase2 := &TestCase{ - Config: TConfig{ - Name: "TestCase2", - Weight: 3, - }, + Config: NewConfig("TestCase2").SetWeight(3), } testcase3 := &TestCasePath{demoTestCaseJSONPath} diff --git a/step.go b/step.go index 53d35c2..2498a3e 100644 --- a/step.go +++ b/step.go @@ -2,6 +2,44 @@ package hrp import "fmt" +// NewConfig returns a new constructed testcase config with specified testcase name. +func NewConfig(name string) *TConfig { + return &TConfig{ + Name: name, + Variables: make(map[string]interface{}), + } +} + +func (c *TConfig) WithVariables(variables map[string]interface{}) *TConfig { + c.Variables = variables + return c +} + +func (c *TConfig) SetBaseURL(baseURL string) *TConfig { + c.BaseURL = baseURL + return c +} + +func (c *TConfig) SetVerifySSL(verify bool) *TConfig { + c.Verify = verify + return c +} + +func (c *TConfig) WithParameters(parameters map[string]interface{}) *TConfig { + c.Parameters = parameters + return c +} + +func (c *TConfig) ExportVars(vars ...string) *TConfig { + c.Export = vars + return c +} + +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{ diff --git a/step_test.go b/step_test.go index fc7db69..38a91ab 100644 --- a/step_test.go +++ b/step_test.go @@ -74,9 +74,7 @@ func TestRunRequestPostDataToStruct(t *testing.T) { } func TestRunRequestRun(t *testing.T) { - config := &TConfig{ - BaseURL: "https://postman-echo.com", - } + config := NewConfig("test").SetBaseURL("https://postman-echo.com") runner := NewRunner(t).SetDebug(true) if _, err := runner.runStep(stepGET, config); err != nil { t.Fatalf("tStep.Run() error: %s", err)