refactor: use raw models

This commit is contained in:
debugtalk
2021-09-22 19:58:50 +08:00
parent 3f9b8be720
commit 4c2d7673fa
3 changed files with 37 additions and 42 deletions

View File

@@ -1,10 +1,5 @@
package httpboomer 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 type enumHTTPMethod string
const ( const (
@@ -18,26 +13,26 @@ const (
) )
type TConfig struct { type TConfig struct {
Name string `json:"name"` Name string `json:"name"`
Verify bool `json:"verify"` Verify bool `json:"verify"`
BaseURL string `json:"base_url"` BaseURL string `json:"base_url"`
Variables Variables `json:"variables"` Variables map[string]interface{} `json:"variables"`
Parameters Variables `json:"parameters"` Parameters map[string]interface{} `json:"parameters"`
Export []string `json:"export"` Export []string `json:"export"`
Weight int `json:"weight"` Weight int `json:"weight"`
} }
type TRequest struct { type TRequest struct {
Method enumHTTPMethod `json:"method"` Method enumHTTPMethod `json:"method"`
URL string `json:"url"` URL string `json:"url"`
Params Params `json:"params"` Params map[string]interface{} `json:"params"`
Headers Headers `json:"headers"` Headers map[string]string `json:"headers"`
Cookies Cookies `json:"cookies"` Cookies map[string]string `json:"cookies"`
Data interface{} `json:"data"` Data interface{} `json:"data"`
JSON interface{} `json:"json"` JSON interface{} `json:"json"`
Timeout float32 `json:"timeout"` Timeout float32 `json:"timeout"`
AllowRedirects bool `json:"allow_redirects"` AllowRedirects bool `json:"allow_redirects"`
Verify bool `json:"verify"` Verify bool `json:"verify"`
} }
type TValidator struct { type TValidator struct {
@@ -48,15 +43,15 @@ type TValidator struct {
} }
type TStep struct { type TStep struct {
Name string `json:"name"` Name string `json:"name"`
Request *TRequest `json:"request"` Request *TRequest `json:"request"`
TestCase *TestCase `json:"testcase"` TestCase *TestCase `json:"testcase"`
Variables Variables `json:"variables"` Variables map[string]interface{} `json:"variables"`
SetupHooks []string `json:"setup_hooks"` SetupHooks []string `json:"setup_hooks"`
TeardownHooks []string `json:"teardown_hooks"` TeardownHooks []string `json:"teardown_hooks"`
Extract map[string]string `json:"extract"` Extract map[string]string `json:"extract"`
Validators []TValidator `json:"validators"` Validators []TValidator `json:"validators"`
Export []string `json:"export"` Export []string `json:"export"`
} }
// interface for all types of steps // interface for all types of steps

10
step.go
View File

@@ -8,7 +8,7 @@ func Step(name string) *step {
TStep: &TStep{ TStep: &TStep{
Name: name, Name: name,
Request: &TRequest{}, Request: &TRequest{},
Variables: make(Variables), Variables: make(map[string]interface{}),
}, },
} }
} }
@@ -23,7 +23,7 @@ func (s *step) WithRunner(runner *Runner) *step {
return s return s
} }
func (s *step) WithVariables(variables Variables) *step { func (s *step) WithVariables(variables map[string]interface{}) *step {
s.TStep.Variables = variables s.TStep.Variables = variables
return s return s
} }
@@ -136,17 +136,17 @@ func (s *requestWithOptionalArgs) SetAuth(auth map[string]string) *requestWithOp
return s return s
} }
func (s *requestWithOptionalArgs) WithParams(params Params) *requestWithOptionalArgs { func (s *requestWithOptionalArgs) WithParams(params map[string]interface{}) *requestWithOptionalArgs {
s.step.Request.Params = params s.step.Request.Params = params
return s return s
} }
func (s *requestWithOptionalArgs) WithHeaders(headers Headers) *requestWithOptionalArgs { func (s *requestWithOptionalArgs) WithHeaders(headers map[string]string) *requestWithOptionalArgs {
s.step.Request.Headers = headers s.step.Request.Headers = headers
return s return s
} }
func (s *requestWithOptionalArgs) WithCookies(cookies Cookies) *requestWithOptionalArgs { func (s *requestWithOptionalArgs) WithCookies(cookies map[string]string) *requestWithOptionalArgs {
s.step.Request.Cookies = cookies s.step.Request.Cookies = cookies
return s return s
} }

View File

@@ -7,17 +7,17 @@ import (
var ( var (
stepGET = Step("get with params"). stepGET = Step("get with params").
GET("https://postman-echo.com/get"). GET("https://postman-echo.com/get").
WithParams(Params{"foo1": "bar1", "foo2": "bar2"}). WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(Headers{"User-Agent": "HttpBoomer"}). WithHeaders(map[string]string{"User-Agent": "HttpBoomer"}).
WithCookies(Cookies{"user": "debugtalk"}). WithCookies(map[string]string{"user": "debugtalk"}).
Validate(). Validate().
AssertEqual("status_code", 200, "check status code") AssertEqual("status_code", 200, "check status code")
stepPOSTData = Step("post form data"). stepPOSTData = Step("post form data").
POST("https://postman-echo.com/post"). POST("https://postman-echo.com/post").
WithParams(Params{"foo1": "bar1", "foo2": "bar2"}). WithParams(map[string]interface{}{"foo1": "bar1", "foo2": "bar2"}).
WithHeaders(Headers{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}). WithHeaders(map[string]string{"User-Agent": "HttpBoomer", "Content-Type": "application/x-www-form-urlencoded"}).
WithData("a=1&b=2"). WithData("a=1&b=2").
WithCookies(Cookies{"user": "debugtalk"}). WithCookies(map[string]string{"user": "debugtalk"}).
Validate(). Validate().
AssertEqual("status_code", 200, "check status code") AssertEqual("status_code", 200, "check status code")
) )