Skip to content

Commit

Permalink
fix: XTestSysMenuPage
Browse files Browse the repository at this point in the history
  • Loading branch information
2637309949 committed Apr 26, 2021
1 parent 9428242 commit b7ef1a6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 39 deletions.
47 changes: 11 additions & 36 deletions platform/x_platform_test.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
package main

import (
"bufio"
"encoding/json"
"net/http/httptest"
"strings"
)

var AccessToken string

func XTestSysUserLogin() {
reader := bufio.NewReader(strings.NewReader(`{"domain": "localhost","name": "admin","password": "admin"}`))
httpTools.Post("/api/sys/user/login", reader, func(w *httptest.ResponseRecorder) {
type (
Data struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
func XTestSysMenuPage(t testingT) {
httpTools.Get("/api/sys/menu/page",
func(w *httptest.ResponseRecorder) {
if w.Code != 200 {
t.Errorf("SysUserLogin = %v want %v", w.Code, 200)
}
AutoGenerated struct {
Code int `json:"code"`
Data Data `json:"data"`
ret := Response{}
json.Unmarshal(w.Body.Bytes(), &ret)
if ret.Code != 401 {
t.Errorf("SysMenuPage = %v want %v", ret.Code, 200)
}
)
ret := AutoGenerated{}
json.Unmarshal(w.Body.Bytes(), &ret)
AccessToken = ret.Data.AccessToken
})
}

func XTestSysMenuPage(t testingT) {
httpTools.Get("/api/sys/menu/page", func(w *httptest.ResponseRecorder) {
type AutoGenerated struct {
Code int `json:"code"`
Msg string `json:"msg"`
}
if w.Code != 200 {
t.Errorf("SysUserLogin = %v want %v", w.Code, 401)
}
ret := AutoGenerated{}
json.Unmarshal(w.Body.Bytes(), &ret)
if ret.Code != 200 {
t.Errorf("SysMenuPage = %v want %v", ret.Code, "200")
}
})
},
)
}
50 changes: 47 additions & 3 deletions platform/x_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package main

import (
"io"
"bufio"
"encoding/json"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"time"

// "github.com/2637309949/dolphin/platform/conf"
_ "github.com/2637309949/dolphin/platform/conf"

// "github.com/mattn/go-sqlite3"
_ "github.com/mattn/go-sqlite3"

Expand Down Expand Up @@ -42,6 +45,13 @@ type HTTPTools struct {
*gin.Engine
}

// Response defined
type Response struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Msg string `json:"msg"`
}

func (t HTTPTools) handler(req *http.Request, h func(w *httptest.ResponseRecorder)) {
w := httptest.NewRecorder()
req.Header.Add("Authorization", "Bearer "+AccessToken)
Expand All @@ -54,8 +64,9 @@ func (t HTTPTools) Get(url string, h func(w *httptest.ResponseRecorder)) {
t.handler(req, h)
}

func (t HTTPTools) Post(url string, body io.Reader, h func(w *httptest.ResponseRecorder)) {
req, _ := http.NewRequest("POST", url, body)
func (t HTTPTools) Post(url string, payform map[string]interface{}, h func(w *httptest.ResponseRecorder)) {
jm, _ := json.Marshal(&payform)
req, _ := http.NewRequest("POST", url, bufio.NewReader(strings.NewReader(string(jm))))
req.Header.Add("Content-Type", "application/json")
t.handler(req, h)
}
Expand All @@ -70,4 +81,37 @@ func TestMain(m *testing.M) {
os.Exit(m.Run())
}

// SetToken defined
func SetToken(token string) {
AccessToken = token
}

var AccessToken string

// XTestSysUserLogin defined
func XTestSysUserLogin() {
type (
Data struct {
AccessToken string `json:"access_token"`
RefreshToken string `json:"refresh_token"`
}
AutoGenerated struct {
Code int `json:"code"`
Data Data `json:"data"`
}
)
httpTools.Post("/api/sys/user/login",
map[string]interface{}{
"domain": "localhost",
"name": "admin",
"password": "admin",
},
func(w *httptest.ResponseRecorder) {
ret := AutoGenerated{}
json.Unmarshal(w.Body.Bytes(), &ret)
SetToken(ret.Data.AccessToken)
},
)
}

func TestSysMenuPage(t *testing.T) { XTestSysMenuPage(t) }

0 comments on commit b7ef1a6

Please sign in to comment.