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 10:44:19 +08:00
parent d3b3b80d17
commit a5f7eea9c3
16 changed files with 190 additions and 1136 deletions

View File

@@ -6,7 +6,6 @@ import (
"encoding/csv"
"encoding/hex"
builtinJSON "encoding/json"
"errors"
"fmt"
"math"
"math/rand"
@@ -17,6 +16,7 @@ import (
"strings"
"time"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
"gopkg.in/yaml.v3"
@@ -235,13 +235,12 @@ func LoadFile(path string, structObj interface{}) (err error) {
log.Info().Str("path", path).Msg("load file")
file, err := readFile(path)
if err != nil {
log.Error().Err(err).Msg("read file failed")
return err
return errors.Wrap(err, "read file failed")
}
ext := filepath.Ext(path)
switch ext {
case ".json":
case ".json", ".har":
decoder := json.NewDecoder(bytes.NewReader(file))
decoder.UseNumber()
err = decoder.Decode(structObj)

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

View File

@@ -9,8 +9,9 @@ import (
)
var (
harPath = "../../../examples/hrp/har/demo.har"
harPath2 = "../../../examples/hrp/har/postman-echo.har"
harPath = "../../../examples/data/har/demo.har"
harPath2 = "../../../examples/data/har/postman-echo.har"
profilePath = "../../../examples/data/har/profile.yml"
)
func TestGenJSON(t *testing.T) {
@@ -47,6 +48,26 @@ func TestLoadHAR(t *testing.T) {
}
}
func TestLoadHARWithProfile(t *testing.T) {
har := NewHAR(harPath)
har.SetProfile(profilePath)
_, err := har.load()
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t,
map[string]interface{}{"Content-Type": "application/x-www-form-urlencoded"},
har.profileJSON["headers"]) {
t.Fail()
}
if !assert.Equal(t,
map[string]interface{}{"UserName": "debugtalk"},
har.profileJSON["cookies"]) {
t.Fail()
}
}
func TestMakeTestCase(t *testing.T) {
har := NewHAR(harPath)
tCase, err := har.makeTestCase()
@@ -115,12 +136,105 @@ func TestMakeTestCase(t *testing.T) {
}
func TestGetFilenameWithoutExtension(t *testing.T) {
filename := getFilenameWithoutExtension("../../../examples/hrp/har/postman-echo.har")
filename := getFilenameWithoutExtension(harPath2)
if !assert.Equal(t, "postman-echo", filename) {
t.Fail()
}
}
func TestMakeRequestHeaders(t *testing.T) {
har := NewHAR("")
entry := &Entry{
Request: Request{
Method: "POST",
Headers: []NVP{
{Name: "Content-Type", Value: "application/json; charset=utf-8"},
},
},
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t, map[string]string{
"Content-Type": "application/json; charset=utf-8",
}, step.Request.Headers) {
t.Fail()
}
}
func TestMakeRequestHeadersWithProfile(t *testing.T) {
har := NewHAR("")
har.SetProfile(profilePath)
entry := &Entry{
Request: Request{
Method: "POST",
Headers: []NVP{
{Name: "Content-Type", Value: "application/json; charset=utf-8"},
},
},
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t, map[string]string{
"Content-Type": "application/x-www-form-urlencoded",
}, step.Request.Headers) {
t.Fail()
}
}
func TestMakeRequestCookies(t *testing.T) {
har := NewHAR("")
entry := &Entry{
Request: Request{
Method: "POST",
Cookies: []Cookie{
{Name: "abc", Value: "123"},
{Name: "UserName", Value: "leolee"},
},
},
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t, map[string]string{
"abc": "123",
"UserName": "leolee",
}, step.Request.Cookies) {
t.Fail()
}
}
func TestMakeRequestCookiesWithProfile(t *testing.T) {
har := NewHAR("")
har.SetProfile(profilePath)
entry := &Entry{
Request: Request{
Method: "POST",
Cookies: []Cookie{
{Name: "abc", Value: "123"},
{Name: "UserName", Value: "leolee"},
},
},
}
step, err := har.prepareTestStep(entry)
if !assert.NoError(t, err) {
t.Fail()
}
if !assert.Equal(t, map[string]string{
"UserName": "debugtalk",
}, step.Request.Cookies) {
t.Fail()
}
}
func TestMakeRequestDataParams(t *testing.T) {
har := NewHAR("")
entry := &Entry{