Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

176 rindas
5.9KB

  1. package main
  2. import (
  3. "bytes"
  4. "fmt"
  5. "log"
  6. "net/http"
  7. "net/http/httptest"
  8. "testing"
  9. )
  10. //when we setup wechate parameters,we chat will verify us
  11. func TestInitialSetup(t *testing.T) {
  12. SetupConfig()
  13. req := buildReqWechatAPISetup()
  14. rr, _ := getHTTPResponse(req, answerInitialAuth)
  15. // Check the response body is what we expect.
  16. expected := `913461463450840893`
  17. if rr.Body.String() != expected {
  18. t.Errorf("handler returned unexpected body: got %v want %v",
  19. rr.Body.String(), expected)
  20. }
  21. }
  22. func TestWebRootHandler(t *testing.T) {
  23. SetupConfig()
  24. req := buildReqWechatWebRoot()
  25. rr, _ := getHTTPResponse(req, webrootHandler)
  26. // Check the response body is what we expect.
  27. expected := `Hi there, I love dummydir!
  28. echostr => [913461463450840893]
  29. signature => [e39de9f2e28079c01ebb4b803dfc3442b819545c]`
  30. if rr.Body.String() != expected {
  31. t.Errorf("handler returned unexpected body: got %v want %v",
  32. rr.Body.String(), expected)
  33. }
  34. }
  35. //Send encrypted text Message ("test"")to server and get encrypted feedback
  36. //we only check decrypted ToUserName should be the one we sent out.
  37. //as decrypt itself is already a good proof of its working state.
  38. func TestPostTxtMsg(t *testing.T) {
  39. SetupConfig()
  40. req := buildReqWechatPostTxtMsg()
  41. rr, _ := getHTTPResponse(req, apiV1Main)
  42. m := ReadEncryptedMsg(rr.Body.String())
  43. xml := Decode(m.Encrypt)
  44. h := ReadCommonHeader(xml)
  45. expected := "oUN420bxqFqlx0ZQHciUOesZO3PE"
  46. if h.ToUserName != expected {
  47. t.Errorf("expect ToUserName: %v \r\nbut got %v",
  48. expected, h.ToUserName)
  49. }
  50. }
  51. func getHTTPResponse(req *http.Request, handler http.HandlerFunc) (rr *httptest.ResponseRecorder, err error) {
  52. // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
  53. // directly and pass in our Request and ResponseRecorder.
  54. rr = httptest.NewRecorder()
  55. handler.ServeHTTP(rr, req)
  56. // Check the status code is what we expect.
  57. if status := rr.Code; status != http.StatusOK {
  58. err = fmt.Errorf("wrong HTTP status code: got %v want %v",
  59. status, http.StatusOK)
  60. }
  61. return
  62. }
  63. // POST /api?signature=f06bb28c1d3847815d498fc0a343b11b4d03e095&timestamp=1493212928&nonce=1461107899&openid=oUN420bxqFqlx0ZQHciUOesZO3PE&encrypt_type=aes&msg_signature=61a50d4656b13a7bbeecf53a5a85fbf37835762f HTTP/1.1
  64. // Host: wechat.hitxy.org.au
  65. // Accept: */*
  66. // Cache-Control: no-cache
  67. // Connection: Keep-Alive
  68. // Content-Length: 534
  69. // Content-Type: text/xml
  70. // Pragma: no-cache
  71. // User-Agent: Mozilla/4.0
  72. // X-Forwarded-For: 103.7.30.105
  73. // X-Forwarded-Host: wechat.hitxy.org.au
  74. // X-Forwarded-Server: wechat.hitxy.org.au
  75. // <xml>
  76. // <ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
  77. // <Encrypt><![CDATA[HKILXQjAOV4Zi5Zb8gQ8zt6EPA6cBCRYSq90PZuyMqZSGhtjMESSgveIps74rS2+Q5aZPJhytXIkmuE+dxMTkV06qGNSYuSurXsoJE7bNfrE/Nmxq6GwKH1rwHXk3c50NoHW/h6/jCXu8x0oY1oW/ea1tLRGY4xeoQ9voCuvVYRFSyuU7Zz2QjlbP+AG8mCnGBxUwqrthqWFe7wDEkYa38EoD9DrjrQKRc4Hn2ZIHYN569cn5PDvsif+5FUX4p+3gEkgk/HVxicC7wT9wYzNSk9HH7bET3V7hnhJ+PJa+ZEH7HAPzry61UZ1gghf4dJAGVE9D8R4/0M6DpKGCQBGXmlI/Q3NjN0jx9NAqffPRxsoW4BF7mLV8RmfDbJJEa0W5i0buwyluMyVcrF5KT9Bd2DBvsULCXfuwwp01DmJdfc=]]></Encrypt>
  78. // </xml>
  79. // decrypt as: <xml><ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
  80. // <FromUserName><![CDATA[oUN420bxqFqlx0ZQHciUOesZO3PE]]></FromUserName>
  81. // <CreateTime>1493212928</CreateTime>
  82. // <MsgType><![CDATA[text]]></MsgType>
  83. // <Content><![CDATA[test]]></Content>
  84. // <MsgId>6413300692136991026</MsgId>
  85. // </xml>
  86. func buildReqWechatPostTxtMsg() *http.Request {
  87. xml := `<xml>
  88. <ToUserName><![CDATA[gh_f09231355c68]]></ToUserName>
  89. <Encrypt><![CDATA[HKILXQjAOV4Zi5Zb8gQ8zt6EPA6cBCRYSq90PZuyMqZSGhtjMESSgveIps74rS2+Q5aZPJhytXIkmuE+dxMTkV06qGNSYuSurXsoJE7bNfrE/Nmxq6GwKH1rwHXk3c50NoHW/h6/jCXu8x0oY1oW/ea1tLRGY4xeoQ9voCuvVYRFSyuU7Zz2QjlbP+AG8mCnGBxUwqrthqWFe7wDEkYa38EoD9DrjrQKRc4Hn2ZIHYN569cn5PDvsif+5FUX4p+3gEkgk/HVxicC7wT9wYzNSk9HH7bET3V7hnhJ+PJa+ZEH7HAPzry61UZ1gghf4dJAGVE9D8R4/0M6DpKGCQBGXmlI/Q3NjN0jx9NAqffPRxsoW4BF7mLV8RmfDbJJEa0W5i0buwyluMyVcrF5KT9Bd2DBvsULCXfuwwp01DmJdfc=]]></Encrypt>
  90. </xml>`
  91. b := bytes.NewBufferString(xml)
  92. req, _ := http.NewRequest("POST", "/api?signature=f06bb28c1d3847815d498fc0a343b11b4d03e095&timestamp=1493212928&nonce=1461107899&openid=oUN420bxqFqlx0ZQHciUOesZO3PE&encrypt_type=aes&msg_signature=61a50d4656b13a7bbeecf53a5a85fbf37835762f", b)
  93. buildReqCommonHeader(req)
  94. return req
  95. }
  96. func buildReqWechatWebRoot() *http.Request {
  97. req, _ := http.NewRequest("GET", "/dummydir", nil)
  98. buildReqCommonHeader(req)
  99. q := req.URL.Query()
  100. q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c")
  101. q.Add("echostr", "913461463450840893")
  102. req.URL.RawQuery = q.Encode()
  103. return req
  104. }
  105. func buildReqWechatAPISetup() *http.Request {
  106. // Create a request to pass to our handler.
  107. //We don't have any query body for now, so we'll
  108. // pass 'nil' as the third parameter.
  109. req, err := http.NewRequest("GET", "/apii", nil)
  110. if err != nil {
  111. log.Fatal(err)
  112. }
  113. q := req.URL.Query()
  114. q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c")
  115. q.Add("echostr", "913461463450840893")
  116. q.Add("timestamp", "1492970761")
  117. q.Add("nonce", "1850971833")
  118. req.URL.RawQuery = q.Encode()
  119. buildReqCommonHeader(req)
  120. return req
  121. }
  122. func buildReqCommonHeader(r *http.Request) {
  123. //
  124. // example request
  125. //
  126. // GET /api?signature=e39de9f2e28079c01ebb4b803dfc3442b819545c&echostr=913461463450840893&timestamp=1492970761&nonce=1850971833 HTTP/1.1
  127. // Host: wechat.hitxy.org.au
  128. // Accept: */*
  129. // Cache-Control: no-cache
  130. // Connection: Keep-Alive
  131. // Pragma: no-cache
  132. // User-Agent: Mozilla/4.0
  133. // X-Forwarded-For: 103.7.30.107
  134. // X-Forwarded-Host: wechat.hitxy.org.au
  135. // X-Forwarded-Server: wechat.hitxy.org.au
  136. h := r.Header
  137. h.Set("Host", "wechat.hitxy.org.au")
  138. h.Set("Accept", "*/*")
  139. h.Set("Cache-Control", "no-cache")
  140. h.Set("Connection", "Keep-Alive")
  141. h.Set("Pragma", "no-cache")
  142. h.Set("User-Agent", "Patrick testcase")
  143. h.Set("X-Forwarded-For", "103.7.30.107")
  144. h.Set("X-Forwarded-Host", "wechat.hitxy.org.au")
  145. h.Set("X-Forwarded-Server", "wechat.hitxy.org.au")
  146. }