Skip to content

Commit 261c045

Browse files
committed
test: TestClosedConnection
1 parent dd043ac commit 261c045

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed

client/rest/client.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,9 @@ func (s *restClient) send(req *http.Request) (*http.Response, error) {
255255

256256
// Try the request
257257
if res, err = s.http.Do(req); err != nil {
258-
// TODO: maybe add a test case for this?
259-
if strings.Contains(err.Error(), "An existing connection was forcibly closed by the remote host.") {
260-
261-
fmt.Printf("remote host force closed connection while requesting %s; attempt %d/%d; trying again", req.URL, retry+1, maxRetries)
258+
closedConnectionMsg := "An existing connection was forcibly closed by the remote host."
259+
if strings.Contains(err.Error(), closedConnectionMsg) || strings.HasSuffix(err.Error(), ": EOF") {
260+
fmt.Printf("remote host force closed connection while requesting %s; attempt %d/%d; trying again\n", req.URL, retry+1, maxRetries)
262261
backoff := math.Pow(5, float64(retry+1))
263262
time.Sleep(time.Second * time.Duration(backoff))
264263
continue

client/rest/client_test.go

+51-14
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,59 @@
1818
package rest
1919

2020
import (
21+
"fmt"
22+
"net/http"
23+
"net/http/httptest"
24+
2125
"testing"
26+
27+
"github.com/bloodhoundad/azurehound/v2/client/config"
2228
)
2329

2430
func TestClosedConnection(t *testing.T) {
25-
// var s *httptest.Server
26-
// var h http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
27-
// fmt.Println("closing client connections")
28-
// s.CloseClientConnections()
29-
// }
30-
// s = httptest.NewServer(h)
31-
// defer s.Close()
32-
33-
// res, err := RestClient.Get(gomock.Any(), s.URL)
34-
// if err == nil {
35-
// t.Fatalf("Something aint right, err should be nil %v", err)
36-
// }
37-
38-
// fmt.Printf("res:%v,err:%v", res, err)
31+
attempt := 0
32+
var testServer *httptest.Server
33+
var mockHandler http.HandlerFunc = func(w http.ResponseWriter, r *http.Request) {
34+
attempt++
35+
testServer.CloseClientConnections()
36+
}
37+
38+
testServer = httptest.NewServer(mockHandler)
39+
defer testServer.Close()
40+
41+
defaultConfig := config.Config{
42+
Username: "azurehound",
43+
Password: "we_collect",
44+
Authority: testServer.URL,
45+
}
46+
47+
if client, err := NewRestClient(testServer.URL, defaultConfig); err != nil {
48+
t.Fatalf("error initializing rest client %v", err)
49+
} else {
50+
if req, err := http.NewRequest(http.MethodGet, testServer.URL, nil); err != nil {
51+
t.Fatalf("error creating request %v", err)
52+
} else {
53+
didSucceed := false
54+
55+
// make request in separate goroutine so we can end it early after the first retry
56+
go func() {
57+
// end request on the second attempt after a closed connection
58+
if res, err := client.Send(req); err != nil {
59+
fmt.Println(err)
60+
} else if res.Status == "200" {
61+
didSucceed = true
62+
}
63+
}()
64+
65+
for attempt < 3 {
66+
if attempt > 1 {
67+
return
68+
}
69+
}
70+
71+
if didSucceed {
72+
t.Fatalf("expected an attempted retry but the request succeeded")
73+
}
74+
}
75+
}
3976
}

0 commit comments

Comments
 (0)