123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- package test
- import (
- "encoding/json"
- "github.com/gogf/gf/g"
- "github.com/gogf/gf/g/net/ghttp"
- "gtoken/utils/resp"
- "testing"
- )
- const (
- TestURL string = "http://127.0.0.1:80"
- )
- var (
- Token = g.MapStrStr{}
- Username = "flyfox"
- )
- func TestHello(t *testing.T) {
- t.Log("visit hello and no auth")
- if r, e := ghttp.Post(TestURL+"/hello", "username="+Username); e != nil {
- t.Error(e)
- } else {
- defer r.Close()
- content := string(r.ReadAll())
- t.Log(content)
- var respData resp.Resp
- err := json.Unmarshal([]byte(content), &respData)
- if err != nil {
- t.Error(err)
- }
- if !respData.Success() {
- t.Error(respData.Json())
- }
- }
- }
- func TestSystemUser(t *testing.T) {
- // 未登录
- t.Log("1. not login and visit user")
- if r, e := ghttp.Post(TestURL+"/system/user", "username="+Username); e != nil {
- t.Error(e)
- } else {
- defer r.Close()
- content := string(r.ReadAll())
- t.Log(content)
- var respData resp.Resp
- err := json.Unmarshal([]byte(content), &respData)
- if err != nil {
- t.Error(err)
- }
- if respData.Success() {
- t.Error(respData.Json())
- }
- }
- // 登录,访问用户信息
- t.Log("2. execute login and visit user")
- data := Post(t, "/system/user", "username="+Username)
- if data.Success() {
- t.Log(data.Json())
- } else {
- t.Error(data.Json())
- }
- // 登出
- t.Log("3. execute logout")
- data = Post(t, "/user/logout", "username="+Username)
- if data.Success() {
- t.Log(data.Json())
- } else {
- t.Error(data.Json())
- }
- // 登出访问用户信息
- t.Log("4. visit user")
- data = Post(t, "/system/user", "username="+Username)
- if data.Success() {
- t.Error(data.Json())
- } else {
- t.Log(data.Json())
- }
- }
- func TestLogin(t *testing.T) {
- Username = "testLogin"
- t.Log(" login first ")
- token1 := getToken(t)
- t.Log("token:" + token1)
- t.Log(" login second and same token ")
- t.Log("token:" + getToken(t))
- if token1 != getToken(t) {
- t.Error("token not same ")
- }
- Username = "flyfox"
- }
- func TestLogout(t *testing.T) {
- Username = "testLogout"
- t.Log(" logout test ")
- data := Post(t, "/user/logout", "username="+Username)
- if data.Success() {
- t.Log(data.Json())
- } else {
- t.Error(data.Json())
- }
- Username = "flyfox"
- }
- func Post(t *testing.T, urlPath string, data ...interface{}) resp.Resp {
- client := ghttp.NewClient()
- client.SetHeader("Authorization", "Bearer "+getToken(t))
- content := client.DoRequestContent("POST", TestURL+urlPath, data...)
- var respData resp.Resp
- err := json.Unmarshal([]byte(content), &respData)
- if err != nil {
- t.Error(err)
- }
- return respData
- }
- func getToken(t *testing.T) string {
- if Token[Username] != "" {
- return Token[Username]
- }
- if r, e := ghttp.Post(TestURL+"/login", "username="+Username+"&passwd=123456"); e != nil {
- t.Error(e)
- } else {
- defer r.Close()
- content := string(r.ReadAll())
- var respData resp.Resp
- err := json.Unmarshal([]byte(content), &respData)
- if err != nil {
- t.Error(err)
- }
- if !respData.Success() {
- t.Error("resp fail:" + respData.Json())
- }
- Token[Username] = respData.GetString("token")
- }
- return Token[Username]
- }
|