Skip to content

Commit

Permalink
refactor: NewConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
debugtalk committed Dec 7, 2021
1 parent b8573c2 commit d31c304
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 86 deletions.
2 changes: 1 addition & 1 deletion boomer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 2 additions & 5 deletions boomer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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}
Expand Down
10 changes: 4 additions & 6 deletions examples/demo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 6 additions & 10 deletions examples/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down Expand Up @@ -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{}{
Expand Down
12 changes: 5 additions & 7 deletions examples/function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
8 changes: 3 additions & 5 deletions examples/request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand Down
8 changes: 3 additions & 5 deletions examples/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}{
Expand Down
41 changes: 15 additions & 26 deletions examples/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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{}{
Expand Down Expand Up @@ -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{}{
Expand Down Expand Up @@ -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{}{
Expand Down
9 changes: 3 additions & 6 deletions har2case/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
13 changes: 4 additions & 9 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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").
Expand All @@ -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}

Expand Down
38 changes: 38 additions & 0 deletions step.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
4 changes: 1 addition & 3 deletions step_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d31c304

Please sign in to comment.