From a4f9d073c70917c5fcafac775c0036bc1293e1d4 Mon Sep 17 00:00:00 2001 From: debugtalk Date: Wed, 22 Sep 2021 19:58:50 +0800 Subject: [PATCH] refactor: use raw models --- models.go | 57 ++++++++++++++++++++++++---------------------------- step.go | 10 ++++----- step_test.go | 12 +++++------ 3 files changed, 37 insertions(+), 42 deletions(-) diff --git a/models.go b/models.go index c7b9a870..8dc7b8b4 100644 --- a/models.go +++ b/models.go @@ -1,10 +1,5 @@ package httpboomer -type Variables map[string]interface{} -type Params map[string]interface{} -type Headers map[string]string -type Cookies map[string]string - type enumHTTPMethod string const ( @@ -18,26 +13,26 @@ const ( ) type TConfig struct { - Name string `json:"name"` - Verify bool `json:"verify"` - BaseURL string `json:"base_url"` - Variables Variables `json:"variables"` - Parameters Variables `json:"parameters"` - Export []string `json:"export"` - Weight int `json:"weight"` + Name string `json:"name"` + Verify bool `json:"verify"` + BaseURL string `json:"base_url"` + Variables map[string]interface{} `json:"variables"` + Parameters map[string]interface{} `json:"parameters"` + Export []string `json:"export"` + Weight int `json:"weight"` } type TRequest struct { - Method enumHTTPMethod `json:"method"` - URL string `json:"url"` - Params Params `json:"params"` - Headers Headers `json:"headers"` - Cookies Cookies `json:"cookies"` - Data interface{} `json:"data"` - JSON interface{} `json:"json"` - Timeout float32 `json:"timeout"` - AllowRedirects bool `json:"allow_redirects"` - Verify bool `json:"verify"` + Method enumHTTPMethod `json:"method"` + URL string `json:"url"` + Params map[string]interface{} `json:"params"` + Headers map[string]string `json:"headers"` + Cookies map[string]string `json:"cookies"` + Data interface{} `json:"data"` + JSON interface{} `json:"json"` + Timeout float32 `json:"timeout"` + AllowRedirects bool `json:"allow_redirects"` + Verify bool `json:"verify"` } type TValidator struct { @@ -48,15 +43,15 @@ type TValidator struct { } type TStep struct { - Name string `json:"name"` - Request *TRequest `json:"request"` - TestCase *TestCase `json:"testcase"` - Variables Variables `json:"variables"` - SetupHooks []string `json:"setup_hooks"` - TeardownHooks []string `json:"teardown_hooks"` - Extract map[string]string `json:"extract"` - Validators []TValidator `json:"validators"` - Export []string `json:"export"` + Name string `json:"name"` + Request *TRequest `json:"request"` + TestCase *TestCase `json:"testcase"` + Variables map[string]interface{} `json:"variables"` + SetupHooks []string `json:"setup_hooks"` + TeardownHooks []string `json:"teardown_hooks"` + Extract map[string]string `json:"extract"` + Validators []TValidator `json:"validators"` + Export []string `json:"export"` } // interface for all types of steps diff --git a/step.go b/step.go index 4a945fd6..56d0bddf 100644 --- a/step.go +++ b/step.go @@ -8,7 +8,7 @@ func Step(name string) *step { TStep: &TStep{ Name: name, Request: &TRequest{}, - Variables: make(Variables), + Variables: make(map[string]interface{}), }, } } @@ -23,7 +23,7 @@ func (s *step) WithRunner(runner *Runner) *step { return s } -func (s *step) WithVariables(variables Variables) *step { +func (s *step) WithVariables(variables map[string]interface{}) *step { s.TStep.Variables = variables return s } @@ -136,17 +136,17 @@ func (s *requestWithOptionalArgs) SetAuth(auth map[string]string) *requestWithOp return s } -func (s *requestWithOptionalArgs) WithParams(params Params) *requestWithOptionalArgs { +func (s *requestWithOptionalArgs) WithParams(params map[string]interface{}) *requestWithOptionalArgs { s.step.Request.Params = params return s } -func (s *requestWithOptionalArgs) WithHeaders(headers Headers) *requestWithOptionalArgs { +func (s *requestWithOptionalArgs) WithHeaders(headers map[string]string) *requestWithOptionalArgs { s.step.Request.Headers = headers return s } -func (s *requestWithOptionalArgs) WithCookies(cookies Cookies) *requestWithOptionalArgs { +func (s *requestWithOptionalArgs) WithCookies(cookies map[string]string) *requestWithOptionalArgs { s.step.Request.Cookies = cookies return s } diff --git a/step_test.go b/step_test.go index 88b10c72..1cfc6b4e 100644 --- a/step_test.go +++ b/step_test.go @@ -7,17 +7,17 @@ import ( var ( stepGET = Step("get with params"). GET("https://postman-echo.com/get"). - WithParams(Params{"foo1": "bar1", "foo2": "bar2"}). - WithHeaders(Headers{"User-Agent": "HttpBoomer"}). - WithCookies(Cookies{"user": "debugtalk"}). + WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). + WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}). + WithCookies(map[string]string{"user": "debugtalk"}). Validate(). AssertEqual("status_code", 200, "check status code") stepPOSTData = Step("post form data"). POST("https://postman-echo.com/post"). - WithParams(Params{"foo1": "bar1", "foo2": "bar2"}). - WithHeaders(Headers{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}). + WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}). + WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}). WithData("a=1&b=2"). - WithCookies(Cookies{"user": "debugtalk"}). + WithCookies(map[string]string{"user": "debugtalk"}). Validate(). AssertEqual("status_code", 200, "check status code") )