Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dprotaso committed Jun 25, 2024
1 parent 9b2f573 commit bf64ccd
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
6 changes: 3 additions & 3 deletions test/spoof/error_checks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func TestDNSError(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
resp, err := client.Do(req)
if err != nil {
if resp != nil {
defer resp.Body.Close()
}
if dnsError := isDNSError(err); tt.dnsError != dnsError {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestConnectionRefused(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
resp, err := client.Do(req)
if err != nil {
if resp != nil {
defer resp.Body.Close()
}
if connRefused := isConnectionRefused(err); tt.connRefused != connRefused {
Expand Down Expand Up @@ -144,7 +144,7 @@ func TestTCPTimeout(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
req, _ := http.NewRequest(http.MethodGet, tt.url, nil)
resp, err := client.Do(req)
if err != nil {
if resp != nil {
defer resp.Body.Close()
}
if tcpTimeout := isTCPTimeout(err); tt.tcpTimeout != tcpTimeout {
Expand Down
12 changes: 8 additions & 4 deletions test/spoof/spoof_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,16 @@ type fakeTransport struct {

func (ft *fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
call := ft.calls.Add(1)
if ft.response != nil && call == 2 {
// If both a response and an error is defined, we return just the response on
// the second call to simulate a retry that passes eventually.
if ft.response != nil && ft.err != nil {
if call == 2 {
return ft.response, nil
}
return nil, ft.err
} else if ft.response != nil {
return ft.response, nil
}
return ft.response, ft.err

return nil, ft.err
}

func TestSpoofingClient_CheckEndpointState(t *testing.T) {
Expand Down

0 comments on commit bf64ccd

Please sign in to comment.