http_test.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package http
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "sync"
  9. "testing"
  10. "go-common/app/service/openplatform/ticket-sales/conf"
  11. "go-common/app/service/openplatform/ticket-sales/service"
  12. "go-common/library/conf/paladin"
  13. httpx "go-common/library/net/http/blademaster"
  14. )
  15. var (
  16. once sync.Once
  17. client *httpx.Client
  18. syncOrderURL string
  19. )
  20. func startHTTP() {
  21. if err := paladin.Init(); err != nil {
  22. panic(err)
  23. }
  24. if err := paladin.Watch("ticket-sales.toml", conf.Conf); err != nil {
  25. panic(err)
  26. }
  27. client = httpx.NewClient(conf.Conf.HTTPClient.Read)
  28. Init(conf.Conf, service.New(conf.Conf))
  29. }
  30. func TestGetOrder(t *testing.T) {
  31. syncOrderURL = conf.Conf.UT.DistPrefix + "/distrib/getorder"
  32. fmt.Println(syncOrderURL)
  33. once.Do(startHTTP)
  34. params := url.Values{}
  35. params.Set("oid", "100000004421700")
  36. var err error
  37. var req *http.Request
  38. if req, err = client.NewRequest("GET", syncOrderURL, "127.0.0.1", params); err != nil {
  39. t.Errorf("http.NewRequest(GET, %s) error(%v)", syncOrderURL, err)
  40. t.FailNow()
  41. }
  42. reqURI := req.URL.String()
  43. t.Logf("req uri: %s\n", reqURI)
  44. var res struct {
  45. Code int `json:"code"`
  46. Data string `json:"data"`
  47. }
  48. if err = client.Do(context.TODO(), req, &res); err != nil {
  49. t.Errorf("client.Do() error(%v)", err)
  50. t.FailNow()
  51. }
  52. str, _ := json.Marshal(res)
  53. t.Logf("res: %+v", string(str))
  54. }