File size: 2,301 Bytes
e089492 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | package client
import (
"io"
"net/http"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
// TestCheckStatus_TypesActionableStatuses is the regression for the bug
// that made a lost workspace unrecoverable: every response used to
// collapse into an opaque "status code %d" string, so no caller could
// tell "the server no longer knows my workspace" from any other failure,
// and the client just retried the dead ID forever.
func TestCheckStatus_TypesActionableStatuses(t *testing.T) {
t.Parallel()
tests := []struct {
name string
status int
body string
accept []int
wantErr error
// wantUntyped asserts the error carries no lifecycle meaning, so
// callers keep treating it as an ordinary failure.
wantUntyped bool
wantMsg string
}{
{name: "ok by default", status: http.StatusOK},
{
name: "accepted when listed",
status: http.StatusAccepted,
accept: []int{http.StatusOK, http.StatusAccepted},
},
{
name: "not found triggers workspace recovery",
status: http.StatusNotFound,
wantErr: ErrNotFound,
},
{
name: "service unavailable means retry against a replacement",
status: http.StatusServiceUnavailable,
wantErr: ErrServerShuttingDown,
},
{
name: "server message is surfaced",
status: http.StatusNotFound,
body: `{"message":"workspace not found"}`,
wantErr: ErrNotFound,
wantMsg: "workspace not found",
},
{
name: "conflict carries no lifecycle meaning",
status: http.StatusConflict,
wantUntyped: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
body := tc.body
if body == "" {
body = "{}"
}
err := checkStatus(&http.Response{
StatusCode: tc.status,
Body: io.NopCloser(strings.NewReader(body)),
}, tc.accept...)
if tc.wantErr == nil && !tc.wantUntyped {
require.NoError(t, err)
return
}
require.Error(t, err)
require.Contains(t, err.Error(), "status code")
if tc.wantErr != nil {
require.ErrorIs(t, err, tc.wantErr)
}
if tc.wantUntyped {
require.NotErrorIs(t, err, ErrNotFound)
require.NotErrorIs(t, err, ErrServerShuttingDown)
}
if tc.wantMsg != "" {
require.Contains(t, err.Error(), tc.wantMsg)
}
})
}
}
|