附近小店
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

68 lines
1.4 KiB

  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestVerifySignature(t *testing.T) {
  9. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. if r.Method != "POST" {
  11. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  12. }
  13. path := r.URL.EscapedPath()
  14. if path != apiVerifySignature {
  15. t.Fatalf("Except to path '%s',get '%s'", apiVerifySignature, path)
  16. }
  17. if err := r.ParseForm(); err != nil {
  18. t.Fatal(err)
  19. }
  20. if r.Form.Get("access_token") == "" {
  21. t.Fatalf("access_token can not be empty")
  22. }
  23. params := struct {
  24. OpenID string `json:"openid"`
  25. JSONString string `json:"json_string"`
  26. JSONSignature string `json:"json_signature"`
  27. }{}
  28. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  29. t.Fatal(err)
  30. }
  31. if params.OpenID == "" {
  32. t.Error("Response column openid can not be empty")
  33. }
  34. if params.JSONString == "" {
  35. t.Error("Response column json_string can not be empty")
  36. }
  37. if params.JSONSignature == "" {
  38. t.Error("Response column json_signature can not be empty")
  39. }
  40. w.WriteHeader(http.StatusOK)
  41. raw := `{
  42. "errcode": 0,
  43. "errmsg": "ok",
  44. "is_ok": true
  45. }`
  46. if _, err := w.Write([]byte(raw)); err != nil {
  47. t.Fatal(err)
  48. }
  49. }))
  50. defer ts.Close()
  51. _, err := verifySignature(ts.URL+apiVerifySignature, "mock-access-token", "mock-open-id", "mock-data", "mock-signature")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. }