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.

118 satır
3.1KB

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "net/http/httptest"
  7. "testing"
  8. )
  9. //when we setup wechate parameters,we chat will verify us
  10. func TestInitialSetup(t *testing.T) {
  11. SetupConfig()
  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. SetupConfig()
  23. req := buildReqWechatWebRoot()
  24. rr, _ := getHTTPResponse(req, webrootHandler)
  25. // Check the response body is what we expect.
  26. expected := `Hi there, I love dummydir!
  27. echostr => [913461463450840893]
  28. signature => [e39de9f2e28079c01ebb4b803dfc3442b819545c]`
  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. func getHTTPResponse(req *http.Request, handler http.HandlerFunc) (rr *httptest.ResponseRecorder, err error) {
  35. // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
  36. // directly and pass in our Request and ResponseRecorder.
  37. rr = httptest.NewRecorder()
  38. handler.ServeHTTP(rr, req)
  39. // Check the status code is what we expect.
  40. if status := rr.Code; status != http.StatusOK {
  41. err = fmt.Errorf("wrong HTTP status code: got %v want %v",
  42. status, http.StatusOK)
  43. }
  44. return
  45. }
  46. func buildReqWechatWebRoot() *http.Request {
  47. req, _ := http.NewRequest("GET", "/dummydir", nil)
  48. buildReqCommonHeader(req)
  49. q := req.URL.Query()
  50. q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c")
  51. q.Add("echostr", "913461463450840893")
  52. req.URL.RawQuery = q.Encode()
  53. return req
  54. }
  55. func buildReqWechatAPISetup() *http.Request {
  56. // Create a request to pass to our handler.
  57. //We don't have any query body for now, so we'll
  58. // pass 'nil' as the third parameter.
  59. req, err := http.NewRequest("GET", "/apii", nil)
  60. if err != nil {
  61. log.Fatal(err)
  62. }
  63. q := req.URL.Query()
  64. q.Add("signature", "e39de9f2e28079c01ebb4b803dfc3442b819545c")
  65. q.Add("echostr", "913461463450840893")
  66. q.Add("timestamp", "1492970761")
  67. q.Add("nonce", "1850971833")
  68. req.URL.RawQuery = q.Encode()
  69. buildReqCommonHeader(req)
  70. return req
  71. }
  72. func buildReqCommonHeader(r *http.Request) {
  73. //
  74. // example request
  75. //
  76. // GET /api?signature=e39de9f2e28079c01ebb4b803dfc3442b819545c&echostr=913461463450840893&timestamp=1492970761&nonce=1850971833 HTTP/1.1
  77. // Host: wechat.hitxy.org.au
  78. // Accept: */*
  79. // Cache-Control: no-cache
  80. // Connection: Keep-Alive
  81. // Pragma: no-cache
  82. // User-Agent: Mozilla/4.0
  83. // X-Forwarded-For: 103.7.30.107
  84. // X-Forwarded-Host: wechat.hitxy.org.au
  85. // X-Forwarded-Server: wechat.hitxy.org.au
  86. h := r.Header
  87. h.Set("Host", "wechat.hitxy.org.au")
  88. h.Set("Accept", "*/*")
  89. h.Set("Cache-Control", "no-cache")
  90. h.Set("Connection", "Keep-Alive")
  91. h.Set("Pragma", "no-cache")
  92. h.Set("User-Agent", "Patrick testcase")
  93. h.Set("X-Forwarded-For", "103.7.30.107")
  94. h.Set("X-Forwarded-Host", "wechat.hitxy.org.au")
  95. h.Set("X-Forwarded-Server", "wechat.hitxy.org.au")
  96. }