Go http/websocket Interface Test code

Source: Internet
Author: User

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 string var 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 *)}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", E xpcode:errnopermission,},{data:map[string]string{"token": Websockettoken, "ObjectID": "100000005",},msg: "Icon", ex pcode: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: "//" + 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 Interface Test code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.