瀏覽代碼

read xml common header tested.

master
Patrick Peng Sun 8 年之前
父節點
當前提交
a96c5496ac
共有 3 個文件被更改,包括 58 次插入5 次删除
  1. +6
    -3
      encrypt_test.go
  2. +13
    -2
      inMsg.go
  3. +39
    -0
      inMsg_test.go

+ 6
- 3
encrypt_test.go 查看文件

@@ -1,16 +1,19 @@
package main

import "testing"
import (
"testing"
)

func setupConfig() {
func SetupConfig() {
APIConfig = WechatAPIConfig{
"skdq8vklaurfqemfszuif",
"cmtWK2teRnLOXyO5dw7lJkETv9jCeNAqYyguEu5D8gG",
"wx876e233fde456b7b",
"4a91aa328569b10a9fb97adeb8b0af58"}
}

func TestEncodingMesage(t *testing.T) {
setupConfig()
SetupConfig()

//明文:
var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>

+ 13
- 2
inMsg.go 查看文件

@@ -1,11 +1,15 @@
//analyze xml message
package main

import (
"encoding/xml"
)

//all xml message has these headers
type Header struct {
type CommonHeader struct {
ToUserName string
FromUserName string
CreatTime int64
CreateTime int64
MsgType string
}

@@ -58,3 +62,10 @@ type LinkMsg struct {
Url string
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
}

+ 39
- 0
inMsg_test.go 查看文件

@@ -0,0 +1,39 @@
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)
}

Loading…
取消
儲存