Skip to content

Commit

Permalink
feat (request): expose req timeout (#1878)
Browse files Browse the repository at this point in the history
* feat (request): expose req timeout as string

* refactor (http): update return type of GetTimeOut

* docs (http): add doc to GetTimeOut
  • Loading branch information
aarcex3 authored Oct 5, 2024
1 parent 28b610d commit 0b74893
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
10 changes: 10 additions & 0 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ func (req *Request) SetConnectionClose() {
req.Header.SetConnectionClose()
}

// GetTimeOut retrieves the timeout duration set for the Request.
//
// This method returns a time.Duration that determines how long the request
// can wait before it times out. In the default use case, the timeout applies
// to the entire request lifecycle, including both receiving the response
// headers and the response body.
func (req *Request) GetTimeOut() time.Duration {
return req.timeout
}

// SendFile registers file on the given path to be used as response body
// when Write is called.
//
Expand Down
23 changes: 23 additions & 0 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3174,3 +3174,26 @@ func TestRespCopeToRace(t *testing.T) {
}
}
}

func TestRequestGetTimeOut(t *testing.T) {
tests := []struct {
name string
timeout time.Duration
expected time.Duration
}{
{"Timeout set to 0", 0, 0},
{"Timeout set to 5s", 5 * time.Second, 5 * time.Second},
{"Timeout set to 1m", 1 * time.Minute, 1 * time.Minute},
{"Timeout set to 500ms", 500 * time.Millisecond, 500 * time.Millisecond},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
req := &Request{timeout: test.timeout}

if got := req.GetTimeOut(); got != test.expected {
t.Errorf("GetTimeOut() = %v, want %v", got, test.expected)
}
})
}
}

0 comments on commit 0b74893

Please # to comment.