http_mocking插件新增header

jsonSchema新增测试用例
This commit is contained in:
zhangzeyi
2023-03-01 14:20:08 +08:00
parent 9b92d8db49
commit 2cfa420941
7 changed files with 1409 additions and 50 deletions

View File

@@ -13,10 +13,11 @@ type complete struct {
contentType string contentType string
responseExample string responseExample string
responseSchema map[string]interface{} responseSchema map[string]interface{}
responseHeader map[string]string
} }
func NewComplete(responseStatus int, contentType string, responseExample string, responseSchema map[string]interface{}) *complete { func NewComplete(responseStatus int, contentType string, responseExample string, responseSchema map[string]interface{}, responseHeader map[string]string) *complete {
return &complete{responseStatus: responseStatus, contentType: contentType, responseExample: responseExample, responseSchema: responseSchema} return &complete{responseStatus: responseStatus, contentType: contentType, responseExample: responseExample, responseSchema: responseSchema, responseHeader: responseHeader}
} }
func (c *complete) Complete(org eocontext.EoContext) error { func (c *complete) Complete(org eocontext.EoContext) error {
@@ -31,6 +32,10 @@ func (c *complete) writeHttp(ctx http_context.IHttpContext) error {
ctx.Response().SetHeader("Content-Type", c.contentType) ctx.Response().SetHeader("Content-Type", c.contentType)
ctx.Response().SetStatus(c.responseStatus, "") ctx.Response().SetStatus(c.responseStatus, "")
for k, v := range c.responseHeader {
ctx.Response().SetHeader(k, v)
}
if c.responseExample != "" { if c.responseExample != "" {
ctx.Response().SetBody([]byte(c.responseExample)) ctx.Response().SetBody([]byte(c.responseExample))
return nil return nil

View File

@@ -1,10 +1,11 @@
package http_mocking package http_mocking
type Config struct { type Config struct {
ResponseStatus int `json:"response_status" default:"200" label:"返回响应的 HTTP 状态码仅http路由有效"` ResponseStatus int `json:"response_status" default:"200" label:"返回响应的 HTTP 状态码仅http路由有效"`
ContentType string `json:"content_type" label:"返回响应的 Header Content-Type" enum:"application/json"` ContentType string `json:"content_type" label:"返回响应的 Header Content-Type" enum:"application/json"`
ResponseExample string `json:"response_example" switch:"content_type==='application/json'" label:"返回响应的Body与jsonschema字段二选一"` ResponseExample string `json:"response_example" format:"text" label:"返回响应的Body与jsonschema字段二选一"`
ResponseSchema string `json:"response_schema" switch:"content_type==='application/json'" label:"指定响应的jsonschema对象"` ResponseSchema string `json:"response_schema" format:"text" label:"指定响应的jsonschema对象"`
ResponseHeader map[string]string `json:"response_header" label:"响应头"`
} }
const ( const (

View File

@@ -68,7 +68,7 @@ func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWork
return &Mocking{ return &Mocking{
WorkerBase: drivers.Worker(id, name), WorkerBase: drivers.Worker(id, name),
handler: NewComplete(conf.ResponseStatus, conf.ContentType, conf.ResponseExample, jsonSchema), handler: NewComplete(conf.ResponseStatus, conf.ContentType, conf.ResponseExample, jsonSchema, conf.ResponseHeader),
}, nil }, nil
} }

View File

@@ -18,6 +18,7 @@ type Mocking struct {
contentType string contentType string
responseExample string responseExample string
responseSchema string responseSchema string
responseHeader map[string]string
handler eocontext.CompleteHandler handler eocontext.CompleteHandler
} }
@@ -54,6 +55,7 @@ func (m *Mocking) Reset(v interface{}, workers map[eosc.RequireId]eosc.IWorker)
m.responseExample = conf.ResponseExample m.responseExample = conf.ResponseExample
m.contentType = conf.ContentType m.contentType = conf.ContentType
m.responseStatus = conf.ResponseStatus m.responseStatus = conf.ResponseStatus
m.responseHeader = conf.ResponseHeader
jsonSchema := make(map[string]interface{}) jsonSchema := make(map[string]interface{})
@@ -64,7 +66,7 @@ func (m *Mocking) Reset(v interface{}, workers map[eosc.RequireId]eosc.IWorker)
} }
} }
m.handler = NewComplete(m.responseStatus, m.contentType, m.responseExample, jsonSchema) m.handler = NewComplete(m.responseStatus, m.contentType, m.responseExample, jsonSchema, m.responseHeader)
return nil return nil
} }

View File

@@ -1,14 +1,8 @@
package utils package utils
import ( import (
"fmt"
"testing" "testing"
) )
func TestAes(t *testing.T) { func TestAes(t *testing.T) {
key := Md5("open-api")
enValue := AES_CBC_Encrypt([]byte(Md5("Key123qaz:open-api")), []byte(key))
deValue := AES_CBC_Decrypt(enValue, []byte(key))
log.Debug(enValue)
log.Debug(string(deValue))
} }

View File

@@ -7,6 +7,7 @@ import (
"github.com/brianvoe/gofakeit/v6" "github.com/brianvoe/gofakeit/v6"
"math/rand" "math/rand"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
@@ -57,36 +58,7 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
rule, ruleOk := m["rule"].(map[string]interface{}) rule, ruleOk := m["rule"].(map[string]interface{})
if !ruleOk || len(rule) == 0 { if !ruleOk || len(rule) == 0 {
if template != nil { if template != nil {
if templateStr, ok := template.(string); ok { resultMap[name] = mockConstant(template)
switch templateStr {
case "@cname":
resultMap[name] = gofakeit.Username()
case "@cfirst":
resultMap[name] = gofakeit.FirstName()
case "@clast":
resultMap[name] = gofakeit.LastName()
case "@name", "@name(true)":
resultMap[name] = gofakeit.Name()
case "@first":
resultMap[name] = gofakeit.FirstName()
case "@last":
resultMap[name] = gofakeit.LastName()
case "@email":
resultMap[name] = gofakeit.Email()
case "@ip":
resultMap[name] = gofakeit.IPv4Address()
case "@zip":
resultMap[name] = gofakeit.Address().Zip
case "@city", "@city(true)":
resultMap[name] = gofakeit.Address().Address
case "@url":
resultMap[name] = gofakeit.URL()
default:
resultMap[name] = template
}
continue
}
resultMap[name] = template
continue continue
} }
} }
@@ -146,7 +118,7 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
floats := RandFloats(dminVal, dmaxVal) floats := RandFloats(dminVal, dmaxVal)
randomValue, _ = strconv.ParseFloat(strconv.FormatFloat(randomValue, 'f', int(floats), 64), 64) randomValue, _ = strconv.ParseFloat(strconv.FormatFloat(randomValue, 'f', int(floats), 64), 64)
} else { } else {
randomValue, _ = strconv.ParseFloat(strconv.FormatFloat(randomValue, 'f', 0, 64), 64)
} }
resultMap[name] = randomValue resultMap[name] = randomValue
@@ -170,8 +142,11 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
} }
tempMap := make(map[string]interface{}) tempMap := make(map[string]interface{})
i := 1 i := 1
for k, v := range templateMap { for key, val := range templateMap {
tempMap[k] = v
split := strings.Split(key, "|")
tempMap[split[0]] = mockConstant(val)
if i == randomNum { if i == randomNum {
break break
} }
@@ -190,17 +165,37 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
} }
randomNum := 0 randomNum := 0
if minVal > 0 && maxVal == 0 { if minVal > 0.0 && maxVal == 0.0 {
randomNum = int(minVal) randomNum = int(minVal)
} }
if minVal > 0 && maxVal > 0 { if minVal > 0.0 && maxVal > 0.0 {
randomNum = int(RandInt64(int64(minVal), int64(maxVal))) randomNum = int(RandInt64(int64(minVal), int64(maxVal)))
} }
if randomNum == 1 { //随机选取一个
resultMap[name] = templateList[rand.Intn(len(templateList))]
continue
}
tempList := make([]interface{}, 0) tempList := make([]interface{}, 0)
for i := 0; i < randomNum; i++ { for i := 0; i < randomNum; i++ {
tempList = append(tempList, templateList[rand.Intn(len(templateList))])
for _, templateType := range templateList {
switch templateVal := templateType.(type) {
case map[string]interface{}:
tempMap := make(map[string]interface{})
for key, val := range templateVal {
split := strings.Split(key, "|")
tempMap[split[0]] = mockConstant(val)
}
tempList = append(tempList, tempMap)
default:
tempList = append(tempList, templateType)
}
}
} }
resultMap[name] = tempList resultMap[name] = tempList
@@ -216,6 +211,39 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
return jsonSchemaUnmarshal(value) return jsonSchemaUnmarshal(value)
} }
func mockConstant(v interface{}) interface{} {
if templateStr, ok := v.(string); ok {
templateStr = strings.ToLower(templateStr)
switch templateStr {
case "@cname":
return gofakeit.Username()
case "@cfirst":
return gofakeit.FirstName()
case "@clast":
return gofakeit.LastName()
case "@name", "@name(true)":
return gofakeit.Name()
case "@first":
return gofakeit.FirstName()
case "@last":
return gofakeit.LastName()
case "@email":
return gofakeit.Email()
case "@ip":
return gofakeit.IPv4Address()
case "@zip":
return gofakeit.Address().Zip
case "@city", "@city(true)":
return gofakeit.Address().Address
case "@url":
return gofakeit.URL()
default:
return v
}
}
return v
}
var jsonSchemaFormat = errors.New("json schema format err") var jsonSchemaFormat = errors.New("json schema format err")
func getMinMaxDminDmax(rule map[string]interface{}) (float64, float64, float64, float64, error) { func getMinMaxDminDmax(rule map[string]interface{}) (float64, float64, float64, float64, error) {

1329
utils/json_test.go Normal file

File diff suppressed because it is too large Load Diff