Skip to content

Commit

Permalink
Debug info when some errors happen
Browse files Browse the repository at this point in the history
  • Loading branch information
Albert Callarisa committed Jul 23, 2022
1 parent 16b8bf9 commit 870d9a0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions test/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func (s *Suite) TearDownTest() {
}

func (s *Suite) testClient(bucket, token string) *TestClient {
return newTestClient(s.T(), fmt.Sprintf("localhost:%v", s.MitmPort), s.FakeServerAddr, bucket, token)
return newTestClient(s, fmt.Sprintf("localhost:%v", s.MitmPort), s.FakeServerAddr, bucket, token)
}

func (s *Suite) network() {
Expand Down Expand Up @@ -106,7 +106,6 @@ func (s *Suite) waitForMitm() {
for max > 0 {
logs = s.cmd("docker logs " + s.MitmContainer)
if strings.Contains(logs, mustContain) {
time.Sleep(1 * time.Second)
return
}
time.Sleep(50 * time.Millisecond)
Expand Down Expand Up @@ -163,6 +162,11 @@ func (s *Suite) cmd(c string) string {
return strings.TrimSpace(string(res))
}

func (s *Suite) logMitmLogs() {
logs := s.cmd("docker logs " + s.MitmContainer)
s.T().Log("MITM Logs: ", logs)
}

func TestRunSuite(t *testing.T) {
suite.Run(t, new(Suite))
}
20 changes: 10 additions & 10 deletions test/test_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,17 @@ import (
"io/ioutil"
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/require"
)

type TestClient struct {
t *testing.T
s *Suite
httpClient *http.Client
addr string
}

func newTestClient(t *testing.T, host, addr, bucket, token string) *TestClient {
func newTestClient(s *Suite, host, addr, bucket, token string) *TestClient {
return &TestClient{
t: t,
s: s,
addr: addr,
httpClient: &http.Client{
Transport: &http.Transport{
Expand All @@ -33,17 +30,20 @@ func newTestClient(t *testing.T, host, addr, bucket, token string) *TestClient {

func (c *TestClient) Get() string {
res, err := c.httpClient.Get(c.addr)
require.NoError(c.t, err)
require.Equal(c.t, 200, res.StatusCode)
c.s.Require().NoError(err)
if res.StatusCode != 200 {
c.s.logMitmLogs()
c.s.Require().Equal(200, res.StatusCode)
}
for _, cookie := range res.Cookies() {
require.NotEqual(c.t, "test-cookie", cookie.Name)
c.s.Require().NotEqual("test-cookie", cookie.Name)
}
return c.readBody(res)
}

func (c *TestClient) readBody(res *http.Response) string {
all, err := ioutil.ReadAll(res.Body)
require.NoError(c.t, err)
c.s.Require().NoError(err)
defer res.Body.Close()

return string(all)
Expand Down

0 comments on commit 870d9a0

Please sign in to comment.