@@ -19,6 +19,7 @@ import (
1919 "path/filepath"
2020 "strconv"
2121 "strings"
22+ "sync"
2223 "testing"
2324 "time"
2425)
@@ -88,8 +89,8 @@ func TestMultipartUploadError(t *testing.T) {
8889 Post (ts .URL + "/upload" )
8990
9091 assertNotNil (t , err )
91- assertNotNil (t , resp )
92- assertTrue ( t , errors .Is (err , fs .ErrNotExist ))
92+ assertNil (t , resp )
93+ assertEqual ( t , true , errors .Is (err , fs .ErrNotExist ))
9394}
9495
9596func TestMultipartUploadFiles (t * testing.T ) {
@@ -572,8 +573,7 @@ func TestMultipartReaderErrors(t *testing.T) {
572573
573574 assertNotNil (t , err )
574575 assertEqual (t , errTestErrorReader , err )
575- assertNotNil (t , resp )
576- assertEqual (t , nil , resp .Body )
576+ assertNil (t , resp )
577577 })
578578
579579 t .Run ("multipart files with errorReader" , func (t * testing.T ) {
@@ -583,8 +583,7 @@ func TestMultipartReaderErrors(t *testing.T) {
583583
584584 assertNotNil (t , err )
585585 assertEqual (t , errTestErrorReader , err )
586- assertNotNil (t , resp )
587- assertEqual (t , nil , resp .Body )
586+ assertNil (t , resp )
588587 })
589588
590589 t .Run ("multipart with file not found" , func (t * testing.T ) {
@@ -593,9 +592,8 @@ func TestMultipartReaderErrors(t *testing.T) {
593592 Post ("/upload" )
594593
595594 assertNotNil (t , err )
596- assertTrue (t , errors .Is (err , fs .ErrNotExist ))
597- assertNotNil (t , resp )
598- assertEqual (t , nil , resp .Body )
595+ assertEqual (t , true , errors .Is (err , fs .ErrNotExist ))
596+ assertNil (t , resp )
599597 })
600598}
601599
@@ -668,6 +666,89 @@ func TestMultipartRequest_createMultipart(t *testing.T) {
668666 })
669667}
670668
669+ func TestMultipartUploadFailAutoErrorParse (t * testing.T ) {
670+ type ErrorResponse struct {
671+ Code int `json:"code"`
672+ Message string `json:"message"`
673+ }
674+
675+ ts := createTestServer (func (w http.ResponseWriter , r * http.Request ) {
676+ w .Header ().Set (hdrContentTypeKey , "application/json" )
677+ w .WriteHeader (http .StatusForbidden )
678+ _ , _ = w .Write ([]byte (`{ "code": 403, "message": "forbidden error message" }` ))
679+ })
680+ defer ts .Close ()
681+
682+ c := dcnl ()
683+
684+ t .Run ("single request" , func (t * testing.T ) {
685+ res , err := c .R ().
686+ SetFile ("profile_img" , filepath .Join (getTestDataPath (), "test-img.png" )).
687+ SetResultError (& ErrorResponse {}).
688+ Post (ts .URL )
689+
690+ assertErrorIs (t , io .ErrClosedPipe , err )
691+ assertEqual (t , http .StatusForbidden , res .StatusCode ())
692+
693+ er := res .ResultError ().(* ErrorResponse )
694+ assertEqual (t , 403 , er .Code )
695+ assertEqual (t , "forbidden error message" , er .Message )
696+ })
697+
698+ t .Run ("concurrent requests" , func (t * testing.T ) {
699+ concurrencyCount := 100
700+ wg := sync.WaitGroup {}
701+ for i := 0 ; i < concurrencyCount ; i ++ {
702+ wg .Add (1 )
703+ go func () {
704+ defer wg .Done ()
705+ res , _ := c .R ().
706+ SetFile ("profile_img" , filepath .Join (getTestDataPath (), "test-img.png" )).
707+ SetResultError (& ErrorResponse {}).
708+ Post (ts .URL )
709+
710+ er := res .ResultError ().(* ErrorResponse )
711+ assertEqual (t , http .StatusForbidden , res .StatusCode ())
712+ assertEqual (t , 403 , er .Code )
713+ assertEqual (t , "forbidden error message" , er .Message )
714+ }()
715+ }
716+ wg .Wait ()
717+ })
718+
719+ }
720+
721+ func TestMultipartConcurrentRequests (t * testing.T ) {
722+ ts := createFormPostServer (t )
723+ defer ts .Close ()
724+ defer cleanupFiles (".testdata/upload" )
725+
726+ c := dcnl ()
727+ c .SetFormData (map [string ]string {"zip_code" : "00001" , "city" : "Los Angeles" })
728+
729+ concurrencyCount := 100
730+ wg := sync.WaitGroup {}
731+ for i := 0 ; i < concurrencyCount ; i ++ {
732+ wg .Add (1 )
733+ go func () {
734+ defer wg .Done ()
735+ res , err := c .R ().
736+ SetFormData (map [string ]string {
737+ "welcome1" : "welcome value 1" ,
738+ "welcome2" : "welcome value 2" ,
739+ "welcome3" : "welcome value 3" ,
740+ }).
741+ SetFile ("profile_img" , filepath .Join (getTestDataPath (), "test-img.png" )).
742+ Post (ts .URL + "/upload" )
743+
744+ assertError (t , err )
745+ assertEqual (t , http .StatusOK , res .StatusCode ())
746+ assertEqual (t , true , strings .Contains (res .String (), "test-img.png" ))
747+ }()
748+ }
749+ wg .Wait ()
750+ }
751+
671752type returnValueTestWriter struct {
672753}
673754
0 commit comments