码迷,mamicode.com
首页 > Web开发 > 详细

go http/websocket接口测试代码

时间:2017-06-16 16:45:21      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:ids   cli   websocket   exp   []   form   private   idt   panic   

//wsRouter_test.go
package Network import ( "encoding/json" "io/ioutil" "logs" "net/http" "net/url" "strings" "testing" "time" ) type testData struct { id string data interface{} code int msg string result 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 * 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 ClientRequest var res *ServerResponse err := 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.Code val.msg = res.Message val.result = res.Result if val.code != val.expCode { logs.Print(val) panic(val) } } } func httpTest(test []testData) { var err error for idx, _ := range test { val := &test[idx] var req *http.Request switch tmp := val.data.(type) { case *http.Request: req = tmp case 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 HTTPResult err = json.Unmarshal(body, &res) if err != nil { panic(err) } test.code = res.Code test.msg = res.Message test.result = res.Result if test.code != test.expCode { logs.Print(test) panic(test) } } }

  

//alterIcon_test.go
package Network

import (
	"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.go
package Network

func 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接口测试代码

标签:ids   cli   websocket   exp   []   form   private   idt   panic   

原文地址:http://www.cnblogs.com/cdyboke/p/7027679.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!