SimpleCloudNotifier/scnserver/test/util/requests.go

311 lines
9.7 KiB
Go
Raw Normal View History

package util
import (
"blackforestbytes.com/simplecloudnotifier/api/apierr"
"bytes"
"encoding/json"
"fmt"
"github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"io"
"mime/multipart"
"net/http"
"strings"
"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)
}
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)
}
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)
}
func RequestAuthGetRaw(t *testing.T, akey string, baseURL string, urlSuffix string) string {
return RequestAny[string](t, akey, "GET", baseURL, urlSuffix, nil, false)
}
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)
}
func RequestAuthPostRaw(t *testing.T, akey string, baseURL string, urlSuffix string, body any) string {
return RequestAny[string](t, akey, "POST", baseURL, urlSuffix, body, false)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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)
}
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 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 {
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"))
bytesbody := make([]byte, 0)
contentType := ""
if body != nil {
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()
case RawJSON:
bytesbody = []byte(body.(RawJSON).Body)
contentType = "application/json"
default:
bjson, err := json.Marshal(body)
if err != nil {
TestFailErr(t, err)
}
bytesbody = bjson
contentType = "application/json"
}
}
req, err := http.NewRequest(method, baseURL+urlSuffix, bytes.NewReader(bytesbody))
if err != nil {
TestFailErr(t, err)
}
if contentType != "" {
req.Header.Set("Content-Type", contentType)
}
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)))
}
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
2022-12-09 00:40:50 +01:00
TPrintln("---------------- -------- ----------------")
TPrintln("")
if resp.StatusCode != 200 {
TestFailFmt(t, "Statuscode != 200 (actual = %d)", resp.StatusCode)
}
2023-05-27 23:54:14 +02:00
if deserialize {
var data TResult
2023-05-27 23:54:14 +02:00
if err := json.Unmarshal(respBodyBin, &data); err != nil {
TestFailErr(t, err)
return data
}
return data
} else {
if _, ok := (any(*new(TResult))).([]byte); ok {
return any(respBodyBin).(TResult)
} else if _, ok := (any(*new(TResult))).(string); ok {
return any(string(respBodyBin)).(TResult)
} else {
return *new(TResult)
2023-05-27 23:54:14 +02:00
}
}
}
func RequestAuthAnyShouldFail(t *testing.T, akey string, method string, baseURL string, urlSuffix string, body any, expectedStatusCode int, errcode apierr.APIError) {
client := http.Client{}
TPrintf("[-> REQUEST] (%s) %s%s [%s] (should-fail with %d/%d)\n", method, baseURL, urlSuffix, langext.Conditional(akey == "", "NO AUTH", "AUTH"), expectedStatusCode, errcode)
bytesbody := make([]byte, 0)
2022-11-30 21:51:48 +01:00
contentType := ""
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"
}
}
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)
}
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)))
if (expectedStatusCode != 0 && resp.StatusCode != expectedStatusCode) || (expectedStatusCode == 0 && resp.StatusCode == 200) {
TryPrintTraceObj("---------------- -------- ----------------", respBodyBin, "")
}
2022-12-09 00:40:50 +01:00
TPrintln("---------------- -------- ----------------")
TPrintln("")
if expectedStatusCode != 0 && resp.StatusCode != expectedStatusCode {
TestFailFmt(t, "Statuscode != %d (expected failure, but got %d)", expectedStatusCode, resp.StatusCode)
}
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
}
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']")
}
}
}
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-12-09 00:40:50 +01:00
TPrintln(strings.TrimSpace(v3))
if suffix != "" {
2022-12-09 00:40:50 +01:00
TPrintln(suffix)
}
}
}
}
}