mirror of
https://github.com/eolinker/apinto
synced 2025-10-25 09:40:25 +08:00
http_mocking插件新增header
jsonSchema新增测试用例
This commit is contained in:
@@ -13,10 +13,11 @@ type complete struct {
|
||||
contentType string
|
||||
responseExample string
|
||||
responseSchema map[string]interface{}
|
||||
responseHeader map[string]string
|
||||
}
|
||||
|
||||
func NewComplete(responseStatus int, contentType string, responseExample string, responseSchema map[string]interface{}) *complete {
|
||||
return &complete{responseStatus: responseStatus, contentType: contentType, responseExample: responseExample, responseSchema: responseSchema}
|
||||
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, responseHeader: responseHeader}
|
||||
}
|
||||
|
||||
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().SetStatus(c.responseStatus, "")
|
||||
|
||||
for k, v := range c.responseHeader {
|
||||
ctx.Response().SetHeader(k, v)
|
||||
}
|
||||
|
||||
if c.responseExample != "" {
|
||||
ctx.Response().SetBody([]byte(c.responseExample))
|
||||
return nil
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
package http_mocking
|
||||
|
||||
type Config struct {
|
||||
ResponseStatus int `json:"response_status" default:"200" label:"返回响应的 HTTP 状态码(仅http路由有效)"`
|
||||
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字段二选一"`
|
||||
ResponseSchema string `json:"response_schema" switch:"content_type==='application/json'" label:"指定响应的jsonschema对象"`
|
||||
ResponseStatus int `json:"response_status" default:"200" label:"返回响应的 HTTP 状态码(仅http路由有效)"`
|
||||
ContentType string `json:"content_type" label:"返回响应的 Header Content-Type" enum:"application/json"`
|
||||
ResponseExample string `json:"response_example" format:"text" label:"返回响应的Body,与jsonschema字段二选一"`
|
||||
ResponseSchema string `json:"response_schema" format:"text" label:"指定响应的jsonschema对象"`
|
||||
ResponseHeader map[string]string `json:"response_header" label:"响应头"`
|
||||
}
|
||||
|
||||
const (
|
||||
|
||||
@@ -68,7 +68,7 @@ func Create(id, name string, conf *Config, workers map[eosc.RequireId]eosc.IWork
|
||||
|
||||
return &Mocking{
|
||||
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
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ type Mocking struct {
|
||||
contentType string
|
||||
responseExample string
|
||||
responseSchema string
|
||||
responseHeader map[string]string
|
||||
handler eocontext.CompleteHandler
|
||||
}
|
||||
|
||||
@@ -54,6 +55,7 @@ func (m *Mocking) Reset(v interface{}, workers map[eosc.RequireId]eosc.IWorker)
|
||||
m.responseExample = conf.ResponseExample
|
||||
m.contentType = conf.ContentType
|
||||
m.responseStatus = conf.ResponseStatus
|
||||
m.responseHeader = conf.ResponseHeader
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
@@ -1,14 +1,8 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
100
utils/json.go
100
utils/json.go
@@ -7,6 +7,7 @@ import (
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
"math/rand"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/robertkrimen/otto"
|
||||
@@ -57,36 +58,7 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
|
||||
rule, ruleOk := m["rule"].(map[string]interface{})
|
||||
if !ruleOk || len(rule) == 0 {
|
||||
if template != nil {
|
||||
if templateStr, ok := template.(string); ok {
|
||||
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
|
||||
resultMap[name] = mockConstant(template)
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -146,7 +118,7 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
|
||||
floats := RandFloats(dminVal, dmaxVal)
|
||||
randomValue, _ = strconv.ParseFloat(strconv.FormatFloat(randomValue, 'f', int(floats), 64), 64)
|
||||
} else {
|
||||
|
||||
randomValue, _ = strconv.ParseFloat(strconv.FormatFloat(randomValue, 'f', 0, 64), 64)
|
||||
}
|
||||
|
||||
resultMap[name] = randomValue
|
||||
@@ -170,8 +142,11 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
|
||||
}
|
||||
tempMap := make(map[string]interface{})
|
||||
i := 1
|
||||
for k, v := range templateMap {
|
||||
tempMap[k] = v
|
||||
for key, val := range templateMap {
|
||||
|
||||
split := strings.Split(key, "|")
|
||||
tempMap[split[0]] = mockConstant(val)
|
||||
|
||||
if i == randomNum {
|
||||
break
|
||||
}
|
||||
@@ -190,17 +165,37 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
|
||||
}
|
||||
|
||||
randomNum := 0
|
||||
if minVal > 0 && maxVal == 0 {
|
||||
if minVal > 0.0 && maxVal == 0.0 {
|
||||
randomNum = int(minVal)
|
||||
}
|
||||
|
||||
if minVal > 0 && maxVal > 0 {
|
||||
if minVal > 0.0 && maxVal > 0.0 {
|
||||
randomNum = int(RandInt64(int64(minVal), int64(maxVal)))
|
||||
}
|
||||
|
||||
if randomNum == 1 { //随机选取一个
|
||||
resultMap[name] = templateList[rand.Intn(len(templateList))]
|
||||
continue
|
||||
}
|
||||
|
||||
tempList := make([]interface{}, 0)
|
||||
|
||||
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
|
||||
@@ -216,6 +211,39 @@ func JsonSchemaMockJsUnmarshal(valueMap interface{}) interface{} {
|
||||
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")
|
||||
|
||||
func getMinMaxDminDmax(rule map[string]interface{}) (float64, float64, float64, float64, error) {
|
||||
|
||||
1329
utils/json_test.go
Normal file
1329
utils/json_test.go
Normal file
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user