No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

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