-
Notifications
You must be signed in to change notification settings - Fork 17
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
refactor: Refactor -p/--publish flag test for run command #39
Changes from all commits
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 |
---|---|---|
|
@@ -4,31 +4,26 @@ | |
package fnet | ||
|
||
import ( | ||
"net" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/onsi/ginkgo/v2" | ||
"github.com/onsi/gomega" | ||
) | ||
|
||
// DialAndRead dials the network address, reads the data from the established connection, and asserts it against want. | ||
func DialAndRead(network, address, want string, maxRetry int, retryInterval time.Duration) { | ||
var ( | ||
conn net.Conn | ||
err error | ||
) | ||
// HTTPGetAndAssert sends an HTTP GET request to the specified URL, asserts the response status code against want, and closes the response body. | ||
func HTTPGetAndAssert(url string, want int, maxRetry int, retryInterval time.Duration) { | ||
var err error | ||
for i := 0; i < maxRetry; i++ { | ||
conn, err = net.Dial(network, address) | ||
// #nosec G107 // it does not matter if url is not a constant for testing. | ||
resp, err := http.Get(url) | ||
if err != nil { | ||
time.Sleep(retryInterval) | ||
continue | ||
} | ||
|
||
b := make([]byte, len([]byte(want))) //nolint:makezero // The content of b does not matter, | ||
// but len(b) must be equal to len([]byte(want)) so that conn.Read can read the whole thing. | ||
gomega.Expect(conn.Read(b)).Error().ShouldNot(gomega.HaveOccurred()) | ||
gomega.Expect(b).To(gomega.Equal([]byte(want))) | ||
gomega.Expect(conn.Close()).To(gomega.Succeed()) | ||
gomega.Expect(resp.StatusCode).To(gomega.Equal(want)) | ||
gomega.Expect(resp.Body.Close()).To(gomega.Succeed()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: Usually There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated in #42 |
||
return | ||
} | ||
ginkgo.Fail(err.Error()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The default http client does not have timeout, but we may want to set one (maybe just 3~5 seconds since the traffic is only on localhost) as a good practice. More details: https://stackoverflow.com/a/25344458/6355435
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in #42