diff --git a/handler/request.go b/handler/request.go index 7892ec79..31fffbf4 100644 --- a/handler/request.go +++ b/handler/request.go @@ -1,7 +1,7 @@ package handler import ( - "encoding/json" + "encoding/json/v2" "fmt" "io" "log/slog" diff --git a/handler/request_test.go b/handler/request_test.go index 435d5fc2..22c68e19 100644 --- a/handler/request_test.go +++ b/handler/request_test.go @@ -325,3 +325,59 @@ func TestPackDataTree_EmptyTreeWritesNothing(t *testing.T) { t.Errorf("Body = %q, want nil", pld.Body) } } + +func TestPayloadJSON(t *testing.T) { + tests := []struct { + name string + request Request + wantBody string + wantUploads string + wantError bool + }{ + { + name: "form text", + request: Request{Parsed: true, body: dataTree{"field": ""}}, + wantBody: `{"field":""}`, + }, + { + name: "nested form", + request: Request{Parsed: true, body: dataTree{"field": dataTree{"nested": []string{"one", "two"}}}}, + wantBody: `{"field":{"nested":["one","two"]}}`, + }, + { + name: "invalid form text", + request: Request{Parsed: true, body: dataTree{"field": "\xff"}}, + wantError: true, + }, + { + name: "upload name", + request: Request{Uploads: &Uploads{tree: fileTree{"file": &FileUpload{Name: ".txt"}}}}, + wantUploads: `{"file":{"name":".txt","mime":"","size":0,"error":0,"tmpName":""}}`, + }, + { + name: "invalid upload name", + request: Request{Uploads: &Uploads{tree: fileTree{"file": &FileUpload{Name: "\xff.txt"}}}}, + wantError: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + pld := &payload.Payload{} + protoReq := &httpV1proto.Request{} + err := tt.request.Payload(pld, false, protoReq) + if (err != nil) != tt.wantError { + t.Fatalf("Payload() error = %v, want error %v", err, tt.wantError) + } + if tt.wantError { + return + } + if string(pld.Body) != tt.wantBody { + t.Errorf("Body = %q, want %q", pld.Body, tt.wantBody) + } + if string(protoReq.GetUploads()) != tt.wantUploads { + t.Errorf("Uploads = %q, want %q", protoReq.GetUploads(), tt.wantUploads) + } + }) + } +} diff --git a/handler/uploads.go b/handler/uploads.go index 477899ac..9f290879 100644 --- a/handler/uploads.go +++ b/handler/uploads.go @@ -1,7 +1,7 @@ package handler import ( - "encoding/json" + "encoding/json/v2" "io" "log/slog" "mime/multipart" diff --git a/tests/handler_body_test.go b/tests/handler_body_test.go index 401113fb..54cfc6ab 100644 --- a/tests/handler_body_test.go +++ b/tests/handler_body_test.go @@ -2,7 +2,7 @@ package tests import ( "bytes" - "encoding/json" + "encoding/json/v2" "io" "mime/multipart" "net/http" diff --git a/tests/serve_core_test.go b/tests/serve_core_test.go index 81dd6559..c7649072 100644 --- a/tests/serve_core_test.go +++ b/tests/serve_core_test.go @@ -2,7 +2,7 @@ package tests import ( "bytes" - "encoding/json" + "encoding/json/v2" "io" "mime/multipart" "net/http" diff --git a/tests/uploads_test.go b/tests/uploads_test.go index 5fc92810..8bb52699 100644 --- a/tests/uploads_test.go +++ b/tests/uploads_test.go @@ -4,7 +4,7 @@ import ( "bytes" "crypto/sha512" "encoding/hex" - "encoding/json" + "encoding/json/v2" "fmt" "io" "mime/multipart"