fix: refresh action cache and classify auth failures
This commit is contained in:
@@ -231,6 +231,54 @@ func TestRequestRejectsHTTPStatus(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestBusinessRequestClassifiesReauthenticationFailures(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
status int
|
||||
body string
|
||||
wantSentinel bool
|
||||
wantCode int
|
||||
wantLogin bool
|
||||
}{
|
||||
{name: "HTTP 401", status: http.StatusUnauthorized, body: "expired", wantSentinel: true, wantCode: http.StatusUnauthorized, wantLogin: true},
|
||||
{name: "HTTP 403", status: http.StatusForbidden, body: "forbidden", wantSentinel: true, wantCode: http.StatusForbidden, wantLogin: true},
|
||||
{name: "API -10020", body: `{"code":-10020,"message":"oauth expired"}`, wantSentinel: true, wantCode: -10020},
|
||||
{name: "API -10030", body: `{"code":-10030,"message":"token expired"}`, wantSentinel: true, wantCode: -10030},
|
||||
{name: "HTTP 500", status: http.StatusInternalServerError, body: "failed", wantCode: http.StatusInternalServerError},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
|
||||
if test.status != 0 {
|
||||
writer.WriteHeader(test.status)
|
||||
}
|
||||
_, _ = io.WriteString(writer, test.body)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client := testClient(t, server.Client())
|
||||
client.baseURL = server.URL
|
||||
_, err := client.request(context.Background(), "/business", nil, true)
|
||||
if errors.Is(err, ErrReauthenticationRequired) != test.wantSentinel {
|
||||
t.Fatalf("error = %v, ErrReauthenticationRequired = %v", err, errors.Is(err, ErrReauthenticationRequired))
|
||||
}
|
||||
if test.wantLogin {
|
||||
var loginErr *LoginError
|
||||
if !errors.As(err, &loginErr) || loginErr.Code != test.wantCode {
|
||||
t.Fatalf("error = %v, want LoginError code %d", err, test.wantCode)
|
||||
}
|
||||
return
|
||||
}
|
||||
if test.status == 0 {
|
||||
var apiErr *APIError
|
||||
if !errors.As(err, &apiErr) || apiErr.Code != test.wantCode {
|
||||
t.Fatalf("error = %v, want APIError code %d", err, test.wantCode)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestRejectsOversizedRawResponse(t *testing.T) {
|
||||
server := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, _ *http.Request) {
|
||||
_, _ = writer.Write(bytes.Repeat([]byte{'x'}, maxHTTPResponseBytes+1))
|
||||
|
||||
Reference in New Issue
Block a user