Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(gateway): ensure 'X-Ipfs-Root' header is valid #337

Merged
merged 8 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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