mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-10 23:12:41 +08:00
feat: load from curl text files
This commit is contained in:
@@ -29,6 +29,8 @@ var convertCmd = &cobra.Command{
|
|||||||
fromType = convert.FromTypePostman
|
fromType = convert.FromTypePostman
|
||||||
} else if fromHARFlag {
|
} else if fromHARFlag {
|
||||||
fromType = convert.FromTypeHAR
|
fromType = convert.FromTypeHAR
|
||||||
|
} else if fromCurlFlag {
|
||||||
|
fromType = convert.FromTypeCurl
|
||||||
} else {
|
} else {
|
||||||
fromType = convert.FromTypeJSON
|
fromType = convert.FromTypeJSON
|
||||||
log.Info().Str("fromType", fromType.String()).Msg("set default")
|
log.Info().Str("fromType", fromType.String()).Msg("set default")
|
||||||
@@ -74,6 +76,7 @@ var (
|
|||||||
fromYAMLFlag bool
|
fromYAMLFlag bool
|
||||||
fromPostmanFlag bool
|
fromPostmanFlag bool
|
||||||
fromHARFlag bool
|
fromHARFlag bool
|
||||||
|
fromCurlFlag bool
|
||||||
|
|
||||||
toJSONFlag bool
|
toJSONFlag bool
|
||||||
toYAMLFlag bool
|
toYAMLFlag bool
|
||||||
@@ -87,6 +90,7 @@ func init() {
|
|||||||
convertCmd.Flags().BoolVar(&fromYAMLFlag, "from-yaml", false, "load from yaml case format")
|
convertCmd.Flags().BoolVar(&fromYAMLFlag, "from-yaml", false, "load from yaml case format")
|
||||||
convertCmd.Flags().BoolVar(&fromHARFlag, "from-har", false, "load from HAR format")
|
convertCmd.Flags().BoolVar(&fromHARFlag, "from-har", false, "load from HAR format")
|
||||||
convertCmd.Flags().BoolVar(&fromPostmanFlag, "from-postman", false, "load from postman format")
|
convertCmd.Flags().BoolVar(&fromPostmanFlag, "from-postman", false, "load from postman format")
|
||||||
|
convertCmd.Flags().BoolVar(&fromCurlFlag, "from-curl", false, "load from curl format")
|
||||||
|
|
||||||
convertCmd.Flags().BoolVar(&toJSONFlag, "to-json", true, "convert to JSON case scripts")
|
convertCmd.Flags().BoolVar(&toJSONFlag, "to-json", true, "convert to JSON case scripts")
|
||||||
convertCmd.Flags().BoolVar(&toYAMLFlag, "to-yaml", false, "convert to YAML case scripts")
|
convertCmd.Flags().BoolVar(&toYAMLFlag, "to-yaml", false, "convert to YAML case scripts")
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package builtin
|
package builtin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
"bytes"
|
||||||
"crypto/hmac"
|
"crypto/hmac"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
@@ -352,40 +351,6 @@ func ReadFile(path string) ([]byte, error) {
|
|||||||
return file, nil
|
return file, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func ReadCmdLines(path string) ([]string, error) {
|
|
||||||
var err error
|
|
||||||
path, err = filepath.Abs(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Str("path", path).Msg("convert absolute path failed")
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
log.Error().Err(err).Str("path", path).Msg("open file failed")
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
var line string
|
|
||||||
var lines []string
|
|
||||||
scanner := bufio.NewScanner(file)
|
|
||||||
// FIXME: resize scanner's capacity for lines over 64K
|
|
||||||
for scanner.Scan() {
|
|
||||||
text := strings.TrimSpace(scanner.Text())
|
|
||||||
if text == "" || text == "\n" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if strings.HasSuffix(text, "\\") {
|
|
||||||
line = line + strings.Trim(text, "\\")
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
line = line + text
|
|
||||||
lines = append(lines, line)
|
|
||||||
line = ""
|
|
||||||
}
|
|
||||||
return lines, scanner.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetFileNameWithoutExtension(path string) string {
|
func GetFileNameWithoutExtension(path string) string {
|
||||||
base := filepath.Base(path)
|
base := filepath.Base(path)
|
||||||
ext := filepath.Ext(base)
|
ext := filepath.Ext(base)
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
package convert
|
package convert
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/google/shlex"
|
"github.com/google/shlex"
|
||||||
@@ -11,7 +13,6 @@ import (
|
|||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp"
|
"github.com/httprunner/httprunner/v4/hrp"
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
|
||||||
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
"github.com/httprunner/httprunner/v4/hrp/internal/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -94,12 +95,14 @@ func init() {
|
|||||||
|
|
||||||
// LoadCurlCase loads testcase from one or more curl commands in .txt file
|
// LoadCurlCase loads testcase from one or more curl commands in .txt file
|
||||||
func LoadCurlCase(path string) (*hrp.TCase, error) {
|
func LoadCurlCase(path string) (*hrp.TCase, error) {
|
||||||
cmds, err := builtin.ReadCmdLines(path)
|
cmds, err := readFileLines(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tCase := &hrp.TCase{
|
tCase := &hrp.TCase{
|
||||||
Config: &hrp.TConfig{Name: "testcase converted from curl command"},
|
Config: &hrp.TConfig{
|
||||||
|
Name: "testcase converted from curl command",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range cmds {
|
||||||
tSteps, err := LoadCurlSteps(cmd)
|
tSteps, err := LoadCurlSteps(cmd)
|
||||||
@@ -115,21 +118,24 @@ func LoadCurlCase(path string) (*hrp.TCase, error) {
|
|||||||
return tCase, nil
|
return tCase, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadSingleCurlCase one testcase from one curl command
|
func readFileLines(path string) ([]string, error) {
|
||||||
func LoadSingleCurlCase(cmd string) (*hrp.TCase, error) {
|
file, err := os.Open(path)
|
||||||
tSteps, err := LoadCurlSteps(cmd)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Error().Err(err).Str("path", path).Msg("open file failed")
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tCase := &hrp.TCase{
|
defer file.Close()
|
||||||
Config: &hrp.TConfig{Name: "testcase converted from curl command"},
|
|
||||||
TestSteps: tSteps,
|
var lines []string
|
||||||
|
scanner := bufio.NewScanner(file)
|
||||||
|
for scanner.Scan() {
|
||||||
|
line := strings.TrimSpace(scanner.Text())
|
||||||
|
if line == "" || line == "\n" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
lines = append(lines, line)
|
||||||
}
|
}
|
||||||
err = tCase.MakeCompat()
|
return lines, scanner.Err()
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return tCase, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadCurlSteps loads one teststep from one curl command
|
// LoadCurlSteps loads one teststep from one curl command
|
||||||
|
|||||||
Reference in New Issue
Block a user