Skip to content

Commit f08f731

Browse files
committed
test upload file with cache-control header
1 parent de94a17 commit f08f731

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

Sources/Storage/StorageFileApi.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Foundation
2-
import Helpers
32
import HTTPTypes
3+
import Helpers
44

55
#if canImport(FoundationNetworking)
66
import FoundationNetworking
@@ -419,19 +419,21 @@ public class StorageFileApi: StorageApi, @unchecked Sendable {
419419
/// Downloads a file from a private bucket. For public buckets, make a request to the URL returned
420420
/// from ``StorageFileApi/getPublicURL(path:download:fileName:options:)`` instead.
421421
/// - Parameters:
422-
/// - path: The file path to be downloaded, including the path and file name. For example
423-
/// `folder/image.png`.
422+
/// - path: The file path to be downloaded, including the path and file name. For example `folder/image.png`.
424423
/// - options: Transform the asset before serving it to the client.
425424
@discardableResult
426-
public func download(path: String, options: TransformOptions? = nil) async throws -> Data {
425+
public func download(
426+
path: String,
427+
options: TransformOptions? = nil
428+
) async throws -> Data {
427429
let queryItems = options?.queryItems ?? []
428-
429430
let renderPath = options != nil ? "render/image/authenticated" : "object"
431+
let _path = _getFinalPath(path)
430432

431433
return try await execute(
432434
HTTPRequest(
433435
url: configuration.url
434-
.appendingPathComponent("\(renderPath)/\(bucketId)/\(path)"),
436+
.appendingPathComponent("\(renderPath)/\(_path)"),
435437
method: .get,
436438
query: queryItems
437439
)

Tests/IntegrationTests/StorageFileIntegrationTests.swift

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class StorageFileIntegrationTests: XCTestCase {
1414
configuration: StorageClientConfiguration(
1515
url: URL(string: "\(DotEnv.SUPABASE_URL)/storage/v1")!,
1616
headers: [
17-
"Authorization": "Bearer \(DotEnv.SUPABASE_SERVICE_ROLE_KEY)",
17+
"Authorization": "Bearer \(DotEnv.SUPABASE_SERVICE_ROLE_KEY)"
1818
],
1919
logger: nil
2020
)
@@ -56,7 +56,8 @@ final class StorageFileIntegrationTests: XCTestCase {
5656
}
5757

5858
func testGetPublicURLWithCustomDownload() throws {
59-
let publicURL = try storage.from(bucketName).getPublicURL(path: uploadPath, download: "test.jpg")
59+
let publicURL = try storage.from(bucketName).getPublicURL(
60+
path: uploadPath, download: "test.jpg")
6061
XCTAssertEqual(
6162
publicURL.absoluteString,
6263
"\(DotEnv.SUPABASE_URL)/storage/v1/object/public/\(bucketName)/\(uploadPath)?download=test.jpg"
@@ -68,26 +69,31 @@ final class StorageFileIntegrationTests: XCTestCase {
6869

6970
let url = try await storage.from(bucketName).createSignedURL(path: uploadPath, expiresIn: 2000)
7071
XCTAssertTrue(
71-
url.absoluteString.contains("\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
72+
url.absoluteString.contains(
73+
"\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
7274
)
7375
}
7476

7577
func testSignURL_withDownloadQueryString() async throws {
7678
_ = try await storage.from(bucketName).upload(uploadPath, data: file)
7779

78-
let url = try await storage.from(bucketName).createSignedURL(path: uploadPath, expiresIn: 2000, download: true)
80+
let url = try await storage.from(bucketName).createSignedURL(
81+
path: uploadPath, expiresIn: 2000, download: true)
7982
XCTAssertTrue(
80-
url.absoluteString.contains("\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
83+
url.absoluteString.contains(
84+
"\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
8185
)
8286
XCTAssertTrue(url.absoluteString.contains("&download="))
8387
}
8488

8589
func testSignURL_withCustomFilenameForDownload() async throws {
8690
_ = try await storage.from(bucketName).upload(uploadPath, data: file)
8791

88-
let url = try await storage.from(bucketName).createSignedURL(path: uploadPath, expiresIn: 2000, download: "test.jpg")
92+
let url = try await storage.from(bucketName).createSignedURL(
93+
path: uploadPath, expiresIn: 2000, download: "test.jpg")
8994
XCTAssertTrue(
90-
url.absoluteString.contains("\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
95+
url.absoluteString.contains(
96+
"\(DotEnv.SUPABASE_URL)/storage/v1/object/sign/\(bucketName)/\(uploadPath)")
9197
)
9298
XCTAssertTrue(url.absoluteString.contains("&download=test.jpg"))
9399
}
@@ -192,15 +198,18 @@ final class StorageFileIntegrationTests: XCTestCase {
192198
func testCanUploadWithSignedURLForUpload() async throws {
193199
let res = try await storage.from(bucketName).createSignedUploadURL(path: uploadPath)
194200

195-
let uploadRes = try await storage.from(bucketName).uploadToSignedURL(res.path, token: res.token, data: file)
201+
let uploadRes = try await storage.from(bucketName).uploadToSignedURL(
202+
res.path, token: res.token, data: file)
196203
XCTAssertEqual(uploadRes.path, uploadPath)
197204
}
198205

199206
func testCanUploadOverwritingFilesWithSignedURL() async throws {
200207
try await storage.from(bucketName).upload(uploadPath, data: file)
201208

202-
let res = try await storage.from(bucketName).createSignedUploadURL(path: uploadPath, options: CreateSignedUploadURLOptions(upsert: true))
203-
let uploadRes = try await storage.from(bucketName).uploadToSignedURL(res.path, token: res.token, data: file)
209+
let res = try await storage.from(bucketName).createSignedUploadURL(
210+
path: uploadPath, options: CreateSignedUploadURLOptions(upsert: true))
211+
let uploadRes = try await storage.from(bucketName).uploadToSignedURL(
212+
res.path, token: res.token, data: file)
204213
XCTAssertEqual(uploadRes.path, uploadPath)
205214
}
206215

@@ -353,6 +362,24 @@ final class StorageFileIntegrationTests: XCTestCase {
353362
XCTAssertFalse(exists)
354363
}
355364

365+
func testUploadWithCacheControl() async throws {
366+
try await storage.from(bucketName).upload(
367+
uploadPath,
368+
data: file,
369+
options: FileOptions(
370+
cacheControl: "14400"
371+
)
372+
)
373+
374+
let publicURL = try storage.from(bucketName).getPublicURL(path: uploadPath)
375+
376+
let (_, response) = try await URLSession.shared.data(from: publicURL)
377+
let httpResponse = try XCTUnwrap(response as? HTTPURLResponse)
378+
let cacheControl = try XCTUnwrap(httpResponse.value(forHTTPHeaderField: "cache-control"))
379+
380+
XCTAssertEqual(cacheControl, "public, max-age=14400")
381+
}
382+
356383
private func newBucket(
357384
prefix: String = "",
358385
options: BucketOptions = BucketOptions(public: true)

Tests/StorageTests/SupabaseStorageClient+Test.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,3 @@ extension SupabaseStorageClient {
2727
)
2828
}
2929
}
30-

0 commit comments

Comments
 (0)