| package main | package main | ||||
| import "testing" | |||||
| import ( | |||||
| "testing" | |||||
| ) | |||||
| func setupConfig() { | |||||
| func SetupConfig() { | |||||
| APIConfig = WechatAPIConfig{ | APIConfig = WechatAPIConfig{ | ||||
| "skdq8vklaurfqemfszuif", | "skdq8vklaurfqemfszuif", | ||||
| "cmtWK2teRnLOXyO5dw7lJkETv9jCeNAqYyguEu5D8gG", | "cmtWK2teRnLOXyO5dw7lJkETv9jCeNAqYyguEu5D8gG", | ||||
| "wx876e233fde456b7b", | "wx876e233fde456b7b", | ||||
| "4a91aa328569b10a9fb97adeb8b0af58"} | "4a91aa328569b10a9fb97adeb8b0af58"} | ||||
| } | } | ||||
| func TestEncodingMesage(t *testing.T) { | func TestEncodingMesage(t *testing.T) { | ||||
| setupConfig() | |||||
| SetupConfig() | |||||
| //明文: | //明文: | ||||
| var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName> | var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName> |
| //analyze xml message | //analyze xml message | ||||
| package main | package main | ||||
| import ( | |||||
| "encoding/xml" | |||||
| ) | |||||
| //all xml message has these headers | //all xml message has these headers | ||||
| type Header struct { | |||||
| type CommonHeader struct { | |||||
| ToUserName string | ToUserName string | ||||
| FromUserName string | FromUserName string | ||||
| CreatTime int64 | |||||
| CreateTime int64 | |||||
| MsgType string | MsgType string | ||||
| } | } | ||||
| Url string | Url string | ||||
| MsgId int64 | MsgId int64 | ||||
| } | } | ||||
| //ReadCommonHeader parse xml of common field of wechat post message | |||||
| func ReadCommonHeader(s string) CommonHeader { | |||||
| var r = CommonHeader{} | |||||
| xml.Unmarshal([]byte(s), &r) | |||||
| return r | |||||
| } |
| package main | |||||
| import ( | |||||
| "fmt" | |||||
| "testing" | |||||
| ) | |||||
| func TestReadCommonHeader(t *testing.T) { | |||||
| SetupConfig() | |||||
| var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName> | |||||
| <FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName> | |||||
| <CreateTime>1492972518</CreateTime> | |||||
| <MsgType><![CDATA[event]]></MsgType> | |||||
| <Event><![CDATA[CLICK]]></Event> | |||||
| <EventKey><![CDATA[V1001_TODAY_MUSIC]]></EventKey> | |||||
| </xml>` | |||||
| h := ReadCommonHeader(msg) | |||||
| assertEqual(t, h.ToUserName, "gh_f09231355c68", "ToUserName failed") | |||||
| assertEqual(t, h.FromUserName, "oUN420bxqFqlx0ZQHciUOesZO3PE", "FromUserName failed") | |||||
| assertEqual(t, h.MsgType, "event", "MsgType failed") | |||||
| //for weird reasons assertEqual does not work for integer | |||||
| if h.CreateTime != 1492972518 { | |||||
| errmsg := fmt.Sprintf("CreateTime should be 1492972518, not %d", h.CreateTime) | |||||
| t.Error(errmsg) | |||||
| } | |||||
| } | |||||
| func assertEqual(t *testing.T, a interface{}, b interface{}, message string) { | |||||
| if a == b { | |||||
| return | |||||
| } | |||||
| if len(message) == 0 { | |||||
| message = fmt.Sprintf("%v != %v", a, b) | |||||
| } | |||||
| message = fmt.Sprintf("%v != %v", a, b) | |||||
| t.Fatal(message) | |||||
| } |