Tests[RequestLogSimple]
This commit is contained in:
@@ -140,6 +140,10 @@ func AssertEqual(t *testing.T, key string, expected any, actual any) {
|
||||
|
||||
}
|
||||
|
||||
if langext.IsNil(expected) && langext.IsNil(actual) {
|
||||
return
|
||||
}
|
||||
|
||||
if expected != actual {
|
||||
t.Errorf("Value [%s] differs (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
@@ -173,7 +177,7 @@ func AssertTrue(t *testing.T, key string, v bool) {
|
||||
}
|
||||
|
||||
func AssertNotEqual(t *testing.T, key string, expected any, actual any) {
|
||||
if expected == actual {
|
||||
if expected == actual || (langext.IsNil(expected) && langext.IsNil(actual)) {
|
||||
t.Errorf("Value [%s] does not differ (%T <-> %T):\n", key, expected, actual)
|
||||
|
||||
str1 := fmt.Sprintf("%v", expected)
|
||||
|
||||
@@ -395,6 +395,57 @@ func InitDefaultData(t *testing.T, ws *logic.Application) DefData {
|
||||
return DefData{User: users}
|
||||
}
|
||||
|
||||
type SingleData struct {
|
||||
UserID string
|
||||
AdminKey string
|
||||
SendKey string
|
||||
ReadKey string
|
||||
ClientID string
|
||||
}
|
||||
|
||||
func InitSingleData(t *testing.T, ws *logic.Application) SingleData {
|
||||
|
||||
// set logger to buffer, only output if error occured
|
||||
success := false
|
||||
SetBufLogger()
|
||||
defer func() {
|
||||
ClearBufLogger(!success)
|
||||
if success {
|
||||
log.Info().Msgf("Succesfully initialized default data (%d messages, %d users)", len(messageExamples), len(userExamples))
|
||||
}
|
||||
}()
|
||||
|
||||
baseUrl := "http://127.0.0.1:" + ws.Port
|
||||
|
||||
type R struct {
|
||||
Clients []struct {
|
||||
ClientId string `json:"client_id"`
|
||||
UserId string `json:"user_id"`
|
||||
} `json:"clients"`
|
||||
ReadKey string `json:"read_key"`
|
||||
SendKey string `json:"send_key"`
|
||||
AdminKey string `json:"admin_key"`
|
||||
UserId string `json:"user_id"`
|
||||
}
|
||||
|
||||
r0 := RequestPost[R](t, baseUrl, "/api/v2/users", gin.H{
|
||||
"agent_model": "DUMMY_PHONE",
|
||||
"agent_version": "4X",
|
||||
"client_type": "ANDROID",
|
||||
"fcm_token": "DUMMY_FCM",
|
||||
})
|
||||
|
||||
success = true
|
||||
|
||||
return SingleData{
|
||||
UserID: r0.UserId,
|
||||
AdminKey: r0.AdminKey,
|
||||
SendKey: r0.SendKey,
|
||||
ReadKey: r0.ReadKey,
|
||||
ClientID: r0.Clients[0].ClientId,
|
||||
}
|
||||
}
|
||||
|
||||
func doSubscribe(t *testing.T, baseUrl string, user Userdat, chanOwner Userdat, chanInternalName string) {
|
||||
|
||||
if user == chanOwner {
|
||||
|
||||
@@ -14,44 +14,48 @@ import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func RequestRaw(t *testing.T, baseURL string, urlSuffix string) {
|
||||
RequestAny[Void](t, "", "GET", baseURL, urlSuffix, nil, false)
|
||||
}
|
||||
|
||||
func RequestGet[TResult any](t *testing.T, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil)
|
||||
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil, true)
|
||||
}
|
||||
|
||||
func RequestAuthGet[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string) TResult {
|
||||
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil)
|
||||
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil, true)
|
||||
}
|
||||
|
||||
func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPost[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestPut[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPUT[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestPatch[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthPatch[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestDelete[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body)
|
||||
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body, true)
|
||||
}
|
||||
|
||||
func RequestGetShouldFail(t *testing.T, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
||||
@@ -86,7 +90,7 @@ func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlS
|
||||
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
||||
}
|
||||
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any) TResult {
|
||||
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, deserialize bool) TResult {
|
||||
client := http.Client{}
|
||||
|
||||
TPrintf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY"))
|
||||
@@ -156,8 +160,10 @@ func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL s
|
||||
}
|
||||
|
||||
var data TResult
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
if deserialize {
|
||||
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
||||
TestFailErr(t, err)
|
||||
}
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
Reference in New Issue
Block a user