forked from YaoApp/yao
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request YaoApp#475 from trheyi/main
[add] page config
- Loading branch information
Showing
9 changed files
with
434 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package core | ||
|
||
import ( | ||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
// UnmarshalJSON Custom JSON unmarshal function for PageMock | ||
func (mock *PageMock) UnmarshalJSON(data []byte) error { | ||
|
||
if mock == nil { | ||
return nil | ||
} | ||
|
||
type Alias struct { | ||
Method string `json:"method"` | ||
Params map[string]string `json:"params,omitempty"` | ||
Query map[string]interface{} `json:"query,omitempty"` | ||
Headers map[string]interface{} `json:"headers,omitempty"` | ||
Body interface{} `json:"body,omitempty"` | ||
} | ||
|
||
aux := &Alias{} | ||
if err := jsoniter.Unmarshal(data, &aux); err != nil { | ||
return err | ||
} | ||
|
||
method := aux.Method | ||
if method == "" { | ||
method = "GET" | ||
} | ||
mock.Body = aux.Body | ||
mock.Method = method | ||
mock.Params = aux.Params | ||
mock.Query = convertRecordToMap(aux.Query) | ||
mock.Headers = convertRecordToMap(aux.Headers) | ||
return nil | ||
} | ||
|
||
// Helper function to convert TypeScript Record<string, string | string[]> to map[string][]string | ||
func convertRecordToMap(record map[string]interface{}) map[string][]string { | ||
if record == nil { | ||
return nil | ||
} | ||
|
||
result := make(map[string][]string) | ||
for key, value := range record { | ||
switch v := value.(type) { | ||
case string: | ||
result[key] = []string{v} | ||
case []interface{}: | ||
strValues := make([]string, len(v)) | ||
for i, item := range v { | ||
if str, ok := item.(string); ok { | ||
strValues[i] = str | ||
} | ||
} | ||
result[key] = strValues | ||
} | ||
} | ||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,228 @@ | ||
package core | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
jsoniter "github.com/json-iterator/go" | ||
) | ||
|
||
func TestRequestSourceUnmarshalJSON(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
jsonData string | ||
expected *RequestSource | ||
shouldFail bool | ||
}{ | ||
{ | ||
name: "Valid JSON with string query and headers", | ||
jsonData: `{ | ||
"uid": "123", | ||
"mock": { | ||
"method": "GET", | ||
"query": { | ||
"q1": "value1", | ||
"q2": "value2" | ||
}, | ||
"headers": { | ||
"header1": "value1", | ||
"header2": "value2" | ||
} | ||
} | ||
}`, | ||
expected: &RequestSource{ | ||
UID: "123", | ||
Mock: &PageMock{ | ||
Method: "GET", | ||
Query: map[string][]string{ | ||
"q1": {"value1"}, | ||
"q2": {"value2"}, | ||
}, | ||
Headers: map[string][]string{ | ||
"header1": {"value1"}, | ||
"header2": {"value2"}, | ||
}, | ||
}, | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "Valid JSON with array query and headers", | ||
jsonData: `{ | ||
"uid": "456", | ||
"mock": { | ||
"method": "POST", | ||
"query": { | ||
"q1": ["value1", "value2"], | ||
"q2": ["value3"] | ||
}, | ||
"headers": { | ||
"header1": ["value1", "value2"], | ||
"header2": "value3" | ||
} | ||
} | ||
}`, | ||
expected: &RequestSource{ | ||
UID: "456", | ||
Mock: &PageMock{ | ||
Method: "POST", | ||
Query: map[string][]string{ | ||
"q1": {"value1", "value2"}, | ||
"q2": {"value3"}, | ||
}, | ||
Headers: map[string][]string{ | ||
"header1": {"value1", "value2"}, | ||
"header2": {"value3"}, | ||
}, | ||
}, | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "Valid JSON with invalid query", | ||
jsonData: `{ | ||
"uid": "789", | ||
"mock": { | ||
"method": "PUT", | ||
"query":"1203" | ||
} | ||
}`, | ||
expected: nil, | ||
shouldFail: true, | ||
}, | ||
// Add more test cases here to cover other scenarios. | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
var requestSource RequestSource | ||
err := jsoniter.Unmarshal([]byte(testCase.jsonData), &requestSource) | ||
|
||
if testCase.shouldFail { | ||
if err == nil { | ||
t.Errorf("%s: Expected unmarshal to fail, but it succeeded", testCase.name) | ||
} | ||
} else { | ||
if err != nil { | ||
t.Errorf("Unmarshal failed: %v", err) | ||
} | ||
if !reflect.DeepEqual(requestSource, *testCase.expected) { | ||
t.Errorf("Unmarshaled result does not match expected result") | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestPageConfigUnmarshalJSON(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
jsonData string | ||
expected *PageConfig | ||
shouldFail bool | ||
}{ | ||
{ | ||
name: "Valid JSON with PageSetting and PageMock", | ||
jsonData: `{ | ||
"title": "Page Title", | ||
"mock": { | ||
"method": "GET", | ||
"query": { | ||
"q1": "value1", | ||
"q2": "value2" | ||
}, | ||
"headers": { | ||
"header1": "value1", | ||
"header2": "value2" | ||
} | ||
} | ||
}`, | ||
expected: &PageConfig{ | ||
PageSetting: PageSetting{ | ||
Title: "Page Title", | ||
}, | ||
Mock: &PageMock{ | ||
Method: "GET", | ||
Query: map[string][]string{ | ||
"q1": {"value1"}, | ||
"q2": {"value2"}, | ||
}, | ||
Headers: map[string][]string{ | ||
"header1": {"value1"}, | ||
"header2": {"value2"}, | ||
}, | ||
}, | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "Valid JSON with PageSetting only", | ||
jsonData: `{ | ||
"title": "Page Title" | ||
}`, | ||
expected: &PageConfig{ | ||
PageSetting: PageSetting{ | ||
Title: "Page Title", | ||
}, | ||
Mock: nil, | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "Valid JSON with PageMock only", | ||
jsonData: `{ | ||
"mock": { | ||
"method": "GET", | ||
"query": { | ||
"q1": "value1", | ||
"q2": "value2" | ||
} | ||
} | ||
}`, | ||
expected: &PageConfig{ | ||
Mock: &PageMock{ | ||
Method: "GET", | ||
Query: map[string][]string{ | ||
"q1": {"value1"}, | ||
"q2": {"value2"}, | ||
}, | ||
}, | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "Valid JSON with invalid query", | ||
jsonData: `{ | ||
"title": "Page Title", | ||
"mock": { | ||
"method": "PUT", | ||
"query": "invalid query" | ||
} | ||
}`, | ||
expected: nil, | ||
shouldFail: true, | ||
}, | ||
// Add more test cases here to cover other scenarios. | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(testCase.name, func(t *testing.T) { | ||
var pageConfig PageConfig | ||
err := jsoniter.Unmarshal([]byte(testCase.jsonData), &pageConfig) | ||
|
||
if testCase.shouldFail { | ||
if err == nil { | ||
t.Errorf("%s: Expected unmarshal to fail, but it succeeded", testCase.name) | ||
} | ||
} else { | ||
|
||
if err != nil { | ||
t.Errorf("Unmarshal failed: %v", err) | ||
} | ||
if !reflect.DeepEqual(pageConfig, *testCase.expected) { | ||
t.Errorf("Unmarshaled result does not match expected result") | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,20 @@ | ||
package core | ||
|
||
import jsoniter "github.com/json-iterator/go" | ||
|
||
// Get get the base info | ||
func (page *Page) Get() *Page { | ||
return page | ||
} | ||
|
||
// GetHTML get the html | ||
func (page *Page) GetHTML() {} | ||
|
||
// GetScript get the script | ||
func (page *Page) GetScript() {} | ||
|
||
// GetStyle get the style | ||
func (page *Page) GetStyle() {} | ||
|
||
// GetData get the data | ||
func (page *Page) GetData() {} | ||
|
||
// SaveTemp save the temp | ||
func (page *Page) SaveTemp() {} | ||
|
||
// Save the page | ||
func (page *Page) Save() {} | ||
// GetConfig get the config | ||
func (page *Page) GetConfig() *PageConfig { | ||
if page.Config == nil && page.Codes.CONF.Code != "" { | ||
var config PageConfig | ||
err := jsoniter.Unmarshal([]byte(page.Codes.CONF.Code), &config) | ||
if err == nil { | ||
page.Config = &config | ||
} | ||
} | ||
return page.Config | ||
} |
Oops, something went wrong.