1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-10 00:43:36 +02:00
pocketbase/tools/rest/multi_binder_test.go

109 lines
2.5 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package rest_test
import (
"encoding/json"
2022-07-06 23:19:05 +02:00
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"github.com/labstack/echo/v5"
"github.com/pocketbase/pocketbase/tools/rest"
)
func TestBindBody(t *testing.T) {
scenarios := []struct {
body io.Reader
contentType string
expectBody string
2022-07-06 23:19:05 +02:00
expectError bool
}{
{
strings.NewReader(""),
echo.MIMEApplicationJSON,
`{}`,
2022-07-06 23:19:05 +02:00
false,
},
{
strings.NewReader(`{"test":"invalid`),
echo.MIMEApplicationJSON,
`{}`,
2022-07-06 23:19:05 +02:00
true,
},
{
strings.NewReader(`{"test":123}`),
2022-07-06 23:19:05 +02:00
echo.MIMEApplicationJSON,
`{"test":123}`,
2022-07-06 23:19:05 +02:00
false,
},
{
strings.NewReader(
url.Values{
"string": []string{"str"},
2023-06-26 12:04:15 +02:00
"stings": []string{"str1", "str2", ""},
2023-06-26 11:42:53 +02:00
"number": []string{"-123"},
2023-06-26 12:04:15 +02:00
"numbers": []string{"123", "456.789"},
"bool": []string{"true"},
"bools": []string{"true", "false"},
}.Encode(),
),
2022-07-06 23:19:05 +02:00
echo.MIMEApplicationForm,
2023-06-26 12:04:15 +02:00
`{"bool":true,"bools":[true,false],"number":-123,"numbers":[123,456.789],"stings":["str1","str2",""],"string":"str"}`,
2022-07-06 23:19:05 +02:00
false,
},
}
for i, scenario := range scenarios {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", scenario.body)
req.Header.Set(echo.HeaderContentType, scenario.contentType)
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
data := map[string]any{}
err := rest.BindBody(c, &data)
2022-07-06 23:19:05 +02:00
hasErr := err != nil
if hasErr != scenario.expectError {
t.Errorf("[%d] Expected hasErr %v, got %v", i, scenario.expectError, hasErr)
2022-07-06 23:19:05 +02:00
}
rawBody, err := json.Marshal(data)
if err != nil {
t.Errorf("[%d] Failed to marshal binded body: %v", i, err)
2022-07-06 23:19:05 +02:00
}
if scenario.expectBody != string(rawBody) {
t.Errorf("[%d] Expected body \n%s, \ngot \n%s", i, scenario.expectBody, rawBody)
2022-07-06 23:19:05 +02:00
}
}
}
2022-10-30 10:28:14 +02:00
func TestCopyJsonBody(t *testing.T) {
2022-07-06 23:19:05 +02:00
req := httptest.NewRequest(http.MethodGet, "/", strings.NewReader(`{"test":"test123"}`))
// simulate multiple reads from the same request
result1 := map[string]string{}
2022-10-30 10:28:14 +02:00
rest.CopyJsonBody(req, &result1)
2022-07-06 23:19:05 +02:00
result2 := map[string]string{}
2022-10-30 10:28:14 +02:00
rest.CopyJsonBody(req, &result2)
2022-07-06 23:19:05 +02:00
if len(result1) == 0 {
t.Error("Expected result1 to be filled")
}
if len(result2) == 0 {
t.Error("Expected result2 to be filled")
}
if v, ok := result1["test"]; !ok || v != "test123" {
t.Errorf("Expected result1.test to be %q, got %q", "test123", v)
}
if v, ok := result2["test"]; !ok || v != "test123" {
t.Errorf("Expected result2.test to be %q, got %q", "test123", v)
}
}