-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
🔥 Feature: Add TestConfig to app.Test() for configurable testing #3161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 11 commits
00405f7
71c153b
073ec97
228392e
6c006a0
b07e05d
3c4017e
6112c04
4a97d7b
3639a78
4a2a77d
3776184
78e32a7
20bac97
ca3efd2
1220df7
498df60
438b630
0326c07
6b6674f
334dbe8
b389aaa
98d2fcc
be650d6
250b836
7bae815
b6daa78
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import ( | |
| "encoding/xml" | ||
| "errors" | ||
| "fmt" | ||
| "io" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httputil" | ||
|
|
@@ -864,13 +865,33 @@ func (app *App) Hooks() *Hooks { | |
| return app.hooks | ||
| } | ||
|
|
||
| // TestConfig is a struct holding Test settings | ||
| type TestConfig struct { | ||
| // Sets a timeout duration for the test. | ||
| // | ||
| // Default: time.Second | ||
| Timeout time.Duration | ||
|
|
||
| // When set to true, the test will discard the | ||
| // current http response and give a timeout error. | ||
| // | ||
| // Default: true | ||
| ErrOnTimeout bool | ||
| } | ||
|
|
||
| // Test is used for internal debugging by passing a *http.Request. | ||
| // Timeout is optional and defaults to 1s, -1 will disable it completely. | ||
| func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Response, error) { | ||
| // Set timeout | ||
| to := 1 * time.Second | ||
| if len(timeout) > 0 { | ||
| to = timeout[0] | ||
| // Config is optional and defaults to a 1s error on timeout, | ||
| // -1 timeout will disable it completely. | ||
| func (app *App) Test(req *http.Request, config ...TestConfig) (*http.Response, error) { | ||
| // Default config | ||
| cfg := TestConfig{ | ||
| Timeout: time.Second, | ||
| ErrOnTimeout: true, | ||
| } | ||
|
|
||
| // Override config if provided | ||
| if len(config) > 0 { | ||
| cfg = config[0] | ||
| } | ||
|
|
||
| // Add Content-Length if not provided with body | ||
|
|
@@ -909,12 +930,15 @@ func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Respons | |
| }() | ||
|
|
||
| // Wait for callback | ||
| if to >= 0 { | ||
| if cfg.Timeout >= 0 { | ||
| // With timeout | ||
| select { | ||
| case err = <-channel: | ||
| case <-time.After(to): | ||
| return nil, fmt.Errorf("test: timeout error after %s", to) | ||
| case <-time.After(cfg.Timeout): | ||
| conn.Close() //nolint:errcheck, revive // It is fine to ignore the error here | ||
| if cfg.ErrOnTimeout { | ||
| return nil, fmt.Errorf("test: timeout error after %s", cfg.Timeout) | ||
|
||
| } | ||
| } | ||
| } else { | ||
| // Without timeout | ||
|
|
@@ -932,6 +956,9 @@ func (app *App) Test(req *http.Request, timeout ...time.Duration) (*http.Respons | |
| // Convert raw http response to *http.Response | ||
| res, err := http.ReadResponse(buffer, req) | ||
| if err != nil { | ||
| if errors.Is(err, io.ErrUnexpectedEOF) { | ||
| return nil, errors.New("test: got empty response") | ||
| } | ||
| return nil, fmt.Errorf("failed to read response: %w", err) | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.