mindxdl---common--test_tools.go
// Copyright (c) 2021. Huawei Technologies Co., Ltd. All rights reserved.
// Package common define common utils
package common
import (
"bytes"
"encoding/json"
"errors"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"huawei.com/mindxdl/base/common/constants"
"huawei.com/npu-exporter/hwlog"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
const (
// PUT is Put method
PUT = "PUT"
// GET is Get method
GET = "GET"
// POST is Post method
POST = "POST"
// DELETE is Delete method
DELETE = "DELETE"
// PATCH is Patch method
PATCH = "PATCH"
)
var (
// RoleCode is roleRode
RoleCode = constants.NormalUserRole
)
// TestSuccess if status is ok, pass
func TestSuccess(t *testing.T, getRes []byte, successWay string) {
var response ResultMsg
if err := json.Unmarshal(getRes, &response); err != nil {
hwlog.RunLog.Errorf("Unmarshal param happen err : %v", err)
return
}
status := response.Status
assert.Equal(t, successWay, status)
}
// TestSuccessData need response.Data is right
func TestSuccessData(t *testing.T, getRes []byte, data interface{}) {
var response ResultMsg
if err := json.Unmarshal(getRes, &response); err != nil {
hwlog.RunLog.Errorf("Unmarshal param happen err : %v", err)
return
}
assert.Equal(t, response.Data, data)
}
func performRequest(r *gin.Engine, method, path string, body io.Reader) *httptest.ResponseRecorder {
req, err := http.NewRequest(method, path, body)
if err != nil {
hwlog.RunLog.Errorf("NewRequest happen err : %v", err)
return nil
}
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("sessionid", "123456")
req.Header.Set("userid", "654321")
req.Header.Set("groupid", "111111")
req.Header.Set("roleid", "2")
req.Header.Set("roleCode", RoleCode)
w := httptest.NewRecorder()
r.ServeHTTP(w, req)
return w
}
// Get request get
func Get(router *gin.Engine, uri string) []byte {
w := performRequest(router, "GET", uri, nil)
result := w.Result()
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
hwlog.RunLog.Errorf("Get IOUtil readAll happen err : %v", err)
return nil
}
return body
}
// Delete request delete
func Delete(router *gin.Engine, uri string) []byte {
w := performRequest(router, "DELETE", uri, nil)
result := w.Result()
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
hwlog.RunLog.Errorf("Delete IOUtil readAll happen err : %v", err)
return nil
}
return body
}
// Put request put
func Put(router *gin.Engine, uri string, param map[string]interface{}) []byte {
jsonByte, err := json.Marshal(param)
if err != nil {
hwlog.RunLog.Errorf("Put Marshal param happen err : %v", err)
return nil
}
w := performRequest(router, "PUT", uri, bytes.NewReader(jsonByte))
result := w.Result()
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
hwlog.RunLog.Errorf("Delete IOUtil readAll happen err : %v", err)
return nil
}
return body
}
// RestfulAPI include post get put delete for ut
func RestfulAPI(router *gin.Engine, operate, uri string, param map[string]interface{}) []byte {
header := map[string]string{
"Content-Type": "application/json;charset=UTF-8",
"requestid": "123456",
"userid": "654321",
"groupid": "111111",
"roleid": "2",
"roleCode": RoleCode,
}
return RestfulAPIV2(router, operate, uri, header, param)
}
// RestfulAPIV2 RestfulAPIV2
func RestfulAPIV2(router *gin.Engine, operate, uri string, header map[string]string,
param map[string]interface{}) []byte {
var paramStr string
if len(param) != 0 {
jsonByte, err := json.Marshal(param)
if err != nil {
hwlog.RunLog.Errorf("%s Marshal param happen err : %v", operate, err)
return nil
}
paramStr = string(jsonByte)
}
handler := new(HTTPHandler)
req, err := handler.NewRequest(operate, uri, paramStr)
if err != nil {
hwlog.RunLog.Errorf("NewRequest happen err : %v", err)
return nil
}
handler.SetHeader(req, header)
res := httptest.NewRecorder()
router.ServeHTTP(res, req)
result := res.Result()
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
hwlog.RunLog.Errorf("%s IOUtil readAll happen err : %v", operate, err)
return nil
}
return body
}
// ParseResponseToStruct parse response to struct
func ParseResponseToStruct(rspBytes []byte) (*ResultMsg, error) {
if len(rspBytes) == 0 {
return nil, errors.New("no response")
}
var response ResultMsg
if err := json.Unmarshal(rspBytes, &response); err != nil {
hwlog.RunLog.Errorf("Unmarshal param happen err : %v", err)
return nil, err
}
return &response, nil
}
本文来自博客园,作者:易先讯,转载请注明原文链接:https://www.cnblogs.com/gongxianjin/p/16683151.html