附近小店
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.
 
 
 

213 lines
4.8 KiB

  1. package weapp
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. "net/http"
  7. "net/http/httptest"
  8. "os"
  9. "path"
  10. "testing"
  11. )
  12. func TestCreateQRCode(t *testing.T) {
  13. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  14. if r.Method != "POST" {
  15. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  16. }
  17. ePath := r.URL.EscapedPath()
  18. if ePath != apiCreateQRCode {
  19. t.Fatalf("Except to path '%s',get '%s'", apiCreateQRCode, ePath)
  20. }
  21. if err := r.ParseForm(); err != nil {
  22. t.Fatal(err)
  23. }
  24. if r.Form.Get("access_token") == "" {
  25. t.Fatalf("access_token can not be empty")
  26. }
  27. params := struct {
  28. Path string `json:"path"`
  29. Width int `json:"width,omitempty"`
  30. }{}
  31. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  32. t.Fatal(err)
  33. }
  34. if params.Path == "" {
  35. t.Error("Response column path can not be empty")
  36. }
  37. filename := testIMGName
  38. file, err := os.Open(filename)
  39. if err != nil {
  40. t.Fatal((err))
  41. }
  42. defer file.Close()
  43. ext := path.Ext(filename)
  44. ext = ext[1:len(ext)]
  45. w.Header().Set("Content-Type", "image/"+ext)
  46. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  47. w.WriteHeader(http.StatusOK)
  48. if _, err := io.Copy(w, file); err != nil {
  49. t.Fatal(err)
  50. }
  51. }))
  52. defer ts.Close()
  53. creator := QRCodeCreator{
  54. Path: "mock/path",
  55. Width: 430,
  56. }
  57. resp, _, err := creator.create(ts.URL+apiCreateQRCode, "mock-access-token")
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. resp.Body.Close()
  62. }
  63. func TestGetQRCode(t *testing.T) {
  64. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  65. if r.Method != "POST" {
  66. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  67. }
  68. ePath := r.URL.EscapedPath()
  69. if ePath != apiGetQrCode {
  70. t.Fatalf("Except to path '%s',get '%s'", apiGetQrCode, ePath)
  71. }
  72. if err := r.ParseForm(); err != nil {
  73. t.Fatal(err)
  74. }
  75. if r.Form.Get("access_token") == "" {
  76. t.Fatalf("access_token can not be empty")
  77. }
  78. params := struct {
  79. Path string `json:"path"`
  80. Width int `json:"width,omitempty"`
  81. AutoColor bool `json:"auto_color,omitempty"`
  82. LineColor Color `json:"line_color,omitempty"`
  83. IsHyaline bool `json:"is_hyaline,omitempty"`
  84. }{}
  85. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  86. t.Fatal(err)
  87. }
  88. if params.Path == "" {
  89. t.Error("Response column path can not be empty")
  90. }
  91. filename := testIMGName
  92. file, err := os.Open(filename)
  93. if err != nil {
  94. t.Fatal((err))
  95. }
  96. defer file.Close()
  97. ext := path.Ext(filename)
  98. ext = ext[1:len(ext)]
  99. w.Header().Set("Content-Type", "image/"+ext)
  100. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  101. w.WriteHeader(http.StatusOK)
  102. if _, err := io.Copy(w, file); err != nil {
  103. t.Fatal(err)
  104. }
  105. }))
  106. defer ts.Close()
  107. qr := QRCode{
  108. Path: "mock/path",
  109. Width: 430,
  110. AutoColor: true,
  111. LineColor: Color{"r", "g", "b"},
  112. IsHyaline: true,
  113. }
  114. resp, _, err := qr.get(ts.URL+apiGetQrCode, "mock-access-token")
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. resp.Body.Close()
  119. }
  120. func TestGetUnlimitedQRCode(t *testing.T) {
  121. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  122. if r.Method != "POST" {
  123. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  124. }
  125. ePath := r.URL.EscapedPath()
  126. if ePath != apiGetUnlimitedQRCode {
  127. t.Fatalf("Except to path '%s',get '%s'", apiGetUnlimitedQRCode, ePath)
  128. }
  129. if err := r.ParseForm(); err != nil {
  130. t.Fatal(err)
  131. }
  132. if r.Form.Get("access_token") == "" {
  133. t.Fatalf("access_token can not be empty")
  134. }
  135. params := struct {
  136. Scene string `json:"scene"`
  137. Page string `json:"page,omitempty"`
  138. Width int `json:"width,omitempty"`
  139. AutoColor bool `json:"auto_color,omitempty"`
  140. LineColor Color `json:"line_color,omitempty"`
  141. IsHyaline bool `json:"is_hyaline,omitempty"`
  142. }{}
  143. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  144. t.Fatal(err)
  145. }
  146. if params.Scene == "" {
  147. t.Error("Response column scene can not be empty")
  148. }
  149. filename := testIMGName
  150. file, err := os.Open(filename)
  151. if err != nil {
  152. t.Fatal((err))
  153. }
  154. defer file.Close()
  155. ext := path.Ext(filename)
  156. ext = ext[1:len(ext)]
  157. w.Header().Set("Content-Type", "image/"+ext)
  158. w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", path.Base(filename)))
  159. w.WriteHeader(http.StatusOK)
  160. if _, err := io.Copy(w, file); err != nil {
  161. t.Fatal(err)
  162. }
  163. }))
  164. defer ts.Close()
  165. qr := UnlimitedQRCode{
  166. Scene: "mock-scene-data",
  167. Page: "mock/page",
  168. Width: 430,
  169. AutoColor: true,
  170. LineColor: Color{"r", "g", "b"},
  171. IsHyaline: true,
  172. }
  173. resp, _, err := qr.get(ts.URL+apiGetUnlimitedQRCode, "mock-access-token")
  174. if err != nil {
  175. t.Fatal(err)
  176. }
  177. resp.Body.Close()
  178. }