api_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package test
  2. import (
  3. "encoding/json"
  4. "github.com/gogf/gf/frame/g"
  5. "github.com/gogf/gf/net/ghttp"
  6. "gtoken/gtoken"
  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 gtoken.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 gtoken.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 TestRefresh(t *testing.T) {
  78. // // 登录,访问用户信息
  79. // t.Log("1. execute login and visit user")
  80. // data := Post(t, "/system/user", "username="+Username)
  81. // if data.Success() {
  82. // t.Log(data.Json())
  83. // } else {
  84. // t.Error(data.Json())
  85. // }
  86. //
  87. // for i := 1; i < 9; i++ {
  88. // time.Sleep(2 * time.Second)
  89. // // 登录,访问用户信息
  90. // t.Log("1. execute login and visit user")
  91. // data = Post(t, "/system/user", "username="+Username)
  92. // if data.Success() {
  93. // t.Log(data.Json())
  94. // } else {
  95. // t.Error(data.Json())
  96. // }
  97. // }
  98. //
  99. //}
  100. func TestLogin(t *testing.T) {
  101. Username = "testLogin"
  102. t.Log(" login first ")
  103. token1 := getToken(t)
  104. t.Log("token:" + token1)
  105. t.Log(" login second and same token ")
  106. token2 := getToken(t)
  107. t.Log("token:" + token2)
  108. if token1 != token2 {
  109. t.Error("token not same ")
  110. }
  111. Username = "flyfox"
  112. }
  113. func TestLogout(t *testing.T) {
  114. Username = "testLogout"
  115. t.Log(" logout test ")
  116. data := Post(t, "/user/logout", "username="+Username)
  117. if data.Success() {
  118. t.Log(data.Json())
  119. } else {
  120. t.Error(data.Json())
  121. }
  122. Username = "flyfox"
  123. }
  124. func Post(t *testing.T, urlPath string, data ...interface{}) gtoken.Resp {
  125. client := ghttp.NewClient()
  126. client.SetHeader("Authorization", "Bearer "+getToken(t))
  127. content := client.RequestContent("POST", TestURL+urlPath, data...)
  128. var respData gtoken.Resp
  129. err := json.Unmarshal([]byte(content), &respData)
  130. if err != nil {
  131. t.Error(err)
  132. }
  133. return respData
  134. }
  135. func getToken(t *testing.T) string {
  136. if Token[Username] != "" {
  137. return Token[Username]
  138. }
  139. if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
  140. t.Error(e)
  141. } else {
  142. defer r.Close()
  143. content := string(r.ReadAll())
  144. var respData gtoken.Resp
  145. err := json.Unmarshal([]byte(content), &respData)
  146. if err != nil {
  147. t.Error(err)
  148. }
  149. if !respData.Success() {
  150. t.Error("resp fail:" + respData.Json())
  151. }
  152. Token[Username] = respData.GetString("token")
  153. }
  154. return Token[Username]
  155. }