Skip to content

Commit

Permalink
Cosmetic fixes in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
gavv committed Apr 4, 2023
1 parent 0a26f7b commit fc222dd
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 73 deletions.
2 changes: 1 addition & 1 deletion e2e/redirect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func testRedirects(t *testing.T, createFn func(httpexpect.Reporter) *httpexpect.
Status(http.StatusPermanentRedirect)
})

t.Run("max-redirects", func(t *testing.T) {
t.Run("max redirects", func(t *testing.T) {
t.Run("no max redirects set", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)
Expand Down
114 changes: 56 additions & 58 deletions e2e/retry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,7 @@ type retryController struct {
statuscode int
}

func (rc *retryController) Reset(countback, statuscode int) {
rc.mu.Lock()
defer rc.mu.Unlock()
rc.countback = countback
rc.statuscode = statuscode
}

func (rc *retryController) GetStatusCode() int {
func (rc *retryController) getStatus() int {
rc.mu.Lock()
defer rc.mu.Unlock()
if rc.countback > 0 {
Expand All @@ -35,20 +28,19 @@ func (rc *retryController) GetStatusCode() int {
return http.StatusOK
}

func (rc *retryController) reset(countback, statuscode int) {
rc.mu.Lock()
defer rc.mu.Unlock()
rc.countback = countback
rc.statuscode = statuscode
}

type transportController struct {
http.RoundTripper

mu sync.Mutex
countback int
}

func (tc *transportController) Reset(countback int) {
tc.mu.Lock()
defer tc.mu.Unlock()

tc.countback = countback
}

func (tc *transportController) RoundTrip(req *http.Request) (*http.Response, error) {
tc.mu.Lock()
defer tc.mu.Unlock()
Expand All @@ -61,6 +53,12 @@ func (tc *transportController) RoundTrip(req *http.Request) (*http.Response, err
return tc.RoundTripper.RoundTrip(req)
}

func (tc *transportController) reset(countback int) {
tc.mu.Lock()
defer tc.mu.Unlock()
tc.countback = countback
}

type timeoutNetworkErr struct {
error
}
Expand All @@ -79,7 +77,7 @@ func createRetryHandler(rc *retryController) http.Handler {
mux.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
b, _ := ioutil.ReadAll(r.Body)

w.WriteHeader(rc.GetStatusCode())
w.WriteHeader(rc.getStatus())
_, _ = w.Write(b)
})

Expand All @@ -90,7 +88,7 @@ func createRetryFastHandler(rc *retryController) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/test":
ctx.SetStatusCode(rc.GetStatusCode())
ctx.SetStatusCode(rc.getStatus())
ctx.SetBody(ctx.Request.Body())
}
}
Expand All @@ -105,27 +103,27 @@ func testRetries(
t.Run("MaxRetries", func(t *testing.T) {
e := createFn(httpexpect.NewAssertReporter(t))

rc.Reset(2, http.StatusInternalServerError)
rc.reset(2, http.StatusInternalServerError)
e.POST("/test").
WithText(`test`).
Expect().
Status(http.StatusInternalServerError).Body().IsEqual(`test`)

rc.Reset(2, http.StatusInternalServerError)
rc.reset(2, http.StatusInternalServerError)
e.POST("/test").
WithText(`test`).
WithMaxRetries(0).
Expect().
Status(http.StatusInternalServerError).Body().IsEqual(`test`)

rc.Reset(2, http.StatusInternalServerError)
rc.reset(2, http.StatusInternalServerError)
e.POST("/test").
WithText(`test`).
WithMaxRetries(1).
Expect().
Status(http.StatusInternalServerError).Body().IsEqual(`test`)

rc.Reset(2, http.StatusInternalServerError)
rc.reset(2, http.StatusInternalServerError)
e.POST("/test").
WithText(`test`).
WithMaxRetries(2).
Expand All @@ -134,12 +132,12 @@ func testRetries(
})

t.Run("DontRetry", func(t *testing.T) {
t.Run("Don't retry internal server error", func(t *testing.T) {
t.Run("dont retry internal server error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusInternalServerError)
tc.Reset(0)
rc.reset(1, http.StatusInternalServerError)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.DontRetry).
Expect().
Expand All @@ -148,12 +146,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Don't retry bad request", func(t *testing.T) {
t.Run("dont retry bad request", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusBadRequest)
tc.Reset(0)
rc.reset(1, http.StatusBadRequest)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.DontRetry).
Expect().
Expand All @@ -162,12 +160,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Don't retry timeout error", func(t *testing.T) {
t.Run("dont retry timeout error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(0, http.StatusOK)
tc.Reset(1)
rc.reset(0, http.StatusOK)
tc.reset(1)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.DontRetry).
Expect()
Expand All @@ -177,12 +175,12 @@ func testRetries(
})

t.Run("RetryTimeoutErrors", func(t *testing.T) {
t.Run("Don't retry internal server error", func(t *testing.T) {
t.Run("dont retry internal server error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusInternalServerError)
tc.Reset(0)
rc.reset(1, http.StatusInternalServerError)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutErrors).
Expect().
Expand All @@ -191,12 +189,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Don't retry bad request", func(t *testing.T) {
t.Run("dont retry bad request", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusBadRequest)
tc.Reset(0)
rc.reset(1, http.StatusBadRequest)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutErrors).
Expect().
Expand All @@ -205,12 +203,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Retry timeout error", func(t *testing.T) {
t.Run("retry timeout error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(0, http.StatusOK)
tc.Reset(1)
rc.reset(0, http.StatusOK)
tc.reset(1)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutErrors).
Expect().
Expand All @@ -221,12 +219,12 @@ func testRetries(
})

t.Run("RetryTimeoutAndServerErrors", func(t *testing.T) {
t.Run("Retry internal server", func(t *testing.T) {
t.Run("retry internal server", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusInternalServerError)
tc.Reset(0)
rc.reset(1, http.StatusInternalServerError)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutAndServerErrors).
Expect().
Expand All @@ -235,12 +233,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Retry bad request", func(t *testing.T) {
t.Run("retry bad request", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusBadRequest)
tc.Reset(0)
rc.reset(1, http.StatusBadRequest)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutAndServerErrors).
Expect().
Expand All @@ -249,12 +247,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Retry timeout error", func(t *testing.T) {
t.Run("retry timeout error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(0, http.StatusOK)
tc.Reset(1)
rc.reset(0, http.StatusOK)
tc.reset(1)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryTimeoutAndServerErrors).
Expect().
Expand All @@ -265,12 +263,12 @@ func testRetries(
})

t.Run("RetryAllErrors", func(t *testing.T) {
t.Run("Retry internal server error", func(t *testing.T) {
t.Run("retry internal server error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusInternalServerError)
tc.Reset(0)
rc.reset(1, http.StatusInternalServerError)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryAllErrors).
Expect().
Expand All @@ -279,12 +277,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Retry bad request", func(t *testing.T) {
t.Run("retry bad request", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(1, http.StatusBadRequest)
tc.Reset(0)
rc.reset(1, http.StatusBadRequest)
tc.reset(0)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryAllErrors).
Expect().
Expand All @@ -293,12 +291,12 @@ func testRetries(
assert.False(t, reporter.failed)
})

t.Run("Retry timeout errors", func(t *testing.T) {
t.Run("retry timeout error", func(t *testing.T) {
reporter := &mockReporter{}
e := createFn(reporter)

rc.Reset(0, http.StatusOK)
tc.Reset(1)
rc.reset(0, http.StatusOK)
tc.reset(1)
e.POST("/test").
WithMaxRetries(1).WithRetryPolicy(httpexpect.RetryAllErrors).
Expect().
Expand Down
Loading

0 comments on commit fc222dd

Please sign in to comment.