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.

175 rindas
5.8KB

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