Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multipart/form-data in PATCH request #536

Merged
merged 2 commits into from Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion middleware.go
Expand Up @@ -128,7 +128,7 @@ func parseRequestHeader(c *Client, r *Request) error {
func parseRequestBody(c *Client, r *Request) (err error) {
if isPayloadSupported(r.Method, c.AllowGetMethodPayload) {
// Handling Multipart
if r.isMultiPart && !(r.Method == MethodPatch) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sabandi HTTP spec is keep on getting better!

if r.isMultiPart {
if err = handleMultipart(c, r); err != nil {
return
}
Expand Down
19 changes: 19 additions & 0 deletions request_test.go
Expand Up @@ -685,6 +685,25 @@ func TestMultiPartUploadFile(t *testing.T) {
assertEqual(t, http.StatusOK, resp.StatusCode())
}

func TestMultiPartUploadFileViaPatch(t *testing.T) {
ts := createFormPatchServer(t)
defer ts.Close()
defer cleanupFiles(".testdata/upload")

basePath := getTestDataPath()

c := dc()
c.SetFormData(map[string]string{"zip_code": "00001", "city": "Los Angeles"})

resp, err := c.R().
SetFile("profile_img", filepath.Join(basePath, "test-img.png")).
SetContentLength(true).
Patch(ts.URL + "/upload")

assertError(t, err)
assertEqual(t, http.StatusOK, resp.StatusCode())
}

func TestMultiPartUploadFileError(t *testing.T) {
ts := createFormPostServer(t)
defer ts.Close()
Expand Down
48 changes: 48 additions & 0 deletions resty_test.go
Expand Up @@ -362,6 +362,54 @@ func createFormPostServer(t *testing.T) *httptest.Server {
return ts
}

func createFormPatchServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
t.Logf("Path: %v", r.URL.Path)
t.Logf("Content-Type: %v", r.Header.Get(hdrContentTypeKey))

if r.Method == MethodPatch {
_ = r.ParseMultipartForm(10e6)

if r.URL.Path == "/upload" {
t.Logf("FirstName: %v", r.FormValue("first_name"))
t.Logf("LastName: %v", r.FormValue("last_name"))

targetPath := filepath.Join(getTestDataPath(), "upload")
_ = os.MkdirAll(targetPath, 0700)

for _, fhdrs := range r.MultipartForm.File {
for _, hdr := range fhdrs {
t.Logf("Name: %v", hdr.Filename)
t.Logf("Header: %v", hdr.Header)
dotPos := strings.LastIndex(hdr.Filename, ".")

fname := fmt.Sprintf("%s-%v%s", hdr.Filename[:dotPos], time.Now().Unix(), hdr.Filename[dotPos:])
t.Logf("Write name: %v", fname)

infile, _ := hdr.Open()
f, err := os.OpenFile(filepath.Join(targetPath, fname), os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
t.Logf("Error: %v", err)
return
}
defer func() {
_ = f.Close()
}()
_, _ = io.Copy(f, infile)

_, _ = w.Write([]byte(fmt.Sprintf("File: %v, uploaded as: %v\n", hdr.Filename, fname)))
}
}

return
}
}
})

return ts
}

func createFilePostServer(t *testing.T) *httptest.Server {
ts := createTestServer(func(w http.ResponseWriter, r *http.Request) {
t.Logf("Method: %v", r.Method)
Expand Down