From 2cfa42094156795b6ad624f4619bd9da4a3fdd94 Mon Sep 17 00:00:00 2001 From: zhangzeyi Date: Wed, 1 Mar 2023 14:20:08 +0800 Subject: [PATCH] =?UTF-8?q?http=5Fmocking=E6=8F=92=E4=BB=B6=E6=96=B0?= =?UTF-8?q?=E5=A2=9Eheader?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit jsonSchema新增测试用例 --- drivers/plugins/http_mocking/complete.go | 9 +- drivers/plugins/http_mocking/config.go | 9 +- drivers/plugins/http_mocking/driver.go | 2 +- drivers/plugins/http_mocking/mocking.go | 4 +- utils/aes_test.go | 6 - utils/json.go | 100 +- utils/json_test.go | 1329 ++++++++++++++++++++++ 7 files changed, 1409 insertions(+), 50 deletions(-) create mode 100644 utils/json_test.go diff --git a/drivers/plugins/http_mocking/complete.go b/drivers/plugins/http_mocking/complete.go index 5c185b1d..33a4a3d5 100644 --- a/drivers/plugins/http_mocking/complete.go +++ b/drivers/plugins/http_mocking/complete.go @@ -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 diff --git a/drivers/plugins/http_mocking/config.go b/drivers/plugins/http_mocking/config.go index e0ca99c8..85e745e8 100644 --- a/drivers/plugins/http_mocking/config.go +++ b/drivers/plugins/http_mocking/config.go @@ -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 ( diff --git a/drivers/plugins/http_mocking/driver.go b/drivers/plugins/http_mocking/driver.go index 049b5ba0..4a19b7ba 100644 --- a/drivers/plugins/http_mocking/driver.go +++ b/drivers/plugins/http_mocking/driver.go @@ -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 } diff --git a/drivers/plugins/http_mocking/mocking.go b/drivers/plugins/http_mocking/mocking.go index f2ffc021..e280c0c6 100644 --- a/drivers/plugins/http_mocking/mocking.go +++ b/drivers/plugins/http_mocking/mocking.go @@ -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 } diff --git a/utils/aes_test.go b/utils/aes_test.go index 9b6727a0..3a6325d2 100644 --- a/utils/aes_test.go +++ b/utils/aes_test.go @@ -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)) } diff --git a/utils/json.go b/utils/json.go index c64bb71d..79df7fd2 100644 --- a/utils/json.go +++ b/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) { diff --git a/utils/json_test.go b/utils/json_test.go new file mode 100644 index 00000000..1366699a --- /dev/null +++ b/utils/json_test.go @@ -0,0 +1,1329 @@ +package utils + +import ( + "encoding/json" + "testing" +) + +// 官方格式json Schema +var test1 = ` +{ + "properties":{ + "field0":{ + "example":"abcd", + "type":"string" + }, + "field1":{ + "example":123.12, + "type":"number" + }, + "field3":{ + "properties":{ + "field3_1":{ + "type":"string" + }, + "field3_2":{ + "properties":{ + "field3_2_1":{ + "example":true, + "type":"boolean" + }, + "field3_2_2":{ + "items":{ + "example":155.55, + "type":"integer" + }, + "type":"array" + } + }, + "type":"object" + } + }, + "type":"object" + }, + "field2":{ + "items":{ + "type":"string" + }, + "type":"array" + } + }, + "type":"object" +}` + +/* +mockJs template + + var template = { + 'name': '@cname', // 生成中文名字 + 'age|18-30': 20, // 生成18~30之间的随机整数 + 'gender|1': ['男', '女'], // 从数组中随机选取一个元素 + 'email': '@email' // 生成邮箱 + } + +Mock.toJSONSchema(template)生成的json schema +*/ +var mock1 = `{ + "template": { + "name": "@cname", + "age|18-30": 20, + "gender|1": [ + "男", + "女" + ], + "email": "@email" + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT" + ], + "properties": [ + { + "name": "name", + "template": "@cname", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "name" + ] + }, + { + "name": "age", + "template": 20, + "type": "number", + "rule": { + "parameters": [ + "age|18-30", + "age", + null, + "18-30", + null + ], + "range": [ + "18-30", + "18", + "30" + ], + "min": 18, + "max": 30, + "count": 22 + }, + "path": [ + "ROOT", + "age" + ] + }, + { + "name": "gender", + "template": [ + "男", + "女" + ], + "type": "array", + "rule": { + "parameters": [ + "gender|1", + "gender", + null, + "1", + null + ], + "range": [ + "1", + "1", + null + ], + "min": 1, + "count": 1 + }, + "path": [ + "ROOT", + "gender" + ], + "items": [ + { + "name": 0, + "template": "男", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "gender", + 0 + ] + }, + { + "name": 1, + "template": "女", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "gender", + 1 + ] + } + ] + }, + { + "name": "email", + "template": "@email", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "email" + ] + } + ] +}` + +/* +mockJs template + + var template = { + 'list|1-10': [{ + 'id|+1': 1, + 'email': '@EMAIL' + }] +} + +Mock.toJSONSchema(template)生成的json schema +*/ + +var mock2 = `{ + "template": { + "list|1-10": [ + { + "id|+1": 7, + "email": "@EMAIL" + } + ] + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT" + ], + "properties": [ + { + "name": "list", + "template": [ + { + "id|+1": 7, + "email": "@EMAIL" + } + ], + "type": "array", + "rule": { + "parameters": [ + "list|1-10", + "list", + null, + "1-10", + null + ], + "range": [ + "1-10", + "1", + "10" + ], + "min": 1, + "max": 10, + "count": 2 + }, + "path": [ + "ROOT", + "list" + ], + "items": [ + { + "name": 0, + "template": { + "id|+1": 7, + "email": "@EMAIL" + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT", + "list", + 0 + ], + "properties": [ + { + "name": "id", + "template": 7, + "type": "number", + "rule": { + "parameters": [ + "id|+1", + "id", + "1", + null, + null + ] + }, + "path": [ + "ROOT", + "list", + 0, + "id" + ] + }, + { + "name": "email", + "template": "@EMAIL", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "list", + 0, + "email" + ] + } + ] + } + ] + } + ] +}` + +/* + var template = { + 'list|1-10': { + 'id|+1': 1, + 'email': '@EMAIL' + } + } +*/ +var mock3 = `{ + "template": { + "list|1-10": { + "id|+1": 1, + "email": "@EMAIL" + } + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT" + ], + "properties": [ + { + "name": "list", + "template": { + "id|+1": 1, + "email": "@EMAIL" + }, + "type": "object", + "rule": { + "parameters": [ + "list|1-10", + "list", + null, + "1-10", + null + ], + "range": [ + "1-10", + "1", + "10" + ], + "min": 1, + "max": 10, + "count": 4 + }, + "path": [ + "ROOT", + "list" + ], + "properties": [ + { + "name": "id", + "template": 1, + "type": "number", + "rule": { + "parameters": [ + "id|+1", + "id", + "1", + null, + null + ] + }, + "path": [ + "ROOT", + "list", + "id" + ] + }, + { + "name": "email", + "template": "@EMAIL", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "list", + "email" + ] + } + ] + } + ] +}` + +/* + var template = { + 'key|1-10': '★' + } +*/ +var mock4 = `{ + "template": { + "key|1-10": "★" + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT" + ], + "properties": [ + { + "name": "key", + "template": "★", + "type": "string", + "rule": { + "parameters": [ + "key|1-10", + "key", + null, + "1-10", + null + ], + "range": [ + "1-10", + "1", + "10" + ], + "min": 1, + "max": 10, + "count": 2 + }, + "path": [ + "ROOT", + "key" + ] + } + ] +}` + +/* + var template = { + 'title': 'Syntax Demo', + + 'string1|1-10': '★', + 'string2|3': 'value', + + 'number1|+1': 100, + 'number2|1-100': 100, + 'number3|1-100.1-10': 1, + 'number4|123.1-10': 1, + 'number5|123.3': 1, + 'number6|123.10': 1.123, + + 'boolean1|1': true, + 'boolean2|1-2': true, + + 'object1|2-4': { + '110000': '北京市', + '120000': '天津市', + '130000': '河北省', + '140000': '山西省' + }, + 'object2|2': { + '310000': '上海市', + '320000': '江苏省', + '330000': '浙江省', + '340000': '安徽省' + }, + 'object3|2': { + '310000': '@name', + '320000': '@ip', + '330000': '@email' + }, + + 'array1|1': ['AMD', 'CMD', 'KMD', 'UMD'], + 'array2|1-10': [{ + 'id':10, + 'ip':'@ip' + }], + 'array3|3': ['Mock.js'], + 'array4|3-5': [10,20,30,40] +} +*/ + +var mock5 = `{ + "template": { + "title": "Syntax Demo", + "string1|1-10": "★", + "string2|3": "value", + "number1|+1": 101, + "number2|1-100": 100, + "number3|1-100.1-10": 1, + "number4|123.1-10": 1, + "number5|123.3": 1, + "number6|123.10": 1.123, + "boolean1|1": true, + "boolean2|1-2": true, + "object1|2-4": { + "110000": "北京市", + "120000": "天津市", + "130000": "河北省", + "140000": "山西省" + }, + "object2|2": { + "310000": "上海市", + "320000": "江苏省", + "330000": "浙江省", + "340000": "安徽省" + }, + "object3|2": { + "310000": "@name", + "320000": "@ip", + "330000": "@email" + }, + "array1|1": [ + "AMD", + "CMD", + "KMD", + "UMD" + ], + "array2|1-10": [ + { + "id": 10, + "ip": "@ip" + } + ], + "array3|3": [ + "Mock.js" + ], + "array4|3-5": [ + 10, + 20, + 30, + 40 + ] + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT" + ], + "properties": [ + { + "name": "title", + "template": "Syntax Demo", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "title" + ] + }, + { + "name": "string1", + "template": "★", + "type": "string", + "rule": { + "parameters": [ + "string1|1-10", + "string1", + null, + "1-10", + null + ], + "range": [ + "1-10", + "1", + "10" + ], + "min": 1, + "max": 10, + "count": 5 + }, + "path": [ + "ROOT", + "string1" + ] + }, + { + "name": "string2", + "template": "value", + "type": "string", + "rule": { + "parameters": [ + "string2|3", + "string2", + null, + "3", + null + ], + "range": [ + "3", + "3", + null + ], + "min": 3, + "count": 3 + }, + "path": [ + "ROOT", + "string2" + ] + }, + { + "name": "number1", + "template": 101, + "type": "number", + "rule": { + "parameters": [ + "number1|+1", + "number1", + "1", + null, + null + ] + }, + "path": [ + "ROOT", + "number1" + ] + }, + { + "name": "number2", + "template": 100, + "type": "number", + "rule": { + "parameters": [ + "number2|1-100", + "number2", + null, + "1-100", + null + ], + "range": [ + "1-100", + "1", + "100" + ], + "min": 1, + "max": 100, + "count": 61 + }, + "path": [ + "ROOT", + "number2" + ] + }, + { + "name": "number3", + "template": 1, + "type": "number", + "rule": { + "parameters": [ + "number3|1-100.1-10", + "number3", + null, + "1-100", + "1-10" + ], + "range": [ + "1-100", + "1", + "100" + ], + "min": 1, + "max": 100, + "count": 70, + "decimal": [ + "1-10", + "1", + "10" + ], + "dmin": 1, + "dmax": 10, + "dcount": 6 + }, + "path": [ + "ROOT", + "number3" + ] + }, + { + "name": "number4", + "template": 1, + "type": "number", + "rule": { + "parameters": [ + "number4|123.1-10", + "number4", + null, + "123", + "1-10" + ], + "range": [ + "123", + "123", + null + ], + "min": 123, + "count": 123, + "decimal": [ + "1-10", + "1", + "10" + ], + "dmin": 1, + "dmax": 10, + "dcount": 7 + }, + "path": [ + "ROOT", + "number4" + ] + }, + { + "name": "number5", + "template": 1, + "type": "number", + "rule": { + "parameters": [ + "number5|123.3", + "number5", + null, + "123", + "3" + ], + "range": [ + "123", + "123", + null + ], + "min": 123, + "count": 123, + "decimal": [ + "3", + "3", + null + ], + "dmin": 3, + "dmax": null, + "dcount": 3 + }, + "path": [ + "ROOT", + "number5" + ] + }, + { + "name": "number6", + "template": 1.123, + "type": "number", + "rule": { + "parameters": [ + "number6|123.10", + "number6", + null, + "123", + "10" + ], + "range": [ + "123", + "123", + null + ], + "min": 123, + "count": 123, + "decimal": [ + "10", + "10", + null + ], + "dmin": 10, + "dmax": null, + "dcount": 10 + }, + "path": [ + "ROOT", + "number6" + ] + }, + { + "name": "boolean1", + "template": true, + "type": "boolean", + "rule": { + "parameters": [ + "boolean1|1", + "boolean1", + null, + "1", + null + ], + "range": [ + "1", + "1", + null + ], + "min": 1, + "count": 1 + }, + "path": [ + "ROOT", + "boolean1" + ] + }, + { + "name": "boolean2", + "template": true, + "type": "boolean", + "rule": { + "parameters": [ + "boolean2|1-2", + "boolean2", + null, + "1-2", + null + ], + "range": [ + "1-2", + "1", + "2" + ], + "min": 1, + "max": 2, + "count": 2 + }, + "path": [ + "ROOT", + "boolean2" + ] + }, + { + "name": "object1", + "template": { + "110000": "北京市", + "120000": "天津市", + "130000": "河北省", + "140000": "山西省" + }, + "type": "object", + "rule": { + "parameters": [ + "object1|2-4", + "object1", + null, + "2-4", + null + ], + "range": [ + "2-4", + "2", + "4" + ], + "min": 2, + "max": 4, + "count": 4 + }, + "path": [ + "ROOT", + "object1" + ], + "properties": [ + { + "name": "110000", + "template": "北京市", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object1", + "110000" + ] + }, + { + "name": "120000", + "template": "天津市", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object1", + "120000" + ] + }, + { + "name": "130000", + "template": "河北省", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object1", + "130000" + ] + }, + { + "name": "140000", + "template": "山西省", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object1", + "140000" + ] + } + ] + }, + { + "name": "object2", + "template": { + "310000": "上海市", + "320000": "江苏省", + "330000": "浙江省", + "340000": "安徽省" + }, + "type": "object", + "rule": { + "parameters": [ + "object2|2", + "object2", + null, + "2", + null + ], + "range": [ + "2", + "2", + null + ], + "min": 2, + "count": 2 + }, + "path": [ + "ROOT", + "object2" + ], + "properties": [ + { + "name": "310000", + "template": "上海市", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object2", + "310000" + ] + }, + { + "name": "320000", + "template": "江苏省", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object2", + "320000" + ] + }, + { + "name": "330000", + "template": "浙江省", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object2", + "330000" + ] + }, + { + "name": "340000", + "template": "安徽省", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object2", + "340000" + ] + } + ] + }, + { + "name": "object3", + "template": { + "310000": "@name", + "320000": "@ip", + "330000": "@email" + }, + "type": "object", + "rule": { + "parameters": [ + "object3|2", + "object3", + null, + "2", + null + ], + "range": [ + "2", + "2", + null + ], + "min": 2, + "count": 2 + }, + "path": [ + "ROOT", + "object3" + ], + "properties": [ + { + "name": "310000", + "template": "@name", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object3", + "310000" + ] + }, + { + "name": "320000", + "template": "@ip", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object3", + "320000" + ] + }, + { + "name": "330000", + "template": "@email", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "object3", + "330000" + ] + } + ] + }, + { + "name": "array1", + "template": [ + "AMD", + "CMD", + "KMD", + "UMD" + ], + "type": "array", + "rule": { + "parameters": [ + "array1|1", + "array1", + null, + "1", + null + ], + "range": [ + "1", + "1", + null + ], + "min": 1, + "count": 1 + }, + "path": [ + "ROOT", + "array1" + ], + "items": [ + { + "name": 0, + "template": "AMD", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array1", + 0 + ] + }, + { + "name": 1, + "template": "CMD", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array1", + 1 + ] + }, + { + "name": 2, + "template": "KMD", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array1", + 2 + ] + }, + { + "name": 3, + "template": "UMD", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array1", + 3 + ] + } + ] + }, + { + "name": "array2", + "template": [ + { + "id": 10, + "ip": "@ip" + } + ], + "type": "array", + "rule": { + "parameters": [ + "array2|1-10", + "array2", + null, + "1-10", + null + ], + "range": [ + "1-10", + "1", + "10" + ], + "min": 1, + "max": 10, + "count": 1 + }, + "path": [ + "ROOT", + "array2" + ], + "items": [ + { + "name": 0, + "template": { + "id": 10, + "ip": "@ip" + }, + "type": "object", + "rule": {}, + "path": [ + "ROOT", + "array2", + 0 + ], + "properties": [ + { + "name": "id", + "template": 10, + "type": "number", + "rule": {}, + "path": [ + "ROOT", + "array2", + 0, + "id" + ] + }, + { + "name": "ip", + "template": "@ip", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array2", + 0, + "ip" + ] + } + ] + } + ] + }, + { + "name": "array3", + "template": [ + "Mock.js" + ], + "type": "array", + "rule": { + "parameters": [ + "array3|3", + "array3", + null, + "3", + null + ], + "range": [ + "3", + "3", + null + ], + "min": 3, + "count": 3 + }, + "path": [ + "ROOT", + "array3" + ], + "items": [ + { + "name": 0, + "template": "Mock.js", + "type": "string", + "rule": {}, + "path": [ + "ROOT", + "array3", + 0 + ] + } + ] + }, + { + "name": "array4", + "template": [ + 10, + 20, + 30, + 40 + ], + "type": "array", + "rule": { + "parameters": [ + "array4|3-5", + "array4", + null, + "3-5", + null + ], + "range": [ + "3-5", + "3", + "5" + ], + "min": 3, + "max": 5, + "count": 5 + }, + "path": [ + "ROOT", + "array4" + ], + "items": [ + { + "name": 0, + "template": 10, + "type": "number", + "rule": {}, + "path": [ + "ROOT", + "array4", + 0 + ] + }, + { + "name": 1, + "template": 20, + "type": "number", + "rule": {}, + "path": [ + "ROOT", + "array4", + 1 + ] + }, + { + "name": 2, + "template": 30, + "type": "number", + "rule": {}, + "path": [ + "ROOT", + "array4", + 2 + ] + }, + { + "name": 3, + "template": 40, + "type": "number", + "rule": {}, + "path": [ + "ROOT", + "array4", + 3 + ] + } + ] + } + ] +}` + +func TestJsonSchemaMockJsUnmarshal(t *testing.T) { + type args struct { + valueMap interface{} + } + + valueMap := make(map[string]interface{}) + json.Unmarshal([]byte(mock5), &valueMap) + tests := []struct { + name string + args args + want interface{} + }{ + { + name: "", + args: args{ + valueMap: valueMap, + }, + want: nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := JsonSchemaMockJsUnmarshal(tt.args.valueMap) + bytes, _ := json.Marshal(got) + t.Logf(string(bytes)) + }) + } +}