Skip to content

Commit

Permalink
add patch tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sabandi authored and jeevatkm committed Mar 6, 2023
1 parent 88265ff commit 2f4e675
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
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

0 comments on commit 2f4e675

Please sign in to comment.