Skip to content

Commit

Permalink
refactor: run step with runner
Browse files Browse the repository at this point in the history
  • Loading branch information
debugtalk committed Sep 22, 2021
1 parent 5281ad7 commit 57a1daf
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 228 deletions.
25 changes: 13 additions & 12 deletions extract.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package httpboomer

// implements IStep interface
type StepRequestExtraction struct {
*TStep
type stepRequestExtraction struct {
runner *Runner
step *TStep
}

func (step *StepRequestExtraction) WithJmesPath(jmesPath string, varName string) *StepRequestExtraction {
step.TStep.Extract[varName] = jmesPath
return step
func (s *stepRequestExtraction) WithJmesPath(jmesPath string, varName string) *stepRequestExtraction {
s.step.Extract[varName] = jmesPath
return s
}

func (step *StepRequestExtraction) Validate() *StepRequestValidation {
return &StepRequestValidation{
TStep: step.TStep,
func (s *stepRequestExtraction) Validate() *stepRequestValidation {
return &stepRequestValidation{
TStep: s.step,
}
}

func (step *StepRequestExtraction) ToStruct() *TStep {
return step.TStep
func (s *stepRequestExtraction) ToStruct() *TStep {
return s.step
}

func (step *StepRequestExtraction) Run() error {
return step.TStep.Run()
func (s *stepRequestExtraction) Run() error {
return s.runner.runStep(s.step)
}
145 changes: 0 additions & 145 deletions request.go

This file was deleted.

27 changes: 13 additions & 14 deletions runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"github.com/imroc/req"
)

var defaultRunner = HttpRunner()

func HttpRunner() *Runner {
return &Runner{}
return &Runner{
Client: req.New(),
}
}

type Runner struct {
Client *req.Req
}

func (r *Runner) Run(testcases ...*TestCase) error {
Expand All @@ -25,23 +30,14 @@ func (r *Runner) Run(testcases ...*TestCase) error {
func (r *Runner) runCase(testcase *TestCase) error {
// config := testcase.Config
for _, step := range testcase.TestSteps {
if err := r.runStep(step); err != nil {
if err := step.Run(); err != nil {
return err
}
}
return nil
}

func (r *Runner) runStep(req IStep) error {
return req.Run()
}

func (r *Runner) GetSummary() *TestCaseSummary {
return &TestCaseSummary{}
}

func (step *TStep) Run() error {

func (r *Runner) runStep(step *TStep) error {
var v []interface{}
v = append(v, req.Header(step.Request.Headers))
v = append(v, req.Param(step.Request.Params))
Expand All @@ -53,11 +49,14 @@ func (step *TStep) Run() error {
})
}

req.Debug = true
resp, err := req.Do(string(step.Request.Method), step.Request.URL, v...)
resp, err := r.Client.Do(string(step.Request.Method), step.Request.URL, v...)
if err != nil {
return err
}
resp.Response().Body.Close()
return nil
}

func (r *Runner) GetSummary() *TestCaseSummary {
return &TestCaseSummary{}
}
6 changes: 3 additions & 3 deletions runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ func TestHttpRunner(t *testing.T) {
BaseURL: "http://httpbin.org",
},
TestSteps: []IStep{
RunRequest("headers").
Step("headers").
GET("/headers").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("headers.Host", "httpbin.org", "check http response host"),
RunRequest("user-agent").
Step("user-agent").
GET("/user-agent").
Validate().
AssertEqual("status_code", 200, "check status code").
AssertEqual("body.\"user-agent\"", "python-requests", "check User-Agent"),
RunTestCase("TestCase3").WithVariables(Variables{"var1": "value1"}),
Step("TestCase3").CallRefCase(&TestCase{}),
},
}
testcase2 := &TestCase{
Expand Down
Loading

0 comments on commit 57a1daf

Please sign in to comment.