2022-11-30 13:57:55 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"io"
|
2022-11-30 20:23:31 +01:00
|
|
|
"mime/multipart"
|
2022-11-30 13:57:55 +01:00
|
|
|
"net/http"
|
2022-11-30 20:23:31 +01:00
|
|
|
"strings"
|
2022-11-30 13:57:55 +01:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2023-05-27 23:54:14 +02:00
|
|
|
func RequestRaw(t *testing.T, baseURL string, urlSuffix string) {
|
|
|
|
RequestAny[Void](t, "", "GET", baseURL, urlSuffix, nil, false)
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
func RequestGet[TResult any](t *testing.T, baseURL string, urlSuffix string) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, "", "GET", baseURL, urlSuffix, nil, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthGet[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, akey, "GET", baseURL, urlSuffix, nil, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestPost[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, "", "POST", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthPost[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, akey, "POST", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestPut[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, "", "PUT", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthPUT[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, akey, "PUT", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestPatch[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, "", "PATCH", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthPatch[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, akey, "PATCH", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestDelete[TResult any](t *testing.T, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, "", "DELETE", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthDelete[TResult any](t *testing.T, akey string, baseURL string, urlSuffix string, body any) TResult {
|
2023-05-27 23:54:14 +02:00
|
|
|
return RequestAny[TResult](t, akey, "DELETE", baseURL, urlSuffix, body, true)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
2022-11-30 17:58:04 +01:00
|
|
|
func RequestGetShouldFail(t *testing.T, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, "", "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestPostShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, "", "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestPatchShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, "", "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestDeleteShouldFail(t *testing.T, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, "", "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthGetShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, akey, "GET", baseURL, urlSuffix, nil, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthPostShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, akey, "POST", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthPatchShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, akey, "PATCH", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RequestAuthDeleteShouldFail(t *testing.T, akey string, baseURL string, urlSuffix string, body any, statusCode int, errcode apierr.APIError) {
|
|
|
|
RequestAuthAnyShouldFail(t, akey, "DELETE", baseURL, urlSuffix, body, statusCode, errcode)
|
|
|
|
}
|
|
|
|
|
2023-05-27 23:54:14 +02:00
|
|
|
func RequestAny[TResult any](t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, deserialize bool) TResult {
|
2022-11-30 13:57:55 +01:00
|
|
|
client := http.Client{}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintf("[-> REQUEST] (%s) %s%s [%s] [%s]\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), langext.Conditional(body == nil, "NO BODY", "BODY"))
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
bytesbody := make([]byte, 0)
|
2022-11-30 20:23:31 +01:00
|
|
|
contentType := ""
|
2022-11-30 13:57:55 +01:00
|
|
|
if body != nil {
|
2022-11-30 20:23:31 +01:00
|
|
|
switch bd := body.(type) {
|
|
|
|
case FormData:
|
|
|
|
bodybuffer := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(bodybuffer)
|
|
|
|
for bdk, bdv := range bd {
|
|
|
|
err := writer.WriteField(bdk, bdv)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
bytesbody = bodybuffer.Bytes()
|
|
|
|
contentType = writer.FormDataContentType()
|
2023-08-12 19:04:04 +02:00
|
|
|
case RawJSON:
|
|
|
|
bytesbody = []byte(body.(RawJSON).Body)
|
|
|
|
contentType = "application/json"
|
2022-11-30 20:23:31 +01:00
|
|
|
default:
|
|
|
|
bjson, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
bytesbody = bjson
|
|
|
|
contentType = "application/json"
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody))
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2022-11-30 20:23:31 +01:00
|
|
|
if contentType != "" {
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if akey != "" {
|
|
|
|
req.Header.Set("Authorization", "SCN "+akey)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
respBodyBin, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln("")
|
|
|
|
TPrintf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
|
2023-07-30 16:37:39 +02:00
|
|
|
if len(respBodyBin) > 100_000 {
|
|
|
|
TPrintln("[[RESPONSE TOO LONG]]")
|
|
|
|
} else {
|
|
|
|
TPrintln(langext.TryPrettyPrintJson(string(respBodyBin)))
|
|
|
|
}
|
2022-11-30 20:23:31 +01:00
|
|
|
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln("---------------- -------- ----------------")
|
|
|
|
TPrintln("")
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
2022-12-22 12:43:40 +01:00
|
|
|
TestFailFmt(t, "Statuscode != 200 (actual = %d)", resp.StatusCode)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var data TResult
|
2023-05-27 23:54:14 +02:00
|
|
|
if deserialize {
|
|
|
|
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, expectedStatusCode int, errcode apierr.APIError) {
|
2022-11-30 13:57:55 +01:00
|
|
|
client := http.Client{}
|
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
TPrintf("[-> REQUEST] (%s) %s%s [%s] (should-fail with %d/%d)\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), expectedStatusCode, errcode)
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
bytesbody := make([]byte, 0)
|
2022-11-30 21:51:48 +01:00
|
|
|
contentType := ""
|
2022-11-30 13:57:55 +01:00
|
|
|
if body != nil {
|
2022-11-30 21:51:48 +01:00
|
|
|
switch bd := body.(type) {
|
|
|
|
case FormData:
|
|
|
|
bodybuffer := &bytes.Buffer{}
|
|
|
|
writer := multipart.NewWriter(bodybuffer)
|
|
|
|
for bdk, bdv := range bd {
|
|
|
|
err := writer.WriteField(bdk, bdv)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
err := writer.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
bytesbody = bodybuffer.Bytes()
|
|
|
|
contentType = writer.FormDataContentType()
|
|
|
|
default:
|
|
|
|
bjson, err := json.Marshal(body)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
bytesbody = bjson
|
|
|
|
contentType = "application/json"
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody))
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2022-11-30 21:51:48 +01:00
|
|
|
if contentType != "" {
|
|
|
|
req.Header.Set("Content-Type", contentType)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if akey != "" {
|
|
|
|
req.Header.Set("Authorization", "SCN "+akey)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
defer func() { _ = resp.Body.Close() }()
|
|
|
|
|
|
|
|
respBodyBin, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln("")
|
|
|
|
TPrintf("---------------- RESPONSE (%d) ----------------\n", resp.StatusCode)
|
|
|
|
TPrintln(langext.TryPrettyPrintJson(string(respBodyBin)))
|
2022-12-22 11:22:36 +01:00
|
|
|
if (expectedStatusCode != 0 && resp.StatusCode != expectedStatusCode) || (expectedStatusCode == 0 && resp.StatusCode == 200) {
|
2022-11-30 22:29:12 +01:00
|
|
|
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
|
|
|
|
}
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln("---------------- -------- ----------------")
|
|
|
|
TPrintln("")
|
2022-11-30 13:57:55 +01:00
|
|
|
|
2022-12-22 11:22:36 +01:00
|
|
|
if expectedStatusCode != 0 && resp.StatusCode != expectedStatusCode {
|
|
|
|
TestFailFmt(t, "Statuscode != %d (expected failure, but got %d)", expectedStatusCode, resp.StatusCode)
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
2022-12-22 11:22:36 +01:00
|
|
|
if expectedStatusCode == 0 && resp.StatusCode == 200 {
|
|
|
|
TestFailFmt(t, "Statuscode == %d (expected any failure, but got %d)", resp.StatusCode, resp.StatusCode)
|
2022-12-01 14:45:31 +01:00
|
|
|
}
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
var data gin.H
|
|
|
|
if err := json.Unmarshal(respBodyBin, &data); err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := data["success"]; ok {
|
|
|
|
if v.(bool) {
|
|
|
|
TestFail(t, "Success == true (expected failure)")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TestFail(t, "missing response['success']")
|
|
|
|
}
|
|
|
|
|
2022-12-01 14:45:31 +01:00
|
|
|
if errcode != 0 {
|
|
|
|
if v, ok := data["error"]; ok {
|
|
|
|
if fmt.Sprintf("%v", v) != fmt.Sprintf("%v", errcode) {
|
|
|
|
TestFailFmt(t, "wrong errorcode (expected: %d), (actual: %v)", errcode, v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
TestFail(t, "missing response['error']")
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-11-30 20:23:31 +01:00
|
|
|
|
|
|
|
func TryPrintTraceObj(prefix string, body []byte, suffix string) {
|
|
|
|
v1 := gin.H{}
|
|
|
|
if err := json.Unmarshal(body, &v1); err == nil {
|
|
|
|
if v2, ok := v1["traceObj"]; ok {
|
|
|
|
if v3, ok := v2.(string); ok {
|
|
|
|
if prefix != "" {
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln(prefix)
|
2022-11-30 20:23:31 +01:00
|
|
|
}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln(strings.TrimSpace(v3))
|
2022-11-30 20:23:31 +01:00
|
|
|
|
|
|
|
if suffix != "" {
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln(suffix)
|
2022-11-30 20:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|