feat: add --profile flag for har2case to support overwrite headers/cookies with specified yaml/json profile file

This commit is contained in:
debugtalk
2022-03-26 11:01:21 +08:00
parent 1a93f0acb0
commit da0bec1f77
16 changed files with 190 additions and 1136 deletions
+50 -20
View File
@@ -3,9 +3,7 @@ package har2case
import (
"encoding/base64"
"fmt"
"io"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
@@ -31,13 +29,25 @@ func NewHAR(path string) *har {
}
type har struct {
path string
filterStr string
excludeStr string
outputDir string
path string
filterStr string
excludeStr string
profileJSON map[string]interface{}
outputDir string
}
func (h *har) SetProfile(path string) {
log.Info().Str("path", path).Msg("set profile")
h.profileJSON = make(map[string]interface{})
err := builtin.LoadFile(path, h.profileJSON)
if err != nil {
log.Warn().Str("path", path).
Msg("invalid profile format, ignore!")
}
}
func (h *har) SetOutputDir(dir string) {
log.Info().Str("dir", dir).Msg("set output directory")
h.outputDir = dir
}
@@ -93,23 +103,11 @@ func (h *har) makeTestCase() (*hrp.TCase, error) {
}
func (h *har) load() (*Har, error) {
fp, err := os.Open(h.path)
if err != nil {
return nil, fmt.Errorf("open: %w", err)
}
data, err := io.ReadAll(fp)
fp.Close()
if err != nil {
return nil, fmt.Errorf("read: %w", err)
}
har := &Har{}
err = json.Unmarshal(data, har)
err := builtin.LoadFile(h.path, har)
if err != nil {
return nil, fmt.Errorf("json.Unmarshal error: %w", err)
return nil, errors.Wrap(err, "load har failed")
}
return har, nil
}
@@ -147,6 +145,7 @@ func (h *har) prepareTestStep(entry *Entry) (*hrp.TStep, error) {
Request: &hrp.Request{},
Validators: make([]interface{}, 0),
},
profileJSON: h.profileJSON,
}
if err := step.makeRequestMethod(entry); err != nil {
return nil, err
@@ -174,6 +173,7 @@ func (h *har) prepareTestStep(entry *Entry) (*hrp.TStep, error) {
type tStep struct {
hrp.TStep
profileJSON map[string]interface{}
}
func (s *tStep) makeRequestMethod(entry *Entry) error {
@@ -201,6 +201,21 @@ func (s *tStep) makeRequestParams(entry *Entry) error {
func (s *tStep) makeRequestCookies(entry *Entry) error {
s.Request.Cookies = make(map[string]string)
cookies, ok := s.profileJSON["cookies"]
if ok {
// use cookies from profile
cookies, ok := cookies.(map[string]interface{})
if ok {
for k, v := range cookies {
s.Request.Cookies[k] = fmt.Sprintf("%v", v)
}
return nil
}
log.Warn().Interface("cookies", cookies).
Msg("cookies from profile is not a map, ignore!")
}
// use cookies from har
for _, cookie := range entry.Request.Cookies {
s.Request.Cookies[cookie.Name] = cookie.Value
}
@@ -209,6 +224,21 @@ func (s *tStep) makeRequestCookies(entry *Entry) error {
func (s *tStep) makeRequestHeaders(entry *Entry) error {
s.Request.Headers = make(map[string]string)
headers, ok := s.profileJSON["headers"]
if ok {
// use headers from profile
cookies, ok := headers.(map[string]interface{})
if ok {
for k, v := range cookies {
s.Request.Headers[k] = fmt.Sprintf("%v", v)
}
return nil
}
log.Warn().Interface("headers", headers).
Msg("headers from profile is not a map, ignore!")
}
// use headers from har
for _, header := range entry.Request.Headers {
if strings.EqualFold(header.Name, "cookie") {
continue