Skip to content

Commit

Permalink
fix(gateway): ensure 'X-Ipfs-Root' header is valid (#337)
Browse files Browse the repository at this point in the history
Co-authored-by: Marcin Rataj <lidel@lidel.org>
  • Loading branch information
hacdias and lidel committed Jun 29, 2023
1 parent 120ccc0 commit fca7937
Show file tree
Hide file tree
Showing 21 changed files with 1,212 additions and 1,010 deletions.
39 changes: 25 additions & 14 deletions gateway/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,35 @@ import (
"testing"
"time"

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

func TestErrRetryAfterIs(t *testing.T) {
t.Parallel()
var err error

err = NewErrorRetryAfter(errors.New("test"), 10*time.Second)
assert.True(t, errors.Is(err, &ErrorRetryAfter{}), "pointer to error must be error")
require.True(t, errors.Is(err, &ErrorRetryAfter{}), "pointer to error must be error")

err = fmt.Errorf("wrapped: %w", err)
assert.True(t, errors.Is(err, &ErrorRetryAfter{}), "wrapped pointer to error must be error")
require.True(t, errors.Is(err, &ErrorRetryAfter{}), "wrapped pointer to error must be error")
}

func TestErrRetryAfterAs(t *testing.T) {
t.Parallel()

var (
err error
errRA *ErrorRetryAfter
)

err = NewErrorRetryAfter(errors.New("test"), 25*time.Second)
assert.True(t, errors.As(err, &errRA), "pointer to error must be error")
assert.EqualValues(t, errRA.RetryAfter, 25*time.Second)
require.True(t, errors.As(err, &errRA), "pointer to error must be error")
require.EqualValues(t, errRA.RetryAfter, 25*time.Second)

err = fmt.Errorf("wrapped: %w", err)
assert.True(t, errors.As(err, &errRA), "wrapped pointer to error must be error")
assert.EqualValues(t, errRA.RetryAfter, 25*time.Second)
require.True(t, errors.As(err, &errRA), "wrapped pointer to error must be error")
require.EqualValues(t, errRA.RetryAfter, 25*time.Second)
}

func TestWebError(t *testing.T) {
Expand All @@ -43,37 +46,45 @@ func TestWebError(t *testing.T) {
config := &Config{Headers: map[string][]string{}}

t.Run("429 Too Many Requests", func(t *testing.T) {
t.Parallel()

err := fmt.Errorf("wrapped for testing: %w", NewErrorRetryAfter(ErrTooManyRequests, 0))
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
webError(w, r, config, err, http.StatusInternalServerError)
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode)
assert.Zero(t, len(w.Result().Header.Values("Retry-After")))
require.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode)
require.Zero(t, len(w.Result().Header.Values("Retry-After")))
})

t.Run("429 Too Many Requests with Retry-After header", func(t *testing.T) {
t.Parallel()

err := NewErrorRetryAfter(ErrTooManyRequests, 25*time.Second)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
webError(w, r, config, err, http.StatusInternalServerError)
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode)
assert.Equal(t, "25", w.Result().Header.Get("Retry-After"))
require.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode)
require.Equal(t, "25", w.Result().Header.Get("Retry-After"))
})

t.Run("503 Service Unavailable with Retry-After header", func(t *testing.T) {
t.Parallel()

err := NewErrorRetryAfter(ErrServiceUnavailable, 50*time.Second)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
webError(w, r, config, err, http.StatusInternalServerError)
assert.Equal(t, http.StatusServiceUnavailable, w.Result().StatusCode)
assert.Equal(t, "50", w.Result().Header.Get("Retry-After"))
require.Equal(t, http.StatusServiceUnavailable, w.Result().StatusCode)
require.Equal(t, "50", w.Result().Header.Get("Retry-After"))
})

t.Run("ErrorStatusCode propagates HTTP Status Code", func(t *testing.T) {
t.Parallel()

err := NewErrorStatusCodeFromStatus(http.StatusTeapot)
w := httptest.NewRecorder()
r := httptest.NewRequest(http.MethodGet, "/blah", nil)
webError(w, r, config, err, http.StatusInternalServerError)
assert.Equal(t, http.StatusTeapot, w.Result().StatusCode)
require.Equal(t, http.StatusTeapot, w.Result().StatusCode)
})
}
Loading

0 comments on commit fca7937

Please sign in to comment.