You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
1.1KB

  1. package main
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. func TestReadCommonHeader(t *testing.T) {
  7. SetupConfig()
  8. var msg = `<xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
  9. <FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName>
  10. <CreateTime>1492972518</CreateTime>
  11. <MsgType><![CDATA[event]]></MsgType>
  12. <Event><![CDATA[CLICK]]></Event>
  13. <EventKey><![CDATA[V1001_TODAY_MUSIC]]></EventKey>
  14. </xml>`
  15. h := ReadCommonHeader(msg)
  16. assertEqual(t, h.ToUserName, "gh_f09231355c68", "ToUserName failed")
  17. assertEqual(t, h.FromUserName, "oUN420bxqFqlx0ZQHciUOesZO3PE", "FromUserName failed")
  18. assertEqual(t, h.MsgType, "event", "MsgType failed")
  19. //for weird reasons assertEqual does not work for integer
  20. if h.CreateTime != 1492972518 {
  21. errmsg := fmt.Sprintf("CreateTime should be 1492972518, not %d", h.CreateTime)
  22. t.Error(errmsg)
  23. }
  24. }
  25. func assertEqual(t *testing.T, a interface{}, b interface{}, message string) {
  26. if a == b {
  27. return
  28. }
  29. if len(message) == 0 {
  30. message = fmt.Sprintf("%v != %v", a, b)
  31. }
  32. message = fmt.Sprintf("%v != %v", a, b)
  33. t.Fatal(message)
  34. }