|
| 1 | +package storage |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "mime" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + fs "testing/fstest" |
| 9 | + |
| 10 | + "github.com/h2non/gock" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/supabase/cli/internal/testing/apitest" |
| 13 | + "github.com/supabase/cli/pkg/fetcher" |
| 14 | +) |
| 15 | + |
| 16 | +var mockApi = StorageAPI{Fetcher: fetcher.NewFetcher( |
| 17 | + "http://127.0.0.1", |
| 18 | +)} |
| 19 | + |
| 20 | +func TestParseFileOptionsContentTypeDetection(t *testing.T) { |
| 21 | + tests := []struct { |
| 22 | + name string |
| 23 | + content []byte |
| 24 | + filename string |
| 25 | + opts []func(*FileOptions) |
| 26 | + wantMimeType string |
| 27 | + wantCacheCtrl string |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "detects PNG image", |
| 31 | + content: []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}, // PNG header |
| 32 | + filename: "test.image", |
| 33 | + wantMimeType: "image/png", |
| 34 | + wantCacheCtrl: "max-age=3600", |
| 35 | + }, |
| 36 | + { |
| 37 | + name: "detects JavaScript file", |
| 38 | + content: []byte("const hello = () => console.log('Hello, World!');"), |
| 39 | + filename: "script.js", |
| 40 | + wantMimeType: mime.TypeByExtension(".js"), |
| 41 | + wantCacheCtrl: "max-age=3600", |
| 42 | + }, |
| 43 | + { |
| 44 | + name: "detects CSS file", |
| 45 | + content: []byte(".header { color: #333; font-size: 16px; }"), |
| 46 | + filename: "styles.css", |
| 47 | + wantMimeType: mime.TypeByExtension(".css"), |
| 48 | + wantCacheCtrl: "max-age=3600", |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "detects SQL file", |
| 52 | + content: []byte("SELECT * FROM users WHERE id = 1;"), |
| 53 | + filename: "query.sql", |
| 54 | + wantMimeType: mime.TypeByExtension(".sql"), |
| 55 | + wantCacheCtrl: "max-age=3600", |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "use text/plain as fallback for unrecognized extensions", |
| 59 | + content: []byte("const hello = () => console.log('Hello, World!');"), |
| 60 | + filename: "main.nonexistent", |
| 61 | + wantMimeType: "text/plain; charset=utf-8", |
| 62 | + wantCacheCtrl: "max-age=3600", |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "respects custom content type", |
| 66 | + content: []byte("const hello = () => console.log('Hello, World!');"), |
| 67 | + filename: "custom.js", |
| 68 | + wantMimeType: "application/custom", |
| 69 | + wantCacheCtrl: "max-age=3600", |
| 70 | + opts: []func(*FileOptions){func(fo *FileOptions) { fo.ContentType = "application/custom" }}, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + // Create a temporary file with test content |
| 77 | + fsys := fs.MapFS{tt.filename: &fs.MapFile{Data: tt.content}} |
| 78 | + // Setup mock api |
| 79 | + defer gock.OffAll() |
| 80 | + gock.New("http://127.0.0.1"). |
| 81 | + Post("/storage/v1/object/"+tt.filename). |
| 82 | + MatchHeader("Content-Type", tt.wantMimeType). |
| 83 | + MatchHeader("Cache-Control", tt.wantCacheCtrl). |
| 84 | + Reply(http.StatusOK) |
| 85 | + // Parse options |
| 86 | + err := mockApi.UploadObject(context.Background(), tt.filename, tt.filename, fsys, tt.opts...) |
| 87 | + // Assert results |
| 88 | + assert.NoError(t, err) |
| 89 | + assert.Empty(t, apitest.ListUnmatchedRequests()) |
| 90 | + }) |
| 91 | + } |
| 92 | +} |
0 commit comments