api_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. package test
  2. import (
  3. "encoding/json"
  4. "github.com/gogf/gf/g"
  5. "github.com/gogf/gf/g/net/ghttp"
  6. "gtoken/utils/resp"
  7. "testing"
  8. )
  9. const (
  10. TestURL string = "http://127.0.0.1:80"
  11. )
  12. var (
  13. Token = g.MapStrStr{}
  14. Username = "flyfox"
  15. )
  16. func TestHello(t *testing.T) {
  17. t.Log("visit hello and no auth")
  18. if r, e := ghttp.Post(TestURL+"/hello", "username="+Username); e != nil {
  19. t.Error(e)
  20. } else {
  21. defer r.Close()
  22. content := string(r.ReadAll())
  23. t.Log(content)
  24. var respData resp.Resp
  25. err := json.Unmarshal([]byte(content), &respData)
  26. if err != nil {
  27. t.Error(err)
  28. }
  29. if !respData.Success() {
  30. t.Error(respData.Json())
  31. }
  32. }
  33. }
  34. func TestSystemUser(t *testing.T) {
  35. // 未登录
  36. t.Log("1. not login and visit user")
  37. if r, e := ghttp.Post(TestURL+"/system/user", "username="+Username); e != nil {
  38. t.Error(e)
  39. } else {
  40. defer r.Close()
  41. content := string(r.ReadAll())
  42. t.Log(content)
  43. var respData resp.Resp
  44. err := json.Unmarshal([]byte(content), &respData)
  45. if err != nil {
  46. t.Error(err)
  47. }
  48. if respData.Success() {
  49. t.Error(respData.Json())
  50. }
  51. }
  52. // 登录,访问用户信息
  53. t.Log("2. execute login and visit user")
  54. data := Post(t, "/system/user", "username="+Username)
  55. if data.Success() {
  56. t.Log(data.Json())
  57. } else {
  58. t.Error(data.Json())
  59. }
  60. // 登出
  61. t.Log("3. execute logout")
  62. data = Post(t, "/user/logout", "username="+Username)
  63. if data.Success() {
  64. t.Log(data.Json())
  65. } else {
  66. t.Error(data.Json())
  67. }
  68. // 登出访问用户信息
  69. t.Log("4. visit user")
  70. data = Post(t, "/system/user", "username="+Username)
  71. if data.Success() {
  72. t.Error(data.Json())
  73. } else {
  74. t.Log(data.Json())
  75. }
  76. }
  77. func TestLogin(t *testing.T) {
  78. Username = "testLogin"
  79. t.Log(" login first ")
  80. token1 := getToken(t)
  81. t.Log("token:" + token1)
  82. t.Log(" login second and same token ")
  83. t.Log("token:" + getToken(t))
  84. if token1 != getToken(t) {
  85. t.Error("token not same ")
  86. }
  87. Username = "flyfox"
  88. }
  89. func TestLogout(t *testing.T) {
  90. Username = "testLogout"
  91. t.Log(" logout test ")
  92. data := Post(t, "/user/logout", "username="+Username)
  93. if data.Success() {
  94. t.Log(data.Json())
  95. } else {
  96. t.Error(data.Json())
  97. }
  98. Username = "flyfox"
  99. }
  100. func Post(t *testing.T, urlPath string, data ...interface{}) resp.Resp {
  101. client := ghttp.NewClient()
  102. client.SetHeader("Authorization", "Bearer "+getToken(t))
  103. content := client.DoRequestContent("POST", TestURL+urlPath, data...)
  104. var respData resp.Resp
  105. err := json.Unmarshal([]byte(content), &respData)
  106. if err != nil {
  107. t.Error(err)
  108. }
  109. return respData
  110. }
  111. func getToken(t *testing.T) string {
  112. if Token[Username] != "" {
  113. return Token[Username]
  114. }
  115. if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
  116. t.Error(e)
  117. } else {
  118. defer r.Close()
  119. content := string(r.ReadAll())
  120. var respData resp.Resp
  121. err := json.Unmarshal([]byte(content), &respData)
  122. if err != nil {
  123. t.Error(err)
  124. }
  125. if !respData.Success() {
  126. t.Error("resp fail:" + respData.Json())
  127. }
  128. Token[Username] = respData.GetString("token")
  129. }
  130. return Token[Username]
  131. }