Skip to content

Commit 89bceeb

Browse files
committed
feat(ctx): add Context management to RequestConfig struct
- Add a Context field to the RequestConfig struct - Initialize the Context field with a default value in the New function - Implement a SetContext method to update the Context field - Update the initTest function to use the RequestConfig's Context instead of a new background context Signed-off-by: appleboy <[email protected]>
1 parent e8ffcc8 commit 89bceeb

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

gofight.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type RequestConfig struct {
9696
Cookies H
9797
Debug bool
9898
ContentType string
99+
Context context.Context
99100
}
100101

101102
// UploadFile for upload file struct
@@ -107,7 +108,9 @@ type UploadFile struct {
107108

108109
// New supply initial structure
109110
func New() *RequestConfig {
110-
return &RequestConfig{}
111+
return &RequestConfig{
112+
Context: context.Background(),
113+
}
111114
}
112115

113116
// SetDebug supply enable debug mode.
@@ -117,6 +120,23 @@ func (rc *RequestConfig) SetDebug(enable bool) *RequestConfig {
117120
return rc
118121
}
119122

123+
// SetContext sets the context for the RequestConfig.
124+
// This allows the request to be aware of deadlines, cancellation signals, and other request-scoped values.
125+
// It returns the updated RequestConfig instance.
126+
//
127+
// Parameters:
128+
//
129+
// ctx - the context to be set for the RequestConfig
130+
//
131+
// Returns:
132+
//
133+
// *RequestConfig - the updated RequestConfig instance with the new context
134+
func (rc *RequestConfig) SetContext(ctx context.Context) *RequestConfig {
135+
rc.Context = ctx
136+
137+
return rc
138+
}
139+
120140
// GET is request method.
121141
func (rc *RequestConfig) GET(path string) *RequestConfig {
122142
rc.Path = path
@@ -338,7 +358,7 @@ func (rc *RequestConfig) initTest() (*http.Request, *httptest.ResponseRecorder)
338358

339359
body := bytes.NewBufferString(rc.Body)
340360

341-
req, _ := http.NewRequestWithContext(context.Background(), rc.Method, rc.Path, body)
361+
req, _ := http.NewRequestWithContext(rc.Context, rc.Method, rc.Path, body)
342362
req.RequestURI = req.URL.RequestURI()
343363

344364
if len(qs) > 0 {

0 commit comments

Comments
 (0)