-
Notifications
You must be signed in to change notification settings - Fork 722
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
123 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,21 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"mime" | ||
"mime/multipart" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"path/filepath" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
@@ -1037,3 +1045,42 @@ func Benchmark_parseRequestBody_MultiPart(b *testing.B) { | |
} | ||
} | ||
} | ||
|
||
func TestSaveResponseToFile(t *testing.T) { | ||
c := dcnl() | ||
tempDir := t.TempDir() | ||
|
||
errDirMsg := "test dir error" | ||
mkdirAll = func(_ string, _ os.FileMode) error { | ||
return errors.New(errDirMsg) | ||
} | ||
errFileMsg := "test file error" | ||
createFile = func(_ string) (*os.File, error) { | ||
return nil, errors.New(errFileMsg) | ||
} | ||
t.Cleanup(func() { | ||
mkdirAll = os.MkdirAll | ||
createFile = os.Create | ||
}) | ||
|
||
// dir create error | ||
req1 := c.R() | ||
req1.SetOutputFile(filepath.Join(tempDir, "new-res-dir", "sample.txt")) | ||
err1 := saveResponseIntoFile(c, &Response{Request: req1}) | ||
assertEqual(t, errDirMsg, err1.Error()) | ||
|
||
// file create error | ||
req2 := c.R() | ||
req2.SetOutputFile(filepath.Join(tempDir, "sample.txt")) | ||
err2 := saveResponseIntoFile(c, &Response{Request: req2}) | ||
assertEqual(t, errFileMsg, err2.Error()) | ||
} | ||
|
||
func TestMiddlewareCoverage(t *testing.T) { | ||
c := dcnl() | ||
|
||
req1 := c.R() | ||
req1.URL = "//invalid-url .local" | ||
err1 := createHTTPRequest(c, req1) | ||
assertEqual(t, true, strings.Contains(err1.Error(), "invalid character")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
|
@@ -98,10 +99,8 @@ func (mf *MultipartField) openFileIfRequired() error { | |
return err | ||
} | ||
|
||
fileStat, err := file.Stat() | ||
if err != nil { | ||
return err | ||
} | ||
// if file open is success, stat will succeed | ||
fileStat, _ := file.Stat() | ||
|
||
mf.Reader = file | ||
mf.FileSize = fileStat.Size() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
// Copyright (c) 2015-present Jeevanandam M ([email protected]), All rights reserved. | ||
// resty source code and usage is governed by a MIT style | ||
// license that can be found in the LICENSE file. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package resty | ||
|
||
|
@@ -9,6 +10,7 @@ import ( | |
"context" | ||
"errors" | ||
"io/fs" | ||
"mime/multipart" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
|
@@ -475,6 +477,30 @@ func TestMultipartReaderErrors(t *testing.T) { | |
}) | ||
} | ||
|
||
type mpWriterError struct{} | ||
|
||
func (mwe *mpWriterError) Write(p []byte) (int, error) { | ||
return 0, errors.New("multipart write error") | ||
} | ||
|
||
func TestRequest_writeFormData(t *testing.T) { | ||
mw := multipart.NewWriter(&mpWriterError{}) | ||
|
||
c := dcnl() | ||
req1 := c.R().SetFormData(map[string]string{ | ||
"name1": "value1", | ||
"name2": "value2", | ||
}) | ||
|
||
err1 := req1.writeFormData(mw) | ||
assertNotNil(t, err1) | ||
assertEqual(t, "multipart write error", err1.Error()) | ||
|
||
err2 := createMultipart(mw, req1) | ||
assertNotNil(t, err2) | ||
assertEqual(t, "multipart write error", err2.Error()) | ||
} | ||
|
||
type returnValueTestWriter struct { | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters