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

search_submit_pages_test.go 1.3 KiB

3 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestSearchSubmitPages(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 != apiSearchSubmitPages {
  15. t.Fatalf("Except to path '%s',get '%s'", apiSearchSubmitPages, 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. Pages []struct {
  25. Path string `json:"path"`
  26. Query string `json:"query"`
  27. } `json:"pages"`
  28. }{}
  29. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  30. t.Fatal(err)
  31. }
  32. if len(params.Pages) != 1 {
  33. t.Fatal("param pages can not be empty")
  34. }
  35. w.WriteHeader(http.StatusOK)
  36. raw := `{
  37. "errcode": 0,
  38. "errmsg": "ok"
  39. }`
  40. if _, err := w.Write([]byte(raw)); err != nil {
  41. t.Fatal(err)
  42. }
  43. }))
  44. defer ts.Close()
  45. sender := SearchSubmitPages{
  46. []SearchSubmitPage{
  47. {
  48. Path: "/pages/index/index",
  49. Query: "id=test",
  50. },
  51. },
  52. }
  53. _, err := sender.send(ts.URL+apiSearchSubmitPages, "mock-access-token")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. }