|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/stretchr/testify/assert" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIdentityTransport_CapturesGotrueIdHeader(t *testing.T) { |
| 11 | + var captured string |
| 12 | + cb := func(id string) { captured = id } |
| 13 | + transport := &identityTransport{ |
| 14 | + RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 15 | + return &http.Response{ |
| 16 | + StatusCode: 200, |
| 17 | + Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, |
| 18 | + }, nil |
| 19 | + }), |
| 20 | + onGotrueID: &cb, |
| 21 | + } |
| 22 | + req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) |
| 23 | + resp, err := transport.RoundTrip(req) |
| 24 | + assert.NoError(t, err) |
| 25 | + assert.Equal(t, 200, resp.StatusCode) |
| 26 | + assert.Equal(t, "user-abc-123", captured) |
| 27 | +} |
| 28 | + |
| 29 | +func TestIdentityTransport_IgnoresWhenHeaderMissing(t *testing.T) { |
| 30 | + var captured string |
| 31 | + cb := func(id string) { captured = id } |
| 32 | + transport := &identityTransport{ |
| 33 | + RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 34 | + return &http.Response{ |
| 35 | + StatusCode: 200, |
| 36 | + Header: http.Header{}, |
| 37 | + }, nil |
| 38 | + }), |
| 39 | + onGotrueID: &cb, |
| 40 | + } |
| 41 | + req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) |
| 42 | + _, err := transport.RoundTrip(req) |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.Empty(t, captured) |
| 45 | +} |
| 46 | + |
| 47 | +func TestIdentityTransport_NilCallbackDoesNotPanic(t *testing.T) { |
| 48 | + transport := &identityTransport{ |
| 49 | + RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 50 | + return &http.Response{ |
| 51 | + StatusCode: 200, |
| 52 | + Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, |
| 53 | + }, nil |
| 54 | + }), |
| 55 | + onGotrueID: nil, |
| 56 | + } |
| 57 | + req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) |
| 58 | + resp, err := transport.RoundTrip(req) |
| 59 | + assert.NoError(t, err) |
| 60 | + assert.Equal(t, 200, resp.StatusCode) |
| 61 | +} |
| 62 | + |
| 63 | +func TestIdentityTransport_NilFuncBehindPointerDoesNotPanic(t *testing.T) { |
| 64 | + var cb func(string) // nil func |
| 65 | + transport := &identityTransport{ |
| 66 | + RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 67 | + return &http.Response{ |
| 68 | + StatusCode: 200, |
| 69 | + Header: http.Header{"X-Gotrue-Id": []string{"user-abc-123"}}, |
| 70 | + }, nil |
| 71 | + }), |
| 72 | + onGotrueID: &cb, |
| 73 | + } |
| 74 | + req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) |
| 75 | + resp, err := transport.RoundTrip(req) |
| 76 | + assert.NoError(t, err) |
| 77 | + assert.Equal(t, 200, resp.StatusCode) |
| 78 | +} |
| 79 | + |
| 80 | +func TestIdentityTransport_InnerTransportError(t *testing.T) { |
| 81 | + var captured string |
| 82 | + cb := func(id string) { captured = id } |
| 83 | + transport := &identityTransport{ |
| 84 | + RoundTripper: roundTripFunc(func(req *http.Request) (*http.Response, error) { |
| 85 | + return nil, assert.AnError |
| 86 | + }), |
| 87 | + onGotrueID: &cb, |
| 88 | + } |
| 89 | + req, _ := http.NewRequest("GET", "https://api.supabase.io/v1/projects", nil) |
| 90 | + resp, err := transport.RoundTrip(req) |
| 91 | + assert.Error(t, err) |
| 92 | + assert.Nil(t, resp) |
| 93 | + assert.Empty(t, captured) |
| 94 | +} |
| 95 | + |
| 96 | +// roundTripFunc is a test helper to create inline RoundTrippers. |
| 97 | +type roundTripFunc func(*http.Request) (*http.Response, error) |
| 98 | + |
| 99 | +func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { |
| 100 | + return f(req) |
| 101 | +} |
0 commit comments