GOWA - WhatsApp REST API with support for UI, Multi Account, Webhooks, and MCP, and Chatwoot. Built with Golang for efficient memory use.
Go botgogolanggolang-whatsappgolang-whatsapp-apirestrest-apiwhatsappwhatsapp-apiwhatsapp-api-gowhatsapp-multi-devicewhatsapp-web-multi-device
fix: don't panic on non-multipart POST /send/file requests (#748)
Aldino Kemal255f72752a06
diff --git a/src/ui/rest/send.go b/src/ui/rest/send.goindex 76c3d48c..1aaf39a0 100644--- a/src/ui/rest/send.go+++ b/src/ui/rest/send.go@@ -76,10 +76,11 @@ func (controller *Send) SendFile(c *fiber.Ctx) error {err := c.BodyParser(&request)utils.PanicIfNeeded(err)- file, err := c.FormFile("file")- utils.PanicIfNeeded(err)+ // Try to get file but ignore error if not provided (e.g. file_url is used instead)+ if file, errFile := c.FormFile("file"); errFile == nil {+ request.File = file+ }- request.File = fileutils.SanitizePhone(&request.Phone)response, err := controller.Service.SendFile(whatsapp.ContextWithDevice(c.UserContext(), getDeviceFromCtx(c)), request)diff --git a/src/ui/rest/send_test.go b/src/ui/rest/send_test.gonew file mode 100644index 00000000..b916f991--- /dev/null+++ b/src/ui/rest/send_test.go@@ -0,0 +1,113 @@+package rest++import (+ "bytes"+ "context"+ "mime/multipart"+ "net/http"+ "net/http/httptest"+ "strings"+ "testing"++ domainSend "github.com/aldinokemal/go-whatsapp-web-multidevice/domains/send"+ "github.com/aldinokemal/go-whatsapp-web-multidevice/ui/rest/middleware"+ "github.com/gofiber/fiber/v2"+)++// sendFileStubUsecase implements domainSend.ISendUsecase by embedding the+// interface (so unrelated methods are never invoked by these tests) while+// recording the FileRequest actually received by SendFile.+type sendFileStubUsecase struct {+ domainSend.ISendUsecase+ receivedRequest domainSend.FileRequest+ called bool+}++func (s *sendFileStubUsecase) SendFile(_ context.Context, request domainSend.FileRequest) (domainSend.GenericResponse, error) {+ s.called = true+ s.receivedRequest = request+ return domainSend.GenericResponse{Status: "ok"}, nil+}++func newSendFileTestApp(stub *sendFileStubUsecase) *fiber.App {+ app := fiber.New()+ app.Use(middleware.Recovery())+ controller := Send{Service: stub}+ app.Post("/send/file", controller.SendFile)+ return app+}++// TestSendFileJSONBodyWithFileURLDoesNotPanic is a regression test for+// https://github.com/aldinokemal/go-whatsapp-web-multidevice/issues/744:+// a JSON request carrying file_url (no multipart file part) must reach the+// usecase instead of panicking into a 500 from the unguarded FormFile error.+func TestSendFileJSONBodyWithFileURLDoesNotPanic(t *testing.T) {+ stub := &sendFileStubUsecase{}+ app := newSendFileTestApp(stub)++ body := `{"phone":"[email protected]","file_url":"https://example.com/doc.pdf"}`+ req := httptest.NewRequest(http.MethodPost, "/send/file", strings.NewReader(body))+ req.Header.Set("Content-Type", "application/json")++ resp, err := app.Test(req)+ if err != nil {+ t.Fatalf("app.Test: %v", err)+ }++ if resp.StatusCode != fiber.StatusOK {+ t.Fatalf("status = %d, want %d (request should not panic into a 500)", resp.StatusCode, fiber.StatusOK)+ }+ if !stub.called {+ t.Fatalf("usecase SendFile was not called")+ }+ if stub.receivedRequest.FileURL == nil || *stub.receivedRequest.FileURL != "https://example.com/doc.pdf" {+ t.Fatalf("FileURL = %v, want https://example.com/doc.pdf", stub.receivedRequest.FileURL)+ }+ if stub.receivedRequest.File != nil {+ t.Fatalf("File = %+v, want nil since no multipart file part was sent", stub.receivedRequest.File)+ }+}++// TestSendFileMultipartStillPopulatesFile ensures the original multipart+// upload path keeps working after guarding the FormFile error.+func TestSendFileMultipartStillPopulatesFile(t *testing.T) {+ stub := &sendFileStubUsecase{}+ app := newSendFileTestApp(stub)++ var buf bytes.Buffer+ writer := multipart.NewWriter(&buf)+ if err := writer.WriteField("phone", "[email protected]"); err != nil {+ t.Fatalf("WriteField: %v", err)+ }+ part, err := writer.CreateFormFile("file", "doc.pdf")+ if err != nil {+ t.Fatalf("CreateFormFile: %v", err)+ }+ if _, err := part.Write([]byte("%PDF-1.4 fake content")); err != nil {+ t.Fatalf("write part: %v", err)+ }+ if err := writer.Close(); err != nil {+ t.Fatalf("writer.Close: %v", err)+ }++ req := httptest.NewRequest(http.MethodPost, "/send/file", &buf)+ req.Header.Set("Content-Type", writer.FormDataContentType())++ resp, err := app.Test(req)+ if err != nil {+ t.Fatalf("app.Test: %v", err)+ }++ if resp.StatusCode != fiber.StatusOK {+ t.Fatalf("status = %d, want %d", resp.StatusCode, fiber.StatusOK)+ }+ if !stub.called {+ t.Fatalf("usecase SendFile was not called")+ }+ if stub.receivedRequest.File == nil {+ t.Fatalf("File = nil, want populated multipart.FileHeader")+ }+ if stub.receivedRequest.File.Filename != "doc.pdf" {+ t.Fatalf("File.Filename = %q, want doc.pdf", stub.receivedRequest.File.Filename)+ }+}