api_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. package test
  2. import (
  3. "encoding/json"
  4. "github.com/goflyfox/gtoken/gtoken"
  5. "github.com/gogf/gf/frame/g"
  6. "github.com/gogf/gf/net/ghttp"
  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("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("error:", err)
  28. }
  29. if !respData.Success() {
  30. t.Error("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("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("error:", err)
  47. }
  48. if respData.Success() {
  49. t.Error("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("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("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("error:", data.Json())
  73. } else {
  74. t.Log(data.Json())
  75. }
  76. }
  77. func TestUserLoginFail(t *testing.T) {
  78. // 登录失败
  79. t.Log("1. login fail ")
  80. if r, e := ghttp.Post(TestURL+"/login", "username=&passwd="); e != nil {
  81. t.Error("error:", e)
  82. } else {
  83. defer r.Close()
  84. content := string(r.ReadAll())
  85. var respData gtoken.Resp
  86. err := json.Unmarshal([]byte(content), &respData)
  87. if err != nil {
  88. t.Error("error:", err)
  89. }
  90. if respData.Success() {
  91. t.Error("error:", "login fail:"+respData.Json())
  92. }
  93. }
  94. }
  95. func TestExclude(t *testing.T) {
  96. // 未登录可以访问
  97. t.Log("1. exclude user info")
  98. if r, e := ghttp.Post(TestURL+"/system/user/info", "username="+Username); e != nil {
  99. t.Error("error:", e)
  100. } else {
  101. defer r.Close()
  102. content := string(r.ReadAll())
  103. t.Log(content)
  104. var respData gtoken.Resp
  105. err := json.Unmarshal([]byte(content), &respData)
  106. if err != nil {
  107. t.Error("error:", err)
  108. }
  109. if !respData.Success() {
  110. t.Error("error:", respData.Json())
  111. }
  112. }
  113. if r, e := ghttp.Post(TestURL+"/user/info", "username="+Username); e != nil {
  114. t.Error("error:", e)
  115. } else {
  116. defer r.Close()
  117. content := string(r.ReadAll())
  118. t.Log(content)
  119. var respData gtoken.Resp
  120. err := json.Unmarshal([]byte(content), &respData)
  121. if err != nil {
  122. t.Error("error:", err)
  123. }
  124. if !respData.Success() {
  125. t.Error("error:", respData.Json())
  126. }
  127. }
  128. }
  129. //func TestRefresh(t *testing.T) {
  130. // // 登录,访问用户信息
  131. // t.Log("1. execute login and visit user")
  132. // data := Post(t, "/system/user", "username="+Username)
  133. // if data.Success() {
  134. // t.Log(data.Json())
  135. // } else {
  136. // t.Error("error:", data.Json())
  137. // }
  138. //
  139. // for i := 1; i < 9; i++ {
  140. // time.Sleep(2 * time.Second)
  141. // // 登录,访问用户信息
  142. // t.Log("1. execute login and visit user")
  143. // data = Post(t, "/system/user", "username="+Username)
  144. // if data.Success() {
  145. // t.Log(data.Json())
  146. // } else {
  147. // t.Error("error:", data.Json())
  148. // }
  149. // }
  150. //
  151. //}
  152. func TestLogin(t *testing.T) {
  153. Username = "testLogin"
  154. t.Log(" login first ")
  155. token1 := getToken(t)
  156. t.Log("token:" + token1)
  157. t.Log(" login second and same token ")
  158. token2 := getToken(t)
  159. t.Log("token:" + token2)
  160. if token1 != token2 {
  161. t.Error("error:", "token not same ")
  162. }
  163. Username = "flyfox"
  164. }
  165. func TestLogout(t *testing.T) {
  166. Username = "testLogout"
  167. t.Log(" logout test ")
  168. data := Post(t, "/user/logout", "username="+Username)
  169. if data.Success() {
  170. t.Log(data.Json())
  171. } else {
  172. t.Error("error:", data.Json())
  173. }
  174. Username = "flyfox"
  175. }
  176. func Post(t *testing.T, urlPath string, data ...interface{}) gtoken.Resp {
  177. client := ghttp.NewClient()
  178. client.SetHeader("Authorization", "Bearer "+getToken(t))
  179. content := client.RequestContent("POST", TestURL+urlPath, data...)
  180. var respData gtoken.Resp
  181. err := json.Unmarshal([]byte(content), &respData)
  182. if err != nil {
  183. t.Error("error:", err)
  184. }
  185. return respData
  186. }
  187. func getToken(t *testing.T) string {
  188. if Token[Username] != "" {
  189. return Token[Username]
  190. }
  191. if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
  192. t.Error("error:", e)
  193. } else {
  194. defer r.Close()
  195. content := string(r.ReadAll())
  196. var respData gtoken.Resp
  197. err := json.Unmarshal([]byte(content), &respData)
  198. if err != nil {
  199. t.Error("error:", err)
  200. }
  201. if !respData.Success() {
  202. t.Error("error:", "resp fail:"+respData.Json())
  203. }
  204. Token[Username] = respData.GetString("token")
  205. }
  206. return Token[Username]
  207. }
  208. func TestMultiLogin(t *testing.T) {
  209. Username = "testLogin"
  210. t.Log(" TestMultiLogin start... ")
  211. var token1, token2 string
  212. if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
  213. t.Error("error:", e)
  214. } else {
  215. defer r.Close()
  216. content := string(r.ReadAll())
  217. t.Log("token1 content:" + content)
  218. var respData gtoken.Resp
  219. err := json.Unmarshal([]byte(content), &respData)
  220. if err != nil {
  221. t.Error("error:", err)
  222. }
  223. if !respData.Success() {
  224. t.Error("error:", "resp fail:"+respData.Json())
  225. }
  226. token1 = respData.GetString("token")
  227. }
  228. t.Log("token1:" + token1)
  229. if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
  230. t.Error("error:", e)
  231. } else {
  232. defer r.Close()
  233. content := string(r.ReadAll())
  234. t.Log("token2 content:" + content)
  235. var respData gtoken.Resp
  236. err := json.Unmarshal([]byte(content), &respData)
  237. if err != nil {
  238. t.Error("error:", err)
  239. }
  240. if !respData.Success() {
  241. t.Error("error:", "resp fail:"+respData.Json())
  242. }
  243. token2 = respData.GetString("token")
  244. }
  245. t.Log("token2:" + token2)
  246. if g.Config().GetBool("gtoken.multi-login") {
  247. if token1 != token2 {
  248. t.Error("error:", "token not same ")
  249. }
  250. } else {
  251. if token1 == token2 {
  252. t.Error("error:", "token same ")
  253. }
  254. }
  255. Username = "flyfox"
  256. }