|
| 1 | +package planner |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/base64" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "testing" |
| 9 | + |
| 10 | + kkErrors "github.com/Kong/sdk-konnect-go/models/sdkerrors" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func makeDataURL(mime string, payload []byte) string { |
| 15 | + encoded := base64.StdEncoding.EncodeToString(payload) |
| 16 | + return fmt.Sprintf("data:%s;base64,%s", mime, encoded) |
| 17 | +} |
| 18 | + |
| 19 | +func TestDataURLsEqual_Base64(t *testing.T) { |
| 20 | + t.Parallel() |
| 21 | + |
| 22 | + payload := []byte("hello") |
| 23 | + first := makeDataURL("image/png", payload) |
| 24 | + second := makeDataURL("image/jpeg", payload) |
| 25 | + |
| 26 | + equal, err := dataURLsEqual(first, second) |
| 27 | + require.NoError(t, err) |
| 28 | + require.True(t, equal) |
| 29 | +} |
| 30 | + |
| 31 | +func TestPortalAssetNeedsUpdate_NotFound(t *testing.T) { |
| 32 | + t.Parallel() |
| 33 | + |
| 34 | + desired := makeDataURL("image/png", []byte("asset")) |
| 35 | + planner := &Planner{} |
| 36 | + |
| 37 | + needsUpdate, err := planner.portalAssetNeedsUpdate( |
| 38 | + context.Background(), |
| 39 | + "portal-id", |
| 40 | + desired, |
| 41 | + func(_ context.Context, _ string) (string, error) { |
| 42 | + return "", &kkErrors.SDKError{StatusCode: http.StatusNotFound} |
| 43 | + }, |
| 44 | + ) |
| 45 | + |
| 46 | + require.NoError(t, err) |
| 47 | + require.True(t, needsUpdate) |
| 48 | +} |
| 49 | + |
| 50 | +func TestPortalAssetNeedsUpdate_NoChange(t *testing.T) { |
| 51 | + t.Parallel() |
| 52 | + |
| 53 | + payload := []byte("asset") |
| 54 | + desired := makeDataURL("image/png", payload) |
| 55 | + planner := &Planner{} |
| 56 | + |
| 57 | + needsUpdate, err := planner.portalAssetNeedsUpdate( |
| 58 | + context.Background(), |
| 59 | + "portal-id", |
| 60 | + desired, |
| 61 | + func(_ context.Context, _ string) (string, error) { |
| 62 | + return makeDataURL("image/jpeg", payload), nil |
| 63 | + }, |
| 64 | + ) |
| 65 | + |
| 66 | + require.NoError(t, err) |
| 67 | + require.False(t, needsUpdate) |
| 68 | +} |
| 69 | + |
| 70 | +func TestPortalAssetNeedsUpdate_Change(t *testing.T) { |
| 71 | + t.Parallel() |
| 72 | + |
| 73 | + desired := makeDataURL("image/png", []byte("asset")) |
| 74 | + planner := &Planner{} |
| 75 | + |
| 76 | + needsUpdate, err := planner.portalAssetNeedsUpdate( |
| 77 | + context.Background(), |
| 78 | + "portal-id", |
| 79 | + desired, |
| 80 | + func(_ context.Context, _ string) (string, error) { |
| 81 | + return makeDataURL("image/png", []byte("different")), nil |
| 82 | + }, |
| 83 | + ) |
| 84 | + |
| 85 | + require.NoError(t, err) |
| 86 | + require.True(t, needsUpdate) |
| 87 | +} |
0 commit comments