附近小店
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 

182 行
3.9 KiB

  1. package weapp
  2. import (
  3. "net/http"
  4. "net/http/httptest"
  5. "testing"
  6. )
  7. func TestLogin(t *testing.T) {
  8. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  9. if r.Method != "GET" {
  10. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  11. }
  12. path := r.URL.EscapedPath()
  13. if path != apiLogin {
  14. t.Fatalf("Except to path '%s',get '%s'", apiLogin, path)
  15. }
  16. if err := r.ParseForm(); err != nil {
  17. t.Fatal(err)
  18. }
  19. queries := []string{"appid", "secret", "js_code", "grant_type"}
  20. for _, v := range queries {
  21. content := r.Form.Get(v)
  22. if content == "" {
  23. t.Fatalf("%v can not be empty", v)
  24. }
  25. }
  26. w.WriteHeader(http.StatusOK)
  27. raw := `{
  28. "openid": "mock-openid",
  29. "session_key": "mock-session_key",
  30. "unionid": "mock-unionid",
  31. "errcode": 0,
  32. "errmsg": "mock-errmsg"
  33. }`
  34. if _, err := w.Write([]byte(raw)); err != nil {
  35. t.Fatal(err)
  36. }
  37. }))
  38. defer ts.Close()
  39. _, err := login("mock-appid", "mock-secret", "mock-code", ts.URL+apiLogin)
  40. if err != nil {
  41. t.Fatal(err)
  42. }
  43. }
  44. func TestGetAccessToken(t *testing.T) {
  45. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  46. if r.Method != "GET" {
  47. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  48. }
  49. path := r.URL.EscapedPath()
  50. if path != apiGetAccessToken {
  51. t.Fatalf("Except to path '%s',get '%s'", apiGetAccessToken, path)
  52. }
  53. if err := r.ParseForm(); err != nil {
  54. t.Fatal(err)
  55. }
  56. if r.Form.Get("grant_type") != "client_credential" {
  57. t.Fatal("invalid client_credential")
  58. }
  59. queries := []string{"appid", "secret"}
  60. for _, v := range queries {
  61. content := r.Form.Get(v)
  62. if content == "" {
  63. t.Fatalf("%v can not be empty", v)
  64. }
  65. }
  66. w.WriteHeader(http.StatusOK)
  67. raw := `{"access_token":"ACCESS_TOKEN","expires_in":7200}`
  68. if _, err := w.Write([]byte(raw)); err != nil {
  69. t.Fatal(err)
  70. }
  71. }))
  72. defer ts.Close()
  73. _, err := getAccessToken("mock-appid", "mock-secret", ts.URL+apiGetAccessToken)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. }
  78. func TestGetPaidUnionID(t *testing.T) {
  79. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  80. if r.Method != "GET" {
  81. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  82. }
  83. path := r.URL.EscapedPath()
  84. if path != apiGetPaidUnionID {
  85. t.Fatalf("Except to path '%s',get '%s'", apiGetPaidUnionID, path)
  86. }
  87. if err := r.ParseForm(); err != nil {
  88. t.Fatal(err)
  89. }
  90. queries := []string{"openid", "access_token", "transaction_id"}
  91. for _, v := range queries {
  92. content := r.Form.Get(v)
  93. if content == "" {
  94. t.Fatalf("%v can not be empty", v)
  95. }
  96. }
  97. w.WriteHeader(http.StatusOK)
  98. raw := `{
  99. "unionid": "oTmHYjg-tElZ68xxxxxxxxhy1Rgk",
  100. "errcode": 0,
  101. "errmsg": "ok"
  102. }`
  103. if _, err := w.Write([]byte(raw)); err != nil {
  104. t.Fatal(err)
  105. }
  106. }))
  107. defer ts.Close()
  108. _, err := getPaidUnionID("mock-access-token", "mock-open-id", "mock-transaction-id", ts.URL+apiGetPaidUnionID)
  109. if err != nil {
  110. t.Fatal(err)
  111. }
  112. }
  113. func TestGetPaidUnionIDWithMCH(t *testing.T) {
  114. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  115. if r.Method != "GET" {
  116. t.Fatalf("Expect 'GET' get '%s'", r.Method)
  117. }
  118. path := r.URL.EscapedPath()
  119. if path != apiGetPaidUnionID {
  120. t.Fatalf("Except to path '%s',get '%s'", apiGetPaidUnionID, path)
  121. }
  122. if err := r.ParseForm(); err != nil {
  123. t.Fatal(err)
  124. }
  125. queries := []string{"openid", "access_token", "mch_id", "out_trade_no"}
  126. for _, v := range queries {
  127. content := r.Form.Get(v)
  128. if content == "" {
  129. t.Fatalf("%v can not be empty", v)
  130. }
  131. }
  132. w.WriteHeader(http.StatusOK)
  133. raw := `{
  134. "unionid": "oTmHYjg-tElZ68xxxxxxxxhy1Rgk",
  135. "errcode": 0,
  136. "errmsg": "ok"
  137. }`
  138. if _, err := w.Write([]byte(raw)); err != nil {
  139. t.Fatal(err)
  140. }
  141. }))
  142. defer ts.Close()
  143. _, err := getPaidUnionIDWithMCH("mock-access-token", "mock-open-id", "mock-out-trade-number", "mock-mch-id", ts.URL+apiGetPaidUnionID)
  144. if err != nil {
  145. t.Fatal(err)
  146. }
  147. }