http_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package http
  2. import (
  3. "context"
  4. "net/url"
  5. "testing"
  6. "time"
  7. "go-common/app/admin/main/reply/conf"
  8. "go-common/library/log"
  9. bm "go-common/library/net/http/blademaster"
  10. xtime "go-common/library/time"
  11. )
  12. const (
  13. _monitorState = "http://127.0.0.1:6711/x/internal/replyadmin/monitor/state"
  14. )
  15. func TestHttp(t *testing.T) {
  16. var (
  17. err error
  18. )
  19. if err = conf.Init(); err != nil {
  20. t.Errorf("conf.Init() error(%v)", err)
  21. t.FailNow()
  22. }
  23. log.Init(conf.Conf.Log)
  24. defer log.Close()
  25. client := bm.NewClient(&bm.ClientConfig{
  26. Dial: xtime.Duration(time.Second),
  27. Timeout: xtime.Duration(time.Second),
  28. })
  29. Init(conf.Conf)
  30. // test
  31. testMonitorState(client, t)
  32. }
  33. func testMonitorState(client *bm.Client, t *testing.T) {
  34. var err error
  35. params := url.Values{}
  36. params.Set("type", "1")
  37. params.Set("oid", "1")
  38. params.Set("state", "1")
  39. params.Set("adid", "11")
  40. params.Set("remark", "test")
  41. // send
  42. res := map[string]interface{}{}
  43. if err = client.Post(context.Background(), _monitorState, "", params, &res); err != nil {
  44. t.Errorf("client.Post() error(%v)", err)
  45. t.FailNow()
  46. }
  47. validRes(_monitorState, params, res, t)
  48. }
  49. func validRes(url string, params url.Values, res map[string]interface{}, t *testing.T) {
  50. if code, ok := res["code"]; ok && code.(float64) == 0 {
  51. t.Logf("\nurl:%s\nparams:%s\nres:%v", url, params.Encode(), res)
  52. } else {
  53. t.Errorf("\nurl:%s\nparams:%s\nres:%v", url, params.Encode(), res)
  54. }
  55. }