|
|
|
@ -5,6 +5,7 @@ package httputil
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
"testing"
|
|
|
|
@ -116,3 +117,28 @@ func TestSendError(t *testing.T) {
|
|
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
|
|
|
require.NoError(t, json.NewDecoder(resp.Body).Decode(&body))
|
|
|
|
require.Equal(t, "NOT_FOUND", body.Code)
|
|
|
|
require.Equal(t, "NOT_FOUND", body.Code)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TestMapError_WrappedDomainError verifies wrapped domain errors are recognized.
|
|
|
|
|
|
|
|
func TestMapError_WrappedDomainError(t *testing.T) {
|
|
|
|
|
|
|
|
wrapped := fmt.Errorf("outer: %w", errorspkg.New(errorspkg.CodeValidationFailed, "bad input"))
|
|
|
|
|
|
|
|
status, resp := MapError(wrapped)
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusBadRequest, status)
|
|
|
|
|
|
|
|
require.Equal(t, errorspkg.CodeValidationFailed.String(), resp.Code)
|
|
|
|
|
|
|
|
require.Equal(t, "bad input", resp.Message)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TestTooManyRequestsRetryAfterHeader verifies Retry-After is set as a decimal string.
|
|
|
|
|
|
|
|
func TestTooManyRequestsRetryAfterHeader(t *testing.T) {
|
|
|
|
|
|
|
|
app := fiber.New()
|
|
|
|
|
|
|
|
app.Get("/", func(c fiber.Ctx) error {
|
|
|
|
|
|
|
|
return TooManyRequests(c, "slow down", 120)
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
|
|
|
|
|
|
|
resp, err := app.Test(req)
|
|
|
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusTooManyRequests, resp.StatusCode)
|
|
|
|
|
|
|
|
require.Equal(t, "120", resp.Header.Get("Retry-After"))
|
|
|
|
|
|
|
|
}
|
|
|
|
|