process_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package http
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "net/url"
  8. "strings"
  9. "testing"
  10. . "github.com/smartystreets/goconvey/convey"
  11. )
  12. type result struct {
  13. Code int `gorm:"column:code" json:"code"`
  14. Message string `gorm:"column:message" json:"message"`
  15. }
  16. //http client
  17. func httpDo(method, uri, cookie string, params url.Values) (data *result, err error) {
  18. client := &http.Client{}
  19. req, err := http.NewRequest(method, uri, strings.NewReader(params.Encode()))
  20. if err != nil {
  21. return
  22. }
  23. req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
  24. req.Header.Set("Cookie", cookie)
  25. resp, err := client.Do(req)
  26. if err != nil {
  27. return
  28. }
  29. body, err := ioutil.ReadAll(resp.Body)
  30. if err != nil {
  31. return
  32. }
  33. defer resp.Body.Close()
  34. data = &result{}
  35. json.Unmarshal(body, data)
  36. return
  37. }
  38. func TestHttpDo(t *testing.T) {
  39. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  40. params := url.Values{"id": {"20"}, "state": {"3"}}
  41. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=429fca48067ef8234335cddda8a7d64ba46662e3d9448d38df6184f63a37e165"
  42. t.Error("print log")
  43. if res, err := httpDo("POST", uri, cookie, params); err != nil {
  44. fmt.Println("There are some error~")
  45. fmt.Printf("%s", err)
  46. } else {
  47. fmt.Println("code:", res.Code)
  48. fmt.Println("message:", res.Message)
  49. }
  50. }
  51. func TestProcess(t *testing.T) {
  52. Convey("if apply.State == 1, state =[2,3,4]; expect: code ==0", t, func() {
  53. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  54. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=f78e8fadc7f41048258acf43f5e9abee59f7b7a5bb6312b81fd31a21945fd21e"
  55. params := url.Values{"id": {"7"}, "state": {"2"}}
  56. res, err := httpDo("POST", uri, cookie, params)
  57. if err != nil {
  58. t.Error(err)
  59. } else {
  60. So(res.Code, ShouldEqual, 0)
  61. }
  62. })
  63. }
  64. func TestProcess2(t *testing.T) {
  65. Convey("if apply.State == 2, state =[2,3,4]; expect: code ==0", t, func() {
  66. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  67. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=f78e8fadc7f41048258acf43f5e9abee59f7b7a5bb6312b81fd31a21945fd21e"
  68. params := url.Values{"id": {"8"}, "state": {"3"}}
  69. res, err := httpDo("POST", uri, cookie, params)
  70. if err != nil {
  71. t.Error(err)
  72. } else {
  73. So(res.Code, ShouldEqual, 0)
  74. }
  75. })
  76. }
  77. func TestProcess3(t *testing.T) {
  78. Convey("if apply.State == 3, state =[2,3,4]; expect: code == -400", t, func() {
  79. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  80. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=f78e8fadc7f41048258acf43f5e9abee59f7b7a5bb6312b81fd31a21945fd21e"
  81. params := url.Values{"id": {"9"}, "state": {"2"}}
  82. res, err := httpDo("POST", uri, cookie, params)
  83. if err != nil {
  84. t.Error(err)
  85. } else {
  86. So(res.Code, ShouldEqual, -400)
  87. So(res.Message, ShouldEqual, "只有申请中和打回才可审核")
  88. }
  89. })
  90. }
  91. func TestProcess4(t *testing.T) {
  92. Convey("if apply.State == 4, state =[2,3,4]; expect: code == -400", t, func() {
  93. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  94. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=f78e8fadc7f41048258acf43f5e9abee59f7b7a5bb6312b81fd31a21945fd21e"
  95. params := url.Values{"id": {"10"}, "state": {"2"}}
  96. res, err := httpDo("POST", uri, cookie, params)
  97. if err != nil {
  98. t.Error(err)
  99. } else {
  100. So(res.Code, ShouldEqual, -400)
  101. So(res.Message, ShouldEqual, "只有申请中和打回才可审核")
  102. }
  103. })
  104. }
  105. func TestProcess5(t *testing.T) {
  106. Convey("if apply.State == 1, state !=[2,3,4]; expect: code == -400", t, func() {
  107. uri := "http://127.0.0.1:7331/x/admin/apm/canal/apply/approval/process"
  108. cookie := "username=hedan; _AJSESSIONID=d9dde52c35e2e0389fd8c345873c7d22; sven-apm=f78e8fadc7f41048258acf43f5e9abee59f7b7a5bb6312b81fd31a21945fd21e"
  109. params := url.Values{"id": {"11"}, "state": {"5"}}
  110. res, err := httpDo("POST", uri, cookie, params)
  111. if err != nil {
  112. t.Error(err)
  113. } else {
  114. So(res.Code, ShouldEqual, -400)
  115. So(res.Message, ShouldEqual, "state值范围2,3,4")
  116. }
  117. })
  118. }