mirror of
https://github.com/httprunner/httprunner.git
synced 2026-09-06 16:07:45 +08:00
refactor: merge TestCase with TCase, fix converters
This commit is contained in:
@@ -94,12 +94,12 @@ 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.TestCase, error) {
|
||||||
cmds, err := readFileLines(path)
|
cmds, err := readFileLines(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tCase := &hrp.TCase{
|
tCase := &hrp.TestCase{
|
||||||
Config: &hrp.TConfig{
|
Config: &hrp.TConfig{
|
||||||
Name: "testcase converted from curl command",
|
Name: "testcase converted from curl command",
|
||||||
},
|
},
|
||||||
@@ -109,7 +109,7 @@ func LoadCurlCase(path string) (*hrp.TCase, error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tCase.TestSteps = append(tCase.TestSteps, tSteps...)
|
tCase.TSteps = append(tCase.TSteps, tSteps...)
|
||||||
}
|
}
|
||||||
err = tCase.MakeCompat()
|
err = tCase.MakeCompat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -13,29 +13,29 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
if !assert.NoError(t, err) {
|
if !assert.NoError(t, err) {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, 6, len(tCase.TestSteps)) {
|
if !assert.Equal(t, 6, len(tCase.TSteps)) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// curl httpbin.org
|
// curl httpbin.org
|
||||||
if !assert.Equal(t, "curl httpbin.org", tCase.TestSteps[0].Name) {
|
if !assert.Equal(t, "curl httpbin.org", tCase.TSteps[0].Name) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.EqualValues(t, "GET", tCase.TestSteps[0].Request.Method) {
|
if !assert.EqualValues(t, "GET", tCase.TSteps[0].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "http://httpbin.org", tCase.TestSteps[0].Request.URL) {
|
if !assert.Equal(t, "http://httpbin.org", tCase.TSteps[0].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// curl https://httpbin.org/get?key1=value1&key2=value2
|
// curl https://httpbin.org/get?key1=value1&key2=value2
|
||||||
if !assert.Equal(t, "https://httpbin.org/get", tCase.TestSteps[1].Request.URL) {
|
if !assert.Equal(t, "https://httpbin.org/get", tCase.TSteps[1].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]interface{}{
|
if !assert.Equal(t, map[string]interface{}{
|
||||||
"key1": "value1",
|
"key1": "value1",
|
||||||
"key2": "value2",
|
"key2": "value2",
|
||||||
}, tCase.TestSteps[1].Request.Params) {
|
}, tCase.TSteps[1].Request.Params) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,13 +43,13 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
// -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" \
|
// -H "Authorization: Bearer b7d03a6947b217efb6f3ec3bd3504582" \
|
||||||
// -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' \
|
// -d '{"type":"A","name":"www","data":"162.10.66.0","priority":null,"port":null,"weight":null}' \
|
||||||
// "https://httpbin.org/post"
|
// "https://httpbin.org/post"
|
||||||
if !assert.EqualValues(t, "POST", tCase.TestSteps[2].Request.Method) {
|
if !assert.EqualValues(t, "POST", tCase.TSteps[2].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"Authorization": "Bearer b7d03a6947b217efb6f3ec3bd3504582",
|
"Authorization": "Bearer b7d03a6947b217efb6f3ec3bd3504582",
|
||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
}, tCase.TestSteps[2].Request.Headers) {
|
}, tCase.TSteps[2].Request.Headers) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]interface{}{
|
if !assert.Equal(t, map[string]interface{}{
|
||||||
@@ -59,7 +59,7 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
"priority": nil,
|
"priority": nil,
|
||||||
"type": "A",
|
"type": "A",
|
||||||
"weight": nil,
|
"weight": nil,
|
||||||
}, tCase.TestSteps[2].Request.Body) {
|
}, tCase.TSteps[2].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
"dummyName": "dummyFile",
|
"dummyName": "dummyFile",
|
||||||
"file1": "@file1.txt",
|
"file1": "@file1.txt",
|
||||||
"file2": "@file2.txt",
|
"file2": "@file2.txt",
|
||||||
}, tCase.TestSteps[3].Request.Upload) {
|
}, tCase.TSteps[3].Request.Upload) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
"shipment[is_return]": "true",
|
"shipment[is_return]": "true",
|
||||||
"shipment[parcel][id]": "prcl_WDv2VzHp",
|
"shipment[parcel][id]": "prcl_WDv2VzHp",
|
||||||
"shipment[to_address][id]": "adr_HrBKVA85",
|
"shipment[to_address][id]": "adr_HrBKVA85",
|
||||||
}, tCase.TestSteps[4].Request.Body) {
|
}, tCase.TSteps[4].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -92,13 +92,13 @@ func TestLoadCurlCase(t *testing.T) {
|
|||||||
// --data "key1=value+1&key2=value%3A2"
|
// --data "key1=value+1&key2=value%3A2"
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}, tCase.TestSteps[5].Request.Headers) {
|
}, tCase.TSteps[5].Request.Headers) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]interface{}{
|
if !assert.Equal(t, map[string]interface{}{
|
||||||
"key1": "value 1",
|
"key1": "value 1",
|
||||||
"key2": "value:2",
|
"key2": "value:2",
|
||||||
}, tCase.TestSteps[5].Request.Body) {
|
}, tCase.TSteps[5].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -358,15 +358,15 @@ type TestResult struct {
|
|||||||
|
|
||||||
// ==================== model definition ends here ====================
|
// ==================== model definition ends here ====================
|
||||||
|
|
||||||
func LoadHARCase(path string) (*hrp.TCase, error) {
|
func LoadHARCase(path string) (*hrp.TestCase, error) {
|
||||||
// load har file
|
// load har file
|
||||||
caseHAR, err := loadCaseHAR(path)
|
caseHAR, err := loadCaseHAR(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert to TCase format
|
// convert to TestCase format
|
||||||
return caseHAR.ToTCase()
|
return caseHAR.ToTestCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCaseHAR(path string) (*CaseHar, error) {
|
func loadCaseHAR(path string) (*CaseHar, error) {
|
||||||
@@ -381,16 +381,16 @@ func loadCaseHAR(path string) (*CaseHar, error) {
|
|||||||
return caseHAR, nil
|
return caseHAR, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert CaseHar to TCase format
|
// convert CaseHar to TestCase format
|
||||||
func (c *CaseHar) ToTCase() (*hrp.TCase, error) {
|
func (c *CaseHar) ToTestCase() (*hrp.TestCase, error) {
|
||||||
teststeps, err := c.prepareTestSteps()
|
teststeps, err := c.prepareTestSteps()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
tCase := &hrp.TCase{
|
tCase := &hrp.TestCase{
|
||||||
Config: c.prepareConfig(),
|
Config: c.prepareConfig(),
|
||||||
TestSteps: teststeps,
|
TSteps: teststeps,
|
||||||
}
|
}
|
||||||
err = tCase.MakeCompat()
|
err = tCase.MakeCompat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -36,60 +36,60 @@ func TestLoadTCaseFromHAR(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// make request method
|
// make request method
|
||||||
if !assert.EqualValues(t, "GET", tCase.TestSteps[0].Request.Method) {
|
if !assert.EqualValues(t, "GET", tCase.TSteps[0].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.EqualValues(t, "POST", tCase.TestSteps[1].Request.Method) {
|
if !assert.EqualValues(t, "POST", tCase.TSteps[1].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make request url
|
// make request url
|
||||||
if !assert.Equal(t, "https://postman-echo.com/get", tCase.TestSteps[0].Request.URL) {
|
if !assert.Equal(t, "https://postman-echo.com/get", tCase.TSteps[0].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "https://postman-echo.com/post", tCase.TestSteps[1].Request.URL) {
|
if !assert.Equal(t, "https://postman-echo.com/post", tCase.TSteps[1].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make request params
|
// make request params
|
||||||
if !assert.Equal(t, "HDnY8", tCase.TestSteps[0].Request.Params["foo1"]) {
|
if !assert.Equal(t, "HDnY8", tCase.TSteps[0].Request.Params["foo1"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make request cookies
|
// make request cookies
|
||||||
if !assert.NotEmpty(t, tCase.TestSteps[1].Request.Cookies["sails.sid"]) {
|
if !assert.NotEmpty(t, tCase.TSteps[1].Request.Cookies["sails.sid"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make request headers
|
// make request headers
|
||||||
if !assert.Equal(t, "HttpRunnerPlus", tCase.TestSteps[0].Request.Headers["User-Agent"]) {
|
if !assert.Equal(t, "HttpRunnerPlus", tCase.TSteps[0].Request.Headers["User-Agent"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "postman-echo.com", tCase.TestSteps[0].Request.Headers["Host"]) {
|
if !assert.Equal(t, "postman-echo.com", tCase.TSteps[0].Request.Headers["Host"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make request data
|
// make request data
|
||||||
if !assert.Equal(t, nil, tCase.TestSteps[0].Request.Body) {
|
if !assert.Equal(t, nil, tCase.TSteps[0].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]interface{}{"foo1": "HDnY8", "foo2": 12.3}, tCase.TestSteps[1].Request.Body) {
|
if !assert.Equal(t, map[string]interface{}{"foo1": "HDnY8", "foo2": 12.3}, tCase.TSteps[1].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]string{"foo1": "HDnY8", "foo2": "12.3"}, tCase.TestSteps[2].Request.Body) {
|
if !assert.Equal(t, map[string]string{"foo1": "HDnY8", "foo2": "12.3"}, tCase.TSteps[2].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|
||||||
// make validators
|
// make validators
|
||||||
validator, ok := tCase.TestSteps[0].Validators[0].(hrp.Validator)
|
validator, ok := tCase.TSteps[0].Validators[0].(hrp.Validator)
|
||||||
if !ok || !assert.Equal(t, "status_code", validator.Check) {
|
if !ok || !assert.Equal(t, "status_code", validator.Check) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
validator, ok = tCase.TestSteps[0].Validators[1].(hrp.Validator)
|
validator, ok = tCase.TSteps[0].Validators[1].(hrp.Validator)
|
||||||
if !ok || !assert.Equal(t, "headers.\"Content-Type\"", validator.Check) {
|
if !ok || !assert.Equal(t, "headers.\"Content-Type\"", validator.Check) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
validator, ok = tCase.TestSteps[0].Validators[2].(hrp.Validator)
|
validator, ok = tCase.TSteps[0].Validators[2].(hrp.Validator)
|
||||||
if !ok || !assert.Equal(t, "body.url", validator.Check) {
|
if !ok || !assert.Equal(t, "body.url", validator.Check) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,15 +8,15 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadJSONCase(path string) (*hrp.TCase, error) {
|
func LoadJSONCase(path string) (*hrp.TestCase, error) {
|
||||||
log.Info().Str("path", path).Msg("load json case file")
|
log.Info().Str("path", path).Msg("load json case file")
|
||||||
caseJSON := new(hrp.TCase)
|
caseJSON := new(hrp.TestCase)
|
||||||
err := builtin.LoadFile(path, caseJSON)
|
err := builtin.LoadFile(path, caseJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "load json file failed")
|
return nil, errors.Wrap(err, "load json file failed")
|
||||||
}
|
}
|
||||||
|
|
||||||
if caseJSON.TestSteps == nil {
|
if caseJSON.TSteps == nil {
|
||||||
return nil, errors.New("invalid json case file, missing teststeps")
|
return nil, errors.New("invalid json case file, missing teststeps")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -112,15 +112,15 @@ var contentTypeMap = map[string]string{
|
|||||||
"xml": "application/xml",
|
"xml": "application/xml",
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadPostmanCase(path string) (*hrp.TCase, error) {
|
func LoadPostmanCase(path string) (*hrp.TestCase, error) {
|
||||||
log.Info().Str("path", path).Msg("load postman case file")
|
log.Info().Str("path", path).Msg("load postman case file")
|
||||||
casePostman, err := loadCasePostman(path)
|
casePostman, err := loadCasePostman(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// convert to TCase format
|
// convert to TestCase format
|
||||||
return casePostman.ToTCase()
|
return casePostman.ToTestCase()
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadCasePostman(path string) (*CasePostman, error) {
|
func loadCasePostman(path string) (*CasePostman, error) {
|
||||||
@@ -136,14 +136,14 @@ func loadCasePostman(path string) (*CasePostman, error) {
|
|||||||
return casePostman, nil
|
return casePostman, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *CasePostman) ToTCase() (*hrp.TCase, error) {
|
func (c *CasePostman) ToTestCase() (*hrp.TestCase, error) {
|
||||||
teststeps, err := c.prepareTestSteps()
|
teststeps, err := c.prepareTestSteps()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
tCase := &hrp.TCase{
|
tCase := &hrp.TestCase{
|
||||||
Config: c.prepareConfig(),
|
Config: c.prepareConfig(),
|
||||||
TestSteps: teststeps,
|
TSteps: teststeps,
|
||||||
}
|
}
|
||||||
err = tCase.MakeCompat()
|
err = tCase.MakeCompat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -28,51 +28,51 @@ func TestMakeTestCaseFromCollection(t *testing.T) {
|
|||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
// check method
|
// check method
|
||||||
if !assert.EqualValues(t, "GET", tCase.TestSteps[0].Request.Method) {
|
if !assert.EqualValues(t, "GET", tCase.TSteps[0].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.EqualValues(t, "POST", tCase.TestSteps[1].Request.Method) {
|
if !assert.EqualValues(t, "POST", tCase.TSteps[1].Request.Method) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
// check url
|
// check url
|
||||||
if !assert.Equal(t, "https://postman-echo.com/get", tCase.TestSteps[0].Request.URL) {
|
if !assert.Equal(t, "https://postman-echo.com/get", tCase.TSteps[0].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "https://postman-echo.com/post", tCase.TestSteps[1].Request.URL) {
|
if !assert.Equal(t, "https://postman-echo.com/post", tCase.TSteps[1].Request.URL) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
// check params
|
// check params
|
||||||
if !assert.Equal(t, "v1", tCase.TestSteps[0].Request.Params["k1"]) {
|
if !assert.Equal(t, "v1", tCase.TSteps[0].Request.Params["k1"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
// check cookies (pass, postman collection doesn't contain cookies)
|
// check cookies (pass, postman collection doesn't contain cookies)
|
||||||
// check headers
|
// check headers
|
||||||
if !assert.Equal(t, "application/x-www-form-urlencoded", tCase.TestSteps[2].Request.Headers["Content-Type"]) {
|
if !assert.Equal(t, "application/x-www-form-urlencoded", tCase.TSteps[2].Request.Headers["Content-Type"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "application/json", tCase.TestSteps[3].Request.Headers["Content-Type"]) {
|
if !assert.Equal(t, "application/json", tCase.TSteps[3].Request.Headers["Content-Type"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "text/plain", tCase.TestSteps[4].Request.Headers["Content-Type"]) {
|
if !assert.Equal(t, "text/plain", tCase.TSteps[4].Request.Headers["Content-Type"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "HttpRunner", tCase.TestSteps[5].Request.Headers["User-Agent"]) {
|
if !assert.Equal(t, "HttpRunner", tCase.TSteps[5].Request.Headers["User-Agent"]) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
// check body
|
// check body
|
||||||
if !assert.Equal(t, nil, tCase.TestSteps[0].Request.Body) {
|
if !assert.Equal(t, nil, tCase.TSteps[0].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]string{"k1": "v1", "k2": "v2"}, tCase.TestSteps[2].Request.Body) {
|
if !assert.Equal(t, map[string]string{"k1": "v1", "k2": "v2"}, tCase.TSteps[2].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]interface{}{"k1": "v1", "k2": "v2"}, tCase.TestSteps[3].Request.Body) {
|
if !assert.Equal(t, map[string]interface{}{"k1": "v1", "k2": "v2"}, tCase.TSteps[3].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, "have a nice day", tCase.TestSteps[4].Request.Body) {
|
if !assert.Equal(t, "have a nice day", tCase.TSteps[4].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, nil, tCase.TestSteps[5].Request.Body) {
|
if !assert.Equal(t, nil, tCase.TSteps[5].Request.Body) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadSwaggerCase(path string) (*hrp.TCase, error) {
|
func LoadSwaggerCase(path string) (*hrp.TestCase, error) {
|
||||||
// load swagger file
|
// load swagger file
|
||||||
caseSwagger := new(spec.Swagger)
|
caseSwagger := new(spec.Swagger)
|
||||||
err := builtin.LoadFile(path, caseSwagger)
|
err := builtin.LoadFile(path, caseSwagger)
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
"github.com/httprunner/httprunner/v4/hrp/internal/builtin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func LoadYAMLCase(path string) (*hrp.TCase, error) {
|
func LoadYAMLCase(path string) (*hrp.TestCase, error) {
|
||||||
// load yaml case file
|
// load yaml case file
|
||||||
caseJSON := new(hrp.TCase)
|
caseJSON := new(hrp.TestCase)
|
||||||
err := builtin.LoadFile(path, caseJSON)
|
err := builtin.LoadFile(path, caseJSON)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "load yaml file failed")
|
return nil, errors.Wrap(err, "load yaml file failed")
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ type TCaseConverter struct {
|
|||||||
fromFile string
|
fromFile string
|
||||||
profilePath string
|
profilePath string
|
||||||
outputDir string
|
outputDir string
|
||||||
tCase *hrp.TCase
|
tCase *hrp.TestCase
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadCase loads source file and convert to TCase type
|
// LoadCase loads source file and convert to TCase type
|
||||||
@@ -206,7 +206,7 @@ func (c *TCaseConverter) overrideWithProfile(path string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
log.Info().Interface("profile", profile).Msg("override with profile")
|
log.Info().Interface("profile", profile).Msg("override with profile")
|
||||||
for _, step := range c.tCase.TestSteps {
|
for _, step := range c.tCase.TSteps {
|
||||||
// override original headers and cookies
|
// override original headers and cookies
|
||||||
if profile.Override {
|
if profile.Override {
|
||||||
step.Request.Headers = make(map[string]string)
|
step.Request.Headers = make(map[string]string)
|
||||||
|
|||||||
@@ -47,12 +47,12 @@ func TestLoadHARWithProfileOverride(t *testing.T) {
|
|||||||
for i := 0; i < 3; i++ {
|
for i := 0; i < 3; i++ {
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
|
map[string]string{"Content-Type": "application/x-www-form-urlencoded"},
|
||||||
converter.tCase.TestSteps[i].Request.Headers) {
|
converter.tCase.TSteps[i].Request.Headers) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t,
|
if !assert.Equal(t,
|
||||||
map[string]string{"UserName": "debugtalk"},
|
map[string]string{"UserName": "debugtalk"},
|
||||||
converter.tCase.TestSteps[i].Request.Cookies) {
|
converter.tCase.TSteps[i].Request.Cookies) {
|
||||||
t.FailNow()
|
t.FailNow()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -60,8 +60,8 @@ func TestLoadHARWithProfileOverride(t *testing.T) {
|
|||||||
|
|
||||||
func TestMakeRequestWithProfile(t *testing.T) {
|
func TestMakeRequestWithProfile(t *testing.T) {
|
||||||
caseConverter := &TCaseConverter{
|
caseConverter := &TCaseConverter{
|
||||||
tCase: &hrp.TCase{
|
tCase: &hrp.TestCase{
|
||||||
TestSteps: []*hrp.TStep{
|
TSteps: []*hrp.TStep{
|
||||||
{
|
{
|
||||||
Request: &hrp.Request{
|
Request: &hrp.Request{
|
||||||
Method: hrp.HTTPMethod("POST"),
|
Method: hrp.HTTPMethod("POST"),
|
||||||
@@ -89,20 +89,20 @@ func TestMakeRequestWithProfile(t *testing.T) {
|
|||||||
|
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"Content-Type": "application/x-www-form-urlencoded", "User-Agent": "hrp",
|
"Content-Type": "application/x-www-form-urlencoded", "User-Agent": "hrp",
|
||||||
}, caseConverter.tCase.TestSteps[0].Request.Headers) {
|
}, caseConverter.tCase.TSteps[0].Request.Headers) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"UserName": "debugtalk", "abc": "123",
|
"UserName": "debugtalk", "abc": "123",
|
||||||
}, caseConverter.tCase.TestSteps[0].Request.Cookies) {
|
}, caseConverter.tCase.TSteps[0].Request.Cookies) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMakeRequestWithProfileOverride(t *testing.T) {
|
func TestMakeRequestWithProfileOverride(t *testing.T) {
|
||||||
caseConverter := &TCaseConverter{
|
caseConverter := &TCaseConverter{
|
||||||
tCase: &hrp.TCase{
|
tCase: &hrp.TestCase{
|
||||||
TestSteps: []*hrp.TStep{
|
TSteps: []*hrp.TStep{
|
||||||
{
|
{
|
||||||
Request: &hrp.Request{
|
Request: &hrp.Request{
|
||||||
Method: hrp.HTTPMethod("POST"),
|
Method: hrp.HTTPMethod("POST"),
|
||||||
@@ -131,12 +131,12 @@ func TestMakeRequestWithProfileOverride(t *testing.T) {
|
|||||||
|
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"Content-Type": "application/x-www-form-urlencoded",
|
"Content-Type": "application/x-www-form-urlencoded",
|
||||||
}, caseConverter.tCase.TestSteps[0].Request.Headers) {
|
}, caseConverter.tCase.TSteps[0].Request.Headers) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
if !assert.Equal(t, map[string]string{
|
if !assert.Equal(t, map[string]string{
|
||||||
"UserName": "debugtalk",
|
"UserName": "debugtalk",
|
||||||
}, caseConverter.tCase.TestSteps[0].Request.Cookies) {
|
}, caseConverter.tCase.TSteps[0].Request.Cookies) {
|
||||||
t.Fatal()
|
t.Fatal()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -49,6 +49,36 @@ func (tc *TestCase) loadStruct() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MakeCompat converts TestCase compatible with Golang engine style
|
||||||
|
func (tc *TestCase) MakeCompat() (err error) {
|
||||||
|
defer func() {
|
||||||
|
if p := recover(); p != nil {
|
||||||
|
err = fmt.Errorf("[MakeCompat] convert compat testcase error: %v", p)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
for _, step := range tc.TSteps {
|
||||||
|
// 1. deal with request body compatibility
|
||||||
|
convertCompatRequestBody(step.Request)
|
||||||
|
|
||||||
|
// 2. deal with validators compatibility
|
||||||
|
err = convertCompatValidator(step.Validators)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. deal with extract expr including hyphen
|
||||||
|
convertExtract(step.Extract)
|
||||||
|
|
||||||
|
// 4. deal with mobile step compatibility
|
||||||
|
if step.Android != nil {
|
||||||
|
convertCompatMobileStep(step.Android)
|
||||||
|
} else if step.IOS != nil {
|
||||||
|
convertCompatMobileStep(step.IOS)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (tc *TestCase) Dump2JSON(targetPath string) error {
|
func (tc *TestCase) Dump2JSON(targetPath string) error {
|
||||||
tc.loadStruct()
|
tc.loadStruct()
|
||||||
err := builtin.Dump2JSON(tc, targetPath)
|
err := builtin.Dump2JSON(tc, targetPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user