標籤:ids cli websocket exp [] form private idt panic
//wsRouter_test.go
package Networkimport ("encoding/json""io/ioutil""logs""net/http""net/url""strings""testing""time")type testData struct {id stringdata interface{}code intmsg stringresult interface{}expCode int}var websocketToken stringvar socket string = "127.0.0.1:8001"func TestWsRouter(test *testing.T) {publicFileDir = "../public"privateFileDir = "../private"go LaunchRouter()for len(dbQueue) <= 0 {time.Sleep(time.Millisecond * 1000)}dyTest([]testData{{data: `{"Method":"/createGroup","Args":""}`, expCode: errAssertType}})testAddGroupMemberList()testAddGroupMember()testCreateGroup()testDeleteGroupMember()/*---------------------http-----------------------*/testWsLogin()testAlterIcon()}func dyTest(test []testData) {for idx, _ := range test {val := &test[idx]var req ClientRequestvar res *ServerResponseerr := json.Unmarshal([]byte(val.data.(string)), &req)if err != nil {logs.Print(err.Error())panic(err)}res = dyRouter(req.Method, val.id, req.Args)val.code = res.Codeval.msg = res.Messageval.result = res.Resultif val.code != val.expCode {logs.Print(val)panic(val)}}}func httpTest(test []testData) {var err errorfor idx, _ := range test {val := &test[idx]var req *http.Requestswitch tmp := val.data.(type) {case *http.Request:req = tmpcase url.Values:req, err = http.NewRequest("POST", val.msg, strings.NewReader(tmp.Encode()))if err != nil {panic(err)}default:logs.Print(val)panic(val)}resp, err := http.DefaultClient.Do(req)if err != nil {panic(err)}dealHttpResp(resp, val)}}func dealHttpResp(resp *http.Response, test *testData) {defer resp.Body.Close()contentType := resp.Header["Content-Type"][0]if strings.Index(contentType, "json") != -1 {body, err := ioutil.ReadAll(resp.Body)if err != nil {panic(err)}var res HTTPResulterr = json.Unmarshal(body, &res)if err != nil {panic(err)}test.code = res.Codetest.msg = res.Messagetest.result = res.Resultif test.code != test.expCode {logs.Print(test)panic(test)}}}
//alterIcon_test.gopackage Networkimport ("bytes""io""mime/multipart""net/http""net/url""os""path/filepath")func testAlterIcon() {test := []testData{{data: map[string]string{"token": websocketToken,"objectID": "100000001",},msg: "icon",expCode: Success,},{data: map[string]string{"token": websocketToken,"objectID": "100000000",},msg: "icon",expCode: errNoPermission,},{data: map[string]string{"token": websocketToken,"objectID": "100000005",},msg: "icon",expCode: errNoPermission,},{data: map[string]string{"token": websocketToken,},msg: "icon",expCode: Success,},{data: map[string]string{"objectID": "100000005",},msg: "icon",expCode: errInvalidToken,},{data: map[string]string{"token": websocketToken,},msg: "file",expCode: errParseFileFailed,},}for idx, _ := range test {data, ok := test[idx].data.(map[string]string)if !ok {panic(test[idx].data)}req, err := newfileUploadRequest("http://"+socket+"/alterIcon", data, test[idx].msg, "../public/icon/default.ico")if err != nil {panic(err)}test[idx].data = req}test = append(test, testData{data: url.Values{"token": {websocketToken},"objectID": {"100000001"},"icon": {""},},msg: "http://" + socket + "/alterIcon",expCode: errParseForm,})httpTest(test)}func newfileUploadRequest(uri string, form map[string]string, formFileName, path string) (*http.Request, error) {file, err := os.Open(path)if err != nil {return nil, err}defer file.Close()body := &bytes.Buffer{}writer := multipart.NewWriter(body)part, err := writer.CreateFormFile(formFileName, filepath.Base(path))if err != nil {return nil, err}_, err = io.Copy(part, file)if err != nil {return nil, err}for key, val := range form {err = writer.WriteField(key, val)if err != nil {return nil, err}}err = writer.Close()if err != nil {return nil, err}req, err := http.NewRequest("POST", uri, body)req.Header.Add("Content-Type", writer.FormDataContentType())if err != nil {return nil, err}return req, nil}
//addGroupMember_test.gopackage Networkfunc testAddGroupMember() {var test []testData = []testData{{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000004","100000007","100000008"]}}`, expCode: Success},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"1006","memberIDs":["100000004"]}}`, expCode: errInvalidID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000000","memberIDs":["100000004"]}}`, expCode: errInvalidID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"10000000","memberIDs":["100000004"]}}`, expCode: errNotExistID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":100000006,"memberIDs":["100000004"]}}`, expCode: errAssertType},{id: "100000001",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000004"]}}`, expCode: errNoPermission},{id: "1000000010",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000004"]}}`, expCode: errNoPermission},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006"}}`, expCode: errAssertType},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000006","10000001"]}}`, expCode: errInvalidID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000007","100000004","100000007","100000008"]}}`, expCode: errInvalidID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000009","100000007","100000008"]}}`, expCode: errInvalidID},{id: "100000000",data: `{"Method":"/addGroupMember","Args":{"groupID":"100000006","memberIDs":["100000a","100000007","100000008"]}}`, expCode: errInvalidID},//num memberIDs > 200}dyTest(test)}
go http/websocket介面測試代碼